[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Set another application's size and position in C#

[Set another application's size and position in C#]

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 the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.