VC Study Notes: Dialog Box

Source: Internet
Author: User

VC Study Notes: Dialog Box

SkySeraph NOV.11st 2009 HQU

Email-zgzhaobo@gmail.com QQ-452728574

Latest Modified Date: Oct.31th 2010 HQU

// Note: Sun Xin video Study Notes SkySeraph NOV.11st 2009 HQU

/* [Dialog box topic knowledge ]*/

// Knowledge point: the create dialog box is displayed;

// Create a Single Document Program

Void CMyBoleView: OnDialog () // response function of the dialog box

{

// Create a new dialog box resource and add a related class [CTestDlg] to operate the resource. Add a menu item (view class) to display the dialog box. [Each time a new dialog box is added, a related class needs to be added for related processing]

// Note: You must include testdlg. h In the view Class header file before using this class in the message response function of the menu item.

/* ■ 1. <create, display, and close Modal Dialog Box> */

CTestDlg dlg; // This is a local variable, but it doesn't matter, because the program will temporarily stop and wait until the modal dialog box is closed before running;

Dlg. DoModal (); // create and display the Modal Dialog Box

 

/* ■ 2. <create, display, and destroy non-Modal Dialog Box> */

//> Incorrect syntax: CTestDlg dlg;

// ▲It cannot be defined as a local variable. Two ways to deal with local variables: [1] defining member variables of the view class; [2] defining a pointer and allocating memory on the stack, because the memory on the stack is consistent with the entire life cycle of the Program (provided that the program cannot be manually destroyed)

CTestDlg * pDlg = new CTestDlg;

// ▲>> The pointer variable is a local variable. After the lifecycle ends, the memory address stored in the variable is lost and the program cannot reference it any more. solution:

// [1] defines the member variables of the class, and then releases the memory using the delete function in its destructor;

// [2] Reload the PostNcDestroy virtual function in the CTestDlg class to release the memory pointed to by this pointer.

PDlg-> Create (IDD_DIALOG1, this); // The first parameter can be the resource ID of the dialog box or the Template Name of the dialog box. The second parameter specifies the parent window. If it is NULL, it indicates the main application window.

PDlg-> ShowWindow (SW_SHOW );

// ▲> Differences between non-modal and modal modes: [1] displays the use of [2] local variables [3] After pressing the OK key, the modal function calls the OnOK virtual function, it will call the EndDialog function to terminate the modal dialog box, instead of destroying the modal dialog box, and only make it invisible, therefore, you must rewrite the OnOK virtual function (or OnCancel virtual function) of the base class and call the DestroyWindow function in the rewrite function.

PDlg-> DestroyWindow (); // destroy the non-Modal Dialog Box

Delete pDlg; // release pointer

 

/* ■ 3.1 <dynamic creation mode Dialog Box> */

CTestDlg dlg; // The local variable does not matter, because the program will temporarily stop and wait until the modal dialog box is closed before running.

Dlg. DoModal (); // create and display the Modal Dialog Box

}

 

 

/* ■ 3. <dynamic creation mode Dialog Box> */

// Function: When you click a button in the dialog box (in the right box), a new button is dynamically created in the dialog box.

Void CTestDlg: OnBtnAdd () // response function of the button Add

