HWND hWnd;
hWnd = ::FindWindow(NULL, "指定的窗口标题");//得到目标窗口句柄
if ( hWnd == NULL )
{
AfxMessageBox("没有找到目标程序");
}
else
{
::PostMessage(hWnd, WM_KEYDOWN, VK_F1, 1);
::PostMessage(hWnd,WM_KEYUP, VK_F1, 1); //释放
}
或者:
HWND hWnd;
hWnd = ::FindWindow(NULL, "计算器");//找到目标窗口的句柄
if ( hWnd != NULL )
{
::PostMessage(hWnd, WM_KEYDOWN, VK_F1, 1);
::PostMessage(hWnd,WM_KEYUP, VK_F1, 1); //释放
}
//实现向最小化的记事本打一个“A”进去
#include <windows.h>
void main()
{
HWND hWnd = ::FindWindowW(L"Notepad",NULL); //搜索记事本程序主窗口句柄
HWND hWndc = ::GetWindow(hWnd,GW_CHILD); //获得记事本客户区句柄(该窗口是记事本主窗口的子窗口,即那个白色的可编辑区域)
if(hWndc) //如果获得了该句柄
::SendMessage(hWndc,WM_CHAR,'A',NULL);//发送按键消息
}