3

In the top of the class I have:

    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("User32.dll")]
    private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
    private const int SW_SHOWMAXIMIZED = 3;

Then in a method:

public static void BringToFront(IntPtr handle)
        {
            if (handle == IntPtr.Zero)
                return;

            // Maximize window
            ShowWindow(handle, SW_SHOWMAXIMIZED);

            SetForegroundWindow(handle);
        }

But I don't want it to be maximized but normal size.

EDIT

This is working.

In form1:

Process[] processes = Process.GetProcessesByName(processName);
SetProcessWindow.BringToFront(processes[0].Id);
SetProcessWindow.CenterProcessWindow(processes[0].Id);

In the class:

public static void BringToFront(int processId)
        {
            Process process = Process.GetProcessById(processId);
            IntPtr handle = process.MainWindowHandle;

            if (handle == IntPtr.Zero)
                return;

            ShowWindow(handle, SW_SHOWNORMAL);
            SetForegroundWindow(handle);
        }
Manuel Spechia
  • 389
  • 1
  • 3
  • 16
  • You could use SW_SHOWNORMAL = 1; instead of SW_SHOWMAXIMIZED – Steve Jul 09 '15 at 15:19
  • Tried the SW_SHOWNORMAL = 1; and it didn't show anything. Didn't bring the window to the front at all. Also tried 2 nothing 3 is maximized also 4 maximized. Strange that SW_SHOWNORMAL = 1; doesn't work. – Manuel Spechia Jul 09 '15 at 15:51
  • Perhaps you could find something useful here http://stackoverflow.com/questions/13468331/showwindow-function-doesnt-work-when-target-application-is-run-as-administrator – Steve Jul 09 '15 at 16:03

1 Answers1

1

Use SW_SHOW = 5 instead of SW_SHOWMAIXMIZED.

Incidentally, this was the first link when I did a search for SW_SHOWMAXIMIZED.... Got to smarten up those research muscles!

simon at rcl
  • 7,326
  • 1
  • 17
  • 24