{

//> First, create the modeling state dialog box (in ondig1 of the view class, see 3.1). Then, Add a button in the IDD_DIALOG1 dialog box to Add a message processing function (CTestDlg class, that is, this function)

//> Then add the CButton private member variable m_bt to the CTestDlg class. Whether the m_bt object has been associated with the window to avoid repeated associations and errors. There are three implementation methods:

// Method 1: Boolean-type private member variable m_bIsCreated (initialized to FALSE in its constructor), used to identify whether a button window has been created

If (m_bIsCreated = FALSE) // make sure m_bt is not bound to a window

{

M_bt.Create ("New", BS_DEFPUSHBUTTON | WS_CHILD | WS_VISIBLE, CRect (100,200, 123), this,); // Create of the CButton class is used to Create buttons. If no WS_VISIBLE exists, add ShowWindow.

M_bIsCreated = TRUE;

}

Else

{

M_bt.DestroyWindow ();

M_bIsCreated = FALSE;

}

// Method 2: If you do not need the member variable m_bISCreated, you can also use the following method:

Static BOOL bIsCreated = FALSE; // Note: It must be static. Otherwise, the OnBtAdd function is redefined every time it is called, and the required functions cannot be implemented. For static, when it is loaded for the first time, the system allocates memory space for it and initializes it to FALSE. When OnBtnAdd is entered, the static memory space will not be allocated and initialized!

// For more information about static, see the http://www.cnblogs.com/skyseraph/archive/2010/10/11/1848115.html

If (bIsCreated = FALSE) // make sure m_bt is not bound to a window

{

M_bt.Create ("New", BS_DEFPUSHBUTTON | WS_CHILD | WS_VISIBLE, CRect (100,200, 123), this,); // Add ShowWindow if no WS_VISIBLE exists

BIsCreated = TRUE;

}

Else

{

M_bt.DestroyWindow ();

BIsCreated = FALSE;

}*/

// Method 3: The CWnd class has a member variable m_hWnd used to save the window handle associated with the window object. If the window handle is not associated with any window, the handle is NULL.

If (! M_bt.m_hWnd)

{

M_bt.Create ("New", BS_DEFPUSHBUTTON | WS_CHILD | WS_VISIBLE, CRect (100,200, 123), this );

}

Else

{

M_bt.DestroyWindow ();

}

}

 

  • /* ■ 4. <access to static text controls> */

// Function: When you click a static text (Number1 :), convert the text to "value 1 :"

Void CTestDlg: OnNumber1 () // response function of static text Number1

{

// Add a dialog box and class, and add a menu to display (same as before)

// Add the static text Number1:, change its ID (because the static text is generally only used for its label, its ID is the same by default, in special cases, you need to change it when you need to operate on it)

// Note: by default, static text controls do not send notification messages. You need to check the Notify option.

CString str;

If (GetDlgItem (IDC_NUMBER1)-> GetWindowText (str), str = "Number1:") // one way to use the if statement

// The control is actually a window. To obtain the text displayed on the static text control, you can use the CWnd class GetWindowText to obtain the static text box object. Previously, GetDlgItem must be used to obtain the static text box object.

// HWND GetDlgItem (HWND hDlg, int nIDDlgItem); // virtual CWnd * GetDlgItem (int nID) const;

{

GetDlgItem (IDC_NUMBER1)-> SetWindowText ("value 1 :");

}

Else

{

GetDlgItem (IDC_NUMBER1)-> SetWindowText ("Number1 :");

}

}

 

  • /* ■ 5. <edit box control> */

// Function: enter a number in the first two edit boxes, and click Add to display the number and the Add button in the third edit box.

Void CTestDlg: OnBtnAdd ()

