Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than user32, prefix the name with the module name and a period.
SetWindowPos (user32)
.
Use the System.Windows.Forms.Form.TopMost property.
Alternately, if you want to do this the long way using P/Invoke, keep reading.
C# Signature:
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
int Y, int cx, int cy, uint uFlags);
VB Signature:
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, _
ByVal hWndInsertAfter As IntPtr, _
ByVal X As Integer, _
ByVal Y As Integer, _
ByVal cx As Integer, _
ByVal cy As Integer, _
ByVal uFlags As UInt32) As Boolean
End Function
VB.NET Signature:
Declare Auto Function SetWindowPos Lib "user32" (ByVal hWnd As IntPtr, _
ByVal hWndInsertAfter As IntPtr, _
ByVal X As Integer, _
ByVal Y As Integer, _
ByVal cx As Integer, _
ByVal cy As Integer, _
ByVal uFlags As UInteger) As Boolean
User-Defined Types:
C#:
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
static readonly IntPtr HWND_TOP = new IntPtr(0);
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
VB:
ReadOnly HWND_BOTTOM As New IntPtr(1)
ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
ReadOnly HWND_TOP As New IntPtr(0)
ReadOnly HWND_TOPMOST As New IntPtr(-1)
Shared ReadOnly SWP_NOSIZE As UInt32 = Convert.ToUInt32(&H1)
Shared ReadOnly SWP_NOMOVE As UInt32 = Convert.ToUInt32(&H2)
Shared ReadOnly SWP_NOZORDER As UInt32 = Convert.ToUInt32(&H4)
Shared ReadOnly SWP_NOREDRAW As UInt32 = Convert.ToUInt32(&H8)
Shared ReadOnly SWP_NOACTIVATE As UInt32 = Convert.ToUInt32(&H10)
Shared ReadOnly SWP_FRAMECHANGED As UInt32 = Convert.ToUInt32(&H20) '/* The frame changed: send WM_NCCALCSIZE */
Shared ReadOnly SWP_SHOWWINDOW As UInt32 = Convert.ToUInt32(&H40)
Shared ReadOnly SWP_HIDEWINDOW As UInt32 = Convert.ToUInt32(&H80)
Shared ReadOnly SWP_NOCOPYBITS As UInt32 = Convert.ToUInt32(&H100)
Shared ReadOnly SWP_NOOWNERZORDER As UInt32 = Convert.ToUInt32(&H200) '/* Don't do owner Z ordering */
Shared ReadOnly SWP_NOSENDCHANGING As UInt32 = Convert.ToUInt32(&H400) '/* Don't send WM_WINDOWPOSCHANGING */
Notes:
HWND_TOP will bring a window to the front of the Z-Order only if the thread is in the foreground. Otherwise it will bring the window to the front of the thread's Z-order.
Tips & Tricks:
In .NET, to make a window stay on top you only need to call Me.TopMost = True
Sample Code:
This is a little helper class which can be used with any Form object to make it topmost or not topmost.
C#:
static public class FormHelper
{
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
int Y, int cx, int cy, uint uFlags);
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
static readonly IntPtr HWND_TOP = new IntPtr(0);
Public Class FormHelper
<DllImport("user32.dll")> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, _
ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInt32) As Boolean
End Function
Private Shared ReadOnly HWND_NOTOPMOST As IntPtr = New IntPtr(-2)
Private Shared ReadOnly HWND_TOP As IntPtr = New IntPtr(0)
Private Shared ReadOnly HWND_TOPMOST As IntPtr = New IntPtr(-1)
Private Const SWP_FRAMECHANGED As UInt32 = &H20
Private Const SWP_HIDEWINDOW As UInt32 = &H80
Private Const SWP_NOACTIVATE As UInt32 = &H10
Private Const SWP_NOCOPYBITS As UInt32 = &H100
Private Const SWP_NOMOVE As UInt32 = 2
Private Const SWP_NOOWNERZORDER As UInt32 = &H200
Private Const SWP_NOREDRAW As UInt32 = 8
Private Const SWP_NOSENDCHANGING As UInt32 = &H400
Private Const SWP_NOSIZE As UInt32 = 1
Private Const SWP_NOZORDER As UInt32 = 4
Private Const SWP_SHOWWINDOW As UInt32 = &H40
Private Const TOPMOST_FLAGS As UInt32 = SWP_NOMOVE Or SWP_NOSIZE
Public Shared Sub MakeTopMost(ByVal form As Form)
FormHelper.SetWindowPos(form.Handle, FormHelper.HWND_TOPMOST, 0, 0, 0, 0, 3)
End Sub
Public Shared Sub MakeNormal(ByVal form As Form)
FormHelper.SetWindowPos(form.Handle, FormHelper.HWND_NOTOPMOST, 0, 0, 0, 0, 3)
End Sub
End Class
Alternative Managed API:
Use the System.Windows.Forms.Form.TopMost property.
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).