Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WTL - Button Menu 2

0.00/5 (No votes)
17 Mar 2001 1  
Use of a Push button with a drop down menu

Updated 18 march 2001. Now with keyboard and focus cues (only for W2k/Me)

  • Download source files - 4 Kb
  • Download demo project - 16.5 Kb
  • Sample Image - MenuBtn.gif

    Introduction

    This code was based on articles written by Norm Almond and David Peterson.

    Why a new one? I needed a menu button that acts exactly like a pushbutton. I believe I have done it.

    Nevertheless, my button is not as cool as Norm Almond's is. I mean, there is no code for displaying a bitmap. Feel free to add it yourself or borrow it from Norm Almond's article.

    Implementation

    I have developed two classes: CMenuButton and CAutoMenuButton. CAutoMenuButtonM is the same button as CMenuButton, but adds its caption as the first and default menu item. Useful behavior, isn't it?

    How To Use

    First of all, make your parent class support reflection of notification messages:

    BEGIN_MSG_MAP(CMainDlg)
       MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
       COMMAND_ID_HANDLER(IDOK, OnOK)
       COMMAND_RANGE_HANDLER(ID_FILE_NEW, ID_FILE_CLOSE, OnFileCmd)
       <U>REFLECT_NOTIFICATIONS()</U>
    END_MSG_MAP()

    Then, declare your buttons:

       WTLEX::CAutoMenuButton   m_ctlOK;
       WTLEX::CMenuButton       m_ctlButtons[4];

    And assign it an ID:

       m_ctlOK = GetDlgItem(IDOK);
    
       for(int i = 0; i < 4; i++)
       {
          m_ctlButtons[i] = GetDlgItem(IDC_NORMAL + i); 
          m_ctlButtons[i].SetMenuID(IDR_BTNMENU);
       }

    SetMenuID does not need to be called when you supply it in the constructor:

       CMainDlg() : m_ctlOK(IDR_BTNMENU)
       {
       }

    That's all :)

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here