{

// Method 1: GetDlgItem-> GetWindowText

Int num1, num2, num3;

Char comment [10], ch2 [10], ch3 [10];

 

GetDlgItem (IDC_EDIT1)-> GetWindowText (rows, 10 );

GetDlgItem (IDC_EDIT2)-> GetWindowText (ch2, 10 );

 

Num1 = atoi (random); // atoi converts a numeric string

// Int atoi (const char * str); Convert a string to integer

Num2 = atoi (ch2 );

Num3 = num1 + num2;

 

Itoa (num3, ch3, 10); // converts a value into text in decimal format

// Char * _ itoa (int value, char * str, int radix );

GetDlgItem (IDC_EDIT3)-> SetWindowText (ch3 );

 

// Method 2: GetDlgItemText

Int num1, num2, num3;

Char comment [10], ch2 [10], ch3 [10];

 

GetDlgItemText (IDC_EDIT1, latency, 10); // a combination of GetDlgItem and GetWindowText

GetDlgItemText (IDC_EDIT2, ch2, 10 );

// Virtual int GetDlgItemText (int nID, LPTSTR lpStr, int nMaxCount) const;

 

Num1 = atoi (parts );

Num2 = atoi (ch2 );

Num3 = num1 + num2;

 

Itoa (num3, ch3, 10 );

SetDlgItemText (IDC_EDIT3, ch3 );

 

// Method 3: GetDlgItemInt

Int num1, num2, num3;

Num1 = GetDlgItemInt (IDC_EDIT1); // return the text of the specified control and convert it to an integer value. Negative values can be implemented.

// Virtual UINT GetDlgItemInt (int nID, BOOL * lpTrans = NULL, BOOL bSigned = TRUE) const;

 

Num2 = GetDlgItemInt (IDC_EDIT2 );

Num3 = num1 + num2;

SetDlgItemInt (IDC_EDIT3, num3 );

 

// Method 4: Associate controls with Integer Variables

// Associate the three edit boxes with the three member variables of the dialog box class, and then use these member variables to retrieve and set the text of the edit box

// Description: When associating a variable member, the variable category is value and the type is int [depending on the actual needs]. You can view the header file and source file to see what changes have taken place?

// [Three items in total: add definitions to the macro in the header file, initialize the source file constructor, and associate the dialog box controls in DoDataExchange with class member variables, such as DDX_Text (pDX, IDC_EDIT1, m_num1)...] // functions prefixed with DDX _ are used for data exchange between different controls.

// Note: The DoDateExchange function is never directly called in a program, but called through UpdataData (), another member function of CWnd.

UpdateData (); // obtain data

// BOOL UpdateData (BOOL bSaveAndValidate = TRUE); If the parameter is TRUE, the function is obtaining the data in the dialog box. If the parameter is FALSE, the function is initializing the control in the dialog box.

// When a modal dialog box is created, the UpdateData function is automatically called with the FALSE parameter to initialize the control.

M_num3 = m_num1 + m_num2;

UpdateData (FALSE); // initialization dialog box

// You can also select the IDC_EDIT1 control on the member variables tab in ClassWizard and enter 0 and 100 in the "min... He max..." edit box at the bottom of the dialog box to set the input range of the edit box.

// Add DDV_MinMaxInt (pDXp, n_num1, 0,100) to DoDataExchange );

// DDX: Dialog Data Exchange Dialog box Data Exchange DDV: Dialog Data Validation Dialog box Data verification

 

 

// Method 5: Associate controls with control variables

// The edit box is associated with the control variables. These control variables are the control, that is, the variable category is control, and the type is CEdit/derived from CWnd.

Int num1, num2, num3;

Char comment [10], ch2 [10], ch3 [10];

 

M_edit1.GetWindowText (optional, 10 );

M_edit2.GetWindowText (ch2, 10 );

 

Num1 = atoi (parts );

Num2 = atoi (ch2 );

Num3 = num1 + num2;

 

Itoa (num3, ch3, 10 );

M_edit3.SetWindowText (ch3 );

 

// Method 6: SendMessage

// Mechanism: windows programs are message-based. Therefore, to obtain or set the window text, you only need to know the message for obtaining or setting the window text, and you can send it through sendmessage, to obtain or set the text of the window. In windows, the message for obtaining window text is WM_GETTEXT. After the message is sent, the system copies the specified window text to a cache provided by the caller. In the two parameters of the message, wParam specifies the number of bytes to be copied. lParam is the cache address provided by the caller to save the window text. The window text message is set to WM_SETTEXT. The wParam message is not used, and the lParam message is used to specify the string address of the window text.

Int num1, num2, num3;

Char comment [10], ch2 [10], ch3 [10];

 

: SendMessage (GetDlgItem (IDC_EDIT1)-> m_hWnd, WM_GETTEXT, 10, (LPARAM) Success );

// The Platform SDK and the CWnd class both provide the SendMessage function, plus: Call the Platform SDK Function

: SendMessage (m_edit2.m_hWnd, WM_GETTEXT, 10, (LPARAM) ch2 );

 

Num1 = atoi (parts );

Num2 = atoi (ch2 );

Num3 = num1 + num2;

 

Itoa (num3, ch3, 10 );

M_edit3.SendMessage (WM_SETTEXT, 0, (LPARAM) ch3 );

 

// Method 7: SendDlgItemMessage directly sends a message to the subcontrol of the dialog box.

Int num1, num2, num3;

Char comment [10], ch2 [10], ch3 [10];

 

SendDlgItemMessage (IDC_EDIT1, WM_GETTEXT, 10, (LPARAM) messages); // combines GetDlgItem and SendMessage. You do not need to obtain the child control object before sending the message, because it is used to send messages to its subcontrol in the dialog box.

SendDlgItemMessage (IDC_EDIT2, WM_GETTEXT, 10, (LPARAM) ch2 );

// LRESULT SendDlgItemMessage (HWND hDlg, int nIDDlgItem, UINT Msg, WPARAM wParam, LPARAM lParam );

 

Num1 = atoi (parts );

Num2 = atoi (ch2 );

Num3 = num1 + num2;

 

Itoa (num3, ch3, 10 );

SendDlgItemMessage (IDC_EDIT3, WM_SETTEXT, 0, (LPARAM) ch3 );

 

SendDlgItemMessage (IDC_EDIT3, EM_SETSEL, 1, 3); // you can specify the start position and the end position in the [EM_GETSEL] edit box. wParam indicates the start position and lParam indicates the end position.

// Special case: When wParam is 0. When lParam is-1, all content in the edit box will be checked. This special case is very useful.

// A Message starting with EM _ indicates the Edit Control Message)

M_edit3.SetFocus (); // you need to move the current focus [after clicking it, on the button] To the edit box to make the check.

}

  • /* ■ 6. <Implementation of the dialog box scaling function> */

