Question

My application is built to run in Shell mode (via registry changes).

The user is allowed to define custom 'buttons' to invoke utilities such as CMD, Regedit, Explorer...

When the user presses the button a window is opened with their specified utility.

Pressing the minimize button of the utility's window drives the application code to shrink and move the window to the bottom of the screen where it can be subsequently moved or expanded by the user.

This works nicely with the exception of Explore.exe.

In the following code the proc.MainWindowHandle is empty/zero for Emplorer.exe.

Knowing this is a 'special case' program, is there any way to determine the MainWindowHandle for Explorer.exe so that the program can manage it too?

// Minimize all utility windows
Process[] procs = Process.GetProcesses();
Process currentProcess = Process.GetCurrentProcess();
IntPtr hWnd;
foreach (Process proc in procs)
{
    if (proc.Id != currentProcess.Id)
    {
        if ((hWnd = proc.MainWindowHandle) != IntPtr.Zero)
        {
            WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
            GetWindowPlacement(hWnd, ref wp);

            if (wp.showCmd != (int)ShowWindowCommand.ShowMinimized || wp.showCmd != (int)ShowWindowCommand.Minimize || wp.showCmd != (int)ShowWindowCommand.ShowMinNoActive)
            {
                ShowWindowAsync(hWnd, (int)ShowWindowCommand.ShowMinimized);
            }

            SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.IgnoreMove | SetWindowPosFlags.DoNotActivate | SetWindowPosFlags.IgnoreResize);
        }
    }
}
Was it helpful?

Solution

Of course, the explorer process may be responsible for multiple top-level windows so it might take a little more work to identify the specific one that you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top