In C#, the SetWindowPos function is used to set the position and size of the window.
prototype:
[DllImport("", SetLastError = true)] [return: MarshalAs()] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
Parameter description:
-
hWnd
: The handle of the window to set the position and size. -
hWndInsertAfter
: Determines the Z order of the window, that is, how the windows are placed in the Z order. One of the following values can usually be used:-
: Place the window at the top of the Z-order.
-
new IntPtr(-1)
: Place the window at the top of all non-top-level windows. -
new IntPtr(-2)
: Place the window at the top of all windows.
-
- Other window handles: Place the window at the top of the specified window.
-
X
andY
: X and Y coordinates in the upper left corner of the new position of the window. -
cx
andcy
: New width and height of the window. -
uFlags
: Set the window position and size flag. You can use a combination of the following flags to control the behavior of the window:-
SWP_NOSIZE
: Maintain the current size (ignoring the cx and cy parameters). -
SWP_NOMOVE
: Maintain the current position (ignore the X and Y parameters). -
SWP_NOZORDER
: Maintain the current Z order (ignoring the hWndInsertAfter parameter). -
SWP_SHOWWINDOW
: If the window is hidden when called, the window is displayed. -
SWP_HIDEWINDOW
: Hide window. -
SWP_NOACTIVATE
: The window is not activated. Applicable onlySWP_SHOWWINDOW
Logo. -
SWP_DRAWFRAME
: When resizing the window, repaint the window border (usually withSWP_FRAMECHANGED
Use together). -
SWP_NOOWNERZORDER
: Do not change the Z order of the owner window. -
SWP_NOSENDCHANGING
: Prevent window receptionWM_WINDOWPOSCHANGING
information. -
SWP_FRAMECHANGED
: Make the system sendWM_NCCALCSIZE
Message, even if the window size has not changed. -
SWP_NOCOPYBITS
: Prevent windows from redrawing. -
SWP_NOREPOSITION
: The window is not repositioned even if the size or position changes.
-
Example usage:
1. Change the window size:
using System; using ; // Define constants and method signaturespublic class Win32 { [DllImport("")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); public const uint SWP_SHOWWINDOW = 0x40; } // Get the window handle and call the SetWindowPos function to change the window sizeIntPtr hWnd = // Window handleint newX = // New X coordinatesint newY = // New Y coordinatesint newWidth = // New widthint newHeight = // New heights(hWnd, Win32.HWND_TOPMOST, newX, newY, newWidth, newHeight, Win32.SWP_SHOWWINDOW);
The window handle can be used to obtain the window handle (same as follows), such as:
[DllImport("", SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); IntPtr hWnd = (null, "Window Title"); // Replace the class name or title of the window
2. Move to the upper left corner of the screen:
using System; using ; // Define constants and method signaturespublic class Win32 { public const int SWP_NOSIZE = 0x0001; public const int SWP_NOMOVE = 0x0002; public const int SWP_NOZORDER = 0x0004; public const int SWP_SHOWWINDOW = 0x0040; [DllImport("", SetLastError = true)] [return: MarshalAs()] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); } // Call SetWindowPos function in the code to move the window to the upper left corner of the screenIntPtr hWnd = // Window handle(hWnd, , 0, 0, 0, 0, Win32.SWP_NOSIZE | Win32.SWP_SHOWWINDOW);
3. Make it a Topmost window and move it to the upper left corner of the screen:
using System; using ; // Define constants and method signaturespublic class Win32 { public const int SWP_NOSIZE = 0x0001; public const int SWP_NOMOVE = 0x0002; public const int SWP_NOZORDER = 0x0004; public const int SWP_SHOWWINDOW = 0x0040; public const int HWND_TOPMOST = -1; public const int HWND_NOTOPMOST = -2; [DllImport("", SetLastError = true)] [return: MarshalAs()] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); } // Call the SetWindowPos function in the code to move the window to the upper left corner of the screen and set it to TopmostIntPtr hWnd = // Window handle(hWnd, Win32.HWND_TOPMOST, 0, 0, 0, 0, Win32.SWP_NOSIZE | Win32.SWP_SHOWWINDOW);
4. Display window:
using System; using ; // Define constants and method signaturespublic class Win32 { public const int SW_SHOWNORMAL = 1; [DllImport("")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); } // Get the window handle and call the ShowWindow function to display the windowIntPtr hWnd = // Window handle(hWnd, Win32.SW_SHOWNORMAL);
5. Hide window:
using System; using ; // Define constants and method signaturespublic class Win32 { public const int SW_HIDE = 0; [DllImport("")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); } // Get the window handle and call the ShowWindow function to hide the windowIntPtr hWnd = // Window handle(hWnd, Win32.SW_HIDE);
This is all about this article about C# SetWindowPos function. For more related C# SetWindowPos function content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!