大概的步骤是: 通过FindWindow获得窗口的句柄, 通过FindWindowEx获得窗体控件的句柄, PostMessage发送消息.
以记事本为例:
HWND NotePad = NULL;
NotePad = ::FindWindow(NULL,_T("123 - 记事本"));
::ShowWindow(NotePad,SW_SHOWNORMAL);
::BringWindowToTop(NotePad);
HWND NoteEdit=NULL;
NoteEdit = ::FindWindowEx(NotePad, NULL, _T("Edit"), _T(""));
::PostMessage(NoteEdit, WM_SETTEXT, NULL,(LPARAM)("username"));//输入字符
::PostMessage(NoteEdit,WM_KEYDOWN,VK_SPACE ,0) ; //按空格键
::PostMessage(NoteEdit,WM_CHAR,WPARAM('g'),0);
::PostMessage(NoteEdit,WM_KEYDOWN,VK_RETURN ,0) ; //按回车键
另一个问题,怎样查看窗口上控件的类型和名字?
我使用的是VS自带的工具SPY++ , 如果安装了VS可以在 C:\Program Files\Microsoft Visual Studio 11.0\Common7\Tools中找到它,直接打开界面如下.可以看到记事本视窗中有两个视窗,Edit和msctls_statusbar32.上面的代码中NotePad的值应该是000E0C9A, NoteEdit的值应该是00160C7E.
三 PostMessage 向Windows窗口发送Alt组合键
转自 http://blog.csdn.net/wuxing1129/article/details/4298340
之前找到一个能正确发送的code:(Alt+A)
PostMessage(hWnd,WM_SYSKEYDOWN,VK_MENU,0);
PostMessage(hWnd,WM_SYSKEYDOWN,0x41,0);
Sleep(50);
PostMessage(hWnd,WM_SYSKEYUP,0x41,0);
PostMessage(hWnd,WM_SYSKEYUP,VK_MENU,0);
另一种做法:
偶然间又看到最后一个参数的说明,有所发现!先看WM_SYSKEYDOWN的help
The WM_SYSKEYDOWN message is posted to the window with the keyboard focus when the user holds down the ALT key and then presses another key. It also occurs when no window currently has the keyboard focus; in this case, the WM_SYSKEYDOWN message is sent to the active window. The window that receives the message can distinguish between these two contexts by checking the context code in the lKeyData parameter.
WM_SYSKEYDOWN
nVirtKey = (int) wParam; // virtual-key code
lKeyData = lParam; // key data
Parameters
nVirtKey
Value of wParam. Specifies the virtual-key code of the key being pressed.
lKeyData
Value of lParam. Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table:
Value Description
0-15 Specifies the repeat count. The value is the number of times the keystroke is repeated as a result of the user holding down the key.
16-23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM).
24 Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25-28 Reserved; do not use.
29 Specifies the context code. The value is 1 if the ALT key is down while the key is pressed; it is 0 if the WM_SYSKEYDOWN message is posted to the active window because no window has the keyboard focus.
30 Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.
31 Specifies the transition state. The value is always 0 for a WM_SYSKEYDOWN message.
之前曾经修改过keyData的16-23位为VK_MENU,第30位参数为1,但没效果
请看位29的说明!!
The value is 1 if the ALT key is down while the key is pressed;
当值为1时表示ALT键被按下!这不正是我需要的吗?于是把29位设置为1,函数调用变成
PostMessage(hWnd,WM_SYSKEYDOWN,0x41,1<<29);
经过测试,发现这个就是Alt+A的效果!!
postmessage(edit1.handle,wm_keydown,vk_down,$20000000)
Ctrl : $10000000;
Shift: $08000000;
Alt:20000000