1、GetActiveWindow返回线程的活动窗口,而不是系统的活动窗口。如果要得到用户正在激活的窗口,应该使用 GetForegroundWindow,如可以用GetForegroundWindow判断当前lol是否在玩。
2、例如:
#include <IOSTREAM.H>
#include <Windows.h>
bool execwait(char *cmdline,char *cmd,int timeout);
void main()
{
for (int i = 0;i < 2;i++)
{
HANDLE hCurrentCursor = GetForegroundWindow ();
cout<<hCurrentCursor<<endl;
execwait(NULL,"cmd.exe /c notepad.exe D:\\1.txt",0);
Sleep(2000);
}
}
bool execwait(char *cmdline,char *cmd,int timeout)
{
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory( &pi, sizeof(pi) );
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
CreateProcess(cmdline,cmd,NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
WaitForSingleObject(pi.hProcess ,timeout);
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return true;
}