2

I am facing a problem in “BringWindowToTop” and “ShowWindow” which is occuring in some applications, but not all.

The issue is that if my applications are minimized to the taskbar, and when I open applications one by one, they all open except Outlook.

In the case of Outlook, I found using the GetWindowPlacement dll , it returns its x , y & height , width 0 value.

In Outlook if my application is in restore mode / maximize mode , process.MainWindowHandle has different value , while it is in minimize mode MainWindowHandle has different value.

Below is code :

foreach (var process in System.Diagnostics.Process.GetProcessesByName("outlook"))
{
    ForceForegroundWindow1(process.MainWindowHandle, 2);                           
}

private static void ForceForegroundWindow1(IntPtr hWnd, int Status)
{
    uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);

    uint appThread = GetCurrentThreadId();

    int SW_SHOW = 5;
    if (Status == 0)
        SW_SHOW = 3;
    else if (Status == 2)
        SW_SHOW = 9;

    //SW_SHOW = 1;

    const int SW_RESTORE = 9;

    if (foreThread != appThread)
    {
        AttachThreadInput(foreThread, appThread, true);

        BringWindowToTop(hWnd);

        ShowWindow(hWnd, SW_SHOW);
        //  ShowWindowAsync(hWnd, SW_RESTORE);

        AttachThreadInput(foreThread, appThread, false);
    }
    else
    {
        BringWindowToTop(hWnd);

        ShowWindow(hWnd, SW_SHOW);
        // ShowWindowAsync(hWnd, SW_RESTORE);
    }

}

Can you help me to bring windows on top for all applications?

Jamie
  • 3,890
  • 3
  • 26
  • 35
  • What does the word DLL in the title want to suggest? – Marcel Oct 25 '12 at 11:31
  • MainWindowHandle is merely a guess at the actual "main window" of an application. Particularly often wrong for programs that display multiple top-level windows. Or a splash screen, like Outlook. – Hans Passant Oct 25 '12 at 12:08
  • So, do u have any other idea to get MainWindowHandle of process or showing application window in Restore or maximum mode ? – user1773707 Oct 26 '12 at 06:35

0 Answers0