您的位置:首页 > 移动开发

VC中自定义消息实现(转载于并加以修改)

2008-01-09 10:48 411 查看
 
[align=center]VC中自定义消息实现[/align]
 
1.方法一
Step 1:使用RegisterWindowMessage来确定一个没有被windows本身抢占的消息值,在想添
加消息文件中添加代码:
const UINT WM_USERDEFMSG = ::RegisterWindowMessage(_T("UserDefMsg"));
Step 2:定义消息的处理过程:
在处理消息的类(如Dlg类或者MainFrame等)添加消息处理函数声明:
afx_msg LRESULT OnUserDefMsg(WPARAM wParam,LPARAM lParam);
在实现文件中添加消息处理实现,这里给出一个实现例子:
LRESULT ××::OnUserDefMsg(WPARAM wParam,LPARAM lParam) //××代表处理该消息的类名
{
       MessageBox("响应了自定义的消息,^_^","自定义消息响应",MB_ICONQUESTION | MB_OK);
       return 0;
}
Step 3:添加消息处理宏,在处理该消息的类的消息宏中添加自定义消息的宏,即在BEGIN_MESSAGE_MAP代码块中添加代码:
ON_REGISTERED_MESSAGE(WM_USERDEFMSG, OnUserDefMsg)
 
方法二
Step 1:定义消息,这里不通过RegisterWindowMessage获取,而是自定义方式实现,在想添加消息文件中添加代码(开发Windows95应用程序时,Microsoft推荐用户自定义消息至少是WM_USER+100,因为很多新控件也要使用WM_USER消息。):
#define WM_USERDEFMSG (WM_USER + 101)
Step 2:
在类头文件的AFX_MSG块中说明消息处理函数:
class CMainFrame:public CMDIFrameWnd
{
...
// 一般消息映射函数
protected:
// {{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnTimer(UINT nID
4000
Event);
afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
}
实现消息处理函数。该函数使用WPRAM和LPARAM参数并返回LPESULT。
LPESULT CMainFrame::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
// TODO: 处理用户自定义消息
...
return 0;
}
Step 3:在用户类的消息块中,使用ON_MESSAGE宏指令将消息映射到消息处理函数中。
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_TIMER()
ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
如果用户需要一个整个系统唯一的消息,可以调用SDK函数RegisterWindowMessage并使用ON_REGISTER_MESSAGE宏指令取代ON_MESSAGE宏指令,其余步骤同上。
发送消息
按照上面方式我们已经自定义了消息,并为该消息实现了简单的处理过程,这里就通过发送消息获得自定义消息的响应。在要触发自定义消息地方添加代码:
::SendMessage((the pointer of XX)->GetSafeHwnd(),WM_USERDEFMSG,0L,0L);
这样你就触发了该自定义消息,并将得到提示对话框显示。
 
UINT RegisterWindowMessage(LPCTSTR lpString   // message string);

The RegisterWindowMessage function defines a new window message that is guaranteed to be unique throughout the system. The message value can be used when sending or posting messages. If the message is successfully registered, the return value is a message identifier in the range 0xC000 through 0xFFFF.If the function fails, the return value is zero.
 
lpString
[in] Pointer to a null-terminated string that specifies the message to be registered.
Remarks
The RegisterWindowMessage function is typically used to register messages for communicating between two cooperating applications. If two different applications register the same message string, the applications return the same message value. The message remains registered until the session ends.
Only use RegisterWindowMessage when more than one application must process the same message. For sending private messages within a window class, an application can use any integer in the range WM_USER through 0x7FFF. (Messages in this range are private to a window class, not to an application. For example, predefined control classes such as BUTTON, EDIT, LISTBOX, and COMBOBOX may use values in this range.)
Windows 95/98/Me: RegisterWindowMessageW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.
 
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息