"Reprint" MFC dynamically creates a control and its message response function

Source: Internet
Author: User

Original: http://blog.sina.com.cn/s/blog_4a08244901014ok1.html

These days, we've been specifically investigating how to dynamically create a control and its message response function in MFC.

Refer to the following posts:

(1) http://topic.csdn.net/u/20101204/13/5f1b1e70-2f1c-4205-ba10-41616d16f07e.html

(2) http://www.cppblog.com/deercoder/articles/107132.html

These two posts are more classic and can answer my questions. But how do you dynamically create a message response function? This requires the use of a different command:

On_command_range

Refer to the following posts: http://topic.csdn.net/t/20020728/22/906766.html

Note: On_command_range maps a set of control IDs with their corresponding message response functions, because the message response function of which control ID is to be differentiated is required by the parameter:UINT NID

Here I built a new MFC project, and began to test the use of the above posts. After repeated comparisons, it is found that there is one more detail to note:

You need to include the control ID of the initialization in the string table, as follows:

Open the String table in the resource, double-click on a blank line, and an ID property dialog pops up, enter the ID in the ID edit box, such as: Idc_mybutton, enter the control title or annotation in caption (Note: the caption box cannot be empty, Null will cause the creation to fail), here I enter the button to display the text-the dynamic button.
This way, there is no error in the subsequent operation.

The specific code is as follows:

(1) build the control ID number: add a new Id:idc_mybutton to the string table. Note Keep distance from the value of the default ID

(2) Create a Control object:
Note You cannot define CButton objects directly, such as: CButton m_mybut; This definition can only be used to define control variables for static controls and not for dynamic controls.

For a dialog resource, first associate a class, such as Ctestdlg

Then join in the CTestDlg.cpp position:

On_command_range (IDC_MYBUTTON,IDC_MYBUTTON+10,&CTESTDLG::ONMYBTN)

As shown below:

Begin_message_map (Ctestdlg, CDialog)
On_command_range (IDC_MYBUTTON,IDC_MYBUTTON+10,&CTESTDLG::ONMYBTN)
On_bn_clicked (Idc_button1, &ctestdlg::onbnclickedbutton1)
On_bn_clicked (Idc_button2, &ctestdlg::onbnclickedbutton2)
End_message_map ()
Then add a function to create the control:

cbutton* Ctexteditorview::newmybutton (int nid,crect rect,int nstyle)
{
CString m_caption;
M_caption.loadstring (NID); Take button title
CButton *p_button = new CButton ();
Assert_valid (P_button);
P_button->create (m_caption, Ws_child | ws_visible | Bs_pushbutton | Nstyle, rect, this, NID); Create button
return P_button;
}

Then a new button (Idc_button1) is created using the Toolbox so that the button's message response function is used to create three new buttons. Its message response functions are as follows:

void Ctestdlg::onbnclickedbutton1 ()
{
TODO: Add control notification Handler code here
if (Btn_flag==false)

{

P_mybut[0] = Newmybutton (Idc_mybutton, CRect (20,20,50,60), Bs_defpushbutton);
P_MYBUT[1] = Newmybutton (idc_mybutton+1, CRect (70,20,95,50), 0);
P_MYBUT[2] = Newmybutton (idc_mybutton+50, CRect (120,20,140,35), 0);
btn_flag=p_mybut[0] && p_mybut[1] && p_mybut[2]; Btn_flag=true;
}

}

Also set a button (Idc_button2), press this button to destroy the previously created three new buttons.

void Ctestdlg::onbnclickedbutton2 ()
{
if (btn_flag)
{
if (p_mybut[0]) Delete p_mybut[0];
if (p_mybut[1]) Delete p_mybut[1];
if (p_mybut[2]) Delete p_mybut[2];
Btn_flag=false;
}
}

Set global variable: bool Btn_flag; used to indicate whether Button1 was pressed and three new buttons were created

Set global variables: CButton *p_mybut[3]; to store the new three buttons

The message response function for these three buttons is as follows: Note that you need to add parameters!

void Ctestdlg::onmybtn (UINT nID)
{
int index = Nid-idc_mybutton;

CString strbtn;
Strbtn.format (_t ("button%d"), index);
MessageBox (STRBTN);
}

========================================================

The last function is to press Button1 to generate three new buttons, each of which can trigger a messagebox. Press Button2 to destroy the three newly generated buttons.

"Reprint" MFC dynamically creates a control and its message response function

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.