Set another application’s size and position in C#

[target window]

When I write books, there is usually a maximum size that a screen shot can be. I wrote this program to make it easy to set an example program to exactly that size.

This program uses the FindWindow API function to find the target window and the SetWindowPos API function to size and position it. The following code shows how the program defines those functions. It also defines the SetWindowPosFlags enumeration used by SetWindowPos.

// Define the FindWindow API function.
[DllImport("user32.dll", EntryPoint = "FindWindow",
    SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly,
    string lpWindowName);

// Define the SetWindowPos API function.
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetWindowPos(IntPtr hWnd,
    IntPtr hWndInsertAfter, int X, int Y, int cx, int cy,
    SetWindowPosFlags uFlags);

// Define the SetWindowPosFlags enumeration.
[Flags()]
private enum SetWindowPosFlags : uint
{
    SynchronousWindowPosition = 0x4000,
    DeferErase = 0x2000,
    DrawFrame = 0x0020,
    FrameChanged = 0x0020,
    HideWindow = 0x0080,
    DoNotActivate = 0x0010,
    DoNotCopyBits = 0x0100,
    IgnoreMove = 0x0002,
    DoNotChangeOwnerZOrder = 0x0200,
    DoNotRedraw = 0x0008,
    DoNotReposition = 0x0200,
    DoNotSendChangingEvent = 0x0400,
    IgnoreResize = 0x0001,
    IgnoreZOrder = 0x0004,
    ShowWindow = 0x0040,
}

This program doesn’t set any flags but I’ve included them here in case you need them. If you don’t want them, you could define the last parameter in SetWindowPos to be of type uint and then just pass in the value 0.

When you click the Set button, the program executes the following code.

// Size and position the application.
private void btnSet_Click(object sender, EventArgs e)
{
    // Get the target window's handle.
    IntPtr target_hwnd =
        FindWindowByCaption(IntPtr.Zero, txtAppTitle.Text);
    if (target_hwnd == IntPtr.Zero)
    {
        MessageBox.Show(
            "Could not find a window with the title \"" +
            txtAppTitle.Text + "\"");
        return;
    }

    // Set the window's position.
    int width = int.Parse(txtWidth.Text);
    int height = int.Parse(txtHeight.Text);
    int x = int.Parse(txtX.Text);
    int y = int.Parse(txtY.Text);
    SetWindowPos(target_hwnd, IntPtr.Zero,
        x, y, width, height, 0);
}

The program uses FindWindow to find the target application’s handle. This only works if you enter the target application’s window title exactly.

Next the program uses SetWindowPos to set the target’s size and position. This can have some odd side effects if the window’s size mode doesn’t match its size. For example, if the window is maximized and you set its size to something smaller than the screen, then the title bar looks a bit messed up around the edges.

Also be careful not to move a target window entirely off the screen or you will need to use this program or a similar one to get it back again.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in API, miscellany, system and tagged , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

3 Responses to Set another application’s size and position in C#

  1. Start says:

    Dear sir.
    i have question about c# use with user32.dll , so i want to know how to do get value label at another application . please help me.
    thank

    • RodStephens says:

      Unfortunately that is difficult. If it’s a text box, then you may be able to use keyboard shortcuts such as Tab and Ctrl+C to copy the text to the clipboard. If it’s in a label, you probably can’t do this.

  2. Pingback: Move a window with a title that matches a pattern in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *