问题
I created a very simple Win program. it opens notepad and after 5 seconds it opens calc. the problem is that always the first program opens in background and not in focus (see the picture). the second program opens in focus. i've been wondering about this for a while and i can't figure out why it happens or how to open the first program in focus.
I am using visual studio 2013 with the default Windows Application settings.
EDIT: this is NOT a duplicate question, what I am asking here is WHY the same CreateProcess() function creates one time the process in background and one time in focus!
#include <Windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = { 0 };
si.cb = sizeof(si);
CreateProcess(L"c:\\windows\\system32\\notepad.exe", NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
Sleep(5000);
STARTUPINFO si2 = { 0 };
PROCESS_INFORMATION pi2 = { 0 };
si2.cb = sizeof(si2);
CreateProcess(L"c:\\windows\\system32\\calc.exe", NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si2, &pi2);
return 0;
}
回答1:
found a way to solve this, very strange, and i have no idea why it works, but it works.
if i add:
MSG msg;
TranslateMessage(&msg);
in the WinMain, then the process I create gets in focus, very strange. can someone explain why it works?
来源:https://stackoverflow.com/questions/25034867/how-to-make-createprocess-open-new-process-in-focus-and-not-in-background