VC Study Notes: Enter key

Source: Internet
Author: User

VC Study Notes: Enter key

SkySeraph NOV.9th 2009 HQU QQ-452728574

Latest Modified Date: NOV.9th 2010 HQU

Enter the key to transfer the input focus

0 early stage: create a single document project; add dialog box, join class CTestDlg; add menu, add View class message processing function; View header file contains CTestDlg

1. function: When you press the Enter key in the first edit box, move the input focus to the second edit box... first, you need to disable the Enter key to close the dialog box.

Default button

By Default, the Default button is selected by Default in the properties of the OK button. You only need to double-click or right-click the button and select Cdialg: OK () in the response to logout OnOk ()

Edit box button

If Multiline is selected and multiple rows are accepted, the Return key is effective. WS_TABSTOP is selected by default.

Default button

When you press the Enter key, check whether the specified Default button exists in the dialog box. If yes, call the message response function of the Default button. If no, the virtual OnOK function is called.

2. Two methods to implement the transfer of focus: ① It is the edit box Association class, and this class is used to capture the button message ② modify the edit box window process function

All messages in the window must be reported during the window process. The SetWindowLong function is used to modify the window attributes.

LONG SetWindowLong (HWND hWnd, int nIndex, LONG dwNewLong );

// The return value is the 32-bit integer value specified in the window. If nIndex is GWL_WNDPROC, the address of the window process specified in the window is returned.

3. Where can I put the SetWindowLong function?

In the WM_CREATE message response function?

When responding to this message, the subcontrol of the dialog box has not been created. The dialog box and subcontrol are created only after the message processing is complete.

When the program is running, when the dialog box and Its subwindow are created, a message will appear before it is displayed: WM_INITDIADLG, [add this message Response Function in the CTestDlg class]

4 programs

// ① Edit the edit box window

WNDPROC preProc;

Lresult callback MyProc

(HWND hwnd,

UINT uMsg,

WPARAM wParam,




If (uMsg = WM_CHAR & wParam = 0x0d) // intercept the WM_CHAR message and make corresponding judgment and processing. For the WM_CHAR message, the wParam parameter stores the ASC code of the character.


// Obtain the window handle

// Method 1

//: SetFocus (GetNextWindow (hwnd, GW_HWNDNEXT); // MyProc is a global function. It cannot call a member function of the CWnd class, but can only use the corresponding SDK function.

// HWND GetNextWindow (HWND hWnd, UINT wCmd );

// Method 2

// SetFocus (: GetWindow (hwnd, GW_HWNDNEXT ));

// HWND GetWindow (HWND hWnd, UINT uCmd );

// Method 3

SetFocus (: GetNextDlgTabItem (: GetParent (hwnd), hwnd, FALSE ));

// HWND GetNextDlgTabItem (HWND hDlg, HWND hCtl, BOOL bPrevious );

Return 1;




Return preProc (hwnd, uMsg, wParam, lParam );



// ② Change window properties

BOOL CTestDlg: OnInitDialog ()


CDialog: OnInitDialog ();

// TODO: add additional initialization here

PreProc = (WNDPROC) SetWindowLong (GetDlgItem (IDC_EDIT1)-> m_hWnd, GWL_WNDPROC, (LONG) MyProc );

// LONG SetWindowLong (HWND hWnd, int nIndex, LONG dwNewLong );

// The return value is the 32-bit integer value specified in the window. If nIndex is GWL_WNDPROC, the address of the window process specified in the window is returned.

Return TRUE; // return TRUE unless you set the focus to a control

// Exception: the OCX attribute page should return FALSE.


// ③ Implement the pass-in function in sequence

Void CTestDlg: OnBnClickedOk ()


// Remove the Multiline attribute

// GetDlgItem (IDC_EDIT1)-> GetNextWindow ()-> SetFocus (); // functions cannot be implemented

// GetFocus ()-> GetNextWindow ()-> SetFocus (); // when the last Control calls this function, the returned window pointer is NULL and the access is illegal when SetFocus is called.

// GetFocus ()-> GetWindow ()-> SetFocus (); // same as above

GetNextDlgTabItem (GetFocus ()-> SetFocus (); // OK

// OnOK ();


Conclusion: ① ② ③

  • Reference: Sun Xin video-7


VC return key and close

After creating a dialog box with VC, click the Enter key and the dialog box will be closed automatically. If you want to click Enter, the dialog box will not be closed, and relevant functions can be implemented, you need to redefine the PreTranslateMessage function. The basic process is as follows:

1. In the dialog box, right-click> ClassWizard.

2. Select the Add PreTranslateMessage function:

3. Edit the function PreTranslateMessage:

BOOL RegisterDllDialog: PreTranslateMessage (MSG * pMsg)


// TODO: Add your specialized code here and/or call the base class


/*** @ If you press the Enter key, the system responds to the function to check whether the user is correct .*/

If (pMsg-> message = WM_KEYDOWN & pMsg-> wParam = VK_RETURN)


/* @ Self-defined function */

OnCheckuser ();

Return true;


Return CDialog: PreTranslateMessage (pMsg );


In this way, you will call your own function when you click the Enter key. Of course, if you do not define the function, you will not be able to respond when you press the Enter key.


For the close button in the upper-right corner of the dialog box, the response function is OnClose. You only need to select WM_CLOSE in the Message in the second figure above, and add the function to get the OnClose function. If you want to make some responses when you click this close button, you can process this function.

Void RegisterDllDialog: OnClose ()


// TODO: Add your message handler code here and/or call default

CDialog: OnClose ();


Source Document


Author: SKySeraph

Email/GTalk: QQ: 452728574


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: 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.