#include <windows.h>
int main()
{
HWND parentHwnd, childHwnd;
INPUT input[4];
parentHwnd = FindWindow(TEXT("Notepad"), NULL);
if (parentHwnd)
{
childHwnd = FindWindowEx(parentHwnd, NULL, TEXT("Edit"), NULL);
if (childHwnd)
{
for (int i = 0; i < 10; i++)
{
SendMessage(childHwnd, WM_CHAR, 'A', 0);
}
SetForegroundWindow(parentHwnd);
//CTRL+S
memset(input, 0, sizeof(input));
input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;
input[0].ki.wVk = input[2].ki.wVk = VK_CONTROL;
input[1].ki.wVk = input[3].ki.wVk = 0x53;
input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(4, input, sizeof(INPUT));
//ALT+F4
memset(input, 0, sizeof(input));
input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;
input[0].ki.wVk = input[2].ki.wVk = VK_MENU;
input[1].ki.wVk = input[3].ki.wVk = VK_F4;
input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(4, input, sizeof(INPUT));
}
}
return 0;
}
/*
发送字符串
TCHAR *str = TEXT("Hello World");
SendMessage(childHwnd, WM_SETTEXT, 0, (LPARAM)str);
腾讯QQ
FindWindow(TEXT("TXGuiFoundation"), TEXT("N3verL4nd"));
*/
/*
POINT pt;
char *str = "Hello World";
while (1)
{
Sleep(1000);
GetCursorPos(&pt);
//hwnd = WindowFromPoint(pt);
hwnd = FindWindow(TEXT("Notepad"), NULL);
SendMessage(hwnd, WM_CHAR, (WPARAM)'G', NULL);
}
*/
实现操作:向已经打开的记事本写入数据,保存(CTRL+S),关闭(ALT+F4)。
尝试用SendMessage发送组合键,没有得到解决办法。
如果我们获取QQ窗口的HWND,那么我们就可以自动发消息了。
int main()
{
HWND hwndTX, hwndConsole;
INPUT input[4];
hwndConsole = GetConsoleWindow();
hwndTX = FindWindow(TEXT("TXGuiFoundation"), TEXT("N3verL4nd"));
if (hwndTX != NULL)
{
for (int i = 0; i < 10; i++)
{
SendMessage(hwndTX, WM_CHAR, 'A', 0);
}
SetForegroundWindow(hwndTX);
memset(input, 0, sizeof(input));
input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;
input[0].ki.wVk = input[2].ki.wVk = VK_MENU;
input[1].ki.wVk = input[3].ki.wVk = 0x53;
input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(4, input, sizeof(INPUT));
SetForegroundWindow(hwndConsole);
}
else
{
puts("not found");
}
return 0;
}