how to make CreateProcess open new process in focus and not in background [duplicate]

若如初见. 提交于 2019-12-13 05:03:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!