发送端:
CWnd *pWnd = FindWindow(_T("#32770"),_T("TEST")); //这里窗口类是dialog, 关于需要发送到的窗口类通过spy++ 查看. if (pWnd !=NULL) { pWnd->PostMessage(<span style="color:#ff0000;">WM_USER+17</span>,0,0); } //<span style="font-family: Arial, Helvetica, sans-serif;"> 这段代码应在继承cwnd 子类才能编译同. (查看FindWindow 原型);</span>
接收端:
BOOL CRecvDlg::PreTranslateMessage(MSG* pMsg) { // TODO: 在此添加专用代码和/或调用基类 if(pMsg->message ==<span style="color:#ff0000;"> WM_USER+17</span>) { //处理 } return CDialog::PreTranslateMessage(pMsg); }
// 这里将接收端处理放在PreTranslateMessage 中,因此发送端应选择 PostMessage,SendMessage 消息不放进消息队列, PreTranslateMessage里不能收到这个消息.
通过Title和类名查找窗口
头文件:
#include <windows.h>
HWND FindWnd(std::string &title, std::string &className) { TCHAR strTitle[160] = { 0 }; HWND hwnd = NULL; HWND AfterHwnd = NULL; while (true) { hwnd = ::FindWindowExA(NULL, AfterHwnd, className.c_str(), NULL); if (!hwnd) break; else { if (::GetWindowText(hwnd, strTitle, 160)) { std::string wndTitle = strTitle; if (wndTitle.find(title) != std::string::npos) { return hwnd; } } } AfterHwnd = hwnd; } }