アトリエ ぺっぺ

トップページ > プログラムTips > ダイアログにステータスバー

◆ ダイアログにステータスバー
ダイアログにステータスバーをつける手順を示します。
まず、ステータスバーをつけたいダイアログのメンバに、
CStatusBarのインスタンスを追加します。
class CMyDlg : public CDialog
{
// 構築
public:
    CMyDlg(CWnd* pParent = NULL);    // 標準のコンストラクタ
    //    :
    //    省略
    //    :
    CStatusBar        m_StatusBar;
};
次に、OnInitDialog()メソッドを(もしまだしていなければ)オーバーライドします。


オーバーライドしたOnInitDialog()メソッド内で、
ステータスバーの作成を行います。
BOOL CMyDlg::OnInitDialog() 
{
    CDialog::OnInitDialog();
    
    // ステータスバーを作成・付加
    m_StatusBar.Create(this);
    // ステータスバーの大きさを取得
    CRect rectSt;    
    m_StatusBar.GetWindowRect(rectSt);
    // ダイアログのクライアント領域のサイズを取得
    CRect rectClient;
    GetClientRect(&rectClient);
    // ステータスバーの高さを保持して大きさを
    // ダイアログのクライアントウィンドウの
    // 大きさにあわせる
    const int nHeight = 19;
    rectSt.top = rectClient.bottom - nHeight;
    rectSt.bottom = rectSt.top + nHeight;
    rectSt.left = rectClient.left;
    rectSt.right = rectClient.right;
    m_StatusBar.MoveWindow(rectSt);

    return TRUE;  // コントロールにフォーカスを設定しないとき、戻り値は TRUE となります
                  // 例外: OCX プロパティ ページの戻り値は FALSE となります
}
これでステータスバーを付加することができました。

もしサイズ変更可能なダイアログの場合、
サイズ変更時にステータスバーも動かしてやる必要があります。
サイズ変更時にステータスバーを移動するには、
OnSize()メソッドを(もしまだしていなければ)オーバーライドします。


OnSize()メソッドに、次の内容を追加します。
void CMyDlg::OnSize(UINT nType, int cx, int cy) 
{
    CDialog::OnSize(nType, cx, cy);

    CRect rectClient;
    // ステータスバー
    int nStatusHeight = 0;
    if(m_StatusBar.GetSafeHwnd()){// コントロールの有効性をチェックします。

        CRect rectSt;
        m_StatusBar.GetWindowRect(rectSt);
        // 現在のクライアントウィンドウのサイズを取得
        GetClientRect(&rectClient);
        // 高さを維持しながらサイズを変更する
        int nBtm = rectSt.bottom - rectSt.top;
        rectSt.top = rectClient.bottom - nBtm;
        rectSt.bottom = rectSt.top + nBtm;
        rectSt.left = rectClient.left;
        rectSt.right = rectClient.right;
        m_StatusBar.MoveWindow(rectSt);
        nStatusHeight = rectSt.Height();
    }
}
これで、サイズ変更にも対応できます。

(C) 2002 atelier-peppe
ababa@atelier-peppe.sakura.ne.jp