Tag Archives: hook

Multiple Selection Menu – Part 1


In some applications we can find menus which allow multiple selection. One example is the pop-up menu shown by Microsoft Visual Studio for adding or removing toolbar buttons.

VS Add or Remove Buttons Menu

We’ll try to do something similar in our own MFC application.

Finding a solution

By default, a native Windows pop-up menu is dismissed when the user selects an item. I could not find in the documentation any flag, message or notification that allows changing this behavior. So, one solution may be using a hook. On the first site, may be tempted to use a CBT hook, as shown in a previous article “AfxMessageBox with Auto-close” (see the link, below). That’s because a pop-up menu is a window itself (of pre-defined class “#32768”).
However, easier and better for our purpose is using a message filter hook. This type of hook can be installed by passing WH_MSGFILTER value in the first parameter of SetWindowsHookEx function.

Using an application-wide WH_MSGFILTER hook

Next example sets a WH_MSGFILTER hook in application’s main thread class. The hook procedure processes and filters the messages occurred as a result of menu input.

Demo project

The demo project attached here is a simple SDI MFC application having the “View” menu with multiple selection.

What’s next?

This article just demonstrates how to use a WH_MSGFILTER hook in order to make a menu with multiple selection. We may notice that using an application-wide hook and some hard-coded menu item IDs is not the best choice. What about if we have tens of menus with tens of menu items?
Well, in the next article we’ll implement an MFC-extension class that can serve our initial purpose in a much more flexible and object-oriented manner.


See also