Usage of the ultra-dynamic menu under WIN (2), win dynamic

Source: Internet
Author: User

Usage of the ultra-dynamic menu under WIN (2), win dynamic

Introduction to the ultra-dynamic menu under WIN (I)

Usage of the ultra-dynamic menu under WIN (2)

Author: Huang shansong, published in blog: http://www.cnblogs.com/tomview/

Auto_dynamenu is a c ++ package library that dynamically generates WINDOWS menus. The design idea is to simplify the code generated by dynamic menus as much as possible and display menus (especially right-click menus) anywhere on the program interface) you can easily generate menus. In particular, you can generate different dynamic menus Based on the program's internal data and internal status.

Auto_dynamenu only encapsulates a static interface function, so that the purpose of processing is to put the class implementation code in the class declaration of the header file, in this way, you only need to include the header file, and you do not need to add the implementation file to the project to simplify the operation.

Class interface function definition
/*************************************** **************************************** * ****************** \ * Static int: returned value indicates the menu item selected or the updated nDefaultValue * dynamenu: * HWND hWnd: current window handle * LPPOINT pPoint: displays the menu position, usually 0, automatically determine the display menu position * char * pszMenu: indicates the menu string * int nDefaultMode of the dynamic menu content: automatically update the marked mode of the menu, 0 none, 1 is equal to the mode, 2-Bit mode * int nDefaultValue: default value. Based on this value, follow nDefaultMode to display the selection mark of the menu item \****************************** **************************************** * **************************/class auto_dynamenu
{
public:
static int dynamenu(HWND hWnd, LPPOINT pPoint, char* pszMenu, int nDefaultMode, int nDefaultValue);
};
Parameter: pszMenu

The interface uses a formatted string pszMenu to represent the dynamic menu. The specific format rules are as follows:

(1) Each menu item is a string ending with \ n

(2) the string of a menu item is usually divided into two parts by equal sign =. The first part is the content of the menu to be displayed, and the second part is the value after selecting this menu.

(3) If there is no equal sign in the string of the menu item, it indicates that when the menu item is returned

(3) You can add the following modifier before each menu item string:

  

[A] * (asterisk): indicates a selection mark before this menu item

[B] ^: indicates that this menu item is marked with a check mark.

[C] #: indicates that the menu item is gray.

[D]-(minus sign): indicates that this menu item and the next menu item are divided into different columns (Multi-column menus)

[E] '(button in the upper left corner of the keyboard): represents a meaningless placeholder.

[F] ~ : Indicates a menu segmentation line.

(4) you can use a single line ~ Indicates a menu to split the horizontal line, "~ \ N"

(5) the vertical line character before the equal sign of the menu item string "|" splits the menu into a parent menu and a sub menu.

(6) concatenate the strings of different menu items into a complete string to describe the entire menu.

For example:

Char szMenu [] = "option 1 = 20 \ n" // Option 1 corresponds to the value 20, select this function to return 20 "option 2 = 0x20 \ n" // select 2 corresponding value 0x20, select this function to return 0x20 "option 3 = 0 \ n" // Option 3 corresponds to the value 0, select this function to return INT_MAX (because the function returns 0, indicating that no menu item is selected, so 0 is represented by the INT_MAX return value )"~ \ N "// This indicates a menu split horizontal line" video | common format | avi format =-1 \ n "// multi-level sub-menu, select this response-1 "video | common format | ~ \ N "// multi-level sub-menu internal split horizontal line" video | common format | mkv format =-2 \ n "// multi-level sub-menu, select this response-2 "video | ~ \ N "" video | special format | rdv format =-3 \ n "// multi-level sub-menu, select this return-3 "~ \ N "" # suspend processing \ n "// The menu item that is gray-disabled. It cannot be selected and can be used to display information "~ \ N "" control | ^ start Video = 10 \ n "// display the Start menu of the selected video, select return 10 "control | end video = 11 \ n" // display the unselected menu, select return 11 "play current video 1.avi= d :\\ 1. avi \ n "// returns the string pointer to the file name;

The menu strings shown in the preceding example are as follows:

NDefaultValue = auto_dynamenu: dynamenu (GetSafeHwnd (), 0, pszMenu, nDefaultMode/* 1 or 2 */, nDefaultValue );

