I have a MFC dialog and开发者_如何学编程 I would like to assign function keys to different methods. Is that possible?
For example I would like to type F9 and have the same result as pressing a button.
For Dialog Based application. Create a IDR_ACCELERATOR1 in resource editor for your dialog. And add the Key you wanted and create a Event for the Key for your dialog.
In sampledlg.h please add
HACCEL m_haccel;
In sampledlg.cpp In OnInitDialog() please add
m_haccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));
In sampledlg.cpp In PreTranslateMessage(MSG *pMsg) please add
if (m_haccel)
{
if ((::TranslateAccelerator(*this, m_haccel, pMsg)))
{
return(TRUE);
}
}
If you mean in the main dialog of a dialog based application, you can check http://www.codeproject.com/Articles/37130/Implement-Accelerators-in-a-Dialog-Based-Applicati.aspx
If it's for dialogs in a doc/view application: http://support.microsoft.com/kb/117500/en-us
http://support.microsoft.com/kb/222829/en-us
Easier than I thought. I don't know if this is the best solution but this is what I did:
BOOL MainDlg::PreTranslateMessage(MSG *pMsg)
{
if ( pMsg->message == WM_KEYDOWN)
{
if (( pMsg->wParam == VK_F9))
OnBnClickedButton1(); // Delete Item
}
return CDialog::PreTranslateMessage(pMsg);
}
Now, every time Function Key F9 is pressed function OnBClickedButton1() is called.
精彩评论