It's working with other handles windows.
Process[] processes = Process.GetProcessesByName(processName);
SetProcessWindow.BringToFront(processes[0].Id);
SetProcessWindow.CenterProcessWindow(processes[0].Id);
If the processName
for example is Taskmgr
it will bring to the front of the screen and will center it the Task Manager.
But if for example the processName
is explorer
(not internet explorer but explorer that in this case is a directory on the hard disk) it won't bring it to the front it will not do anything.
This is the SetProcessWindow
class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
namespace Automation
{
class SetProcessWindow
{
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
[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;
private const int SW_SHOWNORMAL = 1;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_SHOWWINDOW = 0x0040;
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
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);
}
public static void CenterProcessWindow(int processId)
{
Process process = Process.GetProcessById(processId);
while (process.MainWindowHandle == IntPtr.Zero)
{
process.Refresh();
break;
}
IntPtr handle = process.MainWindowHandle;
RECT rct;
GetWindowRect(handle, out rct);
Rectangle screen = Screen.FromHandle(handle).Bounds;
Point pt = new Point(screen.Left + screen.Width / 2 - (rct.Right - rct.Left) / 2, screen.Top + screen.Height / 2 - (rct.Bottom - rct.Top) / 2);
SetWindowPos(handle, IntPtr.Zero, pt.X, pt.Y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
}
}
}
Not sure maybe it doesn't bring some windows to the front since I changed in the project properties unchecked prefer 32-bit ?
Anyway some windows it bring to the front and some not.