How to customize the dialog box system menu

Source: Internet
Author: User
Tags define assert definition exit resource

Background

The system menu is the standard feature for each Windows program. Usually the system menu is managed by the Windows system, so we rarely touch it when we're in the usual weave. But, sometimes, we do want to customize our own system menu items. This involves the processing of custom menus, because Windows cannot automatically process our customized system menus. Also, system menus are handled differently from regular menu processing. So how do we implement a custom system menu? I believe that after reading this article, you will get a satisfactory answer.

The example in this article is a typical C++/MFC dialog box program that sets the Ex_wm_toolwindow extension style so that the system menu icon is not visible in the upper-left corner of the title bar, but the system menu can be retrieved by ctrl+space or by right-clicking in the title bar. Example program to customize the System menu, the original menu based on the addition of two menu commands: One is to display the "About" dialog box, the other is "exit." The reason to add an "exit" menu command is because the example program overwrites the default "close" menu command Behavior (ALT-F4) of the dialog box, which is used to hide the dialog box. It is therefore necessary to add a program to the "Exit" exit. In addition, the example program utilizes a third-party system tray handling class, using the system tray icon to show/Hide dialog boxes. Now let's take a look at the details of the implementation with C++/MFC.

Add Menu

The menu item label is defined first in the resource definition file resource.h, or in the standard header file. menu item labels must be unique. Second, Windows handles system menus differently than regular menus, either by default menus or custom menus, and there are no message processing routines like regular menu commands. Let's say we want to add two custom system orders:

#define IDM_ABOUT 16
#define IDM_EXIT 17

Idm_ means that the definition is a menu item ID. The Add menu command is performed in the initialization routines of the dialog box and in the Window creation function (OnInitDialog (), OnCreate ()). such as: BOOL cbabelondlg::oninitdialog ()
{
CDialog::OnInitDialog ();
//Add "about ..." to the system menu and exit menu item
//Resolve bugs in Windows 95
ASSERT ((Idm_aboutbox & 0xfff0) = = Idm_aboutbox);
//Command IDs must be in predefined system dishes After the single
assert (Idm_aboutbox < 0xf000);
//Resolve the Bug
ASSERT (idm_exit & 0xfff0) = Idm_exit) in Windows 95;  
//Command IDs must be after the predefined system menu
ASSERT (Idm_exit < 0xf000);
cmenu* Psysmenu = GetSystemMenu (FALSE);    
if (psysmenu!= NULL)
{
Psysmenu->appendmenu (mf_string,idm_exit, Exit (&x));
Psysmenu->appendmenu (Mf_separator);
Psysmenu->appendmenu (mf_string, Idm_aboutbox, "About (&a) ...");
.....
}
...
//other Initialization
}

There are two assert statements before adding each menu, the purpose of the first assert is to fix bugs that exist in Windows 95, and the second assert to ensure that the custom command IDs are behind predefined system menus to avoid conflicts. Check the MFC documentation for the MSDN library for a description of the system menu, and you'll find the following:“......所有预定义的控制菜单项(也就是系统菜单)的ID号必须大于 0xF000。如果某个应用程序要添加系统菜单,其系统菜单的 ID 号必须小于F000。”

Next, use the GetSystemMenu function to get the system menu pointer. Use parameter FALSE to get pointers when called. If you use TRUE as a parameter, the function resets the menu back to the default state.

If the resulting pointer is valid, the Call menu Add command adds a menu item to the System menu, passes the menu IDs, and the string used to display the menu.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.