Sun Xin VC study notes 7 (1)

Source: Internet
Author: User
Tags integer numbers

1. Dynamic Creation button

Enum {IDD = idd_dlg1}; associate the dialog box with the class...

1) Add the global variable cbutton m_btn;

//It cannot be defined as a local variable. 1) it is a solution;

// Solution 2: Define pointers and allocate memory on the heap, which is the same as the entire application lifecycle.

Pdlg-> Create

Pdlg-> showwindow

2) Where you want to create

// ID 123 can be changed at Will //Indeed

Mode: domodal

Non-modal mode:

Create multiple timesIllegal access:

Method 2:

Static bool biscreate = false; // if it is set to static, it will be initialized for the first time and will not be changed later.

Method 1:

If (m_biscreate = false) {} else... // The following is an upgraded version

Method 3:

If (! M_btn.m_hwnd )//If this condition is not determined, it will be created multiple times.

M_btn.Create("Restoration", bs_defpushbutton | ws_visible | ws_child, crect (100,100, 123), this );

Else

M_btn.destroywindow ();//M_biscreate = false;

2. Copy the control

When you add a control on the DLG, press Ctrl and drag to copy the same control.

3. Control alignment

Alignment of multiple controls in the dialog box. You can use the layout menu or the toolbar in the lower left corner.

4. dynamically edit the static text box

Cstring STR;

If (getdlgitem (idc_number1)-> getwindowtext (STR), STR = "number1 :")

Getdlgitem (idc_number1)-> setwindowtext ("value 1 :");

Else

Getdlgitem (idc_number1)-> setwindowtext ("number1 :");

To make the static text box respond to a message, check the notify option.

Modify the property page

5. edit text box

GET/set text content

1) method 1

Char ch [10];

Getdlgitem (idc_edit1)-> getwindowtext (rows, 10 );

Getdlgitem (idc_edit3)-> setwindowtext (ITOA (atoi ),Token, 10 ));

2) method 2

Getdlgitemtext (idc_edit1, latency, 10 );

Setdlgitemtext (idc_edit3, ITOA (atoi (parts), parts, 10 ));//10 is in hexadecimal format.

3) method 3

Setdlgitemint (idc_edit3, getdlgitemint (idc_edit1 ));//It is useful for strings of integer numbers.

4) Association VARIABLE METHOD

Associate a variable with each edit control,Remember to use updatedata () after setting ()

You can define two types of variables: Value and control.

Dodataexchange () called by the Framework to exchange and validate dialog data

The framework maid set to false when a modal dialog box is created in the default Implementation of cdialog: oninitdialog.

Set Value to int type.

Void ctestdlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
// {Afx_data_map (ctestdlg)
Ddx_control (PDX, idc_edit3, m_edit3 );
Ddx_control (PDX, idc_edit2, m_edit2 );
Ddx_control (PDX, idc_edit1, m_edit1 );
Ddx_text (PDX, idc_edit1, m_num1 );
Ddv_minmaxint (PDX, m_num1, 0,100 );
Ddx_text (PDX, idc_edit2, m_num2 );
Ddv_minmaxint (PDX, m_num2, 0,100 );
Ddx_text (PDX, idc_edit3, m_num3 );
//} Afx_data_map
}

The above variables are associated with the control...

Ddx_text: Associate the control with the member variable dialog data exchange

Ddv_minmaxint: dialog data verification

Msdn search for DDV _ a large number

DodataexchangeIt is called by the Framework to exchange data.

// Problem: If m_num3 = m_num1 + m_num2; m_num1, num2 is displayed correctly in the dialog box, but num1 and num2 are both 0 in the debug run, the value is not obtained. Check msdn as follows:

Never call this function directly. It is called by the updatedata member function. CallUpdatedataTo initialize a dialog box's controls or retrieve data from a dialog box.

BOOL UpdateData( BOOL bSaveAndValidate = TRUE ); 
Bsaveandvalidate

Flag that indicates whether dialog box is being initialized (False) Or data is being retrieved (True).
Updatedata ()Intuitive value acquisition

Control variable: represents the control itself...

5) Use wm_gettext message processing to obtain text

Char limit [10];

You can use the following four methods (m_edit1 is the associated control variable)

Add the following in the onbtnadd function:

: Sendmessage (getdlgitem (idc_edit1)-> m_hwnd, wm_gettext, 10, (lparam) Success );

: Sendmessage (m_edit1.m_hwnd, wm_gettext, 10, (lparam) Success );//A control variable has been associated, so you can directly call its m_hwnd to get the handle.

Getdlgitem (idc_edit1)-> sendmessage (wm_gettext, 10 ,(Lparam) Dependencies );

M_edit1.sendmessage (wm_gettext, 10, (lparam) Success );//This is the simplest individual scenario.

//The following code is the same:

Num1 = atoi (parts );
Num2 = atoi (CH2 );
Num3 = num1 + num2;
ITOA (num3, CH3, 10 );

Again:Set text using wm_settext Message Processing

M_edit3.sendmessage (wm_settext, 0, (lparam) CH3 );

6) send messages directly to the dialog box Control

