Attaching menu items to the system menu

Creating menus and adding menu items to them is a common phenomenon. But adding menu items to a menu provided by framework like system menu is rather uncommon. here is the article which shows how to add items to the system menu. The way an application calls CWnd::GetMenu( ) to obtain a CMenu pointer to its top-level menu, likewise, it can call CWnd::GetSystemMenu( ) to obtain a pointer to its system menu. This is what has been done in the OnCreate( ) handler. The FALSE parameter passed to the GetSystemMenu( ) indicates that you want a pointer to a copy of the system menu that you can modify. Passing TRUE resets the system menu to its default state. Once the pointer to the system menu is obtained, the AppendMenu( ) function is called once to add a separator and then to add the 'About' menu item. Note that the ID for this menu item has been given as 112. This is because it is necessary that the IDs of system menu items are multiples of 16 and the 'About' menu item happens to be the seventh menu item. When the user clicks on an item from the system menu, the window receives a WM_SYSCOMMAND message. We have tackled this message using the handler OnSysCommand( ). The first parameter passed to it contains the ID of the menu item selected in the upper 12 bits. The lower 4 bits are used by Windows internally. Hence, the id has been ANDed with 0xFFF0 to strip off any bits that Windows may have added to it. If the 'About' menu item is selected then an appropriate message is displayed using the MessageBox( ) function. It is necessary to call the base class's OnSysCommand( ) handler so that the selection of other system menu items gets properly processed.

No comments: