mfc笔记: 《MFC Windows程序设计》Shape程序

来源:互联网 发布:淘宝手机详情视频时间 编辑:程序博客网 时间:2024/05/21 20:22
</pre>《MFC Windows程序设计》P169页,书中程序代码没有实现加速键F7,F8,F9 。实现此功能需要:


一、在CMainFrame类中定义变量:   HACCEL m_hAccel;

二、在CMainFrame类中的OnCreate函数中初始化  

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){m_hAccel=LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));

三、在CMainFrame类中重定义虚函数PreTranslateMessage。MainFrm.h文件中添加函数声明,MainFrm.cpp文件中添加函数定义


virtual BOOL PreTranslateMessage(MSG* pMsg);//函数声明

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)//函数定义{if(pMsg->message==WM_KEYDOWN&&pMsg->wParam==VK_ESCAPE)AfxGetMainWnd()->PostMessageW(WM_CLOSE);if(::TranslateAcceleratorA(pMsg->hwnd,m_hAccel,pMsg))return TRUE;return CFrameWndEx::PreTranslateMessage(pMsg);}

其中前两行代码:
if(pMsg->message==WM_KEYDOWN&&pMsg->wParam==VK_ESCAPE)AfxGetMainWnd()->PostMessageW(WM_CLOSE);

功能:按键盘Esc键时,程序退出!


0 0
原创粉丝点击