Prevent CreateProcess from stealing focus from main application
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Prevent CreateProcess from stealing focus from main application

  1. #1
    Join Date
    Apr 1999
    Location
    Jerusalem, Israel
    Posts
    304

    Prevent CreateProcess from stealing focus from main application

    Hello,
    How can I prevent a new application I create by CreateProcess from taking the focus from my application?

    Thanks,
    InfraRed.
    Please rate my post, if it helped you.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,278

    Re: Prevent CreateProcess from stealing focus from main application

    Try to set the wShowWindow flag of lpStartupInfo parameter to SW_SHOWNOACTIVATE or to SW_SHOWMINNOACTIVE
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 1999
    Location
    Jerusalem, Israel
    Posts
    304

    Re: Prevent CreateProcess from stealing focus from main application

    Tried both. Didn't work. The new application window steals my focus!
    InfraRed.
    Please rate my post, if it helped you.

  4. #4
    Join Date
    Apr 1999
    Location
    Jerusalem, Israel
    Posts
    304

    Re: Prevent CreateProcess from stealing focus from main application

    Correction: If I run calc.exe it takes the focus and becomes activated.
    If I run notepad.exe it appears before my application but it's not activated - which is what I want.

    Why calc behaves differently?
    Thanks.
    InfraRed.
    Please rate my post, if it helped you.

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,278

    Re: Prevent CreateProcess from stealing focus from main application

    Ask the calc.exe developer!
    I guess, calc.exe activates itself.
    Victor Nijegorodov

  6. #6
    Join Date
    Apr 1999
    Location
    Jerusalem, Israel
    Posts
    304

    Re: Prevent CreateProcess from stealing focus from main application

    Thanks, I'll ask!...

    Maybe because it's a dialog it behaves like this.
    But I have another application (developed be me) that have a messages only window, and it behaves just like calc... There is no code at all about activating. The only window command I do on this window is ShowWindow(SW_HIDE);!!

    Any idea about that?
    InfraRed.
    Please rate my post, if it helped you.

  7. #7
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,278

    Re: Prevent CreateProcess from stealing focus from main application

    Have a look at the Remarks section of ShowWindow article in MSDN.
    Victor Nijegorodov

  8. #8
    Join Date
    Dec 2005
    Posts
    642

    Lightbulb Re: Prevent CreateProcess from stealing focus from main application

    Quote Originally Posted by infrared
    Tried both. Didn't work. The new application window steals my focus!
    Did you set the dwFlags field in STARTUPINFO to STARTF_USESHOWWINDOW? If you don't, the wShowWindow field is ignored.

  9. #9
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,278

    Re: Prevent CreateProcess from stealing focus from main application

    Quote Originally Posted by googler
    Did you set the dwFlags field in STARTUPINFO to STARTF_USESHOWWINDOW? If you don't, the wShowWindow field is ignored.
    Good point!
    Victor Nijegorodov

  10. #10
    Join Date
    Apr 1999
    Location
    Jerusalem, Israel
    Posts
    304

    Re: Prevent CreateProcess from stealing focus from main application

    Thanks for asking... yes, I did that of course.
    Code:
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_SHOWNOACTIVATE;
    
    bLaunched = ::CreateProcess(NULL, const_cast<LPTSTR>(strPath), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
    Better than that, in my application I call only once ShowWindow with WS_NOACTIVATE.

  11. #11
    Join Date
    Apr 1999
    Location
    Jerusalem, Israel
    Posts
    304

    Re: Prevent CreateProcess from stealing focus from main application

    Found it...
    I have extra call to SetWindowPos with z-order parameter set...

    Thanks.
    InfraRed.
    Please rate my post, if it helped you.

  12. #12
    Join Date
    Nov 2013
    Posts
    1

    Re: Prevent CreateProcess from stealing focus from main application

    Neither answers above solved my problem: I did everything, my child process (which is a dialog based application) stole the focus from the creator and the hourglass appeared for a moment.

    My solution:
    STARTUPINFO startupInfo;
    ZeroMemory(&startupInfo, sizeof(startupInfo));
    startupInfo.cb = sizeof(startupInfo);
    startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEOFFFEEDBACK;
    startupInfo.wShowWindow = SW_SHOWMINNOACTIVE;

    but this is not enough! You have to return from OnInitDialog with FALSE, not TRUE.
    If you do not modify the OnInitDialog return value, you will not succeed, as I know.

    So because the calc.exe returns TRUE, there is no way to keep the focus with CreateProcess I think...

  13. #13
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,763

    Re: Prevent CreateProcess from stealing focus from main application

    why ressurect a 6 year old thread. Make a new post instead;


    there is no way you can prevent an app that is being started from taking the focus if it is coded to do so (other than really dirty tricks). You can typically only do this if you are creating the started app yourself and explicitely code it to not have itself take focus.

    The startup settings in the STARTUPINFO are little more than "guides" for the app to use, but most apps ignore that info when they're being launched.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center