none
How to activate my application...

    Question

  • Hi,

    I know this has been asked numerous times...  My question is:  What is the best way for my application to activate itself.  Many threads mention to use the SetForegroundWindow() win32 function on my main form's window handle.  However, this doesn't seem to be the correct thing to do if, for instance, the user has a modal file-open dialog open in my app.  In this case, the setting my main window as the foreground window puts the app into a goofy state where the main window looks active, but the modal dialog looks inactive.  

    What I'd like to have happen is what you see when you click on an app's entry in the taskbar...  In this case, the modal window is correctly shown as active and the mainwindow as inactive.  

    Any help on this would be most appreciated.  

     

    Thanks.

    Monday, October 18, 2010 7:59 PM

Answers

  • You can use SetForegroundWindow in combination with GetWindow :

                       int SW_RESTORE = 9;
                       int GW_HWNDPREV = 3;

                        IntPtr nextWindow = GetWindow(mainWindowHandle, GW_HWNDPREV);

                        ShowWindow(nextWindow, SW_RESTORE);
                        SetForegroundWindow(nextWindow);

     

    This will make sure you don't have the weird behaviour.

    Regards,

    Fábio


    "To alcohol! The cause of and solution to all of life's problems." - Homer Simpson
    Tuesday, October 19, 2010 5:41 PM

All replies


  • I'm not sure I understand your situation.  With the code below, I can minimize form2, and maximize without issue the instance of form1 on the taskbar, which correctly shows form2 as the active form.  No special coding necessary.

    --
    Mike
    Tuesday, October 19, 2010 1:29 AM
  • try below code;

     

    formInstance.TopMost = true;

    formInstance.TopMost = false;

     

    this lines make your windows form in topmost order and restore the TopMost property to false but your form is top in z-order


    Any fool can know. The point is to understand.(Albert Einstein)
    Tuesday, October 19, 2010 9:04 AM
  • You can use SetForegroundWindow in combination with GetWindow :

                       int SW_RESTORE = 9;
                       int GW_HWNDPREV = 3;

                        IntPtr nextWindow = GetWindow(mainWindowHandle, GW_HWNDPREV);

                        ShowWindow(nextWindow, SW_RESTORE);
                        SetForegroundWindow(nextWindow);

     

    This will make sure you don't have the weird behaviour.

    Regards,

    Fábio


    "To alcohol! The cause of and solution to all of life's problems." - Homer Simpson
    Tuesday, October 19, 2010 5:41 PM
  • Hi BenHines,

    I think I cannot catch you. So if the reply from Fábio Franco had not solved your question, please clarify your question. Then we can clear about your request and can help you to solve your problem easier.

     

    If there’s anything unclear, please feel free to let me know.

     

    Have a nice day!

    Mike

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Thursday, October 21, 2010 6:11 AM