Senddlgitemmessage (idc_edit1, wm_gettext, 10, (lparam) messages); // get text

//Combined: Get the dialog box handle + call the sendmessage Function

UsingSenddlgitemmessageIs identical to retrieving a handle to the specified control and calling the sendmessage function.

Senddlgitemmessage (idc_edit3, wm_settext, 0, (lparam) CH3); // sets the text

Senddlgitemmessage (idc_edit3, em_setsel, 0,-1 );

7) ExploitationEm_getsel,Em_setselMessage Processing

Senddlgitemmessage (idc_edit3, em_setsel, 0, 3 );

//Problem: The selected parts are not displayed. Because after you click the Add button, focus on the Add button.

M_edit3.setfocus ();

Senddlgitemmessage (idc_edit3, em_setsel, 0,-1);//Select All texts

// Summary:

Ngetdlgitem ()-> get (SET) windowtext ()

Ngetdlgitemtext ()/setdlgitemtext ()

Ngetdlgitemint ()/setdlgitemint ()

N associates controls with Integer Variables

N associate controls with control variables

Nsendmessage ()

Nsenddlgitemmessage ()

6. the dialog box is shrunk.

Click "contract <" dialog box to contract, and click "extend>" to expand. See the example code.

Step 1: Add a button and change caption to "shrink <"

When you click, remove a part of the window and change it to "Expansion>"

Ctestdlg: onbutton2 Function

Void ctestdlg: onbutton2 ()
{

//Step 2:
// Todo: add your control notification handler code here
Cstring STR;
If (getdlgitemtext (idc_button2, STR), STR = "shrink <")
{
Setdlgitemtext (idc_button2, "extension> ");
}
Else
{
Setdlgitemtext (idc_button2, "shrink <");
}

//Separator: It is represented by a graphical control. It is pulled into a line and changed to idc_separator. Click sucken on the property page to show the down style.

// Key point: The X coordinate remains unchanged after contraction, but the length of Y direction changes.

Static crect rectlarge;
Static crect rectsmall;

//If the size of the dialog box is retained, it cannot be repeatedly defined. It must be declared as static.
If (rectlarge. isrectnull ())
{

//Note:

Isrectempty

Determines whetherCrectIs empty.CrectIs empty ifWidth and/or height are 0 or negative.

Isrectnull

Determines whetherTop,Bottom,Left, AndRightMember variables are all equal to 0.

Crect rectseparator;
Getwindowrect (& rectlarge );
Getdlgitem (idc_separator)-> getwindowrect (& rectseparator );

Rectsmall. Left = rectlarge. Left;
Rectsmall. Top = rectlarge. Top;
Rectsmall. Right = rectlarge. Right;
Rectsmall. Bottom = rectseparator. Bottom ;//Only the ordinate values in the lower right corner have changed.
}
If (STR = "shrink <")
{
Setwindowpos (null, 0, 0, rectsmall. Width (), rectsmall. Height (),
Swp_nomove | swp_nozorder );

Windows system manages three independent Z-order windows: one for top-level windows, one for brothers windows, and the other for top-level windows. The top-level window overwrites all other non-top-level windows, whether it is an active window or a front-end window. The application creates the top-level window by setting the ws_ex_topmost style.

Generally, Windows puts the newly created window at the top of the Z sequence. You can change the Z sequence by activating another window; in Windows, the active window is always placed at the top of the Z sequence, and the application can use the bringwindowtotop function to place a window at the top of the Z sequence. The setwindowpos and deferwindowpos functions are used to sort the Z order.

At any time, only one top-level window in the system is active. You can click a window (or a subwindow), use Alt + TAB or Alt + ESC to activate a top-level window, and the application calls the setactivewindow function to activate a top-level window.

You can click a window, and use Alt + TAB or Alt + ESC to set the foreground window. The application uses the setforegroundwindow function to set the foreground window. If the new foreground window is a top-level window, the Windows system activates it. In other words, the Windows system activates the corresponding top-level window.

//For more information, see PPT !!

}
Else
{
Setwindowpos (null, 0, 0, rectlarge. Width (), rectlarge. Height (),
Swp_nomove | swp_nozorder );
}
}

//Finally, set the visible of idc_separator to No.

7. There are three methods to switch between multiple edit boxes using the Enter key.

OKButton: properties --> styles --> default botton check, set to default

// Deselect the check box.

// See the following powerful functions.

LONG SetWindowLong( 

HWND hWnd, int nIndex, LONG dwNewLong );

If the function succeeds, the return value is the previous value of the specified 32-bit integer.

//

1) capture the Keyboard Message and process it in the message function (not provided)

Step 1:

2) modify the edit window process: Write the Window Process by yourself to replace the original Window Process (more troublesome)

(1) define Window Process type variables

Wndproc prevproc ;//It is defined before the ctestdlg: oninitdialog () function.

Step 3: how to write the window process? Check wndclass !!

(2) define Window Process Functions

Lresult callback winsunproc (hwnd, uint umsg, wparam, lparam)

