Microsoft Windows 操作系统无法预测一个对象需要处理什么类型的请求以及另一个对象需要什么类型的分配。
为了管理所有这些分配和请求,对象发送消息。
每个对象都有责任决定发送什么消息以及何时发送。
为了发送消息,控件必须创建一个事件。
为了区分两者,消息的名称通常以 WM_ 开头,WM_ 代表窗口消息。
事件的名称通常以 On 开头,表示动作。
事件是发送消息的动作。
消息地图
由于 Windows 是面向消息的操作系统,因此 Windows 环境的大部分编程都涉及消息处理。每次发生诸如击键或鼠标单击之类的事件时,都会向应用程序发送一条消息,然后应用程序必须处理该事件。
为了让编译器管理消息,它们应该包含在类定义中。
所述DECLARE_MESSAGE_MAP宏应在类定义的结尾被设置为显示在下面的代码。
class CMainFrame : public CFrameWnd {
public:
CMainFrame();
protected:
DECLARE_MESSAGE_MAP()
};
实际消息应列在 DECLARE_MESSAGE_MAP 行上方。
要实现消息,您需要创建一个程序正在使用的消息表。
该表使用两个定界宏;
它以BEGIN_MESSAGE_MAP开始,以END_MESSAGE_MAP宏结束。
BEGIN_MESSAGE_MAP 宏采用两个参数,类名和派生类的 MFC 类,如下面的代码所示。
#include <afxwin.h>
class CMainFrame : public CFrameWnd {
public:
CMainFrame();
protected:
DECLARE_MESSAGE_MAP()
};
CMainFrame::CMainFrame() {
// Create the window's frame
Create(NULL, L"MFC Messages Demo", WS_OVERLAPPEDWINDOW,
CRect(120, 100, 700, 480), NULL);
}
class CMessagesApp : public CWinApp {
public:
BOOL InitInstance();
};
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
END_MESSAGE_MAP()
BOOL CMessagesApp::InitInstance(){
m_pMainWnd = new CMainFrame;
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
CMessagesApp theApp;
让我们通过创建一个新的 Win32 项目来查看一个简单的示例。
步骤 1 – 要创建 MFC 项目,请右键单击该项目并选择“属性”。
步骤 2 – 在左侧部分,单击配置属性 → 常规。
步骤 3 – 在“项目默认值”部分中选择“在共享 DLL 中使用 MFC”选项,然后单击“确定”。
第 4 步– 我们需要添加一个新的源文件。
第 5 步– 右键单击您的项目并选择添加 → 新项目。
步骤 6 – 在模板部分,单击 C++ 文件 (.cpp)。
步骤 7 – 单击添加继续。
步骤 8 – 现在,在 *.cpp 文件中添加以下代码。
#include <afxwin.h>
class CMainFrame : public CFrameWnd {
public:
CMainFrame();
protected:
DECLARE_MESSAGE_MAP()
};
CMainFrame::CMainFrame() {
// Create the window's frame
Create(NULL, L"MFC Messages Demo", WS_OVERLAPPEDWINDOW,
CRect(120, 100, 700, 480), NULL);
}
class CMessagesApp : public CWinApp {
public:
BOOL InitInstance();
};
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
END_MESSAGE_MAP()
BOOL CMessagesApp::InitInstance() {
m_pMainWnd = new CMainFrame;
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
CMessagesApp theApp;
视窗讯息
有不同类型的 Windows 消息,例如创建窗口、显示窗口等。以下是一些常用的 Windows 消息。
Message
地图条目
Description
WM_ACTIVATE
ON_WM_ACTIVATE()
The framework calls this member function when a CWnd object is being activated or deactivated.
WM_ACTIVATEA PP
ON_WM_ACTIVATEAPP()
The framework calls this member function to all top-level windows of the task being activated and for all top-level windows of the task being deactivated.
WM_APPCOMM AND
ON_WM_APPCOMMAND()
The framework calls this member function when the user generates an application command event.
WM_CANCELMODE
WM_CANCELMODE()
The framework calls this member function to inform CWnd to cancel any internal mode.
WM_CHILDACTIVATE
ON_WM_CHILDACTIVATE()
If the CWnd object is a multiple document interface (MDI) child window, OnChildActivate is called by the framework when the user clicks the window’s title bar or when the window is activated, moved, or sized.
WM_CLIPBOAR DUPDATE
ON_WM_CLIPBOARDUPDATE()
The framework calls this member function when the contents of the clipboard have changed.
WM_CLOSE
ON_WM_CLOSE()
The framework calls this member function as a signal that the CWnd or an application is to terminate.
WM_CONTEXTMENU
ON_WM_CONTEXTMENU()
Called by the framework when the user has clicked the right mouse button (rightclicked) in the window.
WM_COPYDATA
ON_WM_COPYDATA()
This member function is called by the framework to copy data from one application to another.
WM_CREATE
ON_WM_CREATE()
The framework calls this member function when an application requests that the Windows window be created by calling the Create or CreateEx member function.
WM_CTLCOLOR
ON_WM_CTLCOLOR()
The framework calls this member function when a child control is about to be drawn.
WM_DELETEITEM
ON_WM_DELETEITEM()
The framework calls this member function to inform the owner of an owner-draw list box or combo box that the list box or combo box is destroyed or that items have been removed.
WM_DESTROY
ON_WM_DESTROY()
he framework calls this member function to inform the CWnd object that it is being destroyed.
WM_DRAWITEM
ON_WM_DRAWITEM()
The framework calls this member function for the owner of an owner-draw button control, combo-box control, list-box control, or menu when a visual aspect of the control or menu has changed.
WM_DROPFILES
ON_WM_DROPFILES()
The framework calls this member function when the user releases the left mouse button over a window that has registered itself as the recipient of dropped files.
WM_ENABLE
ON_WM_ENABLE()
The framework calls this member function when an application changes the enabled state of the CWnd object. Syntax.
WM_HELPINFO
ON_WM_HELPINFO()
Handles F1 Help within the application (using the current context).
WM_HOTKEY
ON_WM_HOTKEY()
The framework calls this member function when the user presses a system-wide hot key.
WM_HSCROLL
ON_WM_HSCROLL()
The framework calls this member function when the user clicks a window’s horizontal scroll bar.
WM_KEYDOWN
ON_WM_KEYDOWN()
The framework calls this member function when a nonsystem key is pressed.
WM_KEYUP
ON_WM_KEYUP()
The framework calls this member function when a nonsystem key is released.
WM_KILLFOCUS
ON_WM_KILLFOCUS()
The framework calls this member function immediately before losing the input focus.
WM_LBUTTONDBLCLK
ON_WM_LBUTTONDBLCLK()
The framework calls this member function when the user double-clicks the left mouse button.
WM_LBUTTONDOWN
ON_WM_LBUTTONDOWN()
The framework calls this member function when the user presses the left mouse button.
WM_LBUTTONUP
ON_WM_LBUTTONUP()
The framework calls this member function when the user releases the left mouse button.
WM_MBUTTONDBLCLK
ON_WM_MBUTTONDBLCLK()
The framework calls this member function when the user double-clicks the middle mouse button.
WM_MBUTTONDOWN
ON_WM_MBUTTONDOWN()
The framework calls this member function when the user presses the middle mouse button.
WM_MBUTTONUP
ON_WM_MBUTTONUP()
The framework calls this member function when the user releases the middle mouse button.
WM_MENUSELECT
ON_WM_MENUSELECT()
If the CWnd object is associated with a menu, OnMenuSelect is called by the framework when the user selects a menu item.
WM_MOUSEACTIVATE
ON_WM_MOUSEACTIVATE()
The framework calls this member function when the cursor is in an inactive window and the user presses a mouse button.
WM_MOUSEHOVER
ON_WM_MOUSEHOVER()
The framework calls this member function when the cursor hovers over the client area of the window for the period of time specified in a prior call to TrackMouseEvent.
WM_MOUSEHWHEEL
ON_WM_MOUSEHWHEEL()
The framework calls this member when the current window is composed by the Desktop Window Manager (DWM), and that window is maximized.
WM_MOUSELEAVE
ON_WM_MOUSELEAVE()
The framework calls this member function when the cursor leaves the client area of the window specified in a prior call to TrackMouseEvent.
WM_MOUSEMOVE
ON_WM_MOUSEMOVE()
The framework calls this member function when the mouse cursor moves.
WM_MOVE
ON_WM_MOVE()
The framework calls this member function after the CWnd object has been moved.
WM_PAINT
ON_WM_PAINT()
The framework calls this member function when Windows or an application makes a request to repaint a portion of an application’s window.
WM_SETFOCUS()
ON_WM_SETFOCUS( )
The framework calls this member function after gaining the input focus.
WM_SIZE( )
ON_WM_SIZE( )
The framework calls this member function after the window’s size has changed.
WM_TIMER
ON_WM_TIMER()
The framework calls this member function after each interval specified in the SetTimer member function used to install a timer.
WM_VSCROLL
ON_WM_VSCROLL()
The framework calls this member function when the user clicks the window’s vertical scroll bar.
WM_WINDOWPOSCHANGED
ON_WM_WINDOWPOSCHANGED()
The framework calls this member function when the size, position, or Z-order has changed as a result of a call to the SetWindowPos member function or another window-management function.
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {
// Call the base class to create the window
if (CFrameWnd::OnCreate(lpCreateStruct) == 0) {
// If the window was successfully created, let the user know
MessageBox(L"The window has been created!!!");
// Since the window was successfully created, return 0
return 0;
}
// Otherwise, return -1
return -1;
}
第 4 步– 现在您的 *.cpp 文件将如下面的代码所示。
#include <afxwin.h>
class CMainFrame : public CFrameWnd {
public:
CMainFrame();
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
};
CMainFrame::CMainFrame() {
// Create the window's frame
Create(NULL, L"MFC Messages Demo", WS_OVERLAPPEDWINDOW,
CRect(120, 100, 700, 480), NULL);
}
class CMessagesApp : public CWinApp {
public:
BOOL InitInstance();
};
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
END_MESSAGE_MAP()
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {
// Call the base class to create the window
if (CFrameWnd::OnCreate(lpCreateStruct) == 0) {
// If the window was successfully created, let the user know
MessageBox(L"The window has been created!!!");
// Since the window was successfully created, return 0
return 0;
}
// Otherwise, return -1
return -1;
}
BOOL CMessagesApp::InitInstance() {
m_pMainWnd = new CMainFrame;
m_pMainWnd -> ShowWindow(SW_SHOW);
m_pMainWnd -> UpdateWindow();
return TRUE;
}
CMessagesApp theApp;
Step 5 – 编译并执行上述代码后,您将看到以下输出。
步骤 6 – 当您单击确定时,它将显示主窗口。
命令信息
图形应用程序的主要功能之一是提供允许用户与机器交互的 Windows 控件和资源。我们将学习的控件示例有按钮、列表框、组合框等。
我们在上一课中介绍的一种资源是菜单。当用户点击它们时,这些控件和资源可以启动它们自己的消息。从 Windows 控件或资源发出的消息称为命令消息。
void CMainFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {
switch (nChar) {
case VK_RETURN:
MessageBox(L"You pressed Enter");
break;
case VK_F1:
MessageBox(L"Help is not available at the moment");
break;
case VK_DELETE:
MessageBox(L"Can't Delete This");
break;
default:
MessageBox(L"Whatever");
}
}