VC return Key Usage

Source: Internet
Author: User

Create a dialog box-based project in VC and compile and run the project successfully. I am so happy with the project I just created. Suddenly a whim, pressed a return key, the project flash and gone. Compile and run again. Press the ESC key, and the project will flash away.
Why is this true? The CDialog has the default "ENTER" and "ESC" keys.
To solve this problem, press ENTER and ESC to close the project. The correct solution is to trigger the PreTranslateMessage message and intercept the message in the dialog box between ENTER and ESC.
The Code is as follows:

Code

 Bool cxxx: PreTranslateMessage (MSG * pMsg)
{
If (pMsg-> message = WM_KEYDOWN)
{
Switch (pMsg-> wParam)
{
Case VK_RETURN: // block the Enter key
Return TRUE;
Case VK_ESCAPE: // block the ESC key
Return TRUE;
}
}
Return CDialog: PreTranslateMessage (pMsg );
}

 

 

Okay. Now it's okay. Suddenly I added an edit box and a list box to the dialog box. I want to enter the content in the edit box and press enter to insert the content of the edit box to the list box.

Suddenly, I had no idea how to do it. I thought I would do it in PreTranslateMessage, And I directly returned TRUE. I will return TRUE; just do it above.

The Code is as follows:

 

Code

 BOOL CXXX::PreTranslateMessage(MSG* pMsg) 
{
HWND h1 = ::GetDlgItem(m_hWnd,IDC_EDIT1);
if(pMsg->message == WM_KEYDOWN)
{
switch(pMsg->wParam)
{
case VK_RETURN:
UpdateData();
if (pMsg->hwnd == h1)
{

m_list.AddString(m_edit1);
}
return TRUE;
case VK_ESCAPE:
return TRUE;
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}

 

 

What if there are two edit boxes. Press enter in edit box 1, add the content in edit box 1 to the list box, press enter in edit box 2, and add the content in edit box 2 to the list box.

The Code is as follows:

 

Code

 BOOL CXXX::PreTranslateMessage(MSG* pMsg) 
{
HWND h1 = ::GetDlgItem(m_hWnd,IDC_EDIT1);
HWND h2 = ::GetDlgItem(m_hWnd,IDC_EDIT2);
if(pMsg->message == WM_KEYDOWN)
{
switch(pMsg->wParam)
{
case VK_RETURN:
UpdateData();
if (pMsg->hwnd == h1)
{

m_list.AddString(m_edit1);
}
if (pMsg->hwnd == h2)
{

m_list.AddString(m_edit2);
}
return TRUE;
case VK_ESCAPE:
return TRUE;
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}

 

 

For the following usage, let's talk about the conversion of the VC window class and window handle.
The window class is CWnd, and the window handle is HWND. The code for mutual conversion between CWnd and HWND is as follows:

 CWnd *pWnd;
HWND hWnd;
hWnd = pWnd->GetSafeHwnd(); //CWnd->HWND
pWnd = CWnd::FromHandle(hWnd); //HWND->CWnd

 

 

In this way, the code of the two edit boxes above is changed to the second edit box to obtain CWnd, and then compare it with HWnd.
The modified code is as follows:

Code

 BOOL CXXX::PreTranslateMessage(MSG* pMsg) 
{
HWND h1 = ::GetDlgItem(m_hWnd,IDC_EDIT1);
CWnd *h2=GetDlgItem(IDC_EDIT2);
if(pMsg->message == WM_KEYDOWN)
{
switch(pMsg->wParam)
{
case VK_RETURN:
UpdateData();
if (pMsg->hwnd == h1)
{

m_list.AddString(m_edit1);
}
if (pMsg->hwnd == h2->GetSafeHwnd())
{

m_list.AddString(m_edit2);
}
return TRUE;
case VK_ESCAPE:
return TRUE;
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}

 

 

Finished. The functions are the same.
So happy!
You can also do the following for other controls. Such as: list box
And so on.

Suddenly one day, I found this practice ineffective. Expired on the combo box.
The original hope turned into disappointment. Painful.
The procedure is as follows:

Code

 BOOL CXXX::PreTranslateMessage(MSG* pMsg) 
{
HWND h3 = ::GetDlgItem(m_hWnd,IDC_COMBO1);
if(pMsg->message == WM_KEYDOWN)
{
switch(pMsg->wParam)
{
case VK_RETURN:
UpdateData();
if (pMsg->hwnd == h3)
{

m_list.AddString(m_combo1);
}
return TRUE;
case VK_ESCAPE:
return TRUE;
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}

 

 

So with SPY ++, the combo box has two handles: ComboBox and Edit. Think about it too: the visible part of the combo box is composed of an Edit, the handle obtained by pMsg-> hwnd is the handle referred to by this Edit (because the press enter is typed in this Edit), while HWND h3 =: GetDlgItem (m_hWnd, IDC_COMBO1 ); the resulting handle is the ComboBox handle. pMsg-> hwnd and h3 certainly do not match.
Well, it's easy to find the reason.

Remember this function: CWnd * GetParent () const;
In fact, ComboBox is the parent window of Edit (both the combo box and the Edit box can be regarded as windows ).
Oh, since pMsg-> hwnd is the Edit handle and ComboBox is the parent window of Edit, use GetParent (); to get the handle of the parent window, you can compare it with the obtained handle.
So the Code came out.

The Code is as follows (two combos are included ):

 

Code

 BOOL CEntercomboDlg::PreTranslateMessage(MSG* pMsg) 
{
CWnd *h1=GetDlgItem(IDC_COMBO1);
CWnd *h2=GetDlgItem(IDC_COMBO2);

CWnd* hh = CWnd::FromHandle(pMsg->hwnd);
CWnd* hhp = NULL;
if(pMsg->message == WM_KEYDOWN)
{
switch(pMsg->wParam)
{
case VK_RETURN:
UpdateData();
hhp = hh->GetParent();
if (hhp == h1) {
m_list.AddString(m_combo1);
}
if (hhp == h2) {
m_list.AddString(m_combo2);
}
return TRUE;
case VK_ESCAPE:
return TRUE;
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}

 

 

Oh, good.

This is only one of the applications of the carriage return key in VC.

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.