0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[MFC] CDialog ベースのアプリに CStatusBarCtrl を最小限のコードで追加する

0
Last updated at Posted at 2026-10-28

はじめに

MFC のダイアログベースアプリケーションで処理状態などを表示する場合、CStatusBar を使うとコントロールバーの再配置処理などでコードがやや煩雑になりがちです。

CStatusBarCtrl を直接利用し、ダイアログの OnSize で WM_SIZE メッセージを転送するだけで、最小限のコードでシンプルにステータスバーを実装できます。

実装手順

1. ダイアログクラスにメンバ変数を追加

private:
    CStatusBarCtrl m_StatusBar;

2. OnInitDialog で生成と初期化

Create 時にコントロール ID として AFX_IDW_STATUS_BAR を指定し、ペインを設定します。

BOOL CXxxDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    if (m_StatusBar.Create(WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP,
            CRect(0, 0, 0, 0), this, AFX_IDW_STATUS_BAR)) {
        int parts[] = { -1 };
        m_StatusBar.SetParts(1, parts);
        m_StatusBar.SetText(_T(" ready "), 0, 0);
    }

    return TRUE;
}

3. OnSize でサイズを追従させる

ダイアログのサイズ変更時に WM_SIZE メッセージを送ることで、自動的に下部へ綺麗に配置されます。

void CXxxDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialog::OnSize(nType, cx, cy);

    if (::IsWindow(m_StatusBar.m_hWnd)) {
        m_StatusBar.SendMessage(WM_SIZE, 0, 0);
    }
}


関連記事・詳細

実装の背景や詳細については、以下のブログ記事もあわせてご覧ください。


※ 本記事の構成および文章の作成には、生成 AI を利用しています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?