首先看看MSDN对PreTranslateMessage的说明
Override this function to filter window messages before they are dispatched to the Windows functions TranslateMessage and DispatchMessage The default implementation performs accelerator-key translation, so you must call the CWinApp::PreTranslateMessage member function in your overridden version.
virtual BOOL PreTranslateMessage( |
Parameters
Return Value
Nonzero if the message was fully processed in PreTranslateMessage and should not be processed further. Zero if the message should be processed in the normal way.
Requirements
Header: afxwin.h
------------------------------------------------------
再看看一个具体的例子.
BOOL MyDialog::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
//Return key
if( pMsg->wParam == VK_RETURN )
{ //若焦点在本页面上的一个名叫IDC_EDT_TEST_DATA_FILE的EditBox上,按回车键则焦点转移到下个控件
if(GetFocus() == GetDlgItem(IDC_EDT_TEST_DATA_FILE))
{
GetFocus()->GetNextWindow()->SetFocus();
return TRUE;
}
//若焦点在本页面一个叫IDC_EDT_WAITTIME的EditBox上,按回车键时焦点转移到父窗口的IDC_BEXECUTE按钮上
else if(GetFocus() == GetDlgItem(IDC_EDT_WAITTIME))
{
((CArithmeticExpressionDlg*)this->GetParent())->GetDlgItem(IDC_BEXECUTE)->SetFocus();
return TRUE;
}
}
//[Shift] + [TAB]
else if(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();
return TRUE;
}
}
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();
return TRUE;
}
}
//Escape key
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);
return TRUE;
}
//other events
/*********************some other code***************************/
}
//others
return CDialog::PreTranslateMessage(pMsg);
}
本例子的功能就是在父子窗口之前实现焦点的转移.
例如,当焦点位于子窗口的最后一个控件时,此时接着操作TAB键,就要注焦点转移到父窗口中,并且是紧跟着子窗口的那个控件.
闻香止步 收集于:http://blog.csdn.net/roofwei/archive/2009/02/18/3906112.aspx
淘宝店 摆件 饰品 *木雕系列*:海南黄花梨、越南黄花梨、草花梨、小叶紫檀、黑檀、绿檀木、黄杨木、桃木发簪 木梳 樟木壁挂 佛珠 车饰 摆件
http://shop36570193.taobao.com
朋友,有空来看看,喜欢的朋友请收藏