Using Buttons on a Dialog Bar with CCmdUI

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

I am a Korean and not a native english speacker.
Please, forgive my poor english.

I was very happy after finding Alger Pike’s article,
which is “A CDialogBar with OnInitDialog() and DDX Support”.

I wanted to make floating dialog bar
but MFC did not support for class wizard for dialog bar.
To worse, MFC did not support DDX for dialog bar.

I struggled so long time to solve the problem.
At last I found www.codeguru.com and Alger Pike’s article!
Can you imagine my shout with greatest pleasure?

But I have an another problem.
According to Alger Pike’s explanation, I made my dialog bar.
But every buttons on that bar don’t work!
Oopse….what can I do?

I asked Alger Pike that problem via email.
He gave me a hint.
It is that use CmdUI!

I have some confusions how I can use CmdUI in dialog bar.
Probably you make sense already that I’m a novice to MFC.
Yes, I am.
But at last I soved the quiz.

I’ll give my solution to you.
Don’t waste time for same problem with me.

Thanks again to Alger Pike and www.codeguru.com.
Good luck for you and Alger Pike and www.codeguru.com.

Below is breif explanation about my solution.
You can find complete source code at header file and source file for CMyDialogBar.
CInitDialog is Alger Pike’s class.

Instructions

  1. Make Dialog Resource for your dialog bar

    – Use resource editor provided by Visual C++
    – Change style to child
    – Change border to none (or as you wish)

  2. Make Dialog Class for your dialob bar

    – Double click your dialog resource in resource editor
    – then class wizard will ask if you will make new class, answer yes
    – give name for your own dialog class
    – go to class header file and change parent class from CDialog to CInitDialogBar
    – Don’t forget #include “InitDialogBar.h”
    – go to class source file and replase all “CDialog” with “CInitDialogBar”

  3. Add below to header file for your dialog bar
    Of coursely, you must change OnButton1() to your own function

      protected: void OnUpdateButton1(CCmdUI *pCmdUI); //{{AFX_MSG(CMyDialogBar) afx_msg void OnButton1(); //}}AFX_MSG
  4. Add below to your source file for your dialog bar
    In OnUpdateButton1(), I set button enable always.
    But you can enable and disable according to your need.

      BEGIN_MESSAGE_MAP(CMyDialogBar, CInitDialogBar) //{{AFX_MSG_MAP(CMyDialogBar) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_UPDATE_COMMAND_UI(IDC_BUTTON1, OnUpdateButton1) //}}AFX_MSG_MAP END_MESSAGE_MAP() void CMyDialogBar::OnButton1() { // TODO: Add your control notification handler code here } void CMyDialogBar::OnUpdateButton1(CCmdUI *pCmdUI) { pCmdUI->Enable(TRUE); }
  5. Don’t forget to make InitDialogBar() function.
    Below is my example.

    In header file

      protected: virtual BOOL OnInitDialogBar();

    In source file

      BOOL CMyDialogBar::OnInitDialogBar() { CInitDialogBar::OnInitDialogBar(); return TRUE; }


Download demo project – 50 KB

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read