windows七个消息队列
windows七个消息队列
文章目录
消息队列
cpp
/************************************************************************
Module: CopyData.cpp
Notices: Copyright(c) 2008 Jeffrey Ritcher & Christophe Nasarre
************************************************************************/
#include "CmnHdr.h"
#include "resource.h"
#include <malloc.h>
#include <tchar.h>
#include <windowsx.h>
//
BOOL Dlg_OnCopyData(HWND hWnd, HWND hWndFrom, PCOPYDATASTRUCT pcds) {
Edit_SetText(GetDlgItem(hWnd, pcds->dwData ? IDC_DATA2 : IDC_DATA1),
(PTSTR)pcds->lpData);
return (TRUE);
}
//
//
BOOL Dlg_OnInitDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam) {
chSETDLGICONS(hWnd, IDI_COPYDATA);
//初始化编辑框
Edit_SetText(GetDlgItem(hWnd, IDC_DATA1), TEXT("测试数据"));
Edit_SetText(GetDlgItem(hWnd, IDC_DATA2), TEXT("其他测试数据"));
return (TRUE);
}
//
void Dlg_OnCommand(HWND hWnd, int id, HWND hWndCtrl, UINT codeNotify) {
switch (id) {
case IDCANCEL:
EndDialog(hWnd, id);
break;
case IDC_COPYDATA1:
case IDC_COPYDATA2:
if (codeNotify != BN_CLICKED)
break;
//获取相应的文本框
HWND hWndEdit = GetDlgItem(hWnd,
(id == IDC_COPYDATA1) ? IDC_DATA1 : IDC_DATA2);
//准备一个COPYDATASTRUCT结构体,其内容会被Copy到
//一个内存映像文件中
COPYDATASTRUCT cds;
//指示哪个数据域的内容要被发送(0=ID_DATA1,1=ID_DATA2)
cds.dwData = (DWORD)((id == IDC_COPYDATA1) ? 0 : 1);
//获取文本的长度(字节)
cds.cbData = (Edit_GetTextLength(hWndEdit) + 1) * sizeof(TCHAR);
//在栈中分配一个内容以保存字符串内容
cds.lpData = _alloca(cds.cbData);//此功能已弃用,更安全的版本可用;见_malloca
//将编辑框里的字符串存到cbs.lpData中
Edit_GetText(hWndEdit, (PTSTR)cds.lpData, cds.cbData);
//获取应用程序的标题
TCHAR szCaption[100];
GetWindowText(hWnd, szCaption, sizeof(szCaption) / sizeof(szCaption[0]));
//枚举所有具有相同标题的顶层窗口
HWND hWndT = NULL;
do
{
hWndT = FindWindowEx(NULL, hWndT, NULL, szCaption);
if (hWndT != NULL)
{
FORWARD_WM_COPYDATA(hWndT, hWnd, &cds, SendMessage);
}
} while (hWndT != NULL);
break;
}
}
//
INT_PTR WINAPI Dlg_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
chHANDLE_DLGMSG(hWnd, WM_INITDIALOG, Dlg_OnInitDialog);
chHANDLE_DLGMSG(hWnd, WM_COMMAND, Dlg_OnCommand);
chHANDLE_DLGMSG(hWnd, WM_COPYDATA, Dlg_OnCopyData);
}
return (FALSE);
}
//
int WINAPI _tWinMain(HINSTANCE hInstExe, HINSTANCE hPrev, PTSTR lpCmdLIne, int nShowCmd) {
DialogBox(hInstExe, MAKEINTRESOURCE(IDD_COPYDATA), NULL, Dlg_Proc);
return 0;
}