Source of article: Internet Author: ggg82/CSDN
Nowadays, many user interfaces use toolbars to create menu bars. I recently became interested in this and asked for help online. However, most of the help I received was the source code of BCGControlBar or the source code of SizableRebar. For those who just want their own interface to have this For functional friends, this may be a good choice. Just look at the demo and then directly call other people's class libraries. But for readers like me who are interested in this topic and hope to understand the ins and outs, directly Looking at these source codes without detailed explanation, it is not easy to figure out the reason, at least for a novice like me. For this reason, this article hopes to help people who are still looking for this. Help readers can provide some help.
Let’s watch and talk below:
When receiving the toolbar button press message, we generally use TrackPopupMenuEx to pop up the menu. The key to the problem is that when the menu is not closed, TrackPopupMenuEx does not return and intercepts mouse and keyboard messages. You can see using spy that the toolbar at this time If no message is received, of course there is no way to change the hotspot. This requires us to detect the mouse position ourselves and close the previous menu and display the next menu when the mouse moves to the next hotspot. Here we use the hook function SetWindowsHookEx to install the WH_MSGFILTER hook before calling TrackPupupMenuEx. The code is as follows:
m_hMsgHook = SetWindowsHookEx( WH_MSGFILTER, MessageProc, 0, GetCurrentThreadId() );
MssageProc is a hook function, the code is as follows:
void TrackPopup(HWND hWndToolBar, int iButton)
{
while (iButton >= 0)
{
SendMessage(hWndToolBar,TB_SETHOTITEM,iButton,0);
iPopup = iButton;
//Install hook
g_hMsgHook = SetWindowsHookEx(WH_MSGFILTER, MessageProc, 0, GetCurrentThreadId());
//Popup menu
TrackPopupMenuEx(…);
//Uninstall hook
UnhookWindowsHookEx(g_hMsgHook);
iButton = iNextPop; //The next pop-up item, if it is negative, exit
}
SendMessage(hWndToolBar,TB_SETHOTITEM,-1,0);
}
(Experience and suggestions: If the button uses the style TBSTYLE_DROPDOWN, please do not call the function directly in the message TBN_DROPDOWN. You should use an intermediate message, and then use PostMessa to send the message so that TBN_DROPDOWN can return directly, otherwise the first highlighted hotspot will be eliminated. It’s very troublesome.)
iPopup is the current pop-up item, and iNextPop is the next pop-up item. These variables need to be processed in the function HookMessageProc. The sample code is as follows: