3

What I want to do is to bring the handle to the front and to the center of the screen. Bring it to the front I know how to do i'm using SetForegroundWindow(IntPtr hWnd); and it's working fine. But how do I use the SetWindowPos to force to be in the center of the screen ?

IntPtr handle = process.MainWindowHandle;
if (handle != IntPtr.Zero)
{
    SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
}

Then when I call in the constructor for example to SetWindowPos what should I give it ? handle is fine I know what it should be. But all the resr 0,0,0,0,0,0 and what should be the values fro SWP_NOZORDER and SWP_NOSIZE ?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Manuel Spechia
  • 389
  • 1
  • 3
  • 16
  • That's what MSDN is for – SimpleVar Jul 07 '15 at 14:43
  • http://stackoverflow.com/questions/4601827/how-do-i-center-a-window-onscreen-in-c and http://stackoverflow.com/a/6837499. I don't think you need `SetWindowPos` – Robert Harvey Jul 07 '15 at 14:44
  • @YoryeNathan is referring to this page: [SetWindowPos function (Windows(](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633545.aspx) – cbr Jul 07 '15 at 14:44

1 Answers1

8

Before you can center it, first you must know how big it is. This can be accomplished with the GetWindowRect() API. After that it's simply a matter of calculating the center position taking into account the size of the screen:

public partial class Form1 : Form
{

    [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);

    Process process;

    public Form1()
    {
        InitializeComponent();
        process = Process.GetProcessesByName("calc").FirstOrDefault();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (process == null)
            return;

        IntPtr handle = process.MainWindowHandle;
        if (handle != IntPtr.Zero)
        {
            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);
        }
    }

}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40