using System;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
namespace DevComponents.DotNetBar
{
internal class WinApi
{
#region Windows Messages
public enum WindowsMessages
{
WM_DESTROY = 0x0002,
WM_NCPAINT = 0x0085,
WM_NCCALCSIZE = 0x0083,
WM_NCACTIVATE = 0x0086,
WM_SETTEXT = 0x000C,
WM_INITMENUPOPUP = 0x0117,
WM_WINDOWPOSCHANGED = 0x0047,
WM_NCHITTEST = 0x0084,
WM_ERASEBKGND = 0x0014,
WM_NCMOUSEMOVE = 0xA0,
WM_NCLBUTTONDOWN = 0xA1,
WM_NCMOUSELEAVE = 0x2A2,
WM_NCLBUTTONUP = 0xA2,
WM_NCMOUSEHOVER = 0x2A0,
WM_SETCURSOR = 0x20,
WM_SETICON = 0x0080,
WM_HSCROLL = 0x0114,
WM_VSCROLL = 0x115,
WM_MOUSEWHEEL = 0x020A,
WM_STYLECHANGING = 0x007C,
WM_STYLECHANGED = 0x7D,
WM_NCLBUTTONDBLCLK = 0xA3,
WM_MOUSEACTIVATE = 0x21,
WM_MOUSEMOVE = 0x0200,
WM_MDISETMENU = 0x230,
WM_MDIREFRESHMENU = 0x234,
WM_KEYDOWN = 0x0100,
WM_SIZE = 0x5,
WM_DWMCOMPOSITIONCHANGED = 0x031E,
WM_DRAWITEM = 0x002B,
SBM_SETPOS = 0x00E0,
SBM_SETSCROLLINFO = 0x00E9,
EM_GETMODIFY = 0x00B8,
EM_SETMODIFY = 0x00B9,
WM_LBUTTONUP = 0x0202,
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONDBLCLK = 0x0203,
WM_SYSTIMER = 0x0118,
WM_SETFOCUS = 0x0007,
WM_KILLFOCUS = 0x0008,
WM_PAINT = 0x000F,
WM_COMMAND = 0x0111,
WM_CTLCOLORBTN = 0x0135,
WM_CTLCOLORSCROLLBAR = 0x0137,
WM_MDIACTIVATE = 0x222,
WM_CAPTURECHANGED = 0x0215,
CB_GETDROPPEDSTATE = 0x0157,
WVR_VALIDRECTS = 0x400,
WM_GETMINMAXINFO = 0x0024,
WM_APPCOMMAND = 0x0319,
WM_USER = 0x0400,
WM_REFLECT = WM_USER + 0x1C00,
WM_REFLECTCOMMAND = WM_REFLECT + WM_COMMAND,
WM_DPICHANGED = 0x02E0,
WM_SETREDRAW=0x000B,
WM_MOVE=0x3
}
public enum ComboNotificationCodes : int
{
CBN_DROPDOWN = 7,
CBN_CLOSEUP = 8
}
public enum MouseKeyState : int
{
MK_LBUTTON = 0x0001,
MK_RBUTTON = 0x0002,
MK_SHIFT = 0x0004,
MK_CONTROL = 0x0008,
MK_MBUTTON = 0x0010,
MK_XBUTTON1 = 0x0020,
MK_XBUTTON2 = 0x0040
}
/// Options available when a form is tested for mose positions.
public enum WindowHitTestRegions
{
/// HTERROR: 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).
Error = -2,
/// HTTRANSPARENT: 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).
TransparentOrCovered = -1,
/// HTNOWHERE: On the screen background or on a dividing line between
/// windows.
NoWhere = 0,
/// HTCLIENT: In a client area.
ClientArea = 1,
/// HTCAPTION: In a title bar.
TitleBar = 2,
/// HTSYSMENU: In a window menu or in a Close button in a child window.
SystemMenu = 3,
/// HTGROWBOX: In a size box (same as HTSIZE).
GrowBox = 4,
/// HTMENU: In a menu.
Menu = 5,
/// HTHSCROLL: In a horizontal scroll bar.
HorizontalScrollBar = 6,
/// HTVSCROLL: In the vertical scroll bar.
VerticalScrollBar = 7,
/// HTMINBUTTON: In a Minimize button.
MinimizeButton = 8,
/// HTMAXBUTTON: In a Maximize button.
MaximizeButton = 9,
/// HTLEFT: In the left border of a resizable window (the user can click
/// the mouse to resize the window horizontally).
LeftSizeableBorder = 10,
/// HTRIGHT: In the right border of a resizable window (the user can click
/// the mouse to resize the window horizontally).
RightSizeableBorder = 11,
/// HTTOP: In the upper-horizontal border of a window.
TopSizeableBorder = 12,
/// HTTOPLEFT: In the upper-left corner of a window border.
TopLeftSizeableCorner = 13,
/// HTTOPRIGHT: In the upper-right corner of a window border.
TopRightSizeableCorner = 14,
/// HTBOTTOM: In the lower-horizontal border of a resizable window (the
/// user can click the mouse to resize the window vertically).
BottomSizeableBorder = 15,
/// HTBOTTOMLEFT: In the lower-left corner of a border of a resizable
/// window (the user can click the mouse to resize the window diagonally).
BottomLeftSizeableCorner = 16,
/// HTBOTTOMRIGHT: In the lower-right corner of a border of a resizable
/// window (the user can click the mouse to resize the window diagonally).
BottomRightSizeableCorner = 17,
/// HTBORDER: In the border of a window that does not have a sizing
/// border.
NonSizableBorder = 18,
/// HTOBJECT: Unknown...No Documentation Found
Object = 19,
/// HTCLOSE: In a Close button.
CloseButton = 20,
/// HTHELP: In a Help button.
HelpButton = 21,
/// HTSIZE: In a size box (same as HTGROWBOX). (Same as GrowBox).
SizeBox = GrowBox,
/// HTREDUCE: In a Minimize button. (Same as MinimizeButton).
ReduceButton = MaximizeButton,
/// HTZOOM: In a Maximize button. (Same as MaximizeButton).
ZoomButton = MaximizeButton,
}
public enum GWL
{
GWL_WNDPROC = (-4),
GWL_HINSTANCE = (-6),
GWL_HWNDPARENT = (-8),
GWL_STYLE = (-16),
GWL_EXSTYLE = (-20),
GWL_USERDATA = (-21),
GWL_ID = (-12)
}
public enum RedrawWindowFlags : uint
{
RDW_INVALIDATE = 0x0001,
RDW_INTERNALPAINT = 0x0002,
RDW_ERASE = 0x0004,
RDW_VALIDATE = 0x0008,
RDW_NOINTERNALPAINT = 0x0010,
RDW_NOERASE = 0x0020,
RDW_NOCHILDREN = 0x0040,
RDW_ALLCHILDREN = 0x0080,
RDW_UPDATENOW = 0x0100,
RDW_ERASENOW = 0x0200,
RDW_FRAME = 0x0400,
RDW_NOFRAME = 0x0800
}
[Flags()]
public enum WindowStyles
{
WS_VISIBLE = 0x10000000,
WS_CAPTION = 0x00C00000,
WS_BORDER = 0x800000,
WS_DLGFRAME = 0x400000,
WS_THICKFRAME = 0x00040000,
WS_HSCROLL = 0x100000,
WS_VSCROLL = 0x200000
}
#endregion
#region Windows API
[DllImport("user32", EntryPoint = "SetWindowLongPtr")]
public static extern IntPtr SubclassWindow64(IntPtr hWnd, GWL nIndex, WindowProcDelegate dwNewLong);
[DllImport("user32", EntryPoint = "SetWindowLong")]
public static extern IntPtr SubclassWindow(IntPtr hWnd, GWL nIndex, WindowProcDelegate dwNewLong);
[DllImport("user32", EntryPoint = "SetWindowLongPtr")]
public static extern IntPtr SubclassWindow64(IntPtr hWnd, GWL nIndex, IntPtr dwNewLong);
[DllImport("user32", EntryPoint = "SetWindowLong")]
public static extern IntPtr SubclassWindow(IntPtr hWnd, GWL nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern uint CallWindowProc(IntPtr prevWndFunc, IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam);
public delegate uint WindowProcDelegate(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam);
[DllImport("user32.dll")]
public static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
public static char ToAscii(uint virtualKey)
{
StringBuilder outputBuilder = new StringBuilder(2);
byte[] keyState = new byte[256];
NativeFunctions.GetKeyboardState(keyState);
int result = ToAscii(virtualKey, 0, keyState,
outputBuilder, 0);
if (result == 1)
return outputBuilder[0];
else
return '\0';
}
[DllImport("user32.dll")]
private static extern int ToAscii(uint uVirtKey, uint uScanCode, byte[] lpKeyState, [Out] StringBuilder lpChar, uint uFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern short GetKeyState(int keyCode);
[DllImport("user32.dll")]
public static extern bool IsZoomed(IntPtr hWnd);
//[DllImport("user32.dll")]
//public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[System.Runtime.InteropServices.DllImport("User32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
[System.Runtime.InteropServices.DllImport("User32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
[DllImport("gdi32.dll")]
public static extern int ExcludeClipRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
[DllImport("user32")]
public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern bool GetTextMetrics(HandleRef hdc, TEXTMETRIC tm);
[DllImport("user32.dll")]
public static extern bool SetMenu(IntPtr hWnd, IntPtr hMenu);
[DllImport("user32.dll")]
public static extern IntPtr GetMenu(IntPtr hWnd);
[DllImport("user32")]
public static extern bool RedrawWindow(IntPtr hWnd, [In] ref RECT lprcUpdate, IntPtr hrgnUpdate, RedrawWindowFlags flags);
[DllImport("user32")]
public static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, RedrawWindowFlags flags);
[DllImport("user32")]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT r);
[DllImport("user32.dll", SetLastError = true, EntryPoint = "GetScrollBarInfo")]
public static extern int GetScrollBarInfo(IntPtr hWnd, uint idObject, ref SCROLLBARINFO psbi);
public enum SBOrientation : int
{
SB_HORZ = 0x0,
SB_VERT = 0x1,
SB_CTL = 0x2,
SB_BOTH = 0x3
}
[Flags()]
internal enum ScrollInfoMask:int
{
SIF_RANGE = 0x0001,
SIF_PAGE = 0x0002,
SIF_POS = 0x0004,
SIF_DISABLENOSCROLL = 0x0008,
SIF_TRACKPOS = 0x0010,
SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
}
public enum ScrollBarCommands
{
SB_LINEUP = 0,
SB_LINELEFT = 0,
SB_LINEDOWN = 1,
SB_LINERIGHT = 1,
SB_PAGEUP = 2,
SB_PAGELEFT = 2,
SB_PAGEDOWN = 3,
SB_PAGERIGHT = 3,
SB_THUMBPOSITION = 4,
SB_THUMBTRACK = 5,
SB_TOP = 6,
SB_LEFT = 6,
SB_BOTTOM = 7,
SB_RIGHT = 7,
SB_ENDSCROLL = 8
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetScrollInfo(IntPtr hwnd, SBOrientation fnBar, ref SCROLLINFO lpsi);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int SetScrollInfo(IntPtr hWnd, SBOrientation fnBar, ref SCROLLINFO si, bool redraw);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool AdjustWindowRectEx(ref RECT lpRect, int dwStyle,
bool bMenu, int dwExStyle);
// This static method is required because legacy OSes do not support
// GetWindowLongPtr
public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
{
if (IntPtr.Size == 8)
return GetWindowLongPtr64(hWnd, nIndex);
else
return new IntPtr(GetWindowLong32(hWnd, nIndex));
}
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern int GetWindowLong32(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
public static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
public static IntPtr SetWindowLongPtr(IntPtr hwnd, int nIndex, IntPtr dwNewLong)
{
if (IntPtr.Size == 8)
{
return SetWindowLongPtr64(hwnd, nIndex, dwNewLong);
}
return new IntPtr(SetWindowLongPtr32(hwnd, nIndex, dwNewLong.ToInt32()));
}
[DllImport("user32.dll", EntryPoint = "SetWindowLong", SetLastError = true)]
private static extern int SetWindowLongPtr32(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", SetLastError = true)]
private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int MapWindowPoints(IntPtr hwndFrom, IntPtr hwndTo, ref POINT lpPoints, [MarshalAs(UnmanagedType.U4)] int cPoints);
[DllImport("user32")]
public static extern bool PostMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr CreateDIBSection(IntPtr hdc, BITMAPINFO pbmi, uint iUsage, int ppvBits, IntPtr hSection, uint dwOffset);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
[DllImport("gdi32")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32")]
public static extern bool DeleteObject(int hObject);
public delegate void TimerDelegate(IntPtr hWnd, IntPtr uMsg, IntPtr idEvent, int dwTime);
[DllImport("user32.dll")]
public static extern UIntPtr SetTimer(IntPtr hWnd, UIntPtr nIDEvent, uint uElapse, TimerDelegate lpTimerFunc);
[DllImport("user32.dll")]
public static extern bool KillTimer(IntPtr hWnd, UIntPtr uIDEvent);
//[DllImport("gdi32.dll")]
//public static extern int CombineRgn(IntPtr hrgnDest, IntPtr hrgnSrc1, IntPtr hrgnSrc2, CombineRgnStyles fnCombineMode);
//public enum CombineRgnStyles : int
//{
// RGN_AND = 1,
// RGN_OR = 2,
// RGN_XOR = 3,
// RGN_DIFF = 4,
// RGN_COPY = 5,
// RGN_MIN = RGN_AND,
// RGN_MAX = RGN_COPY
//}
//[DllImport("gdi32.dll")]
//public static extern IntPtr CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
internal static bool ModifyHwndStyle(IntPtr hwnd, int removeStyle, int addStyle)
{
int curWindowStyle = GetWindowLongPtr(hwnd, (int)GWL.GWL_STYLE).ToInt32();
int newStyle = (curWindowStyle & ~removeStyle) | addStyle;
if (curWindowStyle == newStyle)
{
return false;
}
SetWindowLong(hwnd, (int)GWL.GWL_STYLE, newStyle);
return true;
}
#endregion
#region Structures
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class STYLESTRUCT
{
public uint styleOld;
public uint styleNew;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class TEXTMETRIC
{
public int tmHeight;
public int tmAscent;
public int tmDescent;
public int tmInternalLeading;
public int tmExternalLeading;
public int tmAveCharWidth;
public int tmMaxCharWidth;
public int tmWeight;
public int tmOverhang;
public int tmDigitizedAspectX;
public int tmDigitizedAspectY;
public char tmFirstChar;
public char tmLastChar;
public char tmDefaultChar;
public char tmBreakChar;
public byte tmItalic;
public byte tmUnderlined;
public byte tmStruckOut;
public byte tmPitchAndFamily;
public byte tmCharSet;
}
[StructLayout(LayoutKind.Sequential)]
public struct SCROLLBARINFO
{
public int cbSize;
public RECT rcScrollBar;
public int dxyLineButton;
public int xyThumbTop;
public int xyThumbBottom;
public int reserved;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public int[] rgstate;
}
public enum eScrollBarDirection
{
SB_HORZ = 0,
SB_VERT = 1
}
public enum eScrollInfoMask
{
SIF_RANGE = 0x1,
SIF_PAGE = 0x2,
SIF_POS = 0x4,
SIF_DISABLENOSCROLL = 0x8,
SIF_TRACKPOS = 0x10,
SIF_ALL = SIF_RANGE + SIF_PAGE + SIF_POS + SIF_TRACKPOS
}
[StructLayout(LayoutKind.Sequential)]
public struct SCROLLINFO
{
public int cbSize;
public ScrollInfoMask fMask;
public int nMin;
public int nMax;
public int nPage;
public int nPos;
public int nTrackPos;
public override string ToString()
{
return string.Format("nMin={0}, nMax={1}, nPage={2}, nPos={3}, nTrackPos={4}",
nMin, nMax, nPage, nPos, nTrackPos);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
public POINT(System.Drawing.Point p)
{
this.x = p.X;
this.y = p.Y;
}
public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return string.Format("{0},{1}", x, y);
}
}
public enum ShowWindowCommands : int
{
///
/// Hides the window and activates another window.
///
Hide = 0,
///
/// Activates and displays a window. If the window is minimized or
/// maximized, the system restores it to its original size and position.
/// An application should specify this flag when displaying the window
/// for the first time.
///
Normal = 1,
///
/// Activates the window and displays it as a minimized window.
///
ShowMinimized = 2,
///
/// Maximizes the specified window.
///
Maximize = 3, // is this the right value?
///
/// Activates the window and displays it as a maximized window.
///
ShowMaximized = 3,
///
/// Displays a window in its most recent size and position. This value
/// is similar to , except
/// the window is not actived.
///
ShowNoActivate = 4,
///
/// Activates the window and displays it in its current size and position.
///
Show = 5,
///
/// Minimizes the specified window and activates the next top-level
/// window in the Z order.
///
Minimize = 6,
///
/// Displays the window as a minimized window. This value is similar to
/// , except the
/// window is not activated.
///
ShowMinNoActive = 7,
///
/// Displays the window in its current size and position. This value is
/// similar to , except the
/// window is not activated.
///
ShowNA = 8,
///
/// Activates and displays the window. If the window is minimized or
/// maximized, the system restores it to its original size and position.
/// An application should specify this flag when restoring a minimized window.
///
Restore = 9,
///
/// Sets the show state based on the SW_* value specified in the
/// STARTUPINFO structure passed to the CreateProcess function by the
/// program that started the application.
///
ShowDefault = 10,
///
/// Windows 2000/XP: Minimizes a window, even if the thread
/// that owns the window is not responding. This flag should only be
/// used when minimizing windows from a different thread.
///
ForceMinimize = 11
}
public struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public POINT ptMinPosition;
public POINT ptMaxPosition;
public RECT rcNormalPosition;
}
///
/// Retrieves the show state and the restored, minimized, and maximized positions of the specified window.
///
///
/// A handle to the window.
///
///
/// A pointer to the WINDOWPLACEMENT structure that receives the show state and position information.
///
/// Before calling GetWindowPlacement, set the length member to sizeof(WINDOWPLACEMENT). GetWindowPlacement fails if lpwndpl-> length is not set correctly.
///
///
///
/// If the function succeeds, the return value is nonzero.
///
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
///
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);
[StructLayout(LayoutKind.Sequential)]
public struct NCCALCSIZE_PARAMS
{
public RECT rgrc0, rgrc1, rgrc2;
public IntPtr lppos;
}
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x, y;
public int cx, cy;
public int flags;
public override string ToString()
{
return string.Format("x={0}, y={1}, right={2}, bottom={3}", x, y, cx, cy);
}
}
[Serializable, 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_)
{
Left = left_;
Top = top_;
Right = right_;
Bottom = bottom_;
}
public RECT(Rectangle r)
{
Left = r.Left;
Top = r.Top;
Right = r.Right;
Bottom = r.Bottom;
}
public int Height { get { return Bottom - Top; } }
public int Width { get { return Right - Left; } }
public Size Size { get { return new Size(Width, Height); } }
public Point Location { get { return new Point(Left, Top); } }
// Handy method for converting to a System.Drawing.Rectangle
public Rectangle ToRectangle()
{ return Rectangle.FromLTRB(Left, Top, Right, Bottom); }
public static RECT FromRectangle(Rectangle rectangle)
{
return new RECT(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
}
public override int GetHashCode()
{
return Left ^ ((Top << 13) | (Top >> 0x13))
^ ((Width << 0x1a) | (Width >> 6))
^ ((Height << 7) | (Height >> 0x19));
}
public static RECT FromXYWH(int x, int y, int width, int height)
{
return new RECT(x, y, x + width, y + height);
}
#region Operator overloads
public static implicit operator Rectangle(RECT rect)
{
return Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom);
}
public static implicit operator RECT(Rectangle rect)
{
return new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom);
}
public override string ToString()
{
return "Left=" + this.Left + ", Top=" + this.Top + ", Right=" + this.Right + ", Bottom=" + this.Bottom;
}
#endregion
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct PAINTSTRUCT
{
public IntPtr hdc;
public int fErase;
public WinApi.RECT rcPaint;
public int fRestore;
public int fIncUpdate;
public int Reserved1;
public int Reserved2;
public int Reserved3;
public int Reserved4;
public int Reserved5;
public int Reserved6;
public int Reserved7;
public int Reserved8;
}
[StructLayout(LayoutKind.Sequential)]
public class BITMAPINFO
{
public int biSize = 0;
public int biWidth = 0;
public int biHeight = 0;
public short biPlanes = 0;
public short biBitCount = 0;
public int biCompression = 0;
public int biSizeImage = 0;
public int biXPelsPerMeter = 0;
public int biYPelsPerMeter = 0;
public int biClrUsed = 0;
public int biClrImportant = 0;
public byte bmiColors_rgbBlue = 0;
public byte bmiColors_rgbGreen = 0;
public byte bmiColors_rgbRed = 0;
public byte bmiColors_rgbReserved = 0;
}
public enum eObjectId : uint
{
OBJID_CLIENT = 0xFFFFFFFC,
OBJID_VSCROLL = 0xFFFFFFFB,
OBJID_HSCROLL = 0xFFFFFFFA
}
public enum eStateFlags
{
STATE_SYSTEM_INVISIBLE = 0x00008000,
STATE_SYSTEM_OFFSCREEN = 0x00010000,
STATE_SYSTEM_PRESSED = 0x00000008,
STATE_SYSTEM_UNAVAILABLE = 0x00000001
}
[StructLayout(LayoutKind.Sequential)]
public struct MINMAXINFO
{
public Point reserved;
public Size maxSize;
public Point maxPosition;
public Size minTrackSize;
public Size maxTrackSize;
}
#endregion
#region Functions
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int ScrollWindowEx(HandleRef hWnd, int nXAmount, int nYAmount, ref RECT rectScrollRegion, ref RECT rectClip, IntPtr hrgnUpdate, ref RECT prcUpdate, int flags);
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
public static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);
public enum AppCommands
{
APPCOMMAND_BROWSER_BACKWARD = 1,
APPCOMMAND_BROWSER_FORWARD = 2
}
private static int FAPPCOMMAND_MASK = 0xF000;
public static short GetAppCommandLParam(IntPtr lParam)
{
return (short)((short)HIWORD(lParam) & ~FAPPCOMMAND_MASK);
}
public static int LOWORD(int n)
{
return (short)(n & 0xffff);
}
public static int HIWORD(int n)
{
return (int)((n >> 0x10) & 0xffff);
}
public static int LOWORD(IntPtr n)
{
return unchecked((short)(long)n);
//return LOWORD((int)((long)n));
}
public static int HIWORD(IntPtr n)
{
//return (IntPtr.Size == 8 ? unchecked((int)n.ToInt64()) : n.ToInt32()) >> 16;
return unchecked((short)((long)n >> 16));
}
public static IntPtr CreateLParam(int loWord, int hiWord)
{
byte[] bx = BitConverter.GetBytes(loWord);
byte[] by = BitConverter.GetBytes(hiWord);
byte[] blp = new byte[] { bx[0], bx[1], by[0], by[1] };
return new IntPtr(BitConverter.ToInt32(blp, 0));
}
public static int ToInt(IntPtr ptr)
{
int result = 0;
try { result = (int)((long)ptr); }
catch { }
return result;
}
#endregion
#region DWM
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(
IntPtr hWnd,
int dwAttribute,
ref int attributeValue,
int sizeOfValueRetrived
);
[DllImport("dwmapi.dll")]
public static extern int DwmGetWindowAttribute(
IntPtr hwnd,
int dwAttribute,
ref int pvAttribute,
int sizeOfValueRetrived);
[DllImport("dwmapi.dll", PreserveSig = false)]
static extern bool DwmIsCompositionEnabled();
[DllImport("dwmapi.dll")]
public static extern int DwmDefWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, out IntPtr plResult);
[DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "DefWindowProcW")]
public static extern IntPtr DefWindowProc(IntPtr hWnd, WindowsMessages Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "DefWindowProcW")]
public static extern IntPtr DefWindowProc(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins);
public static void ExtendGlass(IntPtr handle, int glassHeight)
{
MARGINS margins = new MARGINS();
margins.cxLeftWidth = 0;
margins.cxRightWidth = 0;
margins.cyTopHeight = glassHeight;
margins.cyBottomHeight = 0;
int result = DwmExtendFrameIntoClientArea(handle, ref margins);
}
public static bool IsGlassEnabled
{
get
{
if (System.Environment.OSVersion.Version.Major < 6)
return false;
return DwmIsCompositionEnabled() && (System.Windows.Forms.Application.VisualStyleState == System.Windows.Forms.VisualStyles.VisualStyleState.ClientAndNonClientAreasEnabled);
}
}
public enum DWMWINDOWATTRIBUTE : int
{
DWMWA_NCRENDERING_ENABLED = 1,
DWMWA_NCRENDERING_POLICY = 2,
DWMWA_TRANSITIONS_FORCEDISABLED = 3,
DWMWA_ALLOW_NCPAINT = 4,
DWMWA_LAST = 5
}
public enum DWMNCRENDERINGPOLICY : int
{
DWMNCRP_USEWINDOWSTYLE = 0,
DWMNCRP_DISABLED = 1,
DWMNCRP_ENABLED = 2,
DWMNCRP_LAST = 3
}
public enum WVR
{
ALIGNTOP = 0x0010,
ALIGNLEFT = 0x0020,
ALIGNBOTTOM = 0x0040,
ALIGNRIGHT = 0x0080,
HREDRAW = 0x0100,
VREDRAW = 0x0200,
VALIDRECTS = 0x0400,
REDRAW = HREDRAW | VREDRAW,
}
#endregion
}
#region Taskbar
public enum TaskbarPosition
{
Unknown = -1,
Left,
Top,
Right,
Bottom,
}
internal sealed class Taskbar
{
private const string ClassName = "Shell_TrayWnd";
private Rectangle _Bounds = Rectangle.Empty;
public Rectangle Bounds
{
get { return _Bounds; }
private set { _Bounds=value;}
}
private TaskbarPosition _Position = TaskbarPosition.Unknown;
public TaskbarPosition Position
{
get { return _Position; }
private set { _Position = value; }
}
public Point Location
{
get
{
return this.Bounds.Location;
}
}
public Size Size
{
get
{
return this.Bounds.Size;
}
}
private bool _AlwaysOnTop = false;
//Always returns false under Windows 7
public bool AlwaysOnTop
{
get
{
return _AlwaysOnTop;
}
private set
{
_AlwaysOnTop = value;
}
}
private bool _AutoHide = false;
public bool AutoHide
{
get
{
return _AutoHide;
}
private set
{
_AutoHide = value;
}
}
public Taskbar()
{
IntPtr taskbarHandle = User32.FindWindow(Taskbar.ClassName, null);
APPBARDATA data = new APPBARDATA();
data.cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA));
data.hWnd = taskbarHandle;
IntPtr result = Shell32.SHAppBarMessage(ABM.GetTaskbarPos, ref data);
if (result == IntPtr.Zero)
throw new InvalidOperationException();
this.Position = (TaskbarPosition)data.uEdge;
this.Bounds = Rectangle.FromLTRB(data.rc.left, data.rc.top, data.rc.right, data.rc.bottom);
data.cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA));
result = Shell32.SHAppBarMessage(ABM.GetState, ref data);
int state = result.ToInt32();
this.AlwaysOnTop = (state & ABS.AlwaysOnTop) == ABS.AlwaysOnTop;
this.AutoHide = (state & ABS.Autohide) == ABS.Autohide;
}
}
public enum ABM : uint
{
New = 0x00000000,
Remove = 0x00000001,
QueryPos = 0x00000002,
SetPos = 0x00000003,
GetState = 0x00000004,
GetTaskbarPos = 0x00000005,
Activate = 0x00000006,
GetAutoHideBar = 0x00000007,
SetAutoHideBar = 0x00000008,
WindowPosChanged = 0x00000009,
SetState = 0x0000000A,
}
public enum ABE : uint
{
Left = 0,
Top = 1,
Right = 2,
Bottom = 3
}
public static class ABS
{
public const int Autohide = 0x0000001;
public const int AlwaysOnTop = 0x0000002;
}
public static class Shell32
{
[DllImport("shell32.dll", SetLastError = true)]
public static extern IntPtr SHAppBarMessage(ABM dwMessage, [In] ref APPBARDATA pData);
}
public static class User32
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
[StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA
{
public uint cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public ABE uEdge;
public RECT rc;
public int lParam;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
#endregion
}