using System; using System.Runtime.InteropServices; using System.Text; namespace MGdu.WinFormUI.Win32 { internal static class WinAPI { #region const public static readonly IntPtr TRUE = new IntPtr(1); public static readonly IntPtr FALSE = IntPtr.Zero; #endregion #region WindowMessages public enum WindowMessages { WM_MOVE = 0x0003, WM_SIZE = 0x0005, WM_ACTIVATE = 0x0006, WM_ACTIVATEAPP = 0x001C, WM_SETCURSOR = 0x0020, WM_MOUSEACTIVATE = 0x0021, WM_GETMINMAXINFO = 0x24, WM_WINDOWPOSCHANGING = 0x0046, WM_WINDOWPOSCHANGED = 0x0047, // non client area WM_NCCREATE = 0x0081, WM_NCDESTROY = 0x0082, WM_NCCALCSIZE = 0x0083, WM_NCHITTEST = 0x84, WM_NCPAINT = 0x0085, WM_NCACTIVATE = 0x0086, // non client mouse WM_NCMOUSEMOVE = 0x00A0, WM_NCLBUTTONDOWN = 0x00A1, WM_NCLBUTTONUP = 0x00A2, WM_NCLBUTTONDBLCLK = 0x00A3, WM_NCRBUTTONDOWN = 0x00A4, WM_NCRBUTTONUP = 0x00A5, WM_NCRBUTTONDBLCLK = 0x00A6, WM_NCMBUTTONDOWN = 0x00A7, WM_NCMBUTTONUP = 0x00A8, WM_NCMBUTTONDBLCLK = 0x00A9, WM_SYSCOMMAND = 0x0112, WM_PARENTNOTIFY = 0x0210, WM_MDINEXT = 0x224, } #endregion #region WindowStyle [Flags] public enum WindowStyle : uint { WS_OVERLAPPED = 0x00000000, WS_POPUP = 0x80000000, WS_CHILD = 0x40000000, WS_MINIMIZE = 0x20000000, WS_VISIBLE = 0x10000000, WS_DISABLED = 0x08000000, WS_CLIPSIBLINGS = 0x04000000, WS_CLIPCHILDREN = 0x02000000, WS_MAXIMIZE = 0x01000000, WS_CAPTION = 0x00C00000, WS_BORDER = 0x00800000, WS_DLGFRAME = 0x00400000, WS_VSCROLL = 0x00200000, WS_HSCROLL = 0x00100000, WS_SYSMENU = 0x00080000, WS_THICKFRAME = 0x00040000, WS_GROUP = 0x00020000, WS_TABSTOP = 0x00010000, WS_MINIMIZEBOX = 0x00020000, WS_MAXIMIZEBOX = 0x00010000, WS_TILED = WS_OVERLAPPED, WS_ICONIC = WS_MINIMIZE, WS_SIZEBOX = WS_THICKFRAME, WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW, WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX), WS_POPUPWINDOW = (WS_POPUP | WS_BORDER | WS_SYSMENU), WS_CHILDWINDOW = (WS_CHILD) } #endregion #region WindowStyleEx [Flags] public enum WindowStyleEx { WS_EX_DLGMODALFRAME = 0x00000001, WS_EX_NOPARENTNOTIFY = 0x00000004, WS_EX_TOPMOST = 0x00000008, WS_EX_ACCEPTFILES = 0x00000010, WS_EX_TRANSPARENT = 0x00000020, WS_EX_MDICHILD = 0x00000040, WS_EX_TOOLWINDOW = 0x00000080, WS_EX_WINDOWEDGE = 0x00000100, WS_EX_CLIENTEDGE = 0x00000200, WS_EX_CONTEXTHELP = 0x00000400, WS_EX_RIGHT = 0x00001000, WS_EX_LEFT = 0x00000000, WS_EX_RTLREADING = 0x00002000, WS_EX_LTRREADING = 0x00000000, WS_EX_LEFTSCROLLBAR = 0x00004000, WS_EX_RIGHTSCROLLBAR = 0x00000000, WS_EX_CONTROLPARENT = 0x00010000, WS_EX_STATICEDGE = 0x00020000, WS_EX_APPWINDOW = 0x00040000, WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE), WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST), WS_EX_LAYERED = 0x00080000, WS_EX_NOINHERITLAYOUT = 0x00100000, // Disable inheritence of mirroring by children WS_EX_LAYOUTRTL = 0x00400000, // Right to left mirroring WS_EX_COMPOSITED = 0x02000000, WS_EX_NOACTIVATE = 0x08000000, } #endregion #region Scrollbar public enum ScrollBar { SB_HORZ = 0, SB_VERT = 1, SB_CTL = 2, SB_BOTH = 3, } #endregion #region NCHITTEST /// /// Location of cursor hot spot returnet in WM_NCHITTEST. /// public enum NCHITTEST { /// /// On the screen background or on a dividing line between windows /// (same as HTNOWHERE, except that the DefWindowProc function produces a system beep to indicate an error). /// HTERROR = (-2), /// /// In a window currently covered by another window in the same thread /// (the message will be sent to underlying windows in the same thread until one of them returns a code that is not HTTRANSPARENT). /// HTTRANSPARENT = (-1), /// /// On the screen background or on a dividing line between windows. /// HTNOWHERE = 0, /// In a client area. HTCLIENT = 1, /// In a title bar. HTCAPTION = 2, /// In a window menu or in a Close button in a child window. HTSYSMENU = 3, /// In a size box (same as HTSIZE). HTGROWBOX = 4, /// In a menu. HTMENU = 5, /// In a horizontal scroll bar. HTHSCROLL = 6, /// In the vertical scroll bar. HTVSCROLL = 7, /// In a Minimize button. HTMINBUTTON = 8, /// In a Maximize button. HTMAXBUTTON = 9, /// In the left border of a resizable window /// (the user can click the mouse to resize the window horizontally). HTLEFT = 10, /// /// In the right border of a resizable window /// (the user can click the mouse to resize the window horizontally). /// HTRIGHT = 11, /// In the upper-horizontal border of a window. HTTOP = 12, /// In the upper-left corner of a window border. HTTOPLEFT = 13, /// In the upper-right corner of a window border. HTTOPRIGHT = 14, /// In the lower-horizontal border of a resizable window /// (the user can click the mouse to resize the window vertically). HTBOTTOM = 15, /// In the lower-left corner of a border of a resizable window /// (the user can click the mouse to resize the window diagonally). HTBOTTOMLEFT = 16, /// In the lower-right corner of a border of a resizable window /// (the user can click the mouse to resize the window diagonally). HTBOTTOMRIGHT = 17, /// In the border of a window that does not have a sizing border. HTBORDER = 18, HTOBJECT = 19, /// In a Close button. HTCLOSE = 20, /// In a Help button. HTHELP = 21, } #endregion #region struct [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; public RECT(int left, int top, int right, int bottom) { this.Left = left; this.Top = top; this.Right = right; this.Bottom = bottom; } public override string ToString() { return "{ Left:" + this.Left + ", Top:" + this.Top + ", Width:" + (this.Right - this.Left).ToString() + ", Height:" + (this.Bottom - this.Top).ToString() + "}"; } } [StructLayout(LayoutKind.Sequential)] public struct WINDOWPOS { public IntPtr hwnd; public IntPtr hWndInsertAfter; public int x; public int y; public int cx; public int cy; public uint flags; } [StructLayout(LayoutKind.Sequential)] public struct NCCALCSIZE_PARAMS { /// /// Contains the new coordinates of a window that has been moved or resized, that is, it is the proposed new window coordinates. /// public RECT rectNewForm; /// /// Contains the coordinates of the window before it was moved or resized. /// public RECT rectOldForm; /// /// Contains the coordinates of the window's client area before the window was moved or resized. /// public RECT rectOldClient; /// /// Pointer to a WINDOWPOS structure that contains the size and position values specified in the operation that moved or resized the window. /// public WINDOWPOS lpPos; } [StructLayout(LayoutKind.Sequential)] public struct SIZE { public Int32 cx; public Int32 cy; public SIZE(Int32 x, Int32 y) { cx = x; cy = y; } } [StructLayout(LayoutKind.Sequential)] public struct BLENDFUNCTION { byte BlendOp; byte BlendFlags; byte SourceConstantAlpha; byte AlphaFormat; public BLENDFUNCTION(byte op, byte flags, byte alpha, byte format) { BlendOp = op; BlendFlags = flags; SourceConstantAlpha = alpha; AlphaFormat = format; } } public enum BlendOp : byte { AC_SRC_OVER = 0x00, AC_SRC_ALPHA = 0x01, } [StructLayout(LayoutKind.Sequential)] public struct POINT { public Int32 x; public Int32 y; public POINT(Int32 x, Int32 y) { this.x = x; this.y = y; } } #endregion // update-layered-window public enum ULWPara { ULW_COLORKEY = 0x00000001, ULW_ALPHA = 0x00000002, ULW_OPAQUE = 0x00000004, ULW_EX_NORESIZE = 0x00000008, } // get-wondow-long public enum GWLPara { GWL_WNDPROC = -4, GWL_HINSTANCE = -6, GWL_HWNDPARENT = -8, GWL_STYLE = -16, GWL_EXSTYLE = -20, GWL_USERDATA = -21, GWL_ID = -12, } // set-window-position public enum SWPPara : uint { SWP_NOSIZE = 0x0001, SWP_NOMOVE = 0x0002, SWP_NOZORDER = 0x0004, SWP_NOREDRAW = 0x0008, SWP_NOACTIVATE = 0x0010, SWP_FRAMECHANGED = 0x0020, SWP_SHOWWINDOW = 0x0040, SWP_HIDEWINDOW = 0x0080, SWP_NOCOPYBITS = 0x0100, SWP_NOOWNERZORDER = 0x0200, SWP_NOSENDCHANGING = 0x0400, } #region non-dll method public static int LOWORD(int value) { return value & 0xFFFF; } public static int HIWORD(int value) { return value >> 16; } #endregion #region dll-import method [DllImport("user32.dll")] public static extern int ShowScrollBar(IntPtr hWnd, int wBar, int bShow); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowLong(IntPtr hWnd, int Index); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int SetWindowLong(IntPtr hWnd, int Index, int Value); [DllImport("user32.dll", ExactSpelling = true)] public static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] public static extern IntPtr GetDC(IntPtr hWnd); [DllImport("gdi32.dll", ExactSpelling = true, PreserveSig = true, SetLastError = true)] public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj); [DllImport("gdi32.dll")] public static extern bool DeleteDC(IntPtr hdc); [DllImport("user32.dll", ExactSpelling = true)] public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr hObject); [DllImport("gdi32.dll", SetLastError = true)] public static extern IntPtr CreateCompatibleDC(IntPtr hdc); [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] public static extern bool UpdateLayeredWindow(IntPtr hwnd , IntPtr hdcDst , ref POINT pptDst , ref SIZE psize , IntPtr hdcSrc , ref POINT pptSrc , uint crKey , [In] ref BLENDFUNCTION pblend , uint dwFlags ); [System.Security.SuppressUnmanagedCodeSecurity] [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); #endregion } }