Link to home
Create AccountLog in
Avatar of minnirok
minnirok

asked on

Drop down button notification

Hi,

I am using a custom toolbar class in my MDI app. I am adding a dropdown button to it, which is working fine. My problem is that I am not receiving any notification as to when the drop down menu for the button is being created, though I have a handler for that event. Here are the pieces I am executing:

    BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
        ...
        ON_NOTIFY(TBN_DROPDOWN, IDR_MYTOOLBAR, OnToolbarDropDown) // call me when user clicks button drop down!

    END_MESSAGE_MAP()

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        ...
        m_MyToolbar.AddDropDownButton(this, BUTTON_ID_DRAW_A_HOUSE, MENU_ID_DRAW_A_HOUSE);
    }

    void CMainFrame::OnToolbarDropDown(NMHDR* pNMHDR, LRESULT* plRes)
    {
        // Shouldn't this be called when the user clicks the drop down arrow for my button?
        AfxMessageBox("user hit drop down for toolbar button");
    }

    void CTrueColorToolBar::AddDropDownButton(CWnd* pParent, UINT uButtonID, UINT uMenuID)
    {
        // Custom toolbar class method for making a button 'drop down' - able.
        GetToolBarCtrl().SendMessage(TB_SETEXTENDEDSTYLE, 0, (LPARAM)TBSTYLE_EX_DRAWDDARROWS);
      
        SetButtonStyle(CommandToIndex(uButtonID), TBSTYLE_DROPDOWN);

        stDropDownInfo DropDownInfo;
        DropDownInfo.pParent      = pParent;
        DropDownInfo.uButtonID      = uButtonID;
        DropDownInfo.uMenuID      = uMenuID;
        m_lstDropDownButton.Add(DropDownInfo);
    }

So my button has a dropdown with the menu I supplied, but my OnToolbarDropDown() handler is never getting called when my menu pops up. Any ideas why it is not getting called? I can supply more source code if necessary,

Thanks
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

I think the window isn't getting the message.
(Have a quick test - put the handler into the main frame of the app)
Avatar of AlexFM
AlexFM

Maybe it is enough to add WM_COMMAND handlers for every menu item? What do you need to do when menu is opened?
Just a sec.
Notify messages will go through the  OnNotify virtual function.

Try overriding the OnNotify (NO message map entry) and test for the message there.
Avatar of minnirok

ASKER

Ok I  added the OnNotify() handler to Main frame, i am getting notify messages when I click on the drop down arrow. But how do I handle only those messages for the drop down activation as opposed to all the rest like when the user is clicking around the window etc?

The reason I need to handle this action is because I want to check off one of the drop down menu items when the menu is created. This way the user can see the last option they chose.

Thanks
The menu is like any other menu.
You handle the menu clicks as Alex suggests, the updating in the update handler (pCmdUI->SetCheck(..))
Hi Andy,

Can you just explain how to do that? This is what I have now:

    BOOL CMainFrame::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
    {
        TRACE("yes being notified!......\n");

        return CMDIFrameWnd::OnNotify(wParam, lParam, pResult);  
    }

But I am getting messages for any type of user interaction now. How do I 'know' that the notify message that is coming in is due to my drop down menu coming up, how do I then access it to set the check mark?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Note: The first item in the ON_UPDATE_COMMAND_UI macro is the Command ID (usually, but not always the same as the button ID)  It will be the same as used in the ON_COMMAND handler that is in the same message map.
I use
((LPNMHDR)lParam)->code
in the OnNotify to get at the ID.
Hi guys,

Sorry I'm a bit confused now -

If I were to add that update command UI mapping, won't I get messages anytime that button is being drawn? How do I know that the dropdown menu for the button is coming up? That's all I'm really interested in. I just want to get a pointer to the menu and check the appropriate item right before it's to be displayed.

Can I have a few more instructions?

Thanks

Menu and toolbar.
You get a message when the user selects one (typically you have an OnDoSomething function to react to that).
Just prior to the menu item / toolbar button being shown there is another message passed.
If you use the wizard to add an event handler for the button you will see TWO messages you can respond to.  If you select the one for the updating then you will see a function like
OnUpdateDoSomething(CCmdUI* pCmdUI)
Now add the line
pCmdUI->SetCheck(true);
in the function and you should get a check mark next to the menu item you have the update handler for.
Ok I guess that's where my confusion is. I've added an update UI function for my toolbar button:

    ON_UPDATE_COMMAND_UI(TOOLBAR_BUTTON_ID, OnUpdateMyToolbarButton)

And its body is:

    void CMainFrame::OnUpdateDrawOneClickDropDown(CCmdUI* pCmdUI)
    {
        pCmdUI->SetCheck(1);
    }

But this just has the effect of making the toolbar button look de-pressed, as if it is selected. I need to know how to check an item off in the drop down menu associated with the button.

I'm increasing the points for this question, sorry to be such a pain, thanks for your continuing help
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Ahhhhh ok that's what I was missing.

Works perfectly now, thanks!