Dynamic menu display and processing example based on return values
Char szMenu [] = "option 1 = 20 \ n" // Option 1 corresponds to the value 20, select this function to return 20 "option 2 = 0x20 \ n" // select 2 corresponding value 0x20, select this function to return 20 "option 3 = 0 \ n" // Option 3 corresponds to the value 0, select this function range INT_MAX (because the function returns 0, it indicates that no menu item is selected, so 0 is represented by the INT_MAX return value )"~ \ N "// This indicates a menu split horizontal line" video | common format | avi format =-1 \ n "// multi-level sub-menu, select this return-1, it can be a negative value "video | common format | ~ \ N "// multi-level sub-menu internal split horizontal line" video | common format | mkv format =-2 \ n "// multi-level sub-menu, select this response-2 "video | ~ \ N "" video | special format | rdv format =-3 \ n "// multi-level sub-menu, select this return-3 "~ \ N "" # suspend processing \ n "// The menu item that is gray-disabled. It cannot be selected and can be used to display information "~ \ N "" control | ^ start Video = 10 \ n "// display the Start menu of the selected video, select return 10 "control | end video = 11 \ n" // display the unselected menu, select return 11 "play current video 1.avi= d :\\ 1. avi \ n "// returns the name string pointer; int index = auto_dynamenu: dynamenu (GetSafeHwnd (), 0, szMenu, 0, 0); switch (index) {case 0: // The menu is not selected and does not process break; case 20: // Option 1 break; case 0x20: // option 2 break; case INT_MAX: // option 3, the value of this menu item is 0, which can be avoided. You do not need to process this special value break; case-1: // avi Video break; case-2: // mkv video break; case-3: // rdv video break; case 10: // start video break; case 11: // stop video break; default: {char * pfile = (char *) index; // The final file menu item is selected, and pfile is "d: \ 1. avi "string pointer} break ;}
Two notes:

(1) The selected tag of the menu item in the above Code is manually specified in the menu string. In this case, nDefaultMode and nDefaultValue are specified as 0.

(2) The menu items in the above Code are hard-coded in the source code. They can be dynamically generated in actual use. A menu string is formed based on the program State. For example:

Char szMenu [1024] = {0}; int n = 0; if (value = 2) n + = sprintf (szMenu + n, "^ "); n + = sprintf (szMenu + n, "value 2 = 2 \ n"); if (value = 4) n + = sprintf (szMenu + n, "^ "); n + = sprintf (szMenu + n, "value 4 = 4 \ n ");
Example of nDefaultMode = 1

(1) Mode 1 is equal mode (nDefaultMode = 1). When the value of a menu item is equal to the value of the input nDefaultValue, this menu item is marked with a selected mark.

Int val = 32; // nDefaultValue char szMenu [] = "integer 1 = 1 \ n" "integer 20 = 20 \ n" "integer 32 = 32 \ n" "integer 0x99 = 0x99 \ n "; // note that no mark is selected in the above menu string // nDeaultMode = 1. When the menu item is equal to nDefaultValue, the selected Mark val = auto_dynamenu: dynamenu (GetSafeHwnd (), 0, szMenu, 1, val); // The returned value is the value corresponding to the currently selected menu item. If no menu is selected, this value remains unchanged.Example of nDefaultMode = 2

Mode 2 is bit mode (nDefaultMode = 2). When the bits corresponding to the value of the menu item are not 0, the selected tag is displayed in front of the menu item.

DWORD flags = 0x82; // The current value nDefaultValue char szMenu [] = "mark 1 = 1 \ n" "Mark 2 = 2 \ n" mark 3 = 4 \ n "" Mark 4 = 8 \ n "" Mark 5 = 0x10 \ n "" mark 6 = 0x20 \ n "" mark 7 = 0x40 \ n "" mark 8 = 0x80 \ n "; // No tag is selected in the preceding menu string. The program automatically adds the selected tag to the corresponding bit based on nDefaultValue. // nDefaultMode = 2 flags = (DWORD) auto_dynamenu :: dynamenu (GetSafeHwnd (), 0, szMenu, 2, flags );

// The returned value is flags. If no menu item is selected, the value remains unchanged.
To be continued

The "super dynamic menu (3) code under WIN" will be published later.

You can download the code and sample programs at the following link:

Http://files.cnblogs.com/files/tomview/dynamenu_20160524.rar

Related Article

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.