Creation of the MFC Dynamic button and its message response (custom message)

Source: Internet
Author: User

Creation of dynamic buttons (multiple):

1. Declare and define the ID of the button control in the class

#define IDC_D_BTN 10000

2. Dynamically create a button in the OnInitDialog () function of the class (preferably a pointer to an object when establishing a button object)

3. Manually releasing the object pointer

Here is an example of dynamically generating multiple buttons:

 cbutton* btn = new  cbutton[5  ]; DWORD dwstyle  = Ws_child | ws_visible |   Bs_pushbutton;  for  (int  i = 0 ; I < 5 ; I++   dynamic button  ", Dwstyle,crect (20  +80  *i,< Span style= "color: #800080;" >20 , 80  +80  *i,60 ), this , Idc_d_btn+i); }  

Note: The first parameter of Create () is the caption of the button, which can be given directly (as shown above), or you can define a string in string table and then use the LoadString () function of the control to read the string in

The program works as follows:

Message Responses for dynamic buttons (multiple):

An MFC message response function has three related information in the program: function prototypes , function implementations , and macros used to correlate message and message response functions . The header file is a declaration of the message response function prototype between the two afx_msg annotation macros. There are two places in the source file: One is the message map macro between the AFX_MSG_MAP annotation macros, the macro used to correlate the message and message response functions, and the other is the implementation code for the message response function in the source file.

Here are the steps to establish a message response for a dynamically generated number of buttons:

1. Declare the message response function in the definition file (. h file) of the dialog class OnButtonClick

void OnButtonClick (UINT UID);

Note: The parameter of the OnButtonClick function, Nid, represents the ID number of the response function corresponding to the button control, and a single button can be set without parameters

2. Define the message map on_command_range (multiple buttons) in the function implementation file (. cpp file) of the dialog box class

Defines the mapping between a button control and its message response function in the message map section (between Begin_message_map and End_message_map) in the function implementation file.

On_command_range (idc_d_btn,idc_d_btn+5-1, OnButtonClick)

For all dynamically created 5 buttons (ID numbers between IDC_D_BTN ~ idc_d_btn+5-1), define their message map function as the OnButtonClick function, resolving the specific response to that button based on its input ID.

If a single button, the message map is on_bn_clicked

On_bn_clicked (idc_button_id, Onbuttonfunc)

3. Implement the message response function OnButtonClick

Give a specific button message response in the function implementation file (. cpp file) of the dialog class

Custom message:

  The message map , looping mechanism is the basic way that Windows programs run. VC + + MFC has a lot of ready-made message handles, but when we need to complete other tasks, we need to customize the message, we encounter some difficulties. Adding user-defined messages is not allowed in MFC ClassWizard, so we must add code to the program so that custom messages can be processed like other messages.

  Windows messages are prefixed with "wm_", and custom messages are generally "um_ this prefix".

1, first define a message in the header file or CPP file (the recommended user-defined message is at least wm_user+100, because many new controls also use Wm_user messages)

#define

2. Add a function declaration to the message map macro

3. Define the message map on_messagein the function implementation file (. cpp file), associate the message and the message response function

On_message (Wm_my_message, OnMyMessage)

4. Implement the message handler function, which uses the Wpram and lparam parameters and returns Lpesult

lpesult cmainframe::onmymessage (WPARAM WPARAM, LPARAM LPARAM)  {      //  TODO: Handling user Custom          messages ... return 0 ;  }  

5. Send a message

0 0);

where GetSafeHwnd () gets a handle to the current window, this message is sent to the current window, and if you want to send a message to another window, simply change the handle, provided the destination window implements the handler for this message.

Note:

If the user needs a message that defines the entire system as unique , you can call the SDK function RegisterWindowMessage to define the message:

Static UINT wm_my_message=registerwindowmessage ("User"

and replace the ON_MESSAGE macro instruction with the on_registered_message macro, the remaining steps are the same as above.

Creation of the MFC Dynamic button and its message response (custom message)

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.