请问VC里面怎么实现自定义消息啊?(在线等待!) 谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #define WM_SOMEMSG WM_APP + 1023afx_msg LRESULT OnSomeMsg(WPARAM wParam, LPARAM lParam);ON_MESSAGE(WM_SOMEMSG, OnSomeMsg);LRESULT CSomeWnd::OnSomeMsg(WPARAM wParam, LPARAM lParam){ return TRUE;} agree#define WM_SOMEMSG WM_APP + 1023(数字可以任定,但推荐从100开始) 同意。但详细点:在你要用的文件的头文件的开头#define WM_SOMEMSG WM_USER + 100你的消息响应函数AFX-MSG LRESULT XXX()定义在//}AFX—MSG之后,...MAP()之前。在。CPP中你的ON_MESSAGE在BEGIN...END 之间。 (1) 手工定义消息,可以这么写 #define WM_MY_MESSAGE(WM_USER+100), MS 推荐的至少是 WM_USER+100 (2)声明消息afx_msg LRESULT OnMyMessage( WPARAM wParam, LPARAM lParam );(3)消息映射在cpp文件中的消息映射中加入:BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code ! ON_WM_CREATE() //}}AFX_MSG_MAP ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)END_MESSAGE_MAP()(4)写消息处理函数,用 WPARAM,LPARAM返回LRESULT. LRESULT CMainFrame::OnMyMessage(WPARAM wparam,LPARAM lParam) { temp目录: Use "GetTempPath" //加入你的处理函数 irectory"} ---------------------------------------说明:以下用一个自创的对话框类(MyMessageDlg)向视图类(MessageTestView)发送自定义消息为例,说明这两种不同方法的自定义消息的总结:消息传递的方法一:使用ON_MESSAGE使用ON_MESSAGE响应消息,必须配合定义消息#define WM_MY_MESSAGE (WM_USER+100)对于发送消息者-MyMessageDlg,在其MyMessageDlg.h中,定义#define WM_MY_MESSAGE (WM_USER+100)在其MyMessageDlg.cpp中要先添加:#include "MainFrm.h"因为使用了CMainFrame*定义对象。并且要有测试消息的函数:void MyMessageDlg::OnButtonMsg() { // TODO: Add your control notification handler code here CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd; //先通过获取当前框架指针 CView * active = pMF->GetActiveView();//才能获取当前视类指针 if(active != NULL) //获取了当前视类指针才能发送消息 active->PostMessage(WM_MY_MESSAGE,0,0); //使用PostMessage发送消息}对于消息的接受者-MessageTestView,在其MessageTestView.h中,也要定义#define WM_MY_MESSAGE (WM_USER+100)并定义消息映射函数-OnMyMessage()protected: //{{AFX_MSG(CMessageTestView) afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam); //}}AFX_MSG DECLARE_MESSAGE_MAP()在其MessageTestView.cpp中,先要声明响应消息:BEGIN_MESSAGE_MAP(CMessageTestView, CEditView) //{{AFX_MSG_MAP(CMessageTestView) ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage) //}}AFX_MSG_MAP再添加消息响应的函数实现:LRESULT CMessageTestView::OnMyMessage(WPARAM wParam, LPARAM lParam){ MessageBox("OnMyMessage!"); return 0;} 消息传递的方法二:使用ON_REGISTERED_MESSAGE使用ON_REGISTERED_MESSAGE注册消息,必须配合static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");对于消息的发送者-MyMessageDlg,在其MyMessageDlg.h中,只要定义static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");就可以了。在其MyMessageDlg.cpp中要先添加:#include "MainFrm.h"因为使用了CMainFrame*定义对象。并且要有测试消息的函数:void MyMessageDlg::OnButtonMsg() { // TODO: Add your control notification handler code here CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd; //先通过获取当前框架指针 CView * active = pMF->GetActiveView();//才能获取当前视类指针 if(active != NULL) //获取了当前视类指针才能发送消息 active->PostMessage(WM_MY_MESSAGE,0,0); //使用PostMessage发送消息}对于消息的接收者-MessageTestView,在其MessageTestView.h中不要定义static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");应该把这个定义放到MessageTestView.cpp中,要不会出现: redefinition在其MessageTestView.h中只要定义消息映射函数protected: //{{AFX_MSG(CMessageTestView) afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam); //}}AFX_MSG DECLARE_MESSAGE_MAP()在其MessageTestView.cpp中,先定义static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");接着注册消息:BEGIN_MESSAGE_MAP(CMessageTestView, CEditView) //{{AFX_MSG_MAP(CMessageTestView) ON_REGISTERED_MESSAGE(WM_MY_MESSAGE,OnMyMessage) //}}AFX_MSG_MAP最后添加消息响应的函数实现:LRESULT CMessageTestView::OnMyMessage(WPARAM wParam, LPARAM lParam){ MessageBox("OnMyMessage!"); return 0;}----------------------------------------------------------------比较两种方法,只是略有不同。但也要小心谨慎,以免出现接收不到消息的情况。 -------------------------------------------------------------------其他注意事项:发送消息的-MyMessageDlg.cpp前也要定义static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");接受消息的-MessageTestView.cpp前也要定义static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");RegisterWindowMessage("Message")中""的内容是什么不重要,写什么都可以,单必须发送者与接受者是一样的内容,例如:"Message" (1) 手工定义消息,可以这么写 #define WM_MY_MESSAGE(WM_USER+100), MS 推荐的至少是 WM_USER+100; (2)写消息处理函数,用 WPARAM,LPARAM返回LRESULT. LRESULT CMainFrame::OnMyMessage(WPARAM wparam,LPARAM lParam) { //加入你的处理函数 } (3) 在类的 AFX_MSG处进行声明,也就是常说的"宏映射" 3:怎样在程序中启动缺省浏览器,并进入某一网址用API函数:ShellExecute 具体用法看VC的MSDN, Example: ShellExcute(hWnd,"open","http://club.netease.com",NULL,NULL,SW_MAXIMIZE ); 建议将消息定义加到stdafx.h里 VC ADO问题 MiniDumpWriteDump的使用中遇到的问题. 打开dmp文件,按F5调试,堆栈里面的符号信息都没有了. 类似网络电视编程 为什么显示结果跟我想要的不对啊?百思不得其解,请大哥们帮忙~+~ Help!目录压缩问题 问一个控制线程执行/停止的问题。 怎样是自己保存的文件在WINDOWS中有一个自定义的图标? 在对话框上如何显时位图? 用VC编程实现显示WORD文档,并能保存修改内容怎么办 怎样获得mouse的当前位置? 显IP的QQ的原理是怎么样的 如何实现vc的那种目录对话框?
{
return TRUE;
}
#define WM_SOMEMSG WM_APP + 1023(数字可以任定,但推荐从100开始)
但详细点:
在你要用的文件的头文件的开头#define WM_SOMEMSG WM_USER + 100
你的消息响应函数AFX-MSG LRESULT XXX()定义在//}AFX—MSG之后,...MAP()之前。
在。CPP中你的ON_MESSAGE在BEGIN...END 之间。
#define WM_MY_MESSAGE(WM_USER+100),
MS 推荐的至少是 WM_USER+100
(2)声明消息
afx_msg LRESULT OnMyMessage( WPARAM wParam, LPARAM lParam );
(3)消息映射在cpp文件中的消息映射中加入:
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)
END_MESSAGE_MAP()
(4)写消息处理函数,用
WPARAM,LPARAM返回LRESULT.
LRESULT CMainFrame::OnMyMessage(WPARAM wparam,LPARAM lParam)
{
temp目录: Use "GetTempPath"
//加入你的处理函数 irectory"
}
---------------------------------------
说明:
以下用一个自创的对话框类(MyMessageDlg)向视图类(MessageTestView)
发送自定义消息为例,说明这两种不同方法的自定义消息的
总结:
消息传递的方法一:使用ON_MESSAGE
使用ON_MESSAGE响应消息,必须配合定义消息#define WM_MY_MESSAGE (WM_USER+100)对于发送消息者-MyMessageDlg,
在其MyMessageDlg.h中,定义#define WM_MY_MESSAGE (WM_USER+100)
在其MyMessageDlg.cpp中要先添加:#include "MainFrm.h"
因为使用了CMainFrame*定义对象。
并且要有测试消息的函数:
void MyMessageDlg::OnButtonMsg()
{
// TODO: Add your control notification handler code here
CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd; //先通过获取当前框架指针
CView * active = pMF->GetActiveView();//才能获取当前视类指针
if(active != NULL) //获取了当前视类指针才能发送消息
active->PostMessage(WM_MY_MESSAGE,0,0); //使用PostMessage发送消息
}对于消息的接受者-MessageTestView,
在其MessageTestView.h中,也要定义#define WM_MY_MESSAGE (WM_USER+100)
并定义消息映射函数-OnMyMessage()
protected:
//{{AFX_MSG(CMessageTestView)
afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
在其MessageTestView.cpp中,
先要声明响应消息:
BEGIN_MESSAGE_MAP(CMessageTestView, CEditView)
//{{AFX_MSG_MAP(CMessageTestView)
ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)
//}}AFX_MSG_MAP
再添加消息响应的函数实现:
LRESULT CMessageTestView::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
MessageBox("OnMyMessage!");
return 0;
}
消息传递的方法二:使用ON_REGISTERED_MESSAGE
使用ON_REGISTERED_MESSAGE注册消息,必须配合
static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");对于消息的发送者-MyMessageDlg,
在其MyMessageDlg.h中,只要
定义static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");
就可以了。
在其MyMessageDlg.cpp中要先添加:#include "MainFrm.h"
因为使用了CMainFrame*定义对象。
并且要有测试消息的函数:
void MyMessageDlg::OnButtonMsg()
{
// TODO: Add your control notification handler code here
CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd; //先通过获取当前框架指针
CView * active = pMF->GetActiveView();//才能获取当前视类指针
if(active != NULL) //获取了当前视类指针才能发送消息
active->PostMessage(WM_MY_MESSAGE,0,0); //使用PostMessage发送消息
}对于消息的接收者-MessageTestView,
在其MessageTestView.h中不要定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");
应该把这个定义放到MessageTestView.cpp中,要不会出现: redefinition
在其MessageTestView.h中只要定义消息映射函数
protected:
//{{AFX_MSG(CMessageTestView)
afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
在其MessageTestView.cpp中,先定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");
接着注册消息:
BEGIN_MESSAGE_MAP(CMessageTestView, CEditView)
//{{AFX_MSG_MAP(CMessageTestView)
ON_REGISTERED_MESSAGE(WM_MY_MESSAGE,OnMyMessage)
//}}AFX_MSG_MAP
最后添加消息响应的函数实现:
LRESULT CMessageTestView::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
MessageBox("OnMyMessage!");
return 0;
}
----------------------------------------------------------------
比较两种方法,只是略有不同。但也要小心谨慎,以免出现接收不到消息的情况。 -------------------------------------------------------------------其他注意事项:发送消息的-MyMessageDlg.cpp前也要定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");接受消息的-MessageTestView.cpp前也要定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");RegisterWindowMessage("Message")中""的内容是什么不重要,写什么都可以,单必须
发送者与接受者是一样的内容,例如:"Message"
(1) 手工定义消息,可以这么写 #define WM_MY_MESSAGE(WM_USER+100), MS 推荐的至少是 WM_USER+100; (2)写消息处理函数,用 WPARAM,LPARAM返回LRESULT. LRESULT CMainFrame::OnMyMessage(WPARAM wparam,LPARAM lParam) { //加入你的处理函数 } (3) 在类的 AFX_MSG处进行声明,也就是常说的"宏映射" 3:怎样在程序中启动缺省浏览器,并进入某一网址
用API函数:ShellExecute 具体用法看VC的MSDN,
Example:
ShellExcute(hWnd,"open","http://club.netease.com",NULL,NULL,SW_MAXIMIZE );