首先看看MSDN对PreTranslateMessage的说明
Override this function to filter window messages before they aredispatched to the Windows functions TranslateMessage andDispatchMessage The default implementation performs accelerator-keytranslation, so you must calltheCWinApp::PreTranslateMessagememberfunction in your overridden version.
virtual BOOL PreTranslateMessage( |
Parameters
- pMsg
A pointer to a MSG structure that contains the message toprocess.
Return Value
Nonzero if the message was fully processedinPreTranslateMessageandshould not be processed further. Zero if the message should beprocessed in the normal way.
Requirements
Header:afxwin.h
------------------------------------------------------
再看看一个具体的例子.
BOOL MyDialog::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
//Returnkey
if(pMsg->wParam == VK_RETURN )
{//若焦点在本页面上的一个名叫IDC_EDT_TEST_DATA_FILE的EditBox上,按回车键则焦点转移到下个控件
if(GetFocus() == GetDlgItem(IDC_EDT_TEST_DATA_FILE))
{
GetFocus()->GetNextWindow()->SetFocus();
returnTRUE;
}
//若焦点在本页面一个叫IDC_EDT_WAITTIME的EditBox上,按回车键时焦点转移到父窗口的IDC_BEXECUTE按钮上
elseif(GetFocus() == GetDlgItem(IDC_EDT_WAITTIME))
{
((CArithmeticExpressionDlg*)this->GetParent())->GetDlgItem(IDC_BEXECUTE)->SetFocus();
returnTRUE;
}
}
//[Shift] + [TAB]
elseif(GetKeyState(VK_SHIFT) & 0x8000&& pMsg->wParam ==VK_TAB)
{
//若焦点在本页面一个叫IDC_EDT_WAITTIME的EditBox上,Shift +TAB是,焦点跳到窗口名叫IDC_RBTN_STEP4的Radio Button上
if(pMsg->hwnd ==(GetDlgItem(IDC_EDT_TEST_DATA_FILE))->m_hWnd )
{
(((CArithmeticExpressionDlg*)this->GetParent())->GetDlgItem(IDC_RBTN_STEP4))->SetFocus();
returnTRUE;
}
}
else if(pMsg->wParam == VK_TAB )
{
//[TAB]
//若焦点在本页面一个叫IDC_EDT_WAITTIME的EditBox上,TAB时焦点转移到父窗口的IDC_BEXECUTE按钮上
if(pMsg->hwnd ==GetDlgItem(IDC_EDT_WAITTIME)->GetSafeHwnd())
{
((CArithmeticExpressionDlg*)this->GetParent())->GetDlgItem(IDC_BEXECUTE)->SetFocus();
returnTRUE;
}
}
//Escapekey
else if(pMsg->wParam == VK_ESCAPE )
{
//按Esc键时,相当于按窗口上有的EXIT按钮.
CButton*pBtnExit =(CButton*)((CArithmeticExpressionDlg*)this->GetParent())->GetDlgItem(IDC_BEXIT);
::SendMessage(pBtnExit->GetSafeHwnd(),WM_LBUTTONDOWN,0,0);
::SendMessage(pBtnExit->GetSafeHwnd(),WM_LBUTTONUP,0,0);
returnTRUE;
}
//other events
}
//others
returnCDialog::PreTranslateMessage(pMsg);
}
本例子的功能就是在父子窗口之前实现焦点的转移.
例如,当焦点位于子窗口的最后一个控件时,此时接着操作TAB键,就要注焦点转移到父窗口中,并且是紧跟着子窗口的那个控件.
闻香止步 收集于:http://blog.csdn.net/roofwei/archive/2009/02/18/3906112.aspx