Visual C++ and MFC Fundamentals Chapter 5: The Document/View Architecture
Practical Learning: Sending Key Down Messages
- To experiment with the ON_KEYDOWN message, change the file as follows:
#include <afxwin.h>
class CMainFrame : public CFrameWnd
{
public:
CMainFrame ();
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
DECLARE_MESSAGE_MAP()
};
CMainFrame::CMainFrame()
{
// Create the window's frame
Create(NULL, "Windows Application", WS_OVERLAPPEDWINDOW,
CRect(120, 100, 700, 480), NULL);
}
class CExerciseApp: public CWinApp
{
public:
BOOL InitInstance();
};
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
void CMainFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch(nChar)
{
case VK_RETURN:
MessageBox("You pressed Enter");
break;
case VK_F1:
MessageBox("Help is not available at the moment");
break;
case VK_DELETE:
MessageBox("Can't Delete This");
break;
default:
MessageBox("Whatever");
}