1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2006 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
31 using System.Drawing.Imaging;
32 using System.ComponentModel;
33 using System.Collections;
34 using System.Diagnostics;
36 using System.Runtime.InteropServices;
38 using System.Threading;
42 namespace System.Windows.Forms {
43 internal class XplatUIWin32 : XplatUIDriver {
44 #region Local Variables
45 private static XplatUIWin32 instance;
46 private static int ref_count;
47 private static IntPtr FosterParent;
49 internal static MouseButtons mouse_state;
50 internal static Point mouse_position;
51 internal static bool grab_confined;
52 internal static IntPtr grab_hwnd;
53 internal static Rectangle grab_area;
54 internal static WndProc wnd_proc;
55 internal static IntPtr prev_mouse_hwnd;
56 internal static IntPtr override_cursor;
57 internal static bool caret_visible;
59 internal static bool themes_enabled;
60 private Hashtable timer_list;
61 private static Queue message_queue;
62 private static IntPtr clip_magic = new IntPtr(27051977);
63 private static int scroll_width;
64 private static int scroll_height;
65 private static Hashtable wm_nc_registered;
66 private static RECT clipped_cursor_rect;
67 private Hashtable registered_classes;
68 private Hwnd HwndCreating; // the Hwnd we are currently creating (see CreateWindow)
70 #endregion // Local Variables
72 #region Private Structs
73 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
74 private struct WNDCLASS {
76 internal WndProc lpfnWndProc;
77 internal int cbClsExtra;
78 internal int cbWndExtra;
79 internal IntPtr hInstance;
80 internal IntPtr hIcon;
81 internal IntPtr hCursor;
82 internal IntPtr hbrBackground;
83 [MarshalAs(UnmanagedType.LPWStr)]
84 internal string lpszMenuName;
85 [MarshalAs(UnmanagedType.LPWStr)]
86 internal string lpszClassName;
89 [StructLayout(LayoutKind.Sequential)]
90 internal struct RECT {
96 public RECT (int left, int top, int right, int bottom)
101 this.bottom = bottom;
104 #region Instance Properties
105 public int Height { get { return bottom - top; } }
106 public int Width { get { return right - left; } }
107 public Size Size { get { return new Size (Width, Height); } }
108 public Point Location { get { return new Point (left, top); } }
111 #region Instance Methods
112 public Rectangle ToRectangle ()
114 return Rectangle.FromLTRB (left, top, right, bottom);
117 public static RECT FromRectangle (Rectangle rectangle)
119 return new RECT (rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
122 public override int GetHashCode ()
124 return left ^ ((top << 13) | (top >> 0x13))
125 ^ ((Width << 0x1a) | (Width >> 6))
126 ^ ((Height << 7) | (Height >> 0x19));
129 public override string ToString ()
131 return String.Format("RECT left={0}, top={1}, right={2}, bottom={3}, width={4}, height={5}", left, top, right, bottom, right-left, bottom-top);
135 #region Operator overloads
136 public static implicit operator Rectangle (RECT rect)
138 return Rectangle.FromLTRB (rect.left, rect.top, rect.right, rect.bottom);
141 public static implicit operator RECT (Rectangle rect)
143 return new RECT (rect.Left, rect.Top, rect.Right, rect.Bottom);
148 internal enum SPIAction {
149 SPI_GETACTIVEWINDOWTRACKING = 0x1000,
150 SPI_GETACTIVEWNDTRKTIMEOUT = 0x2002,
151 SPI_GETANIMATION = 0x0048,
152 SPI_GETCARETWIDTH = 0x2006,
153 SPI_GETCOMBOBOXANIMATION = 0x1004,
154 SPI_GETDRAGFULLWINDOWS = 0x0026,
155 SPI_GETDROPSHADOW = 0x1024,
156 SPI_GETFONTSMOOTHING = 0x004A,
157 SPI_GETFONTSMOOTHINGCONTRAST = 0x200C,
158 SPI_GETFONTSMOOTHINGTYPE = 0x200A,
159 SPI_GETGRADIENTCAPTIONS = 0x1008,
160 SPI_GETHOTTRACKING = 0x100E,
161 SPI_GETICONTITLEWRAP = 0x0019,
162 SPI_GETKEYBOARDSPEED = 0x000A,
163 SPI_GETKEYBOARDDELAY = 0x0016,
164 SPI_GETKEYBOARDCUES = 0x100A,
165 SPI_GETKEYBOARDPREF = 0x0044,
166 SPI_GETLISTBOXSMOOTHSCROLLING = 0x1006,
167 SPI_GETMENUANIMATION = 0x1002,
168 SPI_GETMENUDROPALIGNMENT = 0x001B,
169 SPI_GETMENUFADE = 0x1012,
170 SPI_GETMENUSHOWDELAY = 0x006A,
171 SPI_GETMOUSESPEED = 0x0070,
172 SPI_GETSELECTIONFADE = 0x1014,
173 SPI_GETSNAPTODEFBUTTON = 0x005F,
174 SPI_GETTOOLTIPANIMATION = 0x1016,
175 SPI_GETWORKAREA = 0x0030,
176 SPI_GETMOUSEHOVERWIDTH = 0x0062,
177 SPI_GETMOUSEHOVERHEIGHT = 0x0064,
178 SPI_GETMOUSEHOVERTIME = 0x0066,
179 SPI_GETUIEFFECTS = 0x103E,
180 SPI_GETWHEELSCROLLLINES = 0x0068
183 internal enum WindowPlacementFlags {
187 SW_SHOWMINIMIZED = 2,
188 SW_SHOWMAXIMIZED = 3,
190 SW_SHOWNOACTIVATE = 4,
193 SW_SHOWMINNOACTIVE = 7,
197 SW_FORCEMINIMIZE = 11,
201 [StructLayout(LayoutKind.Sequential)]
202 private struct WINDOWPLACEMENT {
203 internal uint length;
205 internal WindowPlacementFlags showCmd;
206 internal POINT ptMinPosition;
207 internal POINT ptMaxPosition;
208 internal RECT rcNormalPosition;
211 [StructLayout(LayoutKind.Sequential)]
212 internal struct NCCALCSIZE_PARAMS {
216 internal IntPtr lppos;
220 private enum TMEFlags {
221 TME_HOVER = 0x00000001,
222 TME_LEAVE = 0x00000002,
223 TME_NONCLIENT = 0x00000010,
224 TME_QUERY = unchecked((int)0x40000000),
225 TME_CANCEL = unchecked((int)0x80000000)
228 [StructLayout(LayoutKind.Sequential)]
229 private struct TRACKMOUSEEVENT {
231 internal TMEFlags dwFlags;
232 internal IntPtr hWnd;
233 internal int dwHoverTime;
236 [StructLayout(LayoutKind.Sequential)]
237 private struct PAINTSTRUCT {
240 internal RECT rcPaint;
241 internal int fRestore;
242 internal int fIncUpdate;
243 internal int Reserved1;
244 internal int Reserved2;
245 internal int Reserved3;
246 internal int Reserved4;
247 internal int Reserved5;
248 internal int Reserved6;
249 internal int Reserved7;
250 internal int Reserved8;
253 [StructLayout(LayoutKind.Sequential)]
254 internal struct KEYBDINPUT {
256 internal short wScan;
257 internal Int32 dwFlags;
259 internal UIntPtr dwExtraInfo;
262 [StructLayout(LayoutKind.Sequential)]
263 internal struct MOUSEINPUT {
266 internal Int32 mouseData;
267 internal Int32 dwFlags;
269 internal UIntPtr dwExtraInfo;
272 [StructLayout(LayoutKind.Sequential)]
273 internal struct HARDWAREINPUT {
275 internal short wParamL;
276 internal short wParamH;
279 [StructLayout (LayoutKind.Sequential)]
280 internal struct ICONINFO {
282 internal Int32 xHotspot;
283 internal Int32 yHotspot;
284 internal IntPtr hbmMask;
285 internal IntPtr hbmColor;
288 [StructLayout(LayoutKind.Explicit)]
289 internal struct INPUT {
294 internal MOUSEINPUT mi;
297 internal KEYBDINPUT ki;
300 internal HARDWAREINPUT hi;
303 [StructLayout (LayoutKind.Sequential)]
304 public struct ANIMATIONINFO {
305 internal uint cbSize;
306 internal int iMinAnimate;
309 internal enum InputFlags {
310 KEYEVENTF_EXTENDEDKEY = 0x0001,
311 KEYEVENTF_KEYUP = 0x0002,
312 KEYEVENTF_SCANCODE = 0x0003,
313 KEYEVENTF_UNICODE = 0x0004,
316 internal enum ClassStyle {
317 CS_VREDRAW = 0x00000001,
318 CS_HREDRAW = 0x00000002,
319 CS_KEYCVTWINDOW = 0x00000004,
320 CS_DBLCLKS = 0x00000008,
321 CS_OWNDC = 0x00000020,
322 CS_CLASSDC = 0x00000040,
323 CS_PARENTDC = 0x00000080,
324 CS_NOKEYCVT = 0x00000100,
325 CS_NOCLOSE = 0x00000200,
326 CS_SAVEBITS = 0x00000800,
327 CS_BYTEALIGNCLIENT = 0x00001000,
328 CS_BYTEALIGNWINDOW = 0x00002000,
329 CS_GLOBALCLASS = 0x00004000,
332 CS_DROPSHADOW = 0x00020000
335 internal enum SetWindowPosZOrder {
343 internal enum SetWindowPosFlags {
344 SWP_ASYNCWINDOWPOS = 0x4000,
345 SWP_DEFERERASE = 0x2000,
346 SWP_DRAWFRAME = 0x0020,
347 SWP_FRAMECHANGED = 0x0020,
348 SWP_HIDEWINDOW = 0x0080,
349 SWP_NOACTIVATE = 0x0010,
350 SWP_NOCOPYBITS = 0x0100,
352 SWP_NOOWNERZORDER = 0x0200,
353 SWP_NOREDRAW = 0x0008,
354 SWP_NOREPOSITION = 0x0200,
355 SWP_NOENDSCHANGING = 0x0400,
357 SWP_NOZORDER = 0x0004,
358 SWP_SHOWWINDOW = 0x0040
361 internal enum GetSysColorIndex {
363 COLOR_BACKGROUND = 1,
364 COLOR_ACTIVECAPTION = 2,
365 COLOR_INACTIVECAPTION = 3,
368 COLOR_WINDOWFRAME = 6,
370 COLOR_WINDOWTEXT = 8,
371 COLOR_CAPTIONTEXT = 9,
372 COLOR_ACTIVEBORDER = 10,
373 COLOR_INACTIVEBORDER = 11,
374 COLOR_APPWORKSPACE = 12,
375 COLOR_HIGHLIGHT = 13,
376 COLOR_HIGHLIGHTTEXT = 14,
378 COLOR_BTNSHADOW = 16,
381 COLOR_INACTIVECAPTIONTEXT = 19,
382 COLOR_BTNHIGHLIGHT = 20,
383 COLOR_3DDKSHADOW = 21,
389 COLOR_GRADIENTACTIVECAPTION = 27,
390 COLOR_GRADIENTINACTIVECAPTION = 28,
391 COLOR_MENUHIGHLIGHT = 29,
397 COLOR_3DHIGHLIGHT = 20,
398 COLOR_3DHILIGHT = 20,
399 COLOR_BTNHILIGHT = 20,
400 COLOR_MAXVALUE = 24,/* Maximum value */
403 private enum LoadCursorType {
412 IDC_SIZENWSE = 32642,
413 IDC_SIZENESW = 32643,
419 IDC_APPSTARTING = 32650,
424 private enum AncestorType {
431 private enum WindowLong {
442 private enum LogBrushStyle {
451 BS_DIBPATTERN8X8 = 8,
456 private enum LogBrushHatch {
457 HS_HORIZONTAL = 0, /* ----- */
458 HS_VERTICAL = 1, /* ||||| */
459 HS_FDIAGONAL = 2, /* \\\\\ */
460 HS_BDIAGONAL = 3, /* ///// */
461 HS_CROSS = 4, /* +++++ */
462 HS_DIAGCROSS = 5, /* xxxxx */
465 internal struct COLORREF {
472 [StructLayout(LayoutKind.Sequential)]
473 private struct LOGBRUSH {
474 internal LogBrushStyle lbStyle;
475 internal COLORREF lbColor;
476 internal LogBrushHatch lbHatch;
479 [StructLayout(LayoutKind.Sequential)]
480 internal struct TEXTMETRIC {
481 internal int tmHeight;
482 internal int tmAscent;
483 internal int tmDescent;
484 internal int tmInternalLeading;
485 internal int tmExternalLeading;
486 internal int tmAveCharWidth;
487 internal int tmMaxCharWidth;
488 internal int tmWeight;
489 internal int tmOverhang;
490 internal int tmDigitizedAspectX;
491 internal int tmDigitizedAspectY;
492 internal short tmFirstChar;
493 internal short tmLastChar;
494 internal short tmDefaultChar;
495 internal short tmBreakChar;
496 internal byte tmItalic;
497 internal byte tmUnderlined;
498 internal byte tmStruckOut;
499 internal byte tmPitchAndFamily;
500 internal byte tmCharSet;
503 public enum TernaryRasterOperations : uint
505 SRCCOPY = 0x00CC0020,
506 SRCPAINT = 0x00EE0086,
508 SRCINVERT = 0x00660046,
509 SRCERASE = 0x00440328,
510 NOTSRCCOPY = 0x00330008,
511 NOTSRCERASE = 0x001100A6,
512 MERGECOPY = 0x00C000CA,
513 MERGEPAINT = 0x00BB0226,
514 PATCOPY = 0x00F00021,
515 PATPAINT = 0x00FB0A09,
516 PATINVERT = 0x005A0049,
517 DSTINVERT = 0x00550009,
518 BLACKNESS = 0x00000042,
519 WHITENESS = 0x00FF0062
523 private enum ScrollWindowExFlags {
525 SW_SCROLLCHILDREN = 0x0001,
526 SW_INVALIDATE = 0x0002,
528 SW_SMOOTHSCROLL = 0x0010
531 internal enum SystemMetrics {
548 SM_CXFULLSCREEN = 16,
549 SM_CYFULLSCREEN = 17,
550 SM_CYKANJIWINDOW = 18,
551 SM_MOUSEPRESENT = 19,
570 SM_CXICONSPACING = 38,
571 SM_CYICONSPACING = 39,
572 SM_MENUDROPALIGNMENT = 40,
575 SM_CMOUSEBUTTONS = 43,
576 SM_CXFIXEDFRAME = SM_CXDLGFRAME,
577 SM_CYFIXEDFRAME = SM_CYDLGFRAME,
578 SM_CXSIZEFRAME = SM_CXFRAME,
579 SM_CYSIZEFRAME = SM_CYFRAME,
583 SM_CXMINSPACING = 47,
584 SM_CYMINSPACING = 48,
607 SM_MIDEASTENABLED = 74,
608 SM_MOUSEWHEELPRESENT = 75,
609 SM_XVIRTUALSCREEN = 76,
610 SM_YVIRTUALSCREEN = 77,
611 SM_CXVIRTUALSCREEN = 78,
612 SM_CYVIRTUALSCREEN = 79,
614 SM_SAMEDISPLAYFORMAT = 81,
616 SM_CXFOCUSBORDER = 83,
617 SM_CYFOCUSBORDER = 84,
623 // We'll only support _WIN32_IE < 0x0500 for now
624 internal enum NotifyIconMessage {
625 NIM_ADD = 0x00000000,
626 NIM_MODIFY = 0x00000001,
627 NIM_DELETE = 0x00000002,
631 internal enum NotifyIconFlags {
632 NIF_MESSAGE = 0x00000001,
633 NIF_ICON = 0x00000002,
634 NIF_TIP = 0x00000004,
635 NIF_STATE = 0x00000008,
636 NIF_INFO = 0x00000010
639 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
640 internal struct NOTIFYICONDATA {
641 internal uint cbSize;
642 internal IntPtr hWnd;
644 internal NotifyIconFlags uFlags;
645 internal uint uCallbackMessage;
646 internal IntPtr hIcon;
647 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
648 internal string szTip;
649 internal int dwState;
650 internal int dwStateMask;
651 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
652 internal string szInfo;
653 internal int uTimeoutOrVersion;
654 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
655 internal string szInfoTitle;
657 internal ToolTipIcon dwInfoFlags;
659 internal int dwInfoFlags;
664 internal enum DCExFlags {
665 DCX_WINDOW = 0x00000001,
666 DCX_CACHE = 0x00000002,
667 DCX_NORESETATTRS = 0x00000004,
668 DCX_CLIPCHILDREN = 0x00000008,
669 DCX_CLIPSIBLINGS = 0x00000010,
670 DCX_PARENTCLIP = 0x00000020,
671 DCX_EXCLUDERGN = 0x00000040,
672 DCX_INTERSECTRGN = 0x00000080,
673 DCX_EXCLUDEUPDATE = 0x00000100,
674 DCX_INTERSECTUPDATE = 0x00000200,
675 DCX_LOCKWINDOWUPDATE = 0x00000400,
676 DCX_USESTYLE = 0x00010000,
677 DCX_VALIDATE = 0x00200000
680 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
681 internal struct CLIENTCREATESTRUCT {
682 internal IntPtr hWindowMenu;
683 internal uint idFirstChild;
686 private enum ClassLong : int {
688 GCL_HBRBACKGROUND = -10,
692 GCL_CBWNDEXTRA = -18,
693 GCL_CBCLSEXTRA = -20,
701 internal enum GAllocFlags : uint {
703 GMEM_MOVEABLE = 0x0002,
704 GMEM_NOCOMPACT = 0x0010,
705 GMEM_NODISCARD = 0x0020,
706 GMEM_ZEROINIT = 0x0040,
707 GMEM_MODIFY = 0x0080,
708 GMEM_DISCARDABLE = 0x0100,
709 GMEM_NOT_BANKED = 0x1000,
711 GMEM_DDESHARE = 0x2000,
712 GMEM_NOTIFY = 0x4000,
713 GMEM_LOWER = GMEM_NOT_BANKED,
714 GMEM_VALID_FLAGS = 0x7F72,
715 GMEM_INVALID_HANDLE = 0x8000,
716 GHND = (GMEM_MOVEABLE | GMEM_ZEROINIT),
717 GPTR = (GMEM_FIXED | GMEM_ZEROINIT)
720 internal enum ROP2DrawMode : int {
740 internal enum PenStyle : int {
752 internal enum PatBltRop : int {
754 PATINVERT = 0x5a0049,
755 DSTINVERT = 0x550009,
756 BLACKNESS = 0x000042,
757 WHITENESS = 0xff0062,
760 internal enum StockObject : int {
767 HOLLOW_BRUSH = NULL_BRUSH,
772 ANSI_FIXED_FONT = 11,
775 DEVICE_DEFAULT_FONT = 14,
776 DEFAULT_PALETTE = 15,
777 SYSTEM_FIXED_FONT = 16
780 internal enum HatchStyle : int {
790 internal enum SndFlags : int {
793 SND_NODEFAULT = 0x0002,
797 SND_NOWAIT = 0x00002000,
798 SND_ALIAS = 0x00010000,
799 SND_ALIAS_ID = 0x00110000,
800 SND_FILENAME = 0x00020000,
801 SND_RESOURCE = 0x00040004,
803 SND_APPLICATION = 0x0080,
807 internal enum LayeredWindowAttributes : int {
812 public enum ACLineStatus : byte {
818 public enum BatteryFlag : byte {
823 NoSystemBattery = 128,
827 [StructLayout (LayoutKind.Sequential)]
828 public class SYSTEMPOWERSTATUS {
829 public ACLineStatus _ACLineStatus;
830 public BatteryFlag _BatteryFlag;
831 public Byte _BatteryLifePercent;
832 public Byte _Reserved1;
833 public Int32 _BatteryLifeTime;
834 public Int32 _BatteryFullLifeTime;
838 #region Constructor & Destructor
839 private XplatUIWin32() {
840 // Handle singleton stuff first
843 mouse_state = MouseButtons.None;
844 mouse_position = Point.Empty;
846 message_queue = new Queue();
848 themes_enabled = false;
850 wnd_proc = new WndProc(InternalWndProc);
852 FosterParent=Win32CreateWindow(WindowExStyles.WS_EX_TOOLWINDOW, "static", "Foster Parent Window", WindowStyles.WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
854 if (FosterParent==IntPtr.Zero) {
855 Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
858 scroll_height = Win32GetSystemMetrics(SystemMetrics.SM_CYHSCROLL);
859 scroll_width = Win32GetSystemMetrics(SystemMetrics.SM_CXVSCROLL);
861 timer_list = new Hashtable ();
862 registered_classes = new Hashtable ();
864 #endregion // Constructor & Destructor
866 #region Private Support Methods
868 private string RegisterWindowClass (int classStyle)
872 lock (registered_classes) {
873 class_name = (string)registered_classes[classStyle];
875 if (class_name != null)
878 class_name = string.Format ("Mono.WinForms.{0}.{1}", System.Threading.Thread.GetDomainID ().ToString (), classStyle);
882 wndClass.style = classStyle;
883 wndClass.lpfnWndProc = wnd_proc;
884 wndClass.cbClsExtra = 0;
885 wndClass.cbWndExtra = 0;
886 wndClass.hbrBackground = (IntPtr)(GetSysColorIndex.COLOR_WINDOW + 1);
887 wndClass.hCursor = Win32LoadCursor (IntPtr.Zero, LoadCursorType.IDC_ARROW);
888 wndClass.hIcon = IntPtr.Zero;
889 wndClass.hInstance = IntPtr.Zero;
890 wndClass.lpszClassName = class_name;
891 wndClass.lpszMenuName = "";
893 bool result = Win32RegisterClass (ref wndClass);
896 Win32MessageBox (IntPtr.Zero, "Could not register the window class, win32 error " + Win32GetLastError ().ToString (), "Oops", 0);
898 registered_classes[classStyle] = class_name;
904 private static bool RetrieveMessage(ref MSG msg) {
907 if (message_queue.Count == 0) {
911 message = (MSG)message_queue.Dequeue();
917 private static bool StoreMessage(ref MSG msg) {
918 MSG message = new MSG();
921 message_queue.Enqueue(message);
926 internal static String AnsiToString(IntPtr ansi_data) {
927 return (string)Marshal.PtrToStringAnsi(ansi_data);
930 internal static String UnicodeToString(IntPtr unicode_data) {
931 return (string)Marshal.PtrToStringUni(unicode_data);
934 internal static Image DIBtoImage(IntPtr dib_data) {
935 BITMAPINFOHEADER bmi;
946 bmi = (BITMAPINFOHEADER)Marshal.PtrToStructure(dib_data, typeof(BITMAPINFOHEADER));
948 ncolors = (int)bmi.biClrUsed;
950 if (bmi.biBitCount < 24) {
951 ncolors = (int)(1 << bmi.biBitCount);
954 //palettesize = ncolors * 4;
956 imagesize = (int)bmi.biSizeImage;
957 if (imagesize == 0) {
958 imagesize = (int)(((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);
961 switch(bmi.biBitCount) {
962 case 1: { // Monochrome
963 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format1bppIndexed);
964 palette = new int[2];
969 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format4bppIndexed);
970 palette = new int[16];
975 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format8bppIndexed);
976 palette = new int[256];
982 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format32bppArgb);
983 palette = new int[0];
988 throw new Exception("Unexpected number of bits:" + bmi.biBitCount.ToString());
992 if (bmi.biBitCount < 24) {
993 pal = bmp.Palette; // Managed palette
994 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER))), palette, 0, palette.Length);
996 for (int i = 0; i < ncolors; i++) {
997 pal.Entries[i] = Color.FromArgb(palette[i] | unchecked((int)0xff000000));
1002 bytesPerLine = (int)((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3);
1003 bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
1005 imagebits = new byte[bytesPerLine];
1007 for (int y = 0; y < bmi.biHeight; y++) {
1008 // Copy from source to managed
1009 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER)) + palette.Length * 4 + bytesPerLine * y), imagebits, 0, bytesPerLine);
1011 // Copy from managed to dest
1012 Marshal.Copy(imagebits, 0, (IntPtr)((int)bits.Scan0 + bits.Stride * (bmi.biHeight - 1 - y)), imagebits.Length);
1015 bmp.UnlockBits(bits);
1020 internal static byte[] ImageToDIB(Image image) {
1025 ms = new MemoryStream();
1026 image.Save(ms, ImageFormat.Bmp);
1027 buffer = ms.GetBuffer();
1029 // Filter out the file header
1030 retbuf = new byte[buffer.Length];
1031 Array.Copy(buffer, 14, retbuf, 0, buffer.Length - 14);
1035 internal static IntPtr DupGlobalMem(IntPtr mem) {
1041 len = Win32GlobalSize(mem);
1042 mem_ptr = Win32GlobalLock(mem);
1044 dup = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE, (int)len);
1045 dup_ptr = Win32GlobalLock(dup);
1047 Win32CopyMemory(dup_ptr, mem_ptr, (int)len);
1049 Win32GlobalUnlock(mem);
1050 Win32GlobalUnlock(dup);
1055 private int GetSystemParametersInfoInt (SPIAction spi)
1059 Win32SystemParametersInfo (spi, 0, ref value, 0);
1064 private bool GetSystemParametersInfoBool (SPIAction spi)
1068 Win32SystemParametersInfo (spi, 0, ref value, 0);
1072 #endregion // Private Support Methods
1074 #region Static Properties
1075 internal override int ActiveWindowTrackingDelay {
1076 get { return GetSystemParametersInfoInt (SPIAction.SPI_GETACTIVEWNDTRKTIMEOUT); }
1079 internal override int CaretWidth {
1081 // Supported on 2k, XP, 2k3 +
1082 if (Environment.OSVersion.Version.Major < 5)
1083 throw new NotSupportedException ();
1085 return GetSystemParametersInfoInt (SPIAction.SPI_GETCARETWIDTH);
1089 internal override int FontSmoothingContrast {
1091 // Supported on XP, 2k3 +
1092 if (Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0))
1093 throw new NotSupportedException ();
1095 return GetSystemParametersInfoInt (SPIAction.SPI_GETFONTSMOOTHINGCONTRAST);
1099 internal override int FontSmoothingType {
1101 // Supported on XP, 2k3 +
1102 if (Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0))
1103 throw new NotSupportedException ();
1105 return GetSystemParametersInfoInt (SPIAction.SPI_GETFONTSMOOTHINGTYPE);
1109 internal override int HorizontalResizeBorderThickness {
1110 get { return Win32GetSystemMetrics (SystemMetrics.SM_CXSIZEFRAME); }
1113 internal override bool IsActiveWindowTrackingEnabled {
1114 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETACTIVEWINDOWTRACKING); }
1117 internal override bool IsComboBoxAnimationEnabled {
1118 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETCOMBOBOXANIMATION); }
1121 internal override bool IsDropShadowEnabled {
1123 // Supported on XP, 2k3 +
1124 if (Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0))
1125 throw new NotSupportedException ();
1127 return GetSystemParametersInfoBool (SPIAction.SPI_GETDROPSHADOW);
1131 internal override bool IsFontSmoothingEnabled {
1132 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETFONTSMOOTHING); }
1135 internal override bool IsHotTrackingEnabled {
1136 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETHOTTRACKING); }
1139 internal override bool IsIconTitleWrappingEnabled {
1140 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETICONTITLEWRAP); }
1143 internal override bool IsKeyboardPreferred {
1144 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETKEYBOARDPREF); }
1147 internal override bool IsListBoxSmoothScrollingEnabled {
1148 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETLISTBOXSMOOTHSCROLLING); }
1151 internal override bool IsMenuAnimationEnabled {
1152 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETMENUANIMATION); }
1155 internal override bool IsMenuFadeEnabled {
1156 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETMENUFADE); }
1159 internal override bool IsMinimizeRestoreAnimationEnabled {
1161 ANIMATIONINFO ai = new ANIMATIONINFO ();
1162 ai.cbSize = (uint)Marshal.SizeOf (ai);
1164 Win32SystemParametersInfo (SPIAction.SPI_GETANIMATION, 0, ref ai, 0);
1165 return ai.iMinAnimate == 0 ? false : true;
1169 internal override bool IsSelectionFadeEnabled {
1170 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETSELECTIONFADE); }
1173 internal override bool IsSnapToDefaultEnabled {
1174 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETSNAPTODEFBUTTON); }
1177 internal override bool IsTitleBarGradientEnabled {
1178 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETGRADIENTCAPTIONS); }
1181 internal override bool IsToolTipAnimationEnabled {
1182 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETTOOLTIPANIMATION); }
1185 internal override Size MenuBarButtonSize {
1187 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXMENUSIZE),
1188 Win32GetSystemMetrics (SystemMetrics.SM_CYMENUSIZE));
1192 public override Size MenuButtonSize {
1195 Win32GetSystemMetrics (SystemMetrics.SM_CXMENUSIZE),
1196 Win32GetSystemMetrics (SystemMetrics.SM_CYMENUSIZE));
1200 internal override int MenuShowDelay {
1201 get { return GetSystemParametersInfoInt (SPIAction.SPI_GETMENUSHOWDELAY); }
1204 internal override int MouseSpeed {
1205 get { return GetSystemParametersInfoInt (SPIAction.SPI_GETMOUSESPEED); }
1208 internal override LeftRightAlignment PopupMenuAlignment {
1209 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETMENUDROPALIGNMENT) == true ? LeftRightAlignment.Left : LeftRightAlignment.Right; }
1213 internal override PowerStatus PowerStatus {
1215 SYSTEMPOWERSTATUS p = new SYSTEMPOWERSTATUS ();
1217 Win32GetSystemPowerStatus (p);
1219 PowerStatus ps = new PowerStatus ((BatteryChargeStatus)p._BatteryFlag, p._BatteryFullLifeTime, (float)p._BatteryLifePercent / 255f, p._BatteryLifeTime, (PowerLineStatus)p._ACLineStatus);
1226 internal override int SizingBorderWidth {
1227 get { return Win32GetSystemMetrics (SystemMetrics.SM_CXSIZEFRAME); }
1230 internal override Size SmallCaptionButtonSize {
1232 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXSMSIZE),
1233 Win32GetSystemMetrics (SystemMetrics.SM_CYSMSIZE));
1237 internal override bool UIEffectsEnabled {
1238 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETUIEFFECTS); }
1241 internal override int VerticalResizeBorderThickness {
1242 get { return Win32GetSystemMetrics (SystemMetrics.SM_CYSIZEFRAME); }
1245 internal override void RaiseIdle (EventArgs e)
1251 internal override Keys ModifierKeys {
1256 key_state = Keys.None;
1258 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
1259 if ((state & 0x8000) != 0) {
1260 key_state |= Keys.Shift;
1262 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
1263 if ((state & 0x8000) != 0) {
1264 key_state |= Keys.Control;
1267 state = Win32GetKeyState(VirtualKeys.VK_MENU);
1268 if ((state & 0x8000) != 0) {
1269 key_state |= Keys.Alt;
1276 internal override MouseButtons MouseButtons {
1282 internal override Point MousePosition {
1284 return mouse_position;
1288 internal override Size MouseHoverSize {
1293 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref width, 0);
1294 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref height, 0);
1295 return new Size(width, height);
1299 internal override int MouseHoverTime {
1303 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERTIME, 0, ref time, 0);
1308 internal override int MouseWheelScrollDelta {
1311 Win32SystemParametersInfo(SPIAction.SPI_GETWHEELSCROLLLINES, 0, ref delta, 0);
1316 internal override int HorizontalScrollBarHeight {
1318 return scroll_height;
1322 internal override bool UserClipWontExposeParent {
1329 internal override int VerticalScrollBarWidth {
1331 return scroll_width;
1335 internal override int MenuHeight {
1337 return Win32GetSystemMetrics(SystemMetrics.SM_CYMENU);
1341 internal override Size Border3DSize {
1343 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXEDGE),
1344 Win32GetSystemMetrics (SystemMetrics.SM_CYEDGE));
1348 internal override Size BorderSize {
1350 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXBORDER),
1351 Win32GetSystemMetrics (SystemMetrics.SM_CYBORDER));
1355 internal override bool DropTarget {
1362 //throw new NotImplementedException("Need to figure out D'n'D for Win32");
1367 internal override Size CaptionButtonSize {
1369 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXSIZE),
1370 Win32GetSystemMetrics (SystemMetrics.SM_CYSIZE));
1374 internal override int CaptionHeight {
1376 return Win32GetSystemMetrics(SystemMetrics.SM_CYCAPTION);
1380 internal override Size CursorSize {
1382 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CYCURSOR));
1386 internal override bool DragFullWindows {
1389 Win32SystemParametersInfo (SPIAction.SPI_GETDRAGFULLWINDOWS, 0, ref full, 0);
1394 internal override Size DragSize {
1396 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXDRAG), Win32GetSystemMetrics(SystemMetrics.SM_CYDRAG));
1400 internal override Size DoubleClickSize {
1402 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXDOUBLECLK),
1403 Win32GetSystemMetrics (SystemMetrics.SM_CYDOUBLECLK));
1407 internal override int DoubleClickTime {
1409 return Win32GetDoubleClickTime ();
1413 internal override Size FixedFrameBorderSize {
1415 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXFIXEDFRAME),
1416 Win32GetSystemMetrics (SystemMetrics.SM_CYFIXEDFRAME));
1420 internal override Size FrameBorderSize {
1422 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXFRAME), Win32GetSystemMetrics(SystemMetrics.SM_CYFRAME));
1426 internal override Size IconSize {
1428 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXICON), Win32GetSystemMetrics(SystemMetrics.SM_CYICON));
1432 internal override Size MaxWindowTrackSize {
1434 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMAXTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMAXTRACK));
1438 internal override bool MenuAccessKeysUnderlined {
1441 Win32SystemParametersInfo (SPIAction.SPI_GETKEYBOARDCUES, 0, ref underlined, 0);
1442 return (underlined != 0);
1446 internal override Size MinimizedWindowSize {
1448 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINIMIZED), Win32GetSystemMetrics(SystemMetrics.SM_CYMINIMIZED));
1452 internal override Size MinimizedWindowSpacingSize {
1454 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINSPACING), Win32GetSystemMetrics(SystemMetrics.SM_CYMINSPACING));
1458 internal override Size MinimumWindowSize {
1460 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMIN), Win32GetSystemMetrics(SystemMetrics.SM_CYMIN));
1464 internal override Size MinWindowTrackSize {
1466 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMINTRACK));
1470 internal override Size SmallIconSize {
1472 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXSMICON), Win32GetSystemMetrics(SystemMetrics.SM_CYSMICON));
1476 internal override int MouseButtonCount {
1478 return Win32GetSystemMetrics(SystemMetrics.SM_CMOUSEBUTTONS);
1482 internal override bool MouseButtonsSwapped {
1484 return Win32GetSystemMetrics(SystemMetrics.SM_SWAPBUTTON) != 0;
1488 internal override bool MouseWheelPresent {
1490 return Win32GetSystemMetrics(SystemMetrics.SM_MOUSEWHEELPRESENT) != 0;
1494 internal override Rectangle VirtualScreen {
1496 return new Rectangle( Win32GetSystemMetrics(SystemMetrics.SM_XVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_YVIRTUALSCREEN),
1497 Win32GetSystemMetrics(SystemMetrics.SM_CXVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYVIRTUALSCREEN));
1501 internal override Rectangle WorkingArea {
1506 Win32SystemParametersInfo(SPIAction.SPI_GETWORKAREA, 0, ref rect, 0);
1507 return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
1508 //return new Rectangle(0, 0, Win32GetSystemMetrics(SystemMetrics.SM.SM_CXSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYSCREEN));
1512 internal override bool ThemesEnabled {
1514 return XplatUIWin32.themes_enabled;
1518 internal override bool RequiresPositiveClientAreaSize {
1524 public override int ToolWindowCaptionHeight {
1526 return Win32GetSystemMetrics (SystemMetrics.SM_CYSMCAPTION);
1530 public override Size ToolWindowCaptionButtonSize {
1533 Win32GetSystemMetrics (SystemMetrics.SM_CXSMSIZE),
1534 Win32GetSystemMetrics (SystemMetrics.SM_CYSMSIZE));
1537 #endregion // Static Properties
1539 #region Singleton Specific Code
1540 public static XplatUIWin32 GetInstance() {
1541 if (instance==null) {
1542 instance=new XplatUIWin32();
1548 public int Reference {
1555 #region Public Static Methods
1556 internal override IntPtr InitializeDriver() {
1560 internal override void ShutdownDriver(IntPtr token) {
1561 Console.WriteLine("XplatUIWin32 ShutdownDriver called");
1565 internal void Version() {
1566 Console.WriteLine("Xplat version $revision: $");
1569 internal override void AudibleAlert() {
1570 Win32PlaySound("Default", IntPtr.Zero, SndFlags.SND_ALIAS | SndFlags.SND_ASYNC | SndFlags.SND_NOSTOP | SndFlags.SND_NOWAIT);
1573 internal override void GetDisplaySize(out Size size) {
1576 Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
1578 size = new Size(rect.right - rect.left, rect.bottom - rect.top);
1581 internal override void EnableThemes() {
1582 themes_enabled=true;
1585 internal override IntPtr CreateWindow(CreateParams cp) {
1586 IntPtr WindowHandle;
1587 IntPtr ParentHandle;
1592 ParentHandle=cp.Parent;
1594 if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
1595 // We need to use our foster parent window until this poor child gets it's parent assigned
1596 ParentHandle = FosterParent;
1599 if ( ((cp.Style & (int)(WindowStyles.WS_CHILD | WindowStyles.WS_POPUP))==0) && ((cp.ExStyle & (int)WindowExStyles.WS_EX_APPWINDOW) == 0)) {
1600 // If we want to be hidden from the taskbar we need to be 'owned' by
1601 // something not on the taskbar. FosterParent is just that
1602 ParentHandle = FosterParent;
1606 if (cp.HasWindowManager) {
1607 location = Hwnd.GetNextStackedFormLocation (cp, Hwnd.ObjectFromHandle (cp.Parent));
1609 location = new Point (cp.X, cp.Y);
1612 string class_name = RegisterWindowClass (cp.ClassStyle);
1613 HwndCreating = hwnd;
1615 WindowHandle = Win32CreateWindow (cp.WindowExStyle, class_name, cp.Caption, cp.WindowStyle, location.X, location.Y, cp.Width, cp.Height, ParentHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
1617 HwndCreating = null;
1619 if (WindowHandle==IntPtr.Zero) {
1620 int error = Marshal.GetLastWin32Error ();
1622 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1625 hwnd.ClientWindow = WindowHandle;
1627 Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1629 return WindowHandle;
1632 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1633 CreateParams create_params = new CreateParams();
1635 create_params.Caption = "";
1636 create_params.X = X;
1637 create_params.Y = Y;
1638 create_params.Width = Width;
1639 create_params.Height = Height;
1641 create_params.ClassName=XplatUI.DefaultClassName;
1642 create_params.ClassStyle = 0;
1643 create_params.ExStyle=0;
1644 create_params.Parent=IntPtr.Zero;
1645 create_params.Param=0;
1647 return CreateWindow(create_params);
1650 internal override void DestroyWindow(IntPtr handle) {
1653 hwnd = Hwnd.ObjectFromHandle(handle);
1654 Win32DestroyWindow(handle);
1659 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1660 // We do nothing, Form has to handle WM_GETMINMAXINFO
1664 internal override FormWindowState GetWindowState(IntPtr handle) {
1667 style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1668 if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1669 return FormWindowState.Maximized;
1670 } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1671 return FormWindowState.Minimized;
1673 return FormWindowState.Normal;
1676 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1678 case FormWindowState.Normal: {
1679 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1683 case FormWindowState.Minimized: {
1684 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1688 case FormWindowState.Maximized: {
1689 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1695 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1697 Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1698 Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1701 // Certain window data is cached, so changes you make using SetWindowLong
1702 // will not take effect until you call the SetWindowPos function. Specifically,
1703 // if you change any of the frame styles, you must call SetWindowPos with
1704 // the SWP_FRAMECHANGED flag for the cache to be updated properly.
1705 if (cp.control is Form)
1706 XplatUI.RequestNCRecalc (handle);
1709 internal override double GetWindowTransparency(IntPtr handle)
1711 LayeredWindowAttributes lwa;
1715 if (0 == Win32GetLayeredWindowAttributes (handle, out clrRef, out alpha, out lwa))
1718 return ((double)alpha) / 255.0;
1721 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1722 LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1723 byte opacity = (byte)(transparency*255);
1724 COLORREF clrRef = new COLORREF();
1725 if (key != Color.Empty) {
1729 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1734 Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1737 TransparencySupport support;
1738 bool queried_transparency_support;
1739 internal override TransparencySupport SupportsTransparency() {
1740 if (queried_transparency_support)
1744 support = TransparencySupport.None;
1748 Win32SetLayeredWindowAttributes (IntPtr.Zero, new COLORREF (), 255, LayeredWindowAttributes.LWA_ALPHA);
1750 catch (EntryPointNotFoundException) { flag = false; }
1751 catch { /* swallow everything else */ }
1753 if (flag) support |= TransparencySupport.Set;
1757 LayeredWindowAttributes lwa;
1761 Win32GetLayeredWindowAttributes (IntPtr.Zero, out clrRef, out alpha, out lwa);
1763 catch (EntryPointNotFoundException) { flag = false; }
1764 catch { /* swallow everything else */ }
1766 if (flag) support |= TransparencySupport.Get;
1768 queried_transparency_support = true;
1772 internal override void UpdateWindow(IntPtr handle) {
1773 Win32UpdateWindow(handle);
1776 internal override PaintEventArgs PaintEventStart(ref Message msg, IntPtr handle, bool client) {
1779 PaintEventArgs paint_event;
1781 Rectangle clip_rect;
1784 clip_rect = new Rectangle();
1786 ps = new PAINTSTRUCT();
1788 hwnd = Hwnd.ObjectFromHandle(msg.HWnd);
1791 if (Win32GetUpdateRect(msg.HWnd, ref rect, false)) {
1792 if (handle != msg.HWnd) {
1793 // We need to validate the window where the paint message
1794 // was generated, otherwise we'll never stop getting paint
1796 Win32GetClientRect (msg.HWnd, out rect);
1797 Win32ValidateRect (msg.HWnd, ref rect);
1798 hdc = Win32GetDC (handle);
1800 hdc = Win32BeginPaint (handle, ref ps);
1804 hdc = Win32GetDC(handle);
1806 clip_rect = rect.ToRectangle ();
1808 hdc = Win32GetWindowDC (handle);
1810 // HACK this in for now
1811 Win32GetWindowRect (handle, out rect);
1812 clip_rect = new Rectangle (0, 0, rect.Width, rect.Height);
1815 // If we called BeginPaint, store the PAINTSTRUCT,
1816 // otherwise store hdc, so that PaintEventEnd can know
1817 // whether to call EndPaint or ReleaseDC.
1818 if (ps.hdc != IntPtr.Zero) {
1819 hwnd.drawing_stack.Push (ps);
1821 hwnd.drawing_stack.Push (hdc);
1824 Graphics dc = Graphics.FromHdc(hdc);
1825 hwnd.drawing_stack.Push (dc);
1827 paint_event = new PaintEventArgs(dc, clip_rect);
1832 internal override void PaintEventEnd(ref Message m, IntPtr handle, bool client) {
1835 hwnd = Hwnd.ObjectFromHandle(m.HWnd);
1837 Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1840 object o = hwnd.drawing_stack.Pop();
1842 IntPtr hdc = (IntPtr) o;
1843 Win32ReleaseDC (handle, hdc);
1844 } else if (o is PAINTSTRUCT) {
1845 PAINTSTRUCT ps = (PAINTSTRUCT) o;
1846 Win32EndPaint (handle, ref ps);
1851 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1852 Win32MoveWindow(handle, x, y, width, height, true);
1856 internal override void GetWindowPos(IntPtr handle, bool is_toplevel, out int x, out int y, out int width, out int height, out int client_width, out int client_height) {
1861 Win32GetWindowRect(handle, out rect);
1862 width = rect.right - rect.left;
1863 height = rect.bottom - rect.top;
1868 parent = Win32GetAncestor (handle, AncestorType.GA_PARENT);
1869 if (parent != IntPtr.Zero && parent != Win32GetDesktopWindow ())
1870 Win32ScreenToClient(parent, ref pt);
1875 Win32GetClientRect(handle, out rect);
1876 client_width = rect.right - rect.left;
1877 client_height = rect.bottom - rect.top;
1881 internal override void Activate(IntPtr handle) {
1882 Win32SetActiveWindow(handle);
1883 // delayed timer enabled
1885 foreach (Timer t in timer_list.Values) {
1886 if (t.Enabled && t.window == IntPtr.Zero) {
1888 int id = t.GetHashCode ();
1889 Win32SetTimer(handle, id, (uint)t.Interval, IntPtr.Zero);
1895 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1900 rect.right=rc.Right;
1901 rect.bottom=rc.Bottom;
1902 Win32InvalidateRect(handle, ref rect, clear);
1906 internal override void InvalidateNC (IntPtr handle)
1908 // found this gem at
1909 // http://www.dotnet247.com/247reference/msgs/58/292037.aspx
1910 Win32SetWindowPos(handle, IntPtr.Zero,
1912 SetWindowPosFlags.SWP_NOMOVE |
1913 SetWindowPosFlags.SWP_NOSIZE |
1914 SetWindowPosFlags.SWP_NOZORDER |
1915 SetWindowPosFlags.SWP_NOACTIVATE |
1916 SetWindowPosFlags.SWP_DRAWFRAME);
1919 private IntPtr InternalWndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
1921 if (HwndCreating != null && HwndCreating.ClientWindow == IntPtr.Zero)
1922 HwndCreating.ClientWindow = hWnd;
1923 return NativeWindow.WndProc (hWnd, msg, wParam, lParam);
1926 internal override IntPtr DefWndProc(ref Message msg) {
1927 msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1931 internal override void HandleException(Exception e) {
1932 StackTrace st = new StackTrace(e);
1933 Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1934 Console.WriteLine("{0}{1}", e.Message, st.ToString());
1937 internal override void DoEvents() {
1938 MSG msg = new MSG();
1940 if (override_cursor != IntPtr.Zero) {
1941 Cursor.Current = null;
1944 while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1945 XplatUI.TranslateMessage(ref msg);
1946 XplatUI.DispatchMessage(ref msg);
1950 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1951 return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1954 internal override void PostQuitMessage(int exitCode) {
1955 Win32PostQuitMessage(exitCode);
1958 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
1960 if (wm_nc_registered == null)
1961 wm_nc_registered = new Hashtable ();
1963 TMEFlags flags = TMEFlags.TME_NONCLIENT;
1965 flags |= TMEFlags.TME_HOVER;
1967 flags |= TMEFlags.TME_LEAVE;
1969 if (flags == TMEFlags.TME_NONCLIENT) {
1970 if (wm_nc_registered.Contains (hwnd)) {
1971 wm_nc_registered.Remove (hwnd);
1974 if (!wm_nc_registered.Contains (hwnd)) {
1975 wm_nc_registered.Add (hwnd, flags);
1977 wm_nc_registered [hwnd] = flags;
1982 internal override void RequestNCRecalc(IntPtr handle) {
1983 Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOACTIVATE);
1986 internal override void ResetMouseHover(IntPtr handle) {
1987 TRACKMOUSEEVENT tme;
1989 tme = new TRACKMOUSEEVENT();
1990 tme.size = Marshal.SizeOf(tme);
1992 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1993 Win32TrackMouseEvent(ref tme);
1997 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1998 return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
2001 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
2005 if (RetrieveMessage(ref msg)) {
2010 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
2012 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
2018 // We need to fake WM_MOUSE_ENTER
2019 switch (msg.message) {
2020 case Msg.WM_LBUTTONDOWN: {
2021 mouse_state |= MouseButtons.Left;
2025 case Msg.WM_MBUTTONDOWN: {
2026 mouse_state |= MouseButtons.Middle;
2030 case Msg.WM_RBUTTONDOWN: {
2031 mouse_state |= MouseButtons.Right;
2035 case Msg.WM_LBUTTONUP: {
2036 mouse_state &= ~MouseButtons.Left;
2040 case Msg.WM_MBUTTONUP: {
2041 mouse_state &= ~MouseButtons.Middle;
2045 case Msg.WM_RBUTTONUP: {
2046 mouse_state &= ~MouseButtons.Right;
2050 case Msg.WM_ASYNC_MESSAGE: {
2051 XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
2055 case Msg.WM_MOUSEMOVE: {
2056 if (msg.hwnd != prev_mouse_hwnd) {
2057 TRACKMOUSEEVENT tme;
2059 // The current message will be sent out next time around
2060 StoreMessage(ref msg);
2062 // This is the message we want to send at this point
2063 msg.message = Msg.WM_MOUSE_ENTER;
2065 prev_mouse_hwnd = msg.hwnd;
2067 tme = new TRACKMOUSEEVENT();
2068 tme.size = Marshal.SizeOf(tme);
2069 tme.hWnd = msg.hwnd;
2070 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
2071 Win32TrackMouseEvent(ref tme);
2077 case Msg.WM_NCMOUSEMOVE: {
2078 if (wm_nc_registered == null || !wm_nc_registered.Contains (msg.hwnd))
2081 TRACKMOUSEEVENT tme;
2083 tme = new TRACKMOUSEEVENT ();
2084 tme.size = Marshal.SizeOf(tme);
2085 tme.hWnd = msg.hwnd;
2086 tme.dwFlags = (TMEFlags)wm_nc_registered[msg.hwnd];
2087 Win32TrackMouseEvent (ref tme);
2091 case Msg.WM_DROPFILES: {
2092 return Win32DnD.HandleWMDropFiles(ref msg);
2095 case Msg.WM_MOUSELEAVE: {
2096 prev_mouse_hwnd = IntPtr.Zero;
2100 case Msg.WM_TIMER: {
2101 Timer timer=(Timer)timer_list[(int)msg.wParam];
2103 if (timer != null) {
2113 internal override bool TranslateMessage(ref MSG msg) {
2114 return Win32TranslateMessage(ref msg);
2117 internal override IntPtr DispatchMessage(ref MSG msg) {
2118 return Win32DispatchMessage(ref msg);
2121 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
2123 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2125 } else if (!Bottom) {
2126 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2128 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2134 internal override bool SetTopmost(IntPtr hWnd, bool Enabled) {
2136 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
2139 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
2144 internal override bool SetOwner(IntPtr hWnd, IntPtr hWndOwner) {
2145 Win32SetWindowLong(hWnd, WindowLong.GWL_HWNDPARENT, (uint) hWndOwner);
2149 internal override bool Text(IntPtr handle, string text) {
2150 Win32SetWindowText(handle, text);
2154 internal override bool GetText(IntPtr handle, out string text) {
2157 sb = new StringBuilder(256);
2158 Win32GetWindowText(handle, sb, sb.Capacity);
2159 text = sb.ToString();
2163 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
2166 Control c = Control.FromHandle (handle);
2170 f = (Form)Control.FromHandle (handle);
2171 WindowPlacementFlags flags = WindowPlacementFlags.SW_SHOWNORMAL;
2172 switch (f.WindowState) {
2173 case FormWindowState.Normal: flags = WindowPlacementFlags.SW_SHOWNORMAL; break;
2174 case FormWindowState.Minimized: flags = WindowPlacementFlags.SW_MINIMIZE; break;
2175 case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
2178 if (!f.ActivateOnShow)
2179 flags = WindowPlacementFlags.SW_SHOWNOACTIVATE;
2181 Win32ShowWindow (handle, flags);
2184 if (c.ActivateOnShow)
2185 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
2187 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
2191 Win32ShowWindow (handle, WindowPlacementFlags.SW_HIDE);
2196 internal override bool IsEnabled(IntPtr handle) {
2197 return IsWindowEnabled (handle);
2200 internal override bool IsKeyLocked (VirtualKeys key)
2202 return (Win32GetKeyState (key) & 1) == 1;
2205 internal override bool IsVisible(IntPtr handle) {
2206 return IsWindowVisible (handle);
2209 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
2210 Control c = Control.FromHandle (handle);
2211 if (parent == IntPtr.Zero) {
2213 Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
2217 SetVisible (handle, c.is_visible, true);
2220 // The Win32SetParent is lame, it can very well move the window
2221 // ref: http://groups.google.com/group/microsoft.public.vb.winapi/browse_thread/thread/1b82ccc54231ecee/afa82835bfc0422a%23afa82835bfc0422a
2222 // Here we save the position before changing the parent, and if it has changed afterwards restore it.
2223 // Another possibility would be to intercept WM_WINDOWPOSCHANGING and restore the coords there, but this would require plumbing in weird places
2224 // (either inside Control or add handling to InternalWndProc)
2225 // We also need to remove WS_CHILD if making the window parent-less, and add it if we're parenting it.
2228 WindowStyles style, new_style;
2230 Win32GetWindowRect (handle, out rect);
2231 style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
2233 if (parent == IntPtr.Zero) {
2234 new_style = style & ~WindowStyles.WS_CHILD;
2235 result = Win32SetParent (handle, FosterParent);
2237 new_style = style | WindowStyles.WS_CHILD;
2238 result = Win32SetParent (handle, parent);
2240 if (style != new_style && c is Form) {
2241 Win32SetWindowLong (handle, WindowLong.GWL_STYLE, (uint) new_style);
2243 Win32GetWindowRect (handle, out rect2);
2244 if (rect.top != rect2.top && rect.left != rect2.left && c is Form) {
2245 Win32SetWindowPos (handle, IntPtr.Zero, rect.top, rect.left, rect.Width, rect.Height, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOREDRAW | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOENDSCHANGING | SetWindowPosFlags.SWP_NOACTIVATE);
2250 // If we ever start using this, we should probably replace FosterParent with IntPtr.Zero
2251 internal override IntPtr GetParent(IntPtr handle) {
2252 return Win32GetParent(handle);
2255 // This is a nop on win32 and x11
2256 internal override IntPtr GetPreviousWindow(IntPtr handle) {
2260 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
2262 Win32SetCapture(hWnd);
2264 if (ConfineToHwnd != IntPtr.Zero) {
2266 Win32GetWindowRect (ConfineToHwnd, out window_rect);
2267 Win32GetClipCursor (out clipped_cursor_rect);
2268 Win32ClipCursor (ref window_rect);
2272 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
2274 GrabConfined = grab_confined;
2275 GrabArea = grab_area;
2278 internal override void UngrabWindow(IntPtr hWnd) {
2279 if (!(clipped_cursor_rect.top == 0 && clipped_cursor_rect.bottom == 0 && clipped_cursor_rect.left == 0 && clipped_cursor_rect.right == 0)) {
2280 Win32ClipCursor (ref clipped_cursor_rect);
2281 clipped_cursor_rect = new RECT ();
2284 Win32ReleaseCapture();
2285 grab_hwnd = IntPtr.Zero;
2288 internal override bool CalculateWindowRect(ref Rectangle ClientRect, CreateParams cp, Menu menu, out Rectangle WindowRect) {
2291 rect.left=ClientRect.Left;
2292 rect.top=ClientRect.Top;
2293 rect.right=ClientRect.Right;
2294 rect.bottom=ClientRect.Bottom;
2296 if (!Win32AdjustWindowRectEx(ref rect, cp.Style, menu != null, cp.ExStyle)) {
2297 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
2301 WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
2305 internal override void SetCursor(IntPtr window, IntPtr cursor) {
2306 Win32SetCursor(cursor);
2310 internal override void ShowCursor(bool show) {
2311 Win32ShowCursor(show);
2314 internal override void OverrideCursor(IntPtr cursor) {
2315 Win32SetCursor(cursor);
2318 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
2320 Bitmap cursor_bitmap;
2328 // Win32 only allows creation cursors of a certain size
2329 if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
2330 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
2331 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
2333 cursor_bitmap = bitmap;
2337 width = cursor_bitmap.Width;
2338 height = cursor_bitmap.Height;
2340 cursor_bits = new Byte[(width / 8) * height];
2341 mask_bits = new Byte[(width / 8) * height];
2343 for (int y = 0; y < height; y++) {
2344 for (int x = 0; x < width; x++) {
2345 pixel = cursor_bitmap.GetPixel(x, y);
2347 if (pixel == cursor_pixel) {
2348 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
2351 pixel = cursor_mask.GetPixel(x, y);
2353 if (pixel == mask_pixel) {
2354 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
2359 cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
2364 internal override Bitmap DefineStdCursorBitmap (StdCursor id)
2366 // We load the cursor, create a bitmap, draw the cursor onto the bitmap and return the bitmap.
2367 IntPtr cursor = DefineStdCursor (id);
2368 // Windows only have one possible cursor size!
2369 int width = Win32GetSystemMetrics (SystemMetrics.SM_CXCURSOR);
2370 int height = Win32GetSystemMetrics (SystemMetrics.SM_CYCURSOR);
2371 Bitmap bmp = new Bitmap (width, height);
2372 Graphics gc = Graphics.FromImage (bmp);
2373 IntPtr hdc = gc.GetHdc ();
2374 Win32DrawIcon (hdc, 0, 0, cursor);
2375 gc.ReleaseHdc (hdc);
2380 [MonoTODO("Define the missing cursors")]
2381 internal override IntPtr DefineStdCursor(StdCursor id) {
2383 case StdCursor.AppStarting: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
2384 case StdCursor.Arrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2385 case StdCursor.Cross: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
2386 case StdCursor.Default: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2387 case StdCursor.Hand: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
2388 case StdCursor.Help: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
2389 case StdCursor.HSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2390 case StdCursor.IBeam: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
2391 case StdCursor.No: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
2392 case StdCursor.NoMove2D: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2393 case StdCursor.NoMoveHoriz: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2394 case StdCursor.NoMoveVert: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2395 case StdCursor.PanEast: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2396 case StdCursor.PanNE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2397 case StdCursor.PanNorth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2398 case StdCursor.PanNW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2399 case StdCursor.PanSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2400 case StdCursor.PanSouth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2401 case StdCursor.PanSW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2402 case StdCursor.PanWest: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2403 case StdCursor.SizeAll: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
2404 case StdCursor.SizeNESW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
2405 case StdCursor.SizeNS: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
2406 case StdCursor.SizeNWSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
2407 case StdCursor.SizeWE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
2408 case StdCursor.UpArrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
2409 case StdCursor.VSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2410 case StdCursor.WaitCursor: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
2412 throw new NotImplementedException ();
2415 internal override void DestroyCursor(IntPtr cursor) {
2416 if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
2417 Win32DestroyCursor(cursor);
2422 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
2423 ICONINFO ii = new ICONINFO ();
2425 if (!Win32GetIconInfo (cursor, out ii))
2426 throw new Win32Exception ();
2430 hotspot_x = ii.xHotspot;
2431 hotspot_y = ii.yHotspot;
2434 internal override void SetCursorPos(IntPtr handle, int x, int y) {
2435 Win32SetCursorPos(x, y);
2438 internal override Region GetClipRegion(IntPtr hwnd) {
2441 region = new Region();
2443 Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
2448 internal override void SetClipRegion(IntPtr hwnd, Region region) {
2450 Win32SetWindowRgn (hwnd, IntPtr.Zero, true);
2452 Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
2455 internal override void EnableWindow(IntPtr handle, bool Enable) {
2456 Win32EnableWindow(handle, Enable);
2459 internal override void EndLoop(System.Threading.Thread thread) {
2463 internal override object StartLoop(System.Threading.Thread thread) {
2467 internal override void SetModal(IntPtr handle, bool Modal) {
2468 // we do nothing on Win32
2471 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
2474 Win32GetCursorPos(out pt);
2476 if (handle!=IntPtr.Zero) {
2477 Win32ScreenToClient(handle, ref pt);
2484 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
2486 POINT pnt = new POINT();
2490 Win32ScreenToClient (handle, ref pnt);
2496 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {
2497 POINT pnt = new POINT();
2502 Win32ClientToScreen(handle, ref pnt);
2508 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
2511 Win32GetWindowRect(handle, out rect);
2512 x -= rect.left + SystemInformation.FrameBorderSize.Width;
2513 y -= rect.top + SystemInformation.FrameBorderSize.Height;
2515 WindowStyles style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
2516 if (CreateParams.IsSet (style, WindowStyles.WS_CAPTION)) {
2517 y -= ThemeEngine.Current.CaptionHeight;
2521 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {
2524 Win32GetWindowRect(handle, out rect);
2525 x += rect.left + SystemInformation.FrameBorderSize.Width;
2526 y += rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
2530 internal override void SendAsyncMethod (AsyncMethodData method)
2532 Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
2535 internal override void SetTimer (Timer timer)
2539 index = timer.GetHashCode();
2542 timer_list[index]=timer;
2545 if (Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero) == IntPtr.Zero)
2546 timer.window = FosterParent;
2548 timer.window = IntPtr.Zero;
2551 internal override void KillTimer (Timer timer)
2555 index = timer.GetHashCode();
2557 Win32KillTimer(timer.window, index);
2560 timer_list.Remove(index);
2564 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
2565 Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
2566 caret_visible = false;
2569 internal override void DestroyCaret(IntPtr hwnd) {
2570 Win32DestroyCaret();
2573 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
2574 Win32SetCaretPos(x, y);
2577 internal override void CaretVisible(IntPtr hwnd, bool visible) {
2579 if (!caret_visible) {
2580 Win32ShowCaret(hwnd);
2581 caret_visible = true;
2584 if (caret_visible) {
2585 Win32HideCaret(hwnd);
2586 caret_visible = false;
2591 internal override IntPtr GetFocus() {
2592 return Win32GetFocus();
2595 internal override void SetFocus(IntPtr hwnd) {
2596 Win32SetFocus(hwnd);
2599 internal override IntPtr GetActive() {
2600 return Win32GetActiveWindow();
2603 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
2608 tm = new TEXTMETRIC();
2610 dc = Win32GetDC (IntPtr.Zero);
2611 prevobj = Win32SelectObject (dc, font.ToHfont ());
2613 if (Win32GetTextMetrics (dc, ref tm) == false) {
2614 prevobj = Win32SelectObject (dc, prevobj);
2615 Win32DeleteObject (prevobj);
2616 Win32ReleaseDC (IntPtr.Zero, dc);
2621 prevobj = Win32SelectObject (dc, prevobj);
2622 Win32DeleteObject (prevobj);
2623 Win32ReleaseDC (IntPtr.Zero, dc);
2625 ascent = tm.tmAscent;
2626 descent = tm.tmDescent;
2631 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
2635 rect.left = rectangle.X;
2636 rect.top = rectangle.Y;
2637 rect.right = rectangle.Right;
2638 rect.bottom = rectangle.Bottom;
2640 Win32ScrollWindowEx(hwnd, XAmount, YAmount, IntPtr.Zero, ref rect, IntPtr.Zero, IntPtr.Zero, ScrollWindowExFlags.SW_INVALIDATE | ScrollWindowExFlags.SW_ERASE | (with_children ? ScrollWindowExFlags.SW_SCROLLCHILDREN : ScrollWindowExFlags.SW_NONE));
2641 Win32UpdateWindow(hwnd);
2644 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
2645 Win32ScrollWindowEx(hwnd, XAmount, YAmount, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ScrollWindowExFlags.SW_INVALIDATE | ScrollWindowExFlags.SW_ERASE | (with_children ? ScrollWindowExFlags.SW_SCROLLCHILDREN : ScrollWindowExFlags.SW_NONE));
2648 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
2651 nid = new NOTIFYICONDATA();
2653 nid.cbSize = (uint)Marshal.SizeOf(nid);
2656 nid.uCallbackMessage = (uint)Msg.WM_USER;
2657 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2661 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2665 nid.hIcon = icon.Handle;
2666 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2671 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
2674 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
2677 nid = new NOTIFYICONDATA();
2679 nid.cbSize = (uint)Marshal.SizeOf(nid);
2680 nid.hIcon = icon.Handle;
2683 nid.uCallbackMessage = (uint)Msg.WM_USER;
2684 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2688 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2692 nid.hIcon = icon.Handle;
2693 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2696 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2699 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
2702 nid = new NOTIFYICONDATA();
2704 nid.cbSize = (uint)Marshal.SizeOf(nid);
2709 Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
2713 internal override void SystrayBalloon(IntPtr hwnd, int timeout, string title, string text, ToolTipIcon icon)
2717 nid = new NOTIFYICONDATA();
2719 nid.cbSize = (uint)Marshal.SizeOf(nid);
2722 nid.uFlags = NotifyIconFlags.NIF_INFO;
2723 nid.uTimeoutOrVersion = timeout;
2724 nid.szInfoTitle = title;
2726 nid.dwInfoFlags = icon;
2728 Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2732 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
2733 // Nothing to do on Win32
2736 internal override void SetMenu(IntPtr handle, Menu menu) {
2737 // Trigger WM_NCCALC
2738 Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2741 internal override Point GetMenuOrigin(IntPtr handle) {
2742 Form form = Control.FromHandle (handle) as Form;
2744 Hwnd.Borders borders = Hwnd.GetBorders (form.GetCreateParams (), null);
2745 return new Point(borders.left, borders.top);
2747 return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2750 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2751 Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : icon.Handle); // 1 = large icon (0 would be small)
2754 internal override void ClipboardClose(IntPtr handle) {
2755 if (handle != clip_magic) {
2756 throw new ArgumentException("handle is not a valid clipboard handle");
2758 Win32CloseClipboard();
2761 internal override int ClipboardGetID(IntPtr handle, string format) {
2762 if (handle != clip_magic) {
2763 throw new ArgumentException("handle is not a valid clipboard handle");
2765 if (format == "Text" ) return 1;
2766 else if (format == "Bitmap" ) return 2;
2767 else if (format == "MetaFilePict" ) return 3;
2768 else if (format == "SymbolicLink" ) return 4;
2769 else if (format == "DataInterchangeFormat" ) return 5;
2770 else if (format == "Tiff" ) return 6;
2771 else if (format == "OEMText" ) return 7;
2772 else if (format == "DeviceIndependentBitmap" ) return 8;
2773 else if (format == "Palette" ) return 9;
2774 else if (format == "PenData" ) return 10;
2775 else if (format == "RiffAudio" ) return 11;
2776 else if (format == "WaveAudio" ) return 12;
2777 else if (format == "UnicodeText" ) return 13;
2778 else if (format == "EnhancedMetafile" ) return 14;
2779 else if (format == "FileDrop" ) return 15;
2780 else if (format == "Locale" ) return 16;
2782 return (int)Win32RegisterClipboardFormat(format);
2785 internal override IntPtr ClipboardOpen(bool primary_selection) {
2786 // Win32 does not have primary selection
2787 Win32OpenClipboard(FosterParent);
2791 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2796 if (handle != clip_magic) {
2804 format = Win32EnumClipboardFormats(format);
2808 } while (format != 0);
2811 result = new int[count];
2815 format = Win32EnumClipboardFormats(format);
2817 result[count++] = (int)format;
2819 } while (format != 0);
2825 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2830 if (handle != clip_magic) {
2831 throw new ArgumentException("handle is not a valid clipboard handle");
2834 hmem = Win32GetClipboardData((uint)type);
2835 if (hmem == IntPtr.Zero) {
2839 data = Win32GlobalLock(hmem);
2840 if (data == IntPtr.Zero) {
2841 uint error = Win32GetLastError();
2842 Console.WriteLine("Error: {0}", error);
2848 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2849 obj = AnsiToString(data);
2850 } else switch ((ClipboardFormats)type) {
2851 case ClipboardFormats.CF_TEXT: {
2852 obj = AnsiToString(data);
2856 case ClipboardFormats.CF_DIB: {
2857 obj = DIBtoImage(data);
2861 case ClipboardFormats.CF_UNICODETEXT: {
2862 obj = UnicodeToString(data);
2867 if (converter != null && !converter(type, data, out obj)) {
2873 Win32GlobalUnlock(hmem);
2879 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
2884 if (handle != clip_magic) {
2885 throw new ArgumentException("handle is not a valid clipboard handle");
2890 if (!Win32EmptyClipboard())
2891 throw new ExternalException("Win32EmptyClipboard");
2896 if (obj is string) {
2897 type = (int)ClipboardFormats.CF_UNICODETEXT;
2898 } else if (obj is Image) {
2899 type = (int)ClipboardFormats.CF_DIB;
2903 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2904 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2905 if (Win32SetClipboardData((uint)type, hmem) == IntPtr.Zero )
2906 throw new ExternalException("Win32SetClipboardData");
2908 } else switch((ClipboardFormats)type) {
2909 case ClipboardFormats.CF_UNICODETEXT: {
2910 hmem = Marshal.StringToHGlobalUni((string)obj);
2911 if (Win32SetClipboardData((uint)type, hmem) == IntPtr.Zero)
2912 throw new ExternalException("Win32SetClipboardData");
2916 case ClipboardFormats.CF_TEXT: {
2917 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2918 if (Win32SetClipboardData((uint)type, hmem) == IntPtr.Zero)
2919 throw new ExternalException("Win32SetClipboardData");
2923 case ClipboardFormats.CF_BITMAP:
2924 case ClipboardFormats.CF_DIB: {
2925 data = ImageToDIB((Image)obj);
2927 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2928 hmem_ptr = Win32GlobalLock(hmem);
2929 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2930 Win32GlobalUnlock(hmem);
2931 if (Win32SetClipboardData((uint)ClipboardFormats.CF_DIB, hmem) == IntPtr.Zero)
2932 throw new ExternalException("Win32SetClipboardData");
2937 if (converter != null && converter(ref type, obj, out data)) {
2938 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2939 hmem_ptr = Win32GlobalLock(hmem);
2940 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2941 Win32GlobalUnlock(hmem);
2942 if (Win32SetClipboardData((uint)type, hmem) == IntPtr.Zero)
2943 throw new ExternalException("Win32SetClipboardData");
2950 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
2952 Win32DnD.RegisterDropTarget(hwnd);
2954 Win32DnD.UnregisterDropTarget(hwnd);
2958 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
2959 return Win32DnD.StartDrag(hwnd, data, allowedEffects);
2962 // XXX this doesn't work at all for FrameStyle.Dashed - it draws like Thick, and in the Thick case
2963 // the corners are drawn incorrectly.
2964 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
2968 COLORREF clrRef = new COLORREF();
2970 // If we want the standard hatch pattern we would
2971 // need to create a brush
2973 clrRef.R = backColor.R;
2974 clrRef.G = backColor.G;
2975 clrRef.B = backColor.B;
2978 pen = Win32CreatePen (style == FrameStyle.Thick ? PenStyle.PS_SOLID : PenStyle.PS_DASH,
2979 style == FrameStyle.Thick ? 4 : 2, ref clrRef);
2981 hdc = Win32GetDC(IntPtr.Zero);
2982 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2983 oldpen = Win32SelectObject(hdc, pen);
2985 Win32MoveToEx(hdc, rectangle.Left, rectangle.Top, IntPtr.Zero);
2986 if ((rectangle.Width > 0) && (rectangle.Height > 0)) {
2987 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2988 Win32LineTo(hdc, rectangle.Right, rectangle.Bottom);
2989 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2990 Win32LineTo(hdc, rectangle.Left, rectangle.Top);
2992 if (rectangle.Width > 0) {
2993 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2995 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2999 Win32SelectObject(hdc, oldpen);
3000 Win32DeleteObject(pen);
3002 Win32ReleaseDC(IntPtr.Zero, hdc);
3005 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
3010 COLORREF clrRef = new COLORREF();
3015 Win32ClientToScreen(IntPtr.Zero, ref pt);
3017 // If we want the standard hatch pattern we would
3018 // need to create a brush
3020 clrRef.R = backColor.R;
3021 clrRef.G = backColor.G;
3022 clrRef.B = backColor.B;
3025 pen = Win32CreatePen(PenStyle.PS_SOLID, 1, ref clrRef);
3027 hdc = Win32GetDC(IntPtr.Zero);
3028 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
3029 oldpen = Win32SelectObject(hdc, pen);
3031 Win32MoveToEx(hdc, pt.x + start.X, pt.y + start.Y, IntPtr.Zero);
3032 Win32LineTo(hdc, pt.x + end.X, pt.y + end.Y);
3034 Win32SelectObject(hdc, oldpen);
3035 Win32DeleteObject(pen);
3037 Win32ReleaseDC(IntPtr.Zero, hdc);
3040 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
3045 rect.left = rectangle.Left;
3046 rect.top = rectangle.Top;
3047 rect.right = rectangle.Right;
3048 rect.bottom = rectangle.Bottom;
3053 COLORREF clrRef = new COLORREF();
3055 clrRef.R = backColor.R;
3056 clrRef.G = backColor.G;
3057 clrRef.B = backColor.B;
3060 brush = Win32CreateSolidBrush (clrRef);
3062 hdc = Win32GetDC(IntPtr.Zero);
3063 oldbrush = Win32SelectObject(hdc, brush);
3065 Win32PatBlt (hdc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, PatBltRop.DSTINVERT);
3067 Win32SelectObject(hdc, oldbrush);
3068 Win32DeleteObject(brush);
3070 Win32ReleaseDC(IntPtr.Zero, hdc);
3073 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
3082 Win32ClientToScreen(handle, ref pt);
3084 // If we want the standard hatch pattern we would
3085 // need to create a brush
3088 pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
3090 hdc = Win32GetDC(IntPtr.Zero);
3091 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
3092 oldpen = Win32SelectObject(hdc, pen);
3094 Control c = Control.FromHandle (handle);
3097 Win32GetWindowRect (c.Handle, out window_rect);
3098 Region r = new Region (new Rectangle(window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top));
3099 Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
3102 Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
3103 if ((rect.Width > 0) && (rect.Height > 0)) {
3104 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
3105 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
3106 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
3107 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
3109 if (rect.Width > 0) {
3110 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
3112 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
3116 Win32SelectObject(hdc, oldpen);
3117 Win32DeleteObject(pen);
3119 Win32ExtSelectClipRgn(hdc, IntPtr.Zero, (int) ClipCombineMode.RGN_COPY);
3121 Win32ReleaseDC(IntPtr.Zero, hdc);
3124 internal override SizeF GetAutoScaleSize(Font font) {
3127 string magic_string = "The quick brown fox jumped over the lazy dog.";
3128 double magic_number = 44.549996948242189;
3130 g = Graphics.FromHwnd(FosterParent);
3132 width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
3133 return new SizeF(width, font.Height);
3136 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
3137 return Win32SendMessage(hwnd, message, wParam, lParam);
3140 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
3141 return Win32PostMessage(hwnd, message, wParam, lParam);
3144 internal override int SendInput (IntPtr hwnd, Queue keys) {
3145 INPUT[] inputs = new INPUT[keys.Count];
3146 const Int32 INPUT_KEYBOARD = 1;
3149 while (keys.Count > 0) {
3150 MSG msg = (MSG)keys.Dequeue();
3153 inputs[i].ki.wScan = 0;
3154 inputs[i].ki.time = 0;
3155 inputs[i].ki.dwFlags = (Int32)(msg.message == Msg.WM_KEYUP ? InputFlags.KEYEVENTF_KEYUP : 0);
3156 inputs[i].ki.wVk = (short)msg.wParam.ToInt32();
3157 inputs[i].type = INPUT_KEYBOARD;
3160 returns = Win32SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
3162 return (int) returns;
3165 internal override int KeyboardSpeed {
3168 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDSPEED, 0, ref speed, 0);
3170 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
3176 internal override int KeyboardDelay {
3179 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDDELAY, 0, ref delay, 0);
3181 // Return values must range from 0 to 4, 0 meaning 250ms,
3182 // and 4 meaning 1000 ms.
3188 private class WinBuffer
3191 public IntPtr bitmap;
3193 public WinBuffer (IntPtr hdc, IntPtr bitmap)
3196 this.bitmap = bitmap;
3200 internal override void CreateOffscreenDrawable (IntPtr handle, int width, int height, out object offscreen_drawable)
3202 Graphics destG = Graphics.FromHwnd (handle);
3203 IntPtr destHdc = destG.GetHdc ();
3205 IntPtr srcHdc = Win32CreateCompatibleDC (destHdc);
3206 IntPtr srcBmp = Win32CreateCompatibleBitmap (destHdc, width, height);
3207 Win32SelectObject (srcHdc, srcBmp);
3209 offscreen_drawable = new WinBuffer (srcHdc, srcBmp);
3211 destG.ReleaseHdc (destHdc);
3214 internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
3216 return Graphics.FromHdc (((WinBuffer)offscreen_drawable).hdc);
3219 internal override void BlitFromOffscreen (IntPtr dest_handle, Graphics dest_dc, object offscreen_drawable, Graphics offscreen_dc, Rectangle r)
3221 WinBuffer wb = (WinBuffer)offscreen_drawable;
3223 IntPtr destHdc = dest_dc.GetHdc ();
3224 Win32BitBlt (destHdc, r.Left, r.Top, r.Width, r.Height, wb.hdc, r.Left, r.Top, TernaryRasterOperations.SRCCOPY);
3225 dest_dc.ReleaseHdc (destHdc);
3228 internal override void DestroyOffscreenDrawable (object offscreen_drawable)
3230 WinBuffer wb = (WinBuffer)offscreen_drawable;
3232 Win32DeleteObject (wb.bitmap);
3233 Win32DeleteDC (wb.hdc);
3236 internal override void SetForegroundWindow (IntPtr handle)
3238 Win32SetForegroundWindow(handle);
3241 internal override event EventHandler Idle;
3242 #endregion // Public Static Methods
3244 #region Win32 Imports
3245 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
3246 private extern static uint Win32GetLastError();
3248 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3249 internal extern static IntPtr Win32CreateWindow(WindowExStyles dwExStyle, string lpClassName, string lpWindowName, WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lParam);
3251 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
3252 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
3254 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3255 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
3257 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3258 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
3260 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
3261 internal extern static bool Win32TranslateMessage(ref MSG msg);
3263 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3264 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
3266 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
3267 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
3269 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
3270 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
3272 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
3273 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
3275 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3276 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
3278 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3279 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
3281 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
3282 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
3284 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3285 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
3287 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3288 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
3290 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
3291 private extern static IntPtr Win32ShowCursor(bool bShow);
3293 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
3294 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
3296 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
3297 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
3299 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
3300 private extern static bool Win32DestroyCursor(IntPtr hCursor);
3302 [DllImport ("user32.dll", EntryPoint = "DrawIcon", CallingConvention = CallingConvention.StdCall)]
3303 private extern static bool Win32DrawIcon (IntPtr hDC, int X, int Y, IntPtr hIcon);
3305 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3306 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
3308 //[DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3309 //private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
3311 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
3312 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
3314 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
3315 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
3317 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
3318 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
3320 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
3321 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
3323 [DllImport ("user32.dll", EntryPoint = "ValidateRect", CallingConvention = CallingConvention.StdCall)]
3324 private extern static IntPtr Win32ValidateRect (IntPtr hWnd, ref RECT rect);
3326 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
3327 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
3329 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
3330 private extern static IntPtr Win32GetDC(IntPtr hWnd);
3332 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
3333 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
3335 //[DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
3336 //private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
3338 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
3339 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
3341 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3342 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
3344 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
3345 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
3347 //[DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
3348 //private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
3350 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
3351 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
3353 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
3354 private extern static IntPtr Win32ReleaseCapture();
3356 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
3357 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
3359 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
3360 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
3362 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
3363 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
3365 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
3366 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
3368 // This function returns the parent OR THE OWNER!
3369 // Use GetAncestor to only get the parent.
3370 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
3371 private extern static IntPtr Win32GetParent(IntPtr hWnd);
3373 [DllImport ("user32.dll", EntryPoint = "GetAncestor", CallingConvention = CallingConvention.StdCall)]
3374 private extern static IntPtr Win32GetAncestor (IntPtr hWnd, AncestorType flags);
3376 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3377 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
3379 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
3380 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
3382 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
3383 private extern static bool Win32GetCursorPos(out POINT lpPoint);
3385 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
3386 private extern static bool Win32SetCursorPos(int x, int y);
3388 //[DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
3389 //private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
3391 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
3392 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
3394 //[DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
3395 //private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
3397 [DllImport ("gdi32.dll", EntryPoint="CreateSolidBrush", CallingConvention=CallingConvention.StdCall)]
3398 private extern static IntPtr Win32CreateSolidBrush(COLORREF clrRef);
3400 [DllImport ("gdi32.dll", EntryPoint="PatBlt", CallingConvention=CallingConvention.StdCall)]
3401 private extern static int Win32PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, PatBltRop dwRop);
3403 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
3404 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
3406 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
3407 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
3409 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
3410 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
3412 [DllImport ("user32.dll", EntryPoint="GetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
3413 private extern static uint Win32GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out LayeredWindowAttributes pwdFlags);
3415 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
3416 public extern static bool Win32DeleteObject(IntPtr o);
3418 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
3419 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
3421 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
3422 private extern static IntPtr Win32GetDesktopWindow();
3424 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
3425 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
3427 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
3428 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
3430 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
3431 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
3433 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
3434 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
3436 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
3437 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
3439 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
3440 internal extern static IntPtr Win32GetFocus();
3442 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
3443 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
3445 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
3446 private extern static bool Win32DestroyCaret();
3448 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
3449 private extern static bool Win32ShowCaret(IntPtr hwnd);
3451 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
3452 private extern static bool Win32HideCaret(IntPtr hwnd);
3454 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
3455 private extern static bool Win32SetCaretPos(int X, int Y);
3457 //[DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
3458 //private extern static uint Win32GetCaretBlinkTime();
3460 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3461 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
3463 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
3464 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
3466 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3467 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3469 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3470 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3472 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3473 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3475 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3476 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3478 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3479 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3481 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3482 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3484 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3485 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3487 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3488 private extern static IntPtr Win32GetActiveWindow();
3490 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
3491 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
3493 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3494 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
3496 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
3497 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
3499 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
3500 private extern static bool IsWindowEnabled(IntPtr hwnd);
3502 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
3503 private extern static bool IsWindowVisible(IntPtr hwnd);
3505 //[DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
3506 //private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
3508 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3509 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3511 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3512 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3514 [DllImport ("user32.dll", EntryPoint="SendInput", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3515 private extern static UInt32 Win32SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray)] INPUT[] inputs, Int32 cbSize);
3517 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3518 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
3520 //[DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3521 //private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
3523 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3524 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
3526 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3527 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref bool value, uint fWinIni);
3529 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3530 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref ANIMATIONINFO value, uint fWinIni);
3532 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
3533 private extern static bool Win32OpenClipboard(IntPtr hwnd);
3535 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
3536 private extern static bool Win32EmptyClipboard();
3538 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3539 private extern static uint Win32RegisterClipboardFormat(string format);
3541 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
3542 private extern static bool Win32CloseClipboard();
3544 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
3545 private extern static uint Win32EnumClipboardFormats(uint format);
3547 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
3548 private extern static IntPtr Win32GetClipboardData(uint format);
3550 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
3551 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
3553 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
3554 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
3556 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
3557 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
3559 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
3560 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
3562 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
3563 internal extern static uint Win32GlobalSize(IntPtr hMem);
3565 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
3566 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
3568 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
3569 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
3571 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
3572 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
3574 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3575 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
3577 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3578 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
3580 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
3581 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
3583 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3584 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
3586 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3587 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
3589 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
3590 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
3592 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3593 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
3595 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3596 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
3598 [DllImport("gdi32.dll", EntryPoint = "ExcludeClipRect", CallingConvention = CallingConvention.StdCall)]
3599 internal extern static int Win32ExcludeClipRect (IntPtr hdc, int left, int top, int right, int bottom);
3601 [DllImport ("gdi32.dll", EntryPoint="ExtSelectClipRgn", CallingConvention=CallingConvention.StdCall)]
3602 internal extern static int Win32ExtSelectClipRgn(IntPtr hdc, IntPtr hrgn, int mode);
3604 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3605 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
3607 [DllImport ("user32.dll", EntryPoint="GetDoubleClickTime", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3608 private extern static int Win32GetDoubleClickTime ();
3610 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3611 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
3613 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3614 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
3616 [DllImport ("user32.dll", EntryPoint="ClipCursor", CallingConvention=CallingConvention.StdCall)]
3617 internal extern static bool Win32ClipCursor (ref RECT lpRect);
3619 [DllImport ("user32.dll", EntryPoint="GetClipCursor", CallingConvention=CallingConvention.StdCall)]
3620 internal extern static bool Win32GetClipCursor (out RECT lpRect);
3622 [DllImport ("gdi32.dll", EntryPoint="BitBlt", CallingConvention=CallingConvention.StdCall)]
3623 internal static extern bool Win32BitBlt (IntPtr hObject, int nXDest, int nYDest, int nWidth,
3624 int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
3626 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3627 internal static extern IntPtr Win32CreateCompatibleDC (IntPtr hdc);
3629 [DllImport ("gdi32.dll", EntryPoint="DeleteDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3630 internal static extern bool Win32DeleteDC (IntPtr hdc);
3632 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleBitmap", CallingConvention=CallingConvention.StdCall)]
3633 internal static extern IntPtr Win32CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight);
3635 [DllImport ("kernel32.dll", EntryPoint = "GetSystemPowerStatus", CallingConvention = CallingConvention.StdCall)]
3636 internal static extern Boolean Win32GetSystemPowerStatus (SYSTEMPOWERSTATUS sps);
3638 [DllImport ("user32.dll", EntryPoint = "GetIconInfo", CallingConvention = CallingConvention.StdCall)]
3639 internal static extern bool Win32GetIconInfo (IntPtr hIcon, out ICONINFO piconinfo);
3641 [DllImport ("user32.dll", EntryPoint="SetForegroundWindow", CallingConvention=CallingConvention.StdCall)]
3642 extern static bool Win32SetForegroundWindow(IntPtr hWnd);