本篇文章较多图片,如需结论,直接看文章结尾
起初根据摘要我以为只要给该函数传递了相应的message,window消息机制就会自动执行这个相应的操作。
好了,接下来看下message的结构
是一个结构体
起初我的代码是这样写的,以为点击button1会触发button2的click事件
private void button1_Click(object sender, EventArgs e)
{
Message message = Message.Create(button2.Handle,513,IntPtr.Zero,IntPtr.Zero);
WndProc(ref message);
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("我被点击了!");
}
但是结果并不是这样,除了卡了一下,没有别的反应
看了下我之前的C++写界面的代码
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow
) {
static TCHAR szClassName[] = TEXT("Win32Demo"); //窗口类名
HWND hwnd; //窗口句柄
MSG msg; //消息
WNDCLASS wndclass; //窗口类
hInst = hInstance;
/**********第①步:注册窗口类**********/
//为窗口类的各个字段赋值
wndclass.style = CS_HREDRAW | CS_VREDRAW; //窗口风格
wndclass.lpfnWndProc = WndProc; //窗口过程
wndclass.cbClsExtra = 0; //暂时不需要理解
wndclass.cbWndExtra = 0; //暂时不需要理解
wndclass.hInstance = hInstance; //当前窗口句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //窗口图标
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //鼠标样式
wndclass.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_BTNFACE); //窗口背景画刷(灰色)
wndclass.lpszMenuName = NULL; //窗口菜单
wndclass.lpszClassName = szClassName; //窗口类名
//注册窗口
RegisterClass(&wndclass);
/*****第②步:创建窗口(并让窗口显示出来)*****/
hwnd = CreateWindow(
szClassName, //窗口名字
TEXT("Welcome"), //窗口标题(出现在标题栏)
WS_OVERLAPPEDWINDOW, //窗
CW_USEDEFAULT, //初始化时y轴的位置口风格
CW_USEDEFAULT, //初始化时x轴的位置
800, //窗口宽度
500, //窗口高度
NULL, //父窗口句柄
NULL, //窗口菜单句柄
hInstance, //当前窗口的句柄
NULL //不使用该值
);
//显示窗口
ShowWindow(hwnd, iCmdShow);
//更新(绘制)窗口
UpdateWindow(hwnd);
/**********第③步:消息循环**********/
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg); //翻译消息
DispatchMessage(&msg); //分派消息
}
return msg.wParam;
}
//窗口过程
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
//设置缓冲区
static TCHAR szBufSex[10];
static TCHAR szBufMarriage[10];
static TCHAR szBufPet[20];
static TCHAR szBufSubmit[100];
static TCHAR szBufTmp[10];
static HFONT hFont; //逻辑字体
//一组单选按钮
static HWND labSex; //静态文本框--性别
static HWND radioMale; //单选按钮--男
static HWND radioFemale; //单选按钮--女
//一组单选按钮
static HWND labMarriage; //静态文本框--婚姻状况
static HWND radioMarried; //单选按钮--已婚
static HWND radioSingle; //单选按钮--未婚
static HWND radioSecrecy; //单选按钮--保密
//一组复选框
static HWND labPet; //静态文本框--你的宠物
static HWND checkboxDog; //复选框--狗
static HWND checkboxCat; //复选框--猫
static HWND checkboxFish; //复选框--鱼
static HWND checkboxOther; //复选框--其他
static HWND btnSubmit; //按钮控件
switch (message) {
case WM_CREATE:
hFont = CreateFont(
-14, -7, 0, 0, 400,
FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY,
FF_DONTCARE, TEXT("微软雅黑")
);
//选择性别
labSex = CreateWindow(
TEXT("static"), TEXT("你的性别:"),
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT/*文字居右*/,
10, 10, 80, 26,
hWnd, (HMENU)1, hInst, NULL
);
radioMale = CreateWindow(
TEXT("button"), TEXT("男"),
WS_CHILD | WS_VISIBLE | BS_LEFT/*文字居左*/ | BS_AUTORADIOBUTTON /*单选按钮*/ | WS_GROUP,
95, 10, 50, 26,
hWnd, (HMENU)2, hInst, NULL
);
radioFemale = CreateWindow(
TEXT("button"), TEXT("女"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON,
150, 10, 50, 26,
hWnd, (HMENU)2, hInst, NULL
);
//选择婚姻状况
labMarriage = CreateWindow(
TEXT("static"), TEXT("婚姻状况:"),
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT,
10, 40, 80, 26,
hWnd, (HMENU)4, hInst, NULL
);
radioMarried = CreateWindow(
TEXT("button"), TEXT("已婚"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON | WS_GROUP,
95, 40, 65, 26,
hWnd, (HMENU)5, hInst, NULL
);
radioSingle = CreateWindow(
TEXT("button"), TEXT("未婚"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON,
165, 40, 65, 26,
hWnd, (HMENU)6, hInst, NULL
);
radioSecrecy = CreateWindow(
TEXT("button"), TEXT("保密"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON,
235, 40, 100, 26,
hWnd, (HMENU)7, hInst, NULL
);
//你的宠物
labPet = CreateWindow(
TEXT("static"), TEXT("你的宠物:"),
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT,
10, 70, 80, 26,
hWnd, (HMENU)8, hInst, NULL
);
checkboxDog = CreateWindow(
TEXT("button"), TEXT("狗"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX/*复选框*/,
95, 70, 50, 26,
hWnd, (HMENU)9, hInst, NULL
);
checkboxCat = CreateWindow(
TEXT("button"), TEXT("猫"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX,
150, 70, 50, 26,
hWnd, (HMENU)10, hInst, NULL
);
checkboxFish = CreateWindow(
TEXT("button"), TEXT("鱼"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX,
205, 70, 50, 26,
hWnd, (HMENU)11, hInst, NULL
);
checkboxOther = CreateWindow(
TEXT("button"), TEXT("其他"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX,
260, 70, 65, 26,
hWnd, (HMENU)12, hInst, NULL
);
//创建按钮控件
btnSubmit = CreateWindow(TEXT("button"), TEXT("提 交"),
WS_CHILD | WS_VISIBLE | WS_BORDER | BS_FLAT,
95, 110, 200, 36,
hWnd, (HMENU)13, hInst, NULL
);
SendMessage(labSex, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(radioMale, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(radioFemale, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(labMarriage, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(radioMarried, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(radioSingle, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(radioSecrecy, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(labPet, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(checkboxDog, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(checkboxCat, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(checkboxFish, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(checkboxOther, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(btnSubmit, WM_SETFONT, (WPARAM)hFont, NULL);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
if (wmEvent == BN_CLICKED) {
switch (wmId) {
//获取性别
case 2:
case 3:
GetWindowText((HWND)lParam, szBufSex, 10);
break;
//获取婚姻状况
case 5:
case 6:
case 7:
GetWindowText((HWND)lParam, szBufMarriage, 10);
break;
//获取宠物
case 9:
case 10:
case 11:
case 12:
memset(szBufPet, 0, sizeof(szBufPet));
//是否选中狗
if (SendMessage(checkboxDog, BM_GETCHECK, 0, 0) == BST_CHECKED) {
GetWindowText(checkboxDog, szBufTmp, 10);
wsprintf(szBufPet, TEXT("%s %s"), szBufPet, szBufTmp);
}
//是否选中猫
if (SendMessage(checkboxCat, BM_GETCHECK, 0, 0) == BST_CHECKED) {
GetWindowText(checkboxCat, szBufTmp, 10);
wsprintf(szBufPet, TEXT("%s %s"), szBufPet, szBufTmp);
}
//是否选中鱼
if (SendMessage(checkboxFish, BM_GETCHECK, 0, 0) == BST_CHECKED) {
GetWindowText(checkboxFish, szBufTmp, 10);
wsprintf(szBufPet, TEXT("%s %s"), szBufPet, szBufTmp);
}
//是否选中其他
if (SendMessage(checkboxOther, BM_GETCHECK, 0, 0) == BST_CHECKED) {
GetWindowText(checkboxOther, szBufTmp, 10);
wsprintf(szBufPet, TEXT("%s %s"), szBufPet, szBufTmp);
}
break;
case 13:
wsprintf(szBufSubmit, TEXT("你的性别:%s\n婚姻状况:%s\r\n你的宠物:%s"), szBufSex, szBufMarriage, szBufPet);
MessageBox(hWnd, szBufSubmit, TEXT("信息提示"), MB_ICONINFORMATION);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
case WM_DESTROY:
DeleteObject(hFont);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
而后试了试这个
Message message = Message.Create(button2.Handle, 0x0002, IntPtr.Zero, IntPtr.Zero);
WndProc(ref message);
有了响应,窗口被销毁了。
接下来又试了试sendMessage
SendMessage(button2.Handle, 0x0201, 0, 0);
SendMessage(button2.Handle, 0x0202, 0, 0);
可以触发button2的单击事件
这么来看应该是消息和参数传的不对。
接下来用spy++捕获下消息看下(界面如下)
首先捕获button1
button1_click中内容置空,并单机button1
下面是捕获结果
<000111> 00030636 P WM_MOUSEMOVE fwKeys:0000 xPos:49 yPos:13
鼠标移动,移动的时候并没有别的按键状态,坐标49,13
<000112> 00030636 S WM_NCHITTEST xPos:403 yPos:263
确定窗体相对于屏幕的位置 坐标403,263
<000113> 00030636 R WM_NCHITTEST nHittest:HTCLIENT
指示在客户区
<000114> 00030636 S WM_NCHITTEST xPos:403 yPos:263
<000115> 00030636 R WM_NCHITTEST nHittest:HTCLIENT
<000116> 00030636 S WM_MOUSEACTIVATE hwndTopLevel:000B0776 nHittest:HTCLIENT uMsg:WM_LBUTTONDOWN
用户按下鼠标激发此消息,并传递顶层活跃窗体句柄和NCHITTEST的返回值和鼠标操作
<000117> 00030636 R WM_MOUSEACTIVATE fuActivate:MA_ACTIVATE
激活窗体,此鼠标消息被接受
<000118> 00030636 S WM_SETCURSOR hwnd:00030636 nHittest:HTCLIENT wMouseMsg:WM_LBUTTONDOWN
设置button1捕获光标
<000119> 00030636 R WM_SETCURSOR fHaltProcessing:False
返回false,继续处理
<000120> 00030636 P WM_LBUTTONDOWN fwKeys:MK_LBUTTON xPos:49 yPos:13
鼠标按下操作,行为是左击,坐标49,13
<000121> 00030636 P WM_PAINT hdc:00000000
绘制界面
<000122> 00030636 P WM_LBUTTONUP fwKeys:0000 xPos:49 yPos:13
鼠标左击抬起
<000123> 00030636 S WM_PAINT hdc:00000000
<000124> 00030636 R WM_PAINT
<000125> 00030636 S WM_NCHITTEST xPos:403 yPos:263
<000126> 00030636 R WM_NCHITTEST nHittest:HTCLIENT
<000127> 00030636 S WM_CAPTURECHANGED hwndNewCapture:00000000
<000128> 00030636 R WM_CAPTURECHANGED
窗体失去对光标的捕获
<000129> 00030636 S WM_NCHITTEST xPos:403 yPos:263
<000130> 00030636 R WM_NCHITTEST nHittest:HTCLIENT
<000131> 00030636 S WM_NCHITTEST xPos:403 yPos:263
<000132> 00030636 R WM_NCHITTEST nHittest:HTCLIENT
<000133> 00030636 S WM_SETCURSOR hwnd:00030636 nHittest:HTCLIENT wMouseMsg:WM_MOUSEMOVE
<000134> 00030636 R WM_SETCURSOR fHaltProcessing:False
<000135> 00030636 P WM_MOUSEMOVE fwKeys:0000 xPos:49 yPos:13
----------------------------------------------------------分割线----------------------------------------------------------------------
对上面捕获的消息进行解释(英文来自官网,翻译是个人理解)
S 是指使用 SendMessage 发送的消息,R 是使用SendMessage发送消息后得到的返回值。P 是使用PostMessage 发送的消息。
WM_MOUSEMOVE :Posted to a window when the cursor moves. If the mouse is not captured, the message is posted to the window that contains the cursor. Otherwise, the message is posted to the window that has captured the mouse.(大概意思就是鼠标移动的时候发送这个消息,如果鼠标没有被捕获,这个消息就发送到包含鼠标的哪个窗口,如果被捕获,就发送到哪个捕获的窗口(控件))
参数:(wParam是一些状态参数,鼠标按下的键盘按下,LParam是鼠标的坐标)
WM_NCHITTEST :
Sent to a window in order to determine what part of the window corresponds to a particular screen coordinate. This can happen, for example, when the cursor moves, when a mouse button is pressed or released, or in response to a call to a function such as WindowFromPoint. If the mouse is not captured, the message is sent to the window beneath the cursor. Otherwise, the message is sent to the window that has captured the mouse.A window receives this message through its WindowProc function.(发送到窗口以确定该窗口的哪一部分对应于特定的屏幕坐标。当光标移动,按下或释放鼠标键时,或者响应对诸如WindowFromPoint之类的函数的调用时,可能会触发这个消息。如果未捕获鼠标,则消息将发送到光标下方的窗口。否则,消息将发送到捕获鼠标的窗口)
参数:(wParam不使用,LParam是光标的坐标)
返回值:
WM_MOUSEACTIVATE:Sent when the cursor is in an inactive window and the user presses a mouse button. The parent window receives this message only if the child window passes it to the DefWindowProc function.(当光标出于一个非活动窗口,并且用户按钮鼠标按键,只有当子窗体将这个消息交给默认窗口过程的时候,父窗体才会收到这个消息)
参数:(wparam:顶层活跃的父窗口的句柄,lParam:低位是作为处理WM_NCHITTEST消息而返回的命中测试值,高位是鼠标消息)
WM_SETCURSOR:Sent to a window if the mouse causes the cursor to move within a window and mouse input is not captured.(发送到没有捕获鼠标且鼠标移动到该窗体上方的窗体)
参数:
WM_LBUTTONDOWN:Posted when the user presses the left mouse button while the cursor is in the client area of a window. If the mouse is not captured, the message is posted to the window beneath the cursor. Otherwise, the message is posted to the window that has captured the mouse.(简单点意思就是当鼠标左击,发送消息,如果左击时候鼠标被捕获,则发送到捕获鼠标的窗体中,如果没有,则发送给鼠标下方的窗体中)
参数、返回值:
WM_PAINT:The WM_PAINT message is sent when the system or another application makes a request to paint a portion of an application's window. The message is sent when the UpdateWindow or RedrawWindow function is called, or by the DispatchMessage function when the application obtains a WM_PAINT message by using the GetMessage or PeekMessage function.
无参数和返回值
WM_CAPTURECHANGED:Sent to the window that is losing the mouse capture.
参数:
wParam:没有用到。lParam:A handle to the window gaining the mouse capture.
接下来看看这段截取的消息说了点什么(为了大家方便,我把消息的解释放到了哪个捕获的消息下面)
----------------------------------------分割线-----------------------------------------------------------------------------
好,终于分析完一个单击操作了
下面试一试sendmessage
private void button1_Click(object sender, EventArgs e)
{
//Thread th = new Thread(new ThreadStart(WriteTextSafe));
//th.Start();
//Message message = Message.Create(button2.Handle, 0x0002, IntPtr.Zero, IntPtr.Zero);
//WndProc(ref message);
//message.Msg = 0x0111;
//WndProc(ref message);
SendMessage(button2.Handle, 0x0201, 0, 0);
SendMessage(button2.Handle, 0x0202, 0, 0);
//message.Msg = 0x0202;
//base.WndProc(ref message);
}
继续用spy++捕获
button1捕获内容:
<000170> 001605EE P WM_MOUSEHOVER
<000171> 001605EE S WM_NCHITTEST xPos:495 yPos:301
<000172> 001605EE R WM_NCHITTEST nHittest:HTCLIENT
<000173> 001605EE S WM_NCHITTEST xPos:495 yPos:301
<000174> 001605EE R WM_NCHITTEST nHittest:HTCLIENT
<000175> 001605EE S WM_MOUSEACTIVATE hwndTopLevel:001406EC nHittest:HTCLIENT uMsg:WM_LBUTTONDOWN
<000176> 001605EE R WM_MOUSEACTIVATE fuActivate:MA_ACTIVATE
<000177> 001605EE S WM_SETCURSOR hwnd:001605EE nHittest:HTCLIENT wMouseMsg:WM_LBUTTONDOWN
<000178> 001605EE R WM_SETCURSOR fHaltProcessing:False
<000179> 001605EE P WM_LBUTTONDOWN fwKeys:MK_LBUTTON xPos:33 yPos:8
<000180> 001605EE S WM_IME_SETCONTEXT fSet:1 iShow:C000000F
<000181> 001605EE R WM_IME_SETCONTEXT
<000182> 001605EE S WM_SETFOCUS hwndLoseFocus:002105D4
<000183> 001605EE S WM_IME_NOTIFY dwCommand:IMN_SETOPENSTATUS dwCommand:00000008 dwData:00000000
<000184> 001605EE R WM_IME_NOTIFY
<000185> 001605EE S WM_IME_SETCONTEXT fSet:0 iShow:C000000F
<000186> 001605EE R WM_IME_SETCONTEXT
<000187> 001605EE S WM_IME_SETCONTEXT fSet:1 iShow:C000000F
<000188> 001605EE R WM_IME_SETCONTEXT
<000189> 001605EE S WM_GETTEXTLENGTH
<000190> 001605EE R WM_GETTEXTLENGTH cch:7
<000191> 001605EE S WM_GETTEXT cchTextMax:16 lpszText:00BDD6FC
<000192> 001605EE R WM_GETTEXT cchCopied:7 lpszText:00BDD6FC ("b")
<000193> 001605EE S message:0x202B [用户定义:WM_USER+7211] wParam:001605EE lParam:00BDE6DC
<000194> 001605EE R message:0x202B [用户定义:WM_USER+7211] lResult:00000000
<000195> 001605EE R WM_SETFOCUS
<000196> 001605EE P WM_MOUSEMOVE fwKeys:MK_LBUTTON xPos:33 yPos:9
<000197> 001605EE P WM_MOUSEMOVE fwKeys:MK_LBUTTON xPos:33 yPos:9
<000198> 001605EE P WM_PAINT hdc:00000000
<000199> 001605EE P WM_LBUTTONUP fwKeys:0000 xPos:33 yPos:9
<000200> 001605EE S WM_PAINT hdc:00000000
<000201> 001605EE R WM_PAINT
<000202> 001605EE S WM_NCHITTEST xPos:495 yPos:302
<000203> 001605EE R WM_NCHITTEST nHittest:HTCLIENT
<000204> 001605EE S WM_KILLFOCUS hwndGetFocus:001606C4
<000205> 001605EE S WM_CAPTURECHANGED hwndNewCapture:00000000
<000206> 001605EE R WM_CAPTURECHANGED
<000207> 001605EE R WM_KILLFOCUS
<000208> 001605EE S WM_IME_SETCONTEXT fSet:0 iShow:C000000F
<000209> 001605EE R WM_IME_SETCONTEXT
<000210> 001605EE S WM_NCHITTEST xPos:495 yPos:302
<000211> 001605EE R WM_NCHITTEST nHittest:HTCLIENT
<000212> 001605EE S WM_NCHITTEST xPos:495 yPos:302
<000213> 001605EE R WM_NCHITTEST nHittest:HTCLIENT
<000214> 001605EE S WM_SETCURSOR hwnd:001605EE nHittest:HTCLIENT wMouseMsg:WM_MOUSEMOVE
<000215> 001605EE R WM_SETCURSOR fHaltProcessing:False
<000216> 001605EE P WM_MOUSEMOVE fwKeys:0000 xPos:33 yPos:9
<000217> 001605EE P WM_PAINT hdc:00000000
<000218> 001605EE S WM_NCHITTEST xPos:495 yPos:302
<000219> 001605EE R WM_NCHITTEST nHittest:HTCLIENT
<000220> 001605EE S WM_NCHITTEST xPos:495 yPos:302
<000221> 001605EE R WM_NCHITTEST nHittest:HTCLIENT
<000222> 001605EE S WM_SETCURSOR hwnd:001605EE nHittest:HTCLIENT wMouseMsg:WM_MOUSEMOVE
<000223> 001605EE R WM_SETCURSOR fHaltProcessing:False
<000224> 001605EE P WM_MOUSEMOVE fwKeys:0000 xPos:33 yPos:9
button2捕获内容:
<000001> 001606C4 S WM_IME_SETCONTEXT fSet:1 iShow:C000000F
<000002> 001606C4 S WM_IME_NOTIFY dwCommand:IMN_OPENSTATUSWINDOW dwCommand:00000002 dwData:00000000
<000003> 001606C4 R WM_IME_NOTIFY
<000004> 001606C4 R WM_IME_SETCONTEXT
<000005> 001606C4 S WM_SETFOCUS hwndLoseFocus:(null)
<000006> 001606C4 S WM_GETTEXTLENGTH
<000007> 001606C4 R WM_GETTEXTLENGTH cch:7
<000008> 001606C4 S WM_GETTEXT cchTextMax:16 lpszText:00BDD68C
<000009> 001606C4 R WM_GETTEXT cchCopied:7 lpszText:00BDD68C ("b")
<000010> 001606C4 S message:0x202B [用户定义:WM_USER+7211] wParam:001606C4 lParam:00BDE66C
<000011> 001606C4 R message:0x202B [用户定义:WM_USER+7211] lResult:00000000
<000012> 001606C4 R WM_SETFOCUS
<000013> 001606C4 S WM_KILLFOCUS hwndGetFocus:001605EE
<000014> 001606C4 R WM_KILLFOCUS
<000015> 001606C4 S WM_IME_SETCONTEXT fSet:0 iShow:C000000F
<000016> 001606C4 R WM_IME_SETCONTEXT
<000017> 001606C4 P WM_PAINT hdc:00000000
<000018> 001606C4 S WM_LBUTTONDOWN fwKeys:0000 xPos:0 yPos:0
<000019> 001606C4 S WM_IME_SETCONTEXT fSet:1 iShow:C000000F
<000020> 001606C4 R WM_IME_SETCONTEXT
<000021> 001606C4 S WM_SETFOCUS hwndLoseFocus:001605EE
<000022> 001606C4 S WM_GETTEXTLENGTH
<000023> 001606C4 R WM_GETTEXTLENGTH cch:7
<000024> 001606C4 S WM_GETTEXT cchTextMax:16 lpszText:00BDD174
<000025> 001606C4 R WM_GETTEXT cchCopied:7 lpszText:00BDD174 ("b")
<000026> 001606C4 S message:0x202B [用户定义:WM_USER+7211] wParam:001606C4 lParam:00BDE154
<000027> 001606C4 R message:0x202B [用户定义:WM_USER+7211] lResult:00000000
<000028> 001606C4 R WM_SETFOCUS
<000029> 001606C4 R WM_LBUTTONDOWN
<000030> 001606C4 S WM_LBUTTONUP fwKeys:0000 xPos:0 yPos:0
<000031> 001606C4 S WM_PAINT hdc:00000000
<000032> 001606C4 R WM_PAINT
<000033> 001606C4 S WM_NCHITTEST xPos:292 yPos:293
<000034> 001606C4 R WM_NCHITTEST nHittest:HTCLIENT
<000035> 001606C4 S WM_CAPTURECHANGED hwndNewCapture:00000000
<000036> 001606C4 R WM_CAPTURECHANGED
<000037> 001606C4 R WM_LBUTTONUP
<000038> 001606C4 S WM_KILLFOCUS hwndGetFocus:(null)
<000039> 001606C4 R WM_KILLFOCUS
<000040> 001606C4 S WM_IME_SETCONTEXT fSet:0 iShow:C000000F
<000041> 001606C4 S WM_IME_NOTIFY dwCommand:IMN_CLOSESTATUSWINDOW dwCommand:00000001 dwData:00000000
<000042> 001606C4 R WM_IME_NOTIFY
<000043> 001606C4 R WM_IME_SETCONTEXT
<000044> 001606C4 P WM_PAINT hdc:00000000
-----------------------------------------分割线---------------------------------------------------------------------------------------
WM_IME_SETCONTEXT :Sent to an application when a window is activated. ((输入焦点转移到了某个窗口上))
参数:
fSet = (BOOL) wParam;
iShow = (DWORD) lParam;
WM_SETFOCUS :Sent to a window after it has gained the keyboard focus.
参数:
WM_IME_NOTIFY :Sent to an application to notify it of changes to the IME window. A window receives this message through its WindowProc function.
参数:
WM_GETTEXTLENGTH:Determines the length, in characters, of the text associated with a window
参数:
WM_GETTEXT :Copies the text that corresponds to a window into a buffer provided by the caller.
参数:
WM_USER:Used to define private messages for use by private window classes, usually of the form WM_USER+x, where x is an integer value.
说明:
WM_KILLFOCUS :Sent to a window immediately before it loses the keyboard focus.
说明:
再看一下form1截获的消息
可知在使用wndproc发送消息时,这个消息根本没有进来,而使用Sendmessage时,里面又这样一个记录
<000111> 000E09E2 S WM_KILLFOCUS hwndGetFocus:001C0562
button1失去焦点,并将焦点赋给button2。
那么问题来了,我的消息去哪里了?
--------------------------------------------------------分割线------------------------------------------------------------------------
看了下上面的C++程序,并用spy++捕获了一下,才发现犯了个弱智的错误,窗口过程函数,顾名思义是传给窗口的,正确的写法如下
Message message = Message.Create(this.Handle, 0x0111, (IntPtr)2, button2.Handle);
WndProc(ref message);
0x0111代表的是WM_COMMAND(Sent when the user selects a command item from a menu, when a control sends a notification message to its parent window, or when an accelerator keystroke is translated)简单点就是form里面的一些基本操作都会捕获,2代指的是TabIndex。