The compilation of the dialog box user interface program, how to connect the data member and its implementation mechanism to the dialog box control, how to associate the control class with the dialog box control, how to Use the member functions of the dialog box class to send messages to the control and obtain the class pointer of the dialog box control, and how to directly use the dialog box control class to manipulate the control (send messages and directly call the member functions ). How to generate and destroy controls when the program is running. Comparison and analysis of the advantages and disadvantages of the dialog box controls. How to partially contract and expand the dialog box. How to make the text box on the dialog box get the focus immediately after the program starts, how to use setwindowlong to change the callback function of the window, and how to change the default carriage return Processing Method of the text box for demonstration. Another clever way to transfer the focus one by one between multiple input text boxes (with the default button ).
1. Modal Dialog Box and non-Modal Dialog Box
The modal dialog box refers to the dialog box that "the displayed dialog box cannot be clicked below", and the non-modal dialog box is vice versa.
Differences between the two:
1. The template of the non-modal dialog box must have the visible style (visible = true); otherwise, the dialog box will not be visible, and the modal dialog box does not need to set this style. In actual programming, the more safe way is to callCwnd: showwindow (sw_show)To display the dialog box, regardless of whether the dialog box has the visible style.
2. Non-Modal Dialog Box objects are dynamically created using the new operator, rather than being embedded into other objects in the form of member variables or constructed in the form of local variables. Generally, a pointer member variable pointing to the dialog box class should be declared in the dialog box owner's window class, through which the dialog box object can be accessed.
3. CallCdialog: CreateFunction to start the dialog box, insteadCdialog: domodalThis is the key to the difference between the two. Since the create function does not start a new message loop, the dialog box shares the same message loop with the application, so that the dialog box will not monopolize user input. Create returns the result immediately after the dialog box is displayed, while domodal returns the result only after the dialog box is closed. After the return result of create, you cannot determine whether the dialog box is closed or not, so you cannot determine the lifetime of the dialog box object. Therefore, you have to build the dialog box object in the stack, it cannot be constructed in the form of local variables.
4. required callsCwnd: destroywindowInsteadCdialog: enddialogTo close the non-modal dialog box. Calling cwnd: destroywindow is a common method for directly deleting a window. Because the default cdialog: onok and cdialog: oncancel functions call enddialog, the programmer must write their own onok and oncancel functions and call destroywindow in the function to close the dialog box.
5. Because the non-Modal Dialog Box object is built using the new operator, you must use the delete operator to delete the dialog box object after the dialog box is closed. After a window is deleted on the screen, the Framework calls cwnd: postncdestroy, which is a virtual function. The program can delete window objects in this function. The Code is as follows:
Void cmodelessdialog: postncdestroy
{Delete this; // Delete object}
In this way, after the dialog box is deleted, the dialog box objects are automatically deleted. The owner does not need to explicitly call Delete to delete the dialog box objects.
6. There must be a flag indicating whether the non-modal dialog box is opened. The reason for this is that you may choose to open the command again when opening a modal dialog box. The program determines whether to open a new dialog box or simply activate the original Dialog Box Based on the flag. Generally, the pointer to the dialog box object in the owner window can be used as this flag. When the dialog box is closed, a null value is assigned to the pointer to indicate that the dialog box object does not exist.
For example:
Create Modal Dialog Box
Ctestdlg DLG;
DLG. domodal ();
Create non-Modal Dialog Box
Ctestdlg * DLG = new ctestdlg;
DLG-> Create (idd_test_dlg );
DLG-> showwindow (sw_show );
2. Add button controls and create buttons in the dialog box:
You can add a button control in a dialog box and create a dynamic button through the button control. However, you must determine whether a button has been created before each creation, if it is created, it will be destroyed. Otherwise, a button will be created!
Static bool biscreate = false; // It is determined by a static variable! // If (m_biscreate = false) // determines whether the button has been created! If (biscreate = false) {m_btn.create (text ("AAA"), bs_defpushbutton | ws_visible | ws_child, crect (0, 0,100,100), this, 123 ); // create a button! The button is actually a window! // M_biscreate = true; biscreate = true;} else {m_btn.destroywindow (); // m_biscreate = false; biscreate = false;} If (! M_btn.m_hwnd) // you can use the handle of the button to determine! {M_btn.create (text ("AAA"), bs_defpushbutton | ws_visible | ws_child, crect (0, 0,100,100), this, 123);} else {m_btn.destroywindow ();}
3. In the dialog box, perform the following operations on the static text box and the edit text box:
1. The static text box cannot receive announcement messages. If you want it to receive announcement messages, you need to modify the following three aspects: A. Modify the ID of the text box. B. Check notify in the attribute! C. Add a message response function. The Code is as follows:
Cstring STR; If (getdlgitem (idc_number1)-> getwindowtext (STR), STR = text ("number1:") // obtain the sub-control pointer first, modify the content in the static text box! {Getdlgitem (idc_number1)-> setwindowtextw (text ("value 1:");} else {getdlgitem (idc_number1)-> setwindowtextw (text ("number1 :"));}
The implementation function of the above Code is: When you click the content in the static text box, convert it to Chinese if it is English, and convert it to English if it is Chinese!
II.There are seven ways to access the dialog box control:
Getdlgitem ()-> get (SET) windowtext () getdlgitemtext ()/setdlgitemtext () getdlgitemint ()/setdlgitemint () associate the control with the integer variable. Associate the control with the control variable sendmessage () senddlgitemmessage (). The specific implementation code of this method is as follows:
// Seven operations on the content in the edit box! // Method 1: int num1, num2, num3; tchar values [10], CH2 [10], CH3 [10]; getdlgitem (idc_edit1)-> getwindowtextw (values, 10); getdlgitem (idc_edit2)-> getwindowtextw (CH2, 10); num1 = _ ttoi (bytes); // converts a character to a value! Num2 = _ ttoi (CH2); num3 = num1 + num2; _ itot_s (num3, CH3, 10); // converts a value to a character! 10 represents decimal! Instead of the capacity of its array! Getdlgitem (idc_edit3)-> setwindowtextw (CH3); // Method 2: int num1, num2, num3; tchar values [10], CH2 [10], CH3 [10]; getdlgitemtext (idc_edit1, success, 10); // get the content of the text box getdlgitemtext (idc_edit2, CH2, 10); num1 = _ ttoi (SUCCESS); num2 = _ ttoi (CH2 ); num3 = num1 + num2; _ itot_s (num3, CH3, 10); setdlgitemtext (idc_edit3, CH3); // set the content of the text box! // Method 3: int num1, num2, num3; num1 = getdlgitemint (idc_edit1); // obtain the content of the text box and convert it to the integer number! Num2 = getdlgitemint (idc_edit2); num3 = num1 + num2; setdlgitemint (idc_edit3, num3); // Method 4 updatedata (); // refresh the value of the control to the corresponding variable m_num3 = m_num1 + m_num2; // The Int member variable updatedata (false) associated with the text box ); // copy the variable value to the control display // fifth method int num1, num2, num3; tchar values [10], CH2 [10], CH3 [10]; m_edit1.getwindowtextw (values, 10); // The member variable of the control associated with the text box! M_edit2.getwindowtextw (CH2, 10); num1 = _ ttoi (rows); num2 = _ ttoi (CH2); num3 = num1 + num2; _ itot_s (num3, CH3, 10 ); m_edit3.setwindowtextw (CH3); // Method 6/* int num1, num2, num3; tchar values [10], CH2 [10], CH3 [10]; */: sendmessage (getdlgitem (idc_edit1)-> m_hwnd, wm_gettext, 10, (lparam) handle); // use the API function: sendmessage (m_edit1.m_hwnd, wm_gettext, 10, (lparam) response); getdlgitem (idc_edit1)-> sendmessage (wm_gettext, 10, (lpar Am) consumed); m_edit1.sendmessage (wm_gettext, 10, (lparam) consumed); // use the m_edit2.sendmessage (wm_gettext, 10, (lparam) CH2) function in WND ); num1 = _ ttoi (bytes); num2 = _ ttoi (CH2); num3 = num1 + num2; _ itot_s (num3, CH3, 10); m_edit3.sendmessage (wm_settext, 10, (lparam) CH3); // Method 7 int num1, num2, num3; tchar limit [10], CH2 [10], CH3 [10]; senddlgitemmessage (idc_edit1, wm_gettext, 10, (lparam) handle); // its function is equivalent to getdlgitem () and sendmessage (). Function functions! Senddlgitemmessage (idc_edit2, wm_gettext, 10, (lparam) CH2); num1 = _ ttoi (bytes); num2 = _ ttoi (CH2); num3 = num1 + num2; _ itot_s (num3, CH3, 10); senddlgitemmessage (idc_edit3, wm_settext, 10, (lparam) CH3); senddlgitemmessage (idc_edit3, em_setsel, 1, 3 ); // set the content to be checked in the text! M_edit3.setfocus (); // sets the focus!
4. contraction and expansion of the dialog box:
You can use a button on the dialog box to scale the dialog box. The main step is to obtain the size of the window before splitting and the size of the window after splitting, the implemented function is setwindowpos (). The implementation code is as follows:
Cstring STR; If (getdlgitemtext (idc_button2, STR), STR = text ("contract <") {setdlgitemtext (idc_button2, text ("extension> "));} else {setdlgitemtext (idc_button2, text ("shrink <");} static crect rectlarge; // Save the size of the entire dialog box static crect rectsmall; // Save the size of the cut dialog box if (rectlarge. isrectnull () {crect rectseparator; // rectangular size of the split line! Getwindowrect (& rectlarge); getdlgitem (idc_separator)-> getwindowrect (& rectseparator); rectsmall. left = rectlarge. left; rectsmall. right = rectlarge. right; rectsmall. top = rectlarge. top; rectsmall. bottom = rectseparator. bottom;} If (STR = text ("contract <") {setwindowpos (null, 0, 0, rectsmall. width (), rectsmall. height (), swp_nomove | swp_nozorder); // contraction and expansion of the completed window} else {setwindowpos (null, 0, 0, rectlarge. width (), rectlarge. height (), swp_nomove | swp_nozorder );}
5. Focus settings:
There are two methods here. The first method is implemented through the callback function. However, in vs2008, errors may occur, which cannot be solved, and the method is quite troublesome, the following code is provided:
Wndproc prevproc; lresult callback windowproc (hwnd, uint umsg, wparam, lparam) {If (umsg = wm_char & 0x0d = wparam ){//:: setfocus (: getnextwindow (hwnd, gw_hwndnext); // obtain the handle of the next window, and then set the focus //: setfocus (: getwindow (hwnd, gw_hwndnext )); setfocus (: getnextdlgtabitem (getparent (hwnd), hwnd, false); return 1 ;}else {return prevproc (hwnd, umsg, wparam, lparam) ;}} bool ctestdlg:: oninitdialog () {cdialog: oninitdialog (); // todo: add an additional initialization prevproc = (wndproc) setwindowlong (getdlgitem (idc_edit1)-> m_hwnd, gwl_wndproc, (long) windowproc); Return true; // return true unless you set the focus to a control // exception: OCX attribute page should return false}The second method is mainly used in the onok () function. The default feature can be used to complete the functions we need. The following code line can be implemented:
// Getdlgitem (idc_edit1)-> getnextwindow ()-> setfocus (); // getfocus ()-> getnextwindow ()-> setfocus (); // get the window pointer of the current focus, then get the pointer of the next window, and set the focus! /Getfocus ()-> getwindow (gw_hwndnext)-> setfocus (); // same as the above function! Getnextdlgtabitem (getfocus ()-> setfocus ();