首先你在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()
switch(Message)
{
case SWM_TRAYMSG:
switch(lPa)
{
case WM_LBUTTONDOWN:
ShowWindow(hWnd,SW_SHOW);
Shell_NotifyIcon(NIM_DELETE,&niData);
break;
default:
break;
}
case WM_SYSCOMMAND:
if((wPa) == SC_MINIMIZE)
{
ShowWindow(hWnd, SW_HIDE);
Shell_NotifyIcon(NIM_ADD,&niData);
return 1;
}
break;
/*********************/
case WM_INITDIALOG:
//注册explorer restart消息
s_uTaskbarRestart=RegisterWindowMessage(TEXT("TaskbarCreated"));
CheckRadioButton(hWnd,IDC_ONLY,IDC_ALL,IDC_ONLY);
break;
case WM_COMMAND:
switch(LOWORD(wPa))
{
case IDOK:
GetDlgItemText(hWnd,IDC_OBJ,Obj,THE_MAXPATH);
GetDlgItemText(hWnd,IDC_SOURCE,Source,THE_MAXPATH);
if (Obj[0]==0||Source[0]==0)//判断是不是NULL
{
MessageBox(hWnd,TEXT("缺少条件!"),TEXT("警告!"),MB_OK);
break;
}
if (PathIsDirectory(Obj))
{
if (SendMessage(GetDlgItem(hWnd,IDC_ONLY),BM_GETCHECK,0,0)==BST_CHECKED)
{
rez=ChangeTime(Obj,Source);
}
if (SendMessage(GetDlgItem(hWnd,IDC_ALL),BM_GETCHECK,0,0)==BST_CHECKED)
{
rez=ChangeWholeDirTime(Obj,Source,1);
}
if (SendMessage(GetDlgItem(hWnd,IDC_IGNORE),BM_GETCHECK,0,0)==BST_CHECKED)
{
rez=ChangeWholeDirTime(Obj,Source,2);
}
}
else
{
CheckRadioButton(hWnd,IDC_ONLY,IDC_ALL,IDC_ONLY);
rez=ChangeTime(Obj,Source);
}
if (rez)
{
MessageBox(hWnd,TEXT("似乎成功了?"),TEXT("Bingo!"),MB_OK);
}
else
{
MessageBox(hWnd,TEXT("失败了阿..."),TEXT("遗憾!"),MB_OK);
}
break;
case WM_CLOSE:
case IDCANCEL:
ExitProcess(0);
break;
default:
return 0;
}
break;
case WM_DROPFILES:
hDropInfo=(HDROP)wPa;
OnDropFiles(hWnd,hDropInfo);
break;
/*********************/
default:
//任务栏重建消息
if (Message==s_uTaskbarRestart)
{
Shell_NotifyIcon(NIM_ADD,&niData);
}
else
DefWindowProc(hWnd,Message,wPa,lPa);
}
return FALSE;