模拟按键控制记事本程序,发送一直不成功(含代码)
#include<windows.h>
#include<stdio.h>
main()
{
HWND notepad_h;
notepad_h=FindWindow("Notepad",NULL);
if(notepad_h!=NULL)
{
SetForegroundWindow(notepad_h);
//Sleep(15);
keybd_event(VK_MENU,0xb8,0,0);
keybd_event(VkKeyScan('F'),0,0,0);
keybd_event(VkKeyScan('F'),0,0,0);
keybd_event(VkKeyScan('A'),0x9e,0,0);
keybd_event(VkKeyScan('A'),0x9e,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('F'),0,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0xb8,KEYEVENTF_KEYUP,0);
//SendMessage(notepad_h,WM_KEYDOWN,VK_MENU,0);
//SendMessage(notepad_h,WM_CHAR,"F",0);
//SendMessage(notepad_h,WM_CHAR,"A",0);
}
}
h还是不可以
[解决办法]
#include <windows.h>
int main()
{
HWND hWnd = FindWindow("Notepad", "新建 文本文档.txt - 记事本");
HMENU hMenu = NULL;
if (hWnd && (hMenu = GetMenu(hWnd)))
{
HMENU hSubMenu = GetSubMenu(hMenu, 0);
int nMenuID = GetMenuItemID(hSubMenu, 3);//另存为...
SendMessage(hWnd, WM_COMMAND, MAKEWPARAM(nMenuID, 0), NULL);
}
return 0;
}