自定义消息
BEGIN_MSG_MAP()
ON_MESSAGE(WM_MY_MESSAGE, Mymessage)
END_MSG_MAP()
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
#define WM_MY_MESSAGE (WM_USER + 100)
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK Mymessage(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_S_MESSAGE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_S_MESSAGE);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
if (LOWORD(wParam) == ID_SEND)
{
//为什么我发送消息后,消息函数没有反映????
::SendMessage(GetActiveWindow(),
WM_MY_MESSAGE, 0, 0);
return TRUE;
}
}
return FALSE;
}
LRESULT CALLBACK Mymessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// MessageBox (NULL, "Message sent", "Message", MB_OK);
// TextOut(hdc, 100, 200, str, strlen(str));
PAINTSTRUCT ps;
HDC hdc;
TCHAR sztext[MAX_LOADSTRING];
::SendMessage(GetDlgItem((HWND)hWnd, IDC_EDIT1),
WM_GETTEXT, MAX_LOADSTRING, (LPARAM)sztext);
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, sztext, strlen(sztext), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
return 0;
}
if (LOWORD(wParam) == ID_SEND)
{
//为什么在这里我发送消息后,消息函数没有反映????
::SendMessage(GetActiveWindow(),
WM_MY_MESSAGE, 0, 0);
return TRUE;
}
问题点数:50、回复次数:5Top
1 楼goodname008(卢培培,充电中......)回复于 2005-10-15 11:43:54 得分 10
楼主像用Win32 SDK程序模拟MFC吧?
1、句柄要传对。
2、消息映射宏要定义对。
3、Mymessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)好像不应该有前两个参数吧? :DTop
2 楼vcmute(BCare4 H1Rest Good9!)回复于 2005-10-15 11:54:15 得分 40
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_MY_MESSAGE:
Mymessage(hDlg,message,wParam,lParam);
break;
case WM_INITDIALOG:
return TRUE;Top
3 楼heixuejun()回复于 2005-10-15 13:01:28 得分 0
vcmute(横秋)
为什么一定要把自定义的消息加在那里啊!!!Top
4 楼Mackz(在相互)回复于 2005-10-15 13:03:28 得分 0
如果你这个不是MFC程序,类似BEGIN_MSG_MAP这些宏有什么用呢?这些东西不是单独拿出来就可以用的,里面实现还是比较复杂的。
vcmute(横秋) 提供的其实还可以。Top
5 楼heixuejun()回复于 2005-10-15 13:19:04 得分 0
BEGIN_MSG_MAP
有用的!!我都成功了!!Top