{

If (Umsg = wm_char & wparam = 0x0d) // IfWm_char message andYesenter

{

//: Setfocus (::Getnextwindow(Hwnd, gw_hwndnext); // method 1 for obtaining the next window handle

Note:Edit1: // styles -- multilineYou can return to the carriage return message after selecting this option !!

Gw_hwndnext
Returns a handle to the window below the given window.
Gw_hwndprev
Returns a handle to the window abve the given window.

// Setfocus (::Getwindow(Hwnd, gw_hwndnext); // method 2

Setfocus (::Getnextdlgtabitem(: Getparent (hwnd), hwnd, false); // method 3

// Attribute while General --> tab stop (static text does not exist)

// False: Search for the next control. True: Search for the previous control.

Return 1;

}

Else

Return prevproc (hwnd, umsg, wparam, lparam );

}

//Step 2:

(3) Add the function corresponding to wm_initdialog

(4) add in oninitdialog //Response wm_initdialog !!

Note: sent to a dialog box before the dialog box is displayed

Prevproc = (wndproc) setwindowlong (getdlgitem (idc_edit1)-> m_hwnd,Gwl_wndproc, (Long) winsunproc );

Gwl_wndproc

Sets a new address for the window procedure.

(5) Note that the multiline check attribute of the Edit Control is different.

Setwindowlong changes an attribute of the specified window.

3) In onok (the function corresponding to the default button)

//Use the onok (default response) function to automatically press the Enter key.

Void ctestdlg: onok ()
{
// Todo: add extra validation here
// Getdlgitem (idc_edit1)-> getnextwindow ()-> setfocus ();//It is set to edit1, commented on
// Getfocus ()-> getnextwindow ()-> setfocus ();//Press enter until illegal access occurs;

// As you press enter all the time, getfocus ()-> getnextwindow ()-> Returns a null pointer...
// Getfocus ()-> getwindow (gw_hwndnext)-> setfocus (); // gw_hwndnextObtain the next window
Getnextdlgtabitem (getfocus ()-> setfocus ();//True and true. The default value of the second parameter is false. Search for the next control.
// Cdialog: onok ();
}

Getfocus ()-> getnextwindow ()-> setfocus (); // You must judge the last window. Otherwise, an error occurs.

Getfocus ()-> getwindow (gw_hwndnext)-> setfocus (); // same as above

Getnextdlgtabitem (getfocus ()-> setfocus ();

Layout --> tab Order (shortcut cut: Ctrl + D)

If the shrink button is set to default, the idok response function does not respond.

Note: The initial OK ID of the dialog box is idok. Even if the delete button (the onok function exists), The onok function is still returned.

Note that the missing ones, though idok rather than idc_ OK, should be remembered !!

7th course Dialog Box user interface program Compilation

Add button dynamically
Subwindow on the access dialog box:
Get the project pointer on the dialog box: getdlgitem ()
Get window information: getwindowtext ()
Change window information: setwindowtext ()
Directly obtain the information of the items in the specified dialog box: getdlgitemtext () is used by getdlgitem () and getwindowtext.
Of course, setdlgitemtext () is equivalent to getdlgitem () and setitemtext.
Getdlgitemint (), setdlgitemint (), etc. S/getdlgitemint () can process signed integers.
Conversion from character to array: When atoi () is used to convert a type to a specified type, the first letter of the type is used to specify a Letter of the type.
In the doexchange function, place a function prefixed with DDX _ to associate a control with a variable. A function prefixed with DDV _ is used to verify the content of a control.
DDX _ (dialog box Data Exchange) DDV _ (dialog box data verification)
Note: When using data variables to associate controls, always use updatedata ()!
You can also associate a control with a control variable and use its member functions to operate the control, for example, cedit. getwindowtext ()
Sendmessage () is easy to use. Note that the control sends messages to the system for processing.
Senddlgitemmessage ().

Conclusion:
1, getdlgitem ()-> G/setwindowtext ().
2, G/setdlgitemtext ().
3, G/setdlgitemint.
4. associate a control with an integer variable.
5. associate a control with a control variable and use the member functions getwindowtext () and setwindowtext () to access the control.
6. sendmessage (). You can use the platform SDK or the cwnd member function.
7. senddlgitemmessage ().
Two useful messages: em_getsel and em_setsel: Get and set the location of the selected content.
Change the window size. The WND: setwindowpos () dialog box inherits functions from the parent class.
Let's take a look at the members, such as the default button, when using control variables to associate controls.
Message: wm_initdialog. After a dialog box is created with the control on it, it is sent by the dialog box. At that time, the dialog box is not displayed yet.
Sent to a dialog box before the dialog box is displayed.

Scaling dialog box:
Determines whether a rectangle is empty: isrectempty (), isrectnull (). The former determines whether the rectangle area is empty, and the latter determines whether the four coordinate values of the rectangle are 0, regardless of whether the rectangle can be made.
· Setwindowpos () changes the window size and Z order. ·····
Focus:
Setwindowlong ~~~~~~~~ Super powerful functions.
: Getnextwindow ()
: Getwindow ()
: Getnextdlgtabitem ()
Cwnd: getwindow ()
Cwnd: getnextwindow
Cwnd: getnextdlgtabitem ()
The default OK button ID is idok.

 

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.