首先你在Test中定义全局的消息
UINT uMsg = RegisterWindowMessage("xxx");
在它上面加上一个按扭Button1
(由于我是在那个getMsg的View类中定义的消息处理
所以我在按扭中要找到TestMsg的客户窗口
在按扭的消息处理函数中写上:
void CTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
if (uMsg == 0)
{
MessageBox(TEXT("NO message"));
return ;
}
HWND hWnd = ::FindWindow(NULL, "无标题 - GetMsg");
if (hWnd == NULL)
{
MessageBox(TEXT("No Window"));
return ;
}
::SetForegroundWindow(hWnd);
//找到客户窗口
HWND hwnd = ::FindWindowEx(hWnd, NULL, "AfxFrameOrView42d", NULL);
if (hwnd == NULL)
{
MessageBox(TEXT("没有找到客户窗口"));
}
CWnd *pWnd = CWnd::FromHandle(hwnd);
//发送消息
pWnd->SendMessage(uMsg);
}
染后在来看看这个TestMsg程序
我在它的View里定义全局消息
UINT uMsg2 = ::RegisterWindowMessage("xxx");
在GetMsgView.h中定义
protected:
//{{AFX_MSG(CGetMsgView)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
afx_msg void OnMsg2();
DECLARE_MESSAGE_MAP()
在GetMsgView.cpp中定义
BEGIN_MESSAGE_MAP(CGetMsgView, CView)
//{{AFX_MSG_MAP(CGetMsgView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
ON_REGISTERED_MESSAGE(uMsg2, OnMsg2)//这个一定要用这个ON_REGISTERED_MESSAGE,其他的什么ON_MESSAGE是不行的,我就在这花了一些时间,最后在MSDN里查到了
END_MESSAGE_MAP()