Add a button for the dialog box, set the attribute to "contract <", and add a message response function for it.

Void CTestDlg: OnButton1 ()

{

// TODO: Add your control notification handler code here

// ① Button text Conversion Function

CString str;

If (GetDlgItemText (IDC_BUTTON1, str), str = "shrink <")

{

SetDlgItemText (IDC_BUTTON1, "extension> ");

}

Else

{

SetDlgItemText (IDC_BUTTON1, "shrink <");

}

 

// ② Implementation of cutting: place the separation bar first and implement it with an image control. The ID is IDC_SEPERATOR. Use the Sunken style (down state) to remove visibale and make it invisible.

Static CRect rectLarge; // after obtaining the variable for the first time, it does not need to be changed again in the future, so it is set to static

Static CRect rectSmall;

 

// Judge whether the rectangle is empty: CRect has two member functions: IsRectNull and IsRectEmpty. The two are different. Refer to MSDN

If (rectLarge. IsRectNull ())

{

CRect rectSeparator;

GetWindowRect (& rectLarge); // obtain the original size

GetDlgItem (IDC_SEPARATOR)-> GetWindowRect (& rectSeparator );

 

RectSmall. left = rectLarge. left;

RectSmall. top = rectLarge. top;

RectSmall. right = rectLarge. right;

RectSmall. bottom = rectSeparator. bottom;

}

If (str = "shrink <")

{

SetWindowPos (NULL, 0, 0, rectSmall. Width (), rectSmall. Height (),

SWP_NOMOVE | SWP_NOZORDER );

// Virtual BOOL SetWindowPos (const CWnd * pWndInsertAfter, int x, int y, int cx, int cy, UINT nFlags );

// Sets the size, position, and Z order of the control site.

// Window Z sequence: indicates the position of the window in the overlapping heap

}

Else

{

SetWindowPos (NULL, 0, 0, rectLarge. Width (), rectLarge. Height (),

SWP_NOMOVE | SWP_NOZORDER );

}

}

Conclusion: ① implement button change function ② implement window Separation

 

Author: SKySeraph

Email/GTalk: zgzhaobo@gmail.com QQ: 452728574

From: http://www.cnblogs.com/skyseraph/

The copyright of this article is shared by the author and the blog. You are welcome to repost this article. However, you must keep this statement without the author's consent and provide the original article connection clearly on the article page. Please respect the author's Labor achievements.

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.