[DllImport("user32.dll", SetLastError=true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowLong(hWnd As IntPtr, _
<MarshalAs(UnmanagedType.I4)>nIndex As WindowLongFlags) As Integer
End Function
Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" _
(ByVal prmlngWindowHandle As Long, _
ByVal prmlngIndex As WindowLongFlags) As Long
GWL_ID = (-12)
GWL_STYLE = (-16)
GWL_EXSTYLE = (-20)
// Window Styles
const UInt32 WS_OVERLAPPED = 0;
const UInt32 WS_POPUP = 0x80000000;
const UInt32 WS_CHILD = 0x40000000;
const UInt32 WS_MINIMIZE = 0x20000000;
const UInt32 WS_VISIBLE = 0x10000000;
const UInt32 WS_DISABLED = 0x8000000;
const UInt32 WS_CLIPSIBLINGS = 0x4000000;
const UInt32 WS_CLIPCHILDREN = 0x2000000;
const UInt32 WS_MAXIMIZE = 0x1000000;
const UInt32 WS_CAPTION = 0xC00000; // WS_BORDER or WS_DLGFRAME
const UInt32 WS_BORDER = 0x800000;
const UInt32 WS_DLGFRAME = 0x400000;
const UInt32 WS_VSCROLL = 0x200000;
const UInt32 WS_HSCROLL = 0x100000;
const UInt32 WS_SYSMENU = 0x80000;
const UInt32 WS_THICKFRAME = 0x40000;
const UInt32 WS_GROUP = 0x20000;
const UInt32 WS_TABSTOP = 0x10000;
const UInt32 WS_MINIMIZEBOX = 0x20000;
const UInt32 WS_MAXIMIZEBOX = 0x10000;
const UInt32 WS_TILED = WS_OVERLAPPED;
const UInt32 WS_ICONIC = WS_MINIMIZE;
const UInt32 WS_SIZEBOX = WS_THICKFRAME;
// Extended Window Styles
const UInt32 WS_EX_DLGMODALFRAME = 0x0001;
const UInt32 WS_EX_NOPARENTNOTIFY = 0x0004;
const UInt32 WS_EX_TOPMOST = 0x0008;
const UInt32 WS_EX_ACCEPTFILES = 0x0010;
const UInt32 WS_EX_TRANSPARENT = 0x0020;
const UInt32 WS_EX_MDICHILD = 0x0040;
const UInt32 WS_EX_TOOLWINDOW = 0x0080;
const UInt32 WS_EX_WINDOWEDGE = 0x0100;
const UInt32 WS_EX_CLIENTEDGE = 0x0200;
const UInt32 WS_EX_CONTEXTHELP = 0x0400;
const UInt32 WS_EX_RIGHT = 0x1000;
const UInt32 WS_EX_LEFT = 0x0000;
const UInt32 WS_EX_RTLREADING = 0x2000;
const UInt32 WS_EX_LTRREADING = 0x0000;
const UInt32 WS_EX_LEFTSCROLLBAR = 0x4000;
const UInt32 WS_EX_RIGHTSCROLLBAR = 0x0000;
const UInt32 WS_EX_CONTROLPARENT = 0x10000;
const UInt32 WS_EX_STATICEDGE = 0x20000;
const UInt32 WS_EX_APPWINDOW = 0x40000;
const UInt32 WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
const UInt32 WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
const UInt32 WS_EX_LAYERED = 0x00080000;
const UInt32 WS_EX_NOINHERITLAYOUT = 0x00100000; // Disable inheritence of mirroring by children
const UInt32 WS_EX_LAYOUTRTL = 0x00400000; // Right to left mirroring
const UInt32 WS_EX_COMPOSITED = 0x02000000;
const UInt32 WS_EX_NOACTIVATE = 0x08000000;
' Window Styles
Const WS_OVERLAPPED As UInt32 = 0
Const WS_POPUP As UInt32 = &H80000000&
Const WS_CHILD As UInt32 = &H40000000
Const WS_MINIMIZE As UInt32 = &H20000000
Const WS_VISIBLE As UInt32 = &H10000000
Const WS_DISABLED As UInt32 = &H8000000
Const WS_CLIPSIBLINGS As UInt32 = &H4000000
Const WS_CLIPCHILDREN As UInt32 = &H2000000
Const WS_MAXIMIZE As UInt32 = &H1000000
Const WS_CAPTION As UInt32 = &HC00000 ' WS_BORDER or WS_DLGFRAME
Const WS_BORDER As UInt32 = &H800000
Const WS_DLGFRAME As UInt32 = &H400000
Const WS_VSCROLL As UInt32 = &H200000
Const WS_HSCROLL As UInt32 = &H100000
Const WS_SYSMENU As UInt32 = &H80000
Const WS_THICKFRAME As UInt32 = &H40000
Const WS_GROUP As UInt32 = &H20000
Const WS_TABSTOP As UInt32 = &H10000
Const WS_MINIMIZEBOX As UInt32 = &H20000
Const WS_MAXIMIZEBOX As UInt32 = &H10000
Const WS_TILED As UInt32 = WS_OVERLAPPED
Const WS_ICONIC As UInt32 = WS_MINIMIZE
Const WS_SIZEBOX As UInt32 = WS_THICKFRAME
' Extended Window Styles
Const WS_EX_DLGMODALFRAME As UInt32 = &H1
Const WS_EX_NOPARENTNOTIFY As UInt32 = &H4
Const WS_EX_TOPMOST As UInt32 = &H8
Const WS_EX_ACCEPTFILES As UInt32 = &H10
Const WS_EX_TRANSPARENT As UInt32 = &H20
Const WS_EX_MDICHILD As UInt32 = &H40
Const WS_EX_TOOLWINDOW As UInt32 = &H80
Const WS_EX_WINDOWEDGE As UInt32 = &H100
Const WS_EX_CLIENTEDGE As UInt32 = &H200
Const WS_EX_CONTEXTHELP As UInt32 = &H400
Const WS_EX_RIGHT As UInt32 = &H1000
Const WS_EX_LEFT As UInt32 = &H0
Const WS_EX_RTLREADING As UInt32 = &H2000
Const WS_EX_LTRREADING As UInt32 = &H0
Const WS_EX_LEFTSCROLLBAR As UInt32 = &H4000
Const WS_EX_RIGHTSCROLLBAR As UInt32 = &H0
Const WS_EX_CONTROLPARENT As UInt32 = &H10000
Const WS_EX_STATICEDGE As UInt32 = &H20000
Const WS_EX_APPWINDOW As UInt32 = &H40000
Const WS_EX_OVERLAPPEDWINDOW As UInt32 = (WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE)
Const WS_EX_PALETTEWINDOW As UInt32 = (WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW Or WS_EX_TOPMOST)
Const WS_EX_LAYERED As UInt32 = &H80000
Const WS_EX_NOINHERITLAYOUT As UInt32 = &H100000 ' Disable inheritence of mirroring by children
Const WS_EX_LAYOUTRTL As UInt32 = &H400000 ' Right to left mirroring
Const WS_EX_COMPOSITED As UInt32 = &H2000000
Const WS_EX_NOACTIVATE As UInt32 = &H8000000
This function is NOT 64-bit safe when retrieving handle or pointer values. Use GetWindowLongPtr instead when retrieving an IntPtr.
Also, when compiling for a32 bit version, GetWindowsLongPtr still calls this version of the function. the actual GetWindowsLongPtr function is a 64 bit call. MSDN recommends using this method, because theres an internal macro that will choose the correct version to call.
Please add some!
static public class WndInfo
{
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
...
public static bool IsWindowTopMost(IntPtr Handle)
{
return (GetWindowLong(Handle, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0;
}
...
}
Do you know one? Please contribute it!