1.新建两个对话框项目,分别在StdAfx.h中添加自定义消息:
1 | #define WM_CONTROLPRINT WM_USER+1001 |
2.发送端,获得其他窗口句柄的方法FindWindow();
1 | void CAaDlg::OnButtonsend() |
2 | { |
3 | // TODO: Add your control notification handler code here |
4 | //通过窗体名称,获取其他进程窗口句柄 |
5 | CWnd *pWnd=CWnd::FindWindow(NULL,_T("Bb")); |
6 | if(pWnd==NULL) |
7 | { |
8 | AfxMessageBox("接收程序没有运行!"); |
9 | return ; |
10 | } |
11 | |
12 | pWnd->SendMessage(WM_CONTROLPRINT,NULL,0); |
13 | } |
3.接收端,接收其他进程发过来的消息。
1 | //消息响应函数 |
2 | void OnControlPrint(WPARAM wParam, LPARAM lParam); |
3 | void CBbDlg::OnControlPrint(WPARAM wParam, LPARAM lParam) |
4 | { |
5 | if(wParam==0 && lParam==0) |
6 | AfxMessageBox("HelloWorld!"); |
7 | |
8 | return ; |
9 | } |
10 | //添加消息映射 |
11 | BEGIN_MESSAGE_MAP(CBbDlg, CDialog) |
12 | //{{AFX_MSG_MAP(CBbDlg) |
13 | ON_MESSAGE(WM_CONTROLPRINT, OnControlPrint) |
14 | //}}AFX_MSG_MAP |
15 | END_MESSAGE_MAP() |
运行结果:
源码下砸:http://files.cnblogs.com/greatverve/mfc-sendmessage.rar