2008-09-12 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / XplatUIWin32.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25 //
26
27 // NOT COMPLETE
28
29 using System;
30 using System.Drawing;
31 using System.Drawing.Imaging;
32 using System.ComponentModel;
33 using System.Collections;
34 using System.Diagnostics;
35 using System.IO;
36 using System.Runtime.InteropServices;
37 using System.Text;
38 using System.Threading;
39
40
41 /// Win32 Version
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;
48
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;
58
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)
69
70                 #endregion      // Local Variables
71
72                 #region Private Structs
73                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
74                 private struct WNDCLASS {
75                         internal int            style;
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;
87                 }
88
89                 [StructLayout(LayoutKind.Sequential)]
90                 internal struct RECT {
91                         internal int            left;
92                         internal int            top;
93                         internal int            right;
94                         internal int            bottom;
95
96                         public RECT (int left, int top, int right, int bottom)
97                         {
98                                 this.left = left;
99                                 this.top = top;
100                                 this.right = right;
101                                 this.bottom = bottom;
102                         }
103
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); } }
109                         #endregion
110
111                         #region Instance Methods
112                         public Rectangle ToRectangle ()
113                         {
114                                 return Rectangle.FromLTRB (left, top, right, bottom);
115                         }
116
117                         public static RECT FromRectangle (Rectangle rectangle)
118                         {
119                                 return new RECT (rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
120                         }
121
122                         public override int GetHashCode ()
123                         {
124                                 return left ^ ((top << 13) | (top >> 0x13))
125                                   ^ ((Width << 0x1a) | (Width >> 6))
126                                   ^ ((Height << 7) | (Height >> 0x19));
127                         }
128                         
129                         public override string ToString ()
130                         {
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);
132                         }
133                         #endregion
134
135                         #region Operator overloads
136                         public static implicit operator Rectangle (RECT rect)
137                         {
138                                 return Rectangle.FromLTRB (rect.left, rect.top, rect.right, rect.bottom);
139                         }
140
141                         public static implicit operator RECT (Rectangle rect)
142                         {
143                                 return new RECT (rect.Left, rect.Top, rect.Right, rect.Bottom);
144                         }
145                         #endregion
146                 }
147
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
181                 }
182
183                 internal enum WindowPlacementFlags {
184                         SW_HIDE                 = 0,
185                         SW_SHOWNORMAL           = 1,
186                         SW_NORMAL               = 1,
187                         SW_SHOWMINIMIZED        = 2,
188                         SW_SHOWMAXIMIZED        = 3,
189                         SW_MAXIMIZE             = 3,
190                         SW_SHOWNOACTIVATE       = 4,
191                         SW_SHOW                 = 5,
192                         SW_MINIMIZE             = 6,
193                         SW_SHOWMINNOACTIVE      = 7,
194                         SW_SHOWNA               = 8,
195                         SW_RESTORE              = 9,
196                         SW_SHOWDEFAULT          = 10,
197                         SW_FORCEMINIMIZE        = 11,
198                         SW_MAX                  = 11
199                 }
200
201                 [StructLayout(LayoutKind.Sequential)]
202                 private struct WINDOWPLACEMENT {
203                         internal uint                   length;
204                         internal uint                   flags;
205                         internal WindowPlacementFlags   showCmd;
206                         internal POINT                  ptMinPosition;
207                         internal POINT                  ptMaxPosition;
208                         internal RECT                   rcNormalPosition;
209                 }
210
211                 [StructLayout(LayoutKind.Sequential)]
212                 internal struct NCCALCSIZE_PARAMS {
213                         internal RECT           rgrc1;
214                         internal RECT           rgrc2;
215                         internal RECT           rgrc3;
216                         internal IntPtr         lppos;
217                 }
218
219                 [Flags]
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)
226                 }
227
228                 [StructLayout(LayoutKind.Sequential)]
229                 private struct TRACKMOUSEEVENT {
230                         internal int            size;
231                         internal TMEFlags       dwFlags;
232                         internal IntPtr         hWnd;
233                         internal int            dwHoverTime;
234                 }
235
236                 [StructLayout(LayoutKind.Sequential)]
237                 private struct PAINTSTRUCT {
238                         internal IntPtr         hdc;
239                         internal int            fErase;
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;
251                 }
252
253                 [StructLayout(LayoutKind.Sequential)]
254                 internal struct KEYBDINPUT {
255                         internal short wVk;
256                         internal short wScan;
257                         internal Int32 dwFlags;
258                         internal Int32 time;
259                         internal UIntPtr dwExtraInfo;
260                 }
261
262                 [StructLayout(LayoutKind.Sequential)]
263                 internal struct MOUSEINPUT {
264                         internal Int32 dx;
265                         internal Int32 dy;
266                         internal Int32 mouseData;
267                         internal Int32 dwFlags;
268                         internal Int32 time;
269                         internal UIntPtr dwExtraInfo;
270                 }
271
272                 [StructLayout(LayoutKind.Sequential)]
273                 internal struct HARDWAREINPUT {
274                         internal Int32 uMsg;
275                         internal short wParamL;
276                         internal short wParamH;
277                 }
278
279                 [StructLayout (LayoutKind.Sequential)]
280                 internal struct ICONINFO {
281                         internal bool fIcon;
282                         internal Int32 xHotspot;
283                         internal Int32 yHotspot;
284                         internal IntPtr hbmMask;
285                         internal IntPtr hbmColor;
286                 }    
287                 
288                 [StructLayout(LayoutKind.Explicit)]
289                 internal struct INPUT {
290                         [FieldOffset(0)]
291                         internal Int32 type;
292
293                         [FieldOffset(4)]
294                         internal MOUSEINPUT mi;
295
296                         [FieldOffset(4)]
297                         internal KEYBDINPUT ki;
298
299                         [FieldOffset(4)]
300                         internal HARDWAREINPUT hi;
301                 }
302
303                 [StructLayout (LayoutKind.Sequential)]
304                 public struct ANIMATIONINFO {
305                         internal uint cbSize;
306                         internal int iMinAnimate;
307                 }
308                 
309                 internal enum InputFlags {
310                         KEYEVENTF_EXTENDEDKEY   = 0x0001,
311                         KEYEVENTF_KEYUP                 = 0x0002,
312                         KEYEVENTF_SCANCODE              = 0x0003,
313                         KEYEVENTF_UNICODE               = 0x0004,
314                 }
315
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,
330                         CS_IME                          = 0x00010000,
331                         // Windows XP+
332                         CS_DROPSHADOW                   = 0x00020000
333                 }
334
335                 internal enum SetWindowPosZOrder {
336                         HWND_TOP                        = 0,
337                         HWND_BOTTOM                     = 1,
338                         HWND_TOPMOST                    = -1,
339                         HWND_NOTOPMOST                  = -2
340                 }
341
342                 [Flags]
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,
351                         SWP_NOMOVE                      = 0x0002,
352                         SWP_NOOWNERZORDER               = 0x0200,
353                         SWP_NOREDRAW                    = 0x0008,
354                         SWP_NOREPOSITION                = 0x0200,
355                         SWP_NOENDSCHANGING              = 0x0400,
356                         SWP_NOSIZE                      = 0x0001,
357                         SWP_NOZORDER                    = 0x0004,
358                         SWP_SHOWWINDOW                  = 0x0040
359                 }
360
361                 internal enum GetSysColorIndex {
362                         COLOR_SCROLLBAR                 = 0,
363                         COLOR_BACKGROUND                = 1,
364                         COLOR_ACTIVECAPTION             = 2,
365                         COLOR_INACTIVECAPTION           = 3,
366                         COLOR_MENU                      = 4,
367                         COLOR_WINDOW                    = 5,
368                         COLOR_WINDOWFRAME               = 6,
369                         COLOR_MENUTEXT                  = 7,
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,
377                         COLOR_BTNFACE                   = 15,
378                         COLOR_BTNSHADOW                 = 16,
379                         COLOR_GRAYTEXT                  = 17,
380                         COLOR_BTNTEXT                   = 18,
381                         COLOR_INACTIVECAPTIONTEXT       = 19,
382                         COLOR_BTNHIGHLIGHT              = 20,
383                         COLOR_3DDKSHADOW                = 21,
384                         COLOR_3DLIGHT                   = 22,
385                         COLOR_INFOTEXT                  = 23,
386                         COLOR_INFOBK                    = 24,
387                         
388                         COLOR_HOTLIGHT                  = 26,
389                         COLOR_GRADIENTACTIVECAPTION     = 27,
390                         COLOR_GRADIENTINACTIVECAPTION   = 28,
391                         COLOR_MENUHIGHLIGHT             = 29,
392                         COLOR_MENUBAR                   = 30,
393
394                         COLOR_DESKTOP                   = 1,
395                         COLOR_3DFACE                    = 16,
396                         COLOR_3DSHADOW                  = 16,
397                         COLOR_3DHIGHLIGHT               = 20,
398                         COLOR_3DHILIGHT                 = 20,
399                         COLOR_BTNHILIGHT                = 20,
400                         COLOR_MAXVALUE                  = 24,/* Maximum value */
401                 }       
402
403                 private enum LoadCursorType {
404                         First                           = 32512,
405                         IDC_ARROW                       = 32512,
406                         IDC_IBEAM                       = 32513,
407                         IDC_WAIT                        = 32514,
408                         IDC_CROSS                       = 32515,
409                         IDC_UPARROW                     = 32516,
410                         IDC_SIZE                        = 32640,
411                         IDC_ICON                        = 32641,
412                         IDC_SIZENWSE                    = 32642,
413                         IDC_SIZENESW                    = 32643,
414                         IDC_SIZEWE                      = 32644,
415                         IDC_SIZENS                      = 32645,
416                         IDC_SIZEALL                     = 32646,
417                         IDC_NO                          = 32648,
418                         IDC_HAND                        = 32649,
419                         IDC_APPSTARTING                 = 32650,
420                         IDC_HELP                        = 32651,
421                         Last                            = 32651
422                 }
423
424                 private enum AncestorType {
425                         GA_PARENT = 1,
426                         GA_ROOT = 2, 
427                         GA_ROOTOWNER = 3
428                 }
429
430                 [Flags]
431                 private enum WindowLong {
432                         GWL_WNDPROC                     = -4,
433                         GWL_HINSTANCE                   = -6,
434                         GWL_HWNDPARENT                  = -8,
435                         GWL_STYLE                       = -16,
436                         GWL_EXSTYLE                     = -20,
437                         GWL_USERDATA                    = -21,
438                         GWL_ID                          = -12
439                 }
440
441                 [Flags]
442                 private enum LogBrushStyle {
443                         BS_SOLID                        = 0,
444                         BS_NULL                         = 1,
445                         BS_HATCHED                      = 2,
446                         BS_PATTERN                      = 3,
447                         BS_INDEXED                      = 4,
448                         BS_DIBPATTERN                   = 5,
449                         BS_DIBPATTERNPT                 = 6,
450                         BS_PATTERN8X8                   = 7,
451                         BS_DIBPATTERN8X8                = 8,
452                         BS_MONOPATTERN                  = 9
453                 }
454
455                 [Flags]
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 */
463                 }
464
465                 internal struct COLORREF {
466                         internal byte                   R;
467                         internal byte                   G;
468                         internal byte                   B;
469                         internal byte                   A;
470                 }
471
472                 [StructLayout(LayoutKind.Sequential)]
473                 private struct LOGBRUSH {
474                         internal LogBrushStyle          lbStyle;
475                         internal COLORREF               lbColor;
476                         internal LogBrushHatch          lbHatch;
477                 }
478
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; 
501                 }
502
503                 public enum TernaryRasterOperations : uint
504                 {
505                         SRCCOPY = 0x00CC0020,
506                         SRCPAINT = 0x00EE0086,
507                         SRCAND = 0x008800C6,
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
520                 }
521
522                 [Flags]
523                 private enum ScrollWindowExFlags {
524                         SW_NONE                         = 0x0000,
525                         SW_SCROLLCHILDREN               = 0x0001,
526                         SW_INVALIDATE                   = 0x0002,
527                         SW_ERASE                        = 0x0004,
528                         SW_SMOOTHSCROLL                 = 0x0010
529                 }
530
531                 internal enum SystemMetrics {
532                         SM_CXSCREEN                     = 0,
533                         SM_CYSCREEN                     = 1,
534                         SM_CXVSCROLL                    = 2,
535                         SM_CYHSCROLL                    = 3,
536                         SM_CYCAPTION                    = 4,
537                         SM_CXBORDER                     = 5,
538                         SM_CYBORDER                     = 6,
539                         SM_CXDLGFRAME                   = 7,
540                         SM_CYDLGFRAME                   = 8,
541                         SM_CYVTHUMB                     = 9,
542                         SM_CXHTHUMB                     = 10,
543                         SM_CXICON                       = 11,
544                         SM_CYICON                       = 12,
545                         SM_CXCURSOR                     = 13,
546                         SM_CYCURSOR                     = 14,
547                         SM_CYMENU                       = 15,
548                         SM_CXFULLSCREEN                 = 16,
549                         SM_CYFULLSCREEN                 = 17,
550                         SM_CYKANJIWINDOW                = 18,
551                         SM_MOUSEPRESENT                 = 19,
552                         SM_CYVSCROLL                    = 20,
553                         SM_CXHSCROLL                    = 21,
554                         SM_DEBUG                        = 22,
555                         SM_SWAPBUTTON                   = 23,
556                         SM_RESERVED1                    = 24,
557                         SM_RESERVED2                    = 25,
558                         SM_RESERVED3                    = 26,
559                         SM_RESERVED4                    = 27,
560                         SM_CXMIN                        = 28,
561                         SM_CYMIN                        = 29,
562                         SM_CXSIZE                       = 30,
563                         SM_CYSIZE                       = 31,
564                         SM_CXFRAME                      = 32,
565                         SM_CYFRAME                      = 33,
566                         SM_CXMINTRACK                   = 34,
567                         SM_CYMINTRACK                   = 35,
568                         SM_CXDOUBLECLK                  = 36,
569                         SM_CYDOUBLECLK                  = 37,
570                         SM_CXICONSPACING                = 38,
571                         SM_CYICONSPACING                = 39,
572                         SM_MENUDROPALIGNMENT            = 40,
573                         SM_PENWINDOWS                   = 41,
574                         SM_DBCSENABLED                  = 42,
575                         SM_CMOUSEBUTTONS                = 43,
576                         SM_CXFIXEDFRAME                 = SM_CXDLGFRAME,
577                         SM_CYFIXEDFRAME                 = SM_CYDLGFRAME,
578                         SM_CXSIZEFRAME                  = SM_CXFRAME,
579                         SM_CYSIZEFRAME                  = SM_CYFRAME,
580                         SM_SECURE                       = 44,
581                         SM_CXEDGE                       = 45,
582                         SM_CYEDGE                       = 46,
583                         SM_CXMINSPACING                 = 47,
584                         SM_CYMINSPACING                 = 48,
585                         SM_CXSMICON                     = 49,
586                         SM_CYSMICON                     = 50,
587                         SM_CYSMCAPTION                  = 51,
588                         SM_CXSMSIZE                     = 52,
589                         SM_CYSMSIZE                     = 53,
590                         SM_CXMENUSIZE                   = 54,
591                         SM_CYMENUSIZE                   = 55,
592                         SM_ARRANGE                      = 56,
593                         SM_CXMINIMIZED                  = 57,
594                         SM_CYMINIMIZED                  = 58,
595                         SM_CXMAXTRACK                   = 59,
596                         SM_CYMAXTRACK                   = 60,
597                         SM_CXMAXIMIZED                  = 61,
598                         SM_CYMAXIMIZED                  = 62,
599                         SM_NETWORK                      = 63,
600                         SM_CLEANBOOT                    = 67,
601                         SM_CXDRAG                       = 68,
602                         SM_CYDRAG                       = 69,
603                         SM_SHOWSOUNDS                   = 70,
604                         SM_CXMENUCHECK                  = 71,
605                         SM_CYMENUCHECK                  = 72,
606                         SM_SLOWMACHINE                  = 73,
607                         SM_MIDEASTENABLED               = 74,
608                         SM_MOUSEWHEELPRESENT            = 75,
609                         SM_XVIRTUALSCREEN               = 76,
610                         SM_YVIRTUALSCREEN               = 77,
611                         SM_CXVIRTUALSCREEN              = 78,
612                         SM_CYVIRTUALSCREEN              = 79,
613                         SM_CMONITORS                    = 80,
614                         SM_SAMEDISPLAYFORMAT            = 81,
615                         SM_IMMENABLED                   = 82,
616                         SM_CXFOCUSBORDER                = 83,
617                         SM_CYFOCUSBORDER                = 84,
618                         SM_TABLETPC                     = 86,
619                         SM_MEDIACENTER                  = 87,
620                         SM_CMETRICS                     = 88
621                 }
622
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,
628                 }
629
630                 [Flags]
631                 internal enum NotifyIconFlags {
632                         NIF_MESSAGE                     = 0x00000001,
633                         NIF_ICON                        = 0x00000002,
634                         NIF_TIP                         = 0x00000004,
635                         NIF_STATE                       = 0x00000008,
636                         NIF_INFO                        = 0x00000010                    
637                 }
638
639                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
640                 internal struct NOTIFYICONDATA {
641                         internal uint                           cbSize;
642                         internal IntPtr                         hWnd;
643                         internal uint                           uID;
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;
656 #if NET_2_0
657                         internal ToolTipIcon            dwInfoFlags;
658 #else
659                         internal int                            dwInfoFlags;
660 #endif
661                 }
662
663                 [Flags]
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
678                 }
679
680                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
681                 internal struct CLIENTCREATESTRUCT {
682                         internal IntPtr                 hWindowMenu;
683                         internal uint                   idFirstChild;
684                 }
685
686                 private enum ClassLong : int {
687                         GCL_MENUNAME                    = -8,
688                         GCL_HBRBACKGROUND               = -10,
689                         GCL_HCURSOR                     = -12,
690                         GCL_HICON                       = -14,
691                         GCL_HMODULE                     = -16,
692                         GCL_CBWNDEXTRA                  = -18,
693                         GCL_CBCLSEXTRA                  = -20,
694                         GCL_WNDPROC                     = -24,
695                         GCL_STYLE                       = -26,
696                         GCW_ATOM                        = -32,
697                         GCL_HICONSM                     = -34
698                 }
699
700                 [Flags]
701                 internal enum GAllocFlags : uint {
702                         GMEM_FIXED                      = 0x0000,
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,
710                         GMEM_SHARE                      = 0x2000,
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)
718                 }
719
720                 internal enum ROP2DrawMode : int {
721                         R2_BLACK                        = 1,
722                         R2_NOTMERGEPEN                  = 2,
723                         R2_MASKNOTPEN                   = 3,
724                         R2_NOTCOPYPEN                   = 4,
725                         R2_MASKPENNOT                   = 5,
726                         R2_NOT                          = 6,
727                         R2_XORPEN                       = 7,
728                         R2_NOTMASKPEN                   = 8,
729                         R2_MASKPEN                      = 9,
730                         R2_NOTXORPEN                    = 10,
731                         R2_NOP                          = 11,
732                         R2_MERGENOTPEN                  = 12,
733                         R2_COPYPEN                      = 13,
734                         R2_MERGEPENNOT                  = 14,
735                         R2_MERGEPEN                     = 15,
736                         R2_WHITE                        = 16,
737                         R2_LAST                         = 16
738                 }
739
740                 internal enum PenStyle : int {
741                         PS_SOLID                        = 0,
742                         PS_DASH                         = 1,
743                         PS_DOT                          = 2,
744                         PS_DASHDOT                      = 3,
745                         PS_DASHDOTDOT                   = 4,
746                         PS_NULL                         = 5,
747                         PS_INSIDEFRAME                  = 6,
748                         PS_USERSTYLE                    = 7,
749                         PS_ALTERNATE                    = 8
750                 }
751
752                 internal enum PatBltRop : int {
753                         PATCOPY   = 0xf00021,
754                         PATINVERT = 0x5a0049,
755                         DSTINVERT = 0x550009,
756                         BLACKNESS = 0x000042,
757                         WHITENESS = 0xff0062,
758                 }
759
760                 internal enum StockObject : int {
761                         WHITE_BRUSH                     = 0,
762                         LTGRAY_BRUSH                    = 1,
763                         GRAY_BRUSH                      = 2,
764                         DKGRAY_BRUSH                    = 3,
765                         BLACK_BRUSH                     = 4,
766                         NULL_BRUSH                      = 5,
767                         HOLLOW_BRUSH                    = NULL_BRUSH,
768                         WHITE_PEN                       = 6,
769                         BLACK_PEN                       = 7,
770                         NULL_PEN                        = 8,
771                         OEM_FIXED_FONT                  = 10,
772                         ANSI_FIXED_FONT                 = 11,
773                         ANSI_VAR_FONT                   = 12,
774                         SYSTEM_FONT                     = 13,
775                         DEVICE_DEFAULT_FONT             = 14,
776                         DEFAULT_PALETTE                 = 15,
777                         SYSTEM_FIXED_FONT               = 16
778                 }
779
780                 internal enum HatchStyle : int {
781                         HS_HORIZONTAL                   = 0,
782                         HS_VERTICAL                     = 1,
783                         HS_FDIAGONAL                    = 2,
784                         HS_BDIAGONAL                    = 3,
785                         HS_CROSS                        = 4,
786                         HS_DIAGCROSS                    = 5
787                 }
788
789                 [Flags]
790                 internal enum SndFlags : int {
791                         SND_SYNC                        = 0x0000,
792                         SND_ASYNC                       = 0x0001,
793                         SND_NODEFAULT                   = 0x0002,
794                         SND_MEMORY                      = 0x0004,
795                         SND_LOOP                        = 0x0008,
796                         SND_NOSTOP                      = 0x0010,
797                         SND_NOWAIT                      = 0x00002000,
798                         SND_ALIAS                       = 0x00010000,
799                         SND_ALIAS_ID                    = 0x00110000,
800                         SND_FILENAME                    = 0x00020000,
801                         SND_RESOURCE                    = 0x00040004,
802                         SND_PURGE                       = 0x0040,
803                         SND_APPLICATION                 = 0x0080,
804                 }
805
806                 [Flags]
807                 internal enum LayeredWindowAttributes : int {
808                         LWA_COLORKEY            = 0x1,
809                         LWA_ALPHA                       = 0x2,
810                 }
811
812                 public enum ACLineStatus : byte {
813                         Offline = 0,
814                         Online = 1,
815                         Unknown = 255
816                 }
817
818                 public enum BatteryFlag : byte {
819                         High = 1,
820                         Low = 2,
821                         Critical = 4,
822                         Charging = 8,
823                         NoSystemBattery = 128,
824                         Unknown = 255
825                 }
826
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;
835                 }
836                 #endregion
837
838                 #region Constructor & Destructor
839                 private XplatUIWin32() {
840                         // Handle singleton stuff first
841                         ref_count=0;
842
843                         mouse_state = MouseButtons.None;
844                         mouse_position = Point.Empty;
845
846                         message_queue = new Queue();
847
848                         themes_enabled = false;
849
850                         wnd_proc = new WndProc(InternalWndProc);
851
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);
853
854                         if (FosterParent==IntPtr.Zero) {
855                                 Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
856                         }
857
858                         scroll_height = Win32GetSystemMetrics(SystemMetrics.SM_CYHSCROLL);
859                         scroll_width = Win32GetSystemMetrics(SystemMetrics.SM_CXVSCROLL);
860
861                         timer_list = new Hashtable ();
862                         registered_classes = new Hashtable ();
863                 }
864                 #endregion      // Constructor & Destructor
865
866                 #region Private Support Methods
867
868                 private string RegisterWindowClass (int classStyle)
869                 {
870                         string class_name;
871
872                         lock (registered_classes) {
873                                 class_name = (string)registered_classes[classStyle];
874
875                                 if (class_name != null)
876                                         return class_name;
877
878                                 class_name = string.Format ("Mono.WinForms.{0}.{1}", System.Threading.Thread.GetDomainID ().ToString (), classStyle);
879
880                                 WNDCLASS wndClass;
881
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 = "";
892
893                                 bool result = Win32RegisterClass (ref wndClass);
894
895                                 if (result == false)
896                                         Win32MessageBox (IntPtr.Zero, "Could not register the window class, win32 error " + Win32GetLastError ().ToString (), "Oops", 0);
897
898                                 registered_classes[classStyle] = class_name;
899                         }
900                         
901                         return class_name;
902                 }
903
904                 private static bool RetrieveMessage(ref MSG msg) {
905                         MSG     message;
906
907                         if (message_queue.Count == 0) {
908                                 return false;
909                         }
910
911                         message = (MSG)message_queue.Dequeue();
912                         msg = message;
913
914                         return true;
915                 }
916
917                 private static bool StoreMessage(ref MSG msg) {
918                         MSG message = new MSG();
919
920                         message = msg;
921                         message_queue.Enqueue(message);
922
923                         return true;
924                 }
925
926                 internal static String AnsiToString(IntPtr ansi_data) {
927                         return (string)Marshal.PtrToStringAnsi(ansi_data);
928                 }
929
930                 internal static String UnicodeToString(IntPtr unicode_data) {
931                         return (string)Marshal.PtrToStringUni(unicode_data);
932                 }
933
934                 internal static Image DIBtoImage(IntPtr dib_data) {
935                         BITMAPINFOHEADER        bmi;
936                         int                     ncolors;
937                         int                     imagesize;
938                         //int                   palettesize;
939                         Bitmap                  bmp;
940                         BitmapData              bits;
941                         ColorPalette            pal;
942                         int[]                   palette;
943                         byte[]                  imagebits;
944                         int                     bytesPerLine;
945
946                         bmi = (BITMAPINFOHEADER)Marshal.PtrToStructure(dib_data, typeof(BITMAPINFOHEADER));
947
948                         ncolors = (int)bmi.biClrUsed;
949                         if (ncolors == 0) {
950                                 if (bmi.biBitCount < 24) {
951                                         ncolors = (int)(1 << bmi.biBitCount);
952                                 }
953                         }
954                         //palettesize = ncolors * 4;
955
956                         imagesize = (int)bmi.biSizeImage;
957                         if (imagesize == 0) {
958                                 imagesize = (int)(((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);
959                         }
960
961                         switch(bmi.biBitCount) {
962                                 case 1: {       // Monochrome
963                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format1bppIndexed);
964                                         palette = new int[2];
965                                         break;
966                                 }
967
968                                 case 4: {       // 4bpp
969                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format4bppIndexed);
970                                         palette = new int[16];
971                                         break;
972                                 }
973
974                                 case 8: {       // 8bpp
975                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format8bppIndexed);
976                                         palette = new int[256];
977                                         break;
978                                 }
979
980                                 case 24:
981                                 case 32: {      // 32bpp
982                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format32bppArgb);
983                                         palette = new int[0];
984                                         break;
985                                 }
986
987                                 default: {
988                                         throw new Exception("Unexpected number of bits:" + bmi.biBitCount.ToString());
989                                 }
990                         }
991
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);
995
996                                 for (int i = 0; i < ncolors; i++) {
997                                         pal.Entries[i] = Color.FromArgb(palette[i] | unchecked((int)0xff000000));
998                                 }
999                                 bmp.Palette = pal;
1000                         }
1001
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);
1004
1005                         imagebits = new byte[bytesPerLine];
1006
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);
1010
1011                                 // Copy from managed to dest
1012                                 Marshal.Copy(imagebits, 0, (IntPtr)((int)bits.Scan0 + bits.Stride * (bmi.biHeight - 1 - y)), imagebits.Length);
1013                         }
1014
1015                         bmp.UnlockBits(bits);
1016
1017                         return bmp;
1018                 }
1019
1020                 internal static byte[] ImageToDIB(Image image) {
1021                         MemoryStream    ms;
1022                         byte[]          buffer;
1023                         byte[]          retbuf;
1024
1025                         ms = new MemoryStream();
1026                         image.Save(ms, ImageFormat.Bmp);
1027                         buffer = ms.GetBuffer();
1028
1029                         // Filter out the file header
1030                         retbuf = new byte[buffer.Length];
1031                         Array.Copy(buffer, 14, retbuf, 0, buffer.Length - 14);
1032                         return retbuf;
1033                 }
1034
1035                 internal static IntPtr DupGlobalMem(IntPtr mem) {
1036                         IntPtr  dup;
1037                         IntPtr  dup_ptr;
1038                         IntPtr  mem_ptr;
1039                         uint    len;
1040
1041                         len = Win32GlobalSize(mem);
1042                         mem_ptr = Win32GlobalLock(mem);
1043
1044                         dup = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE, (int)len);
1045                         dup_ptr = Win32GlobalLock(dup);
1046
1047                         Win32CopyMemory(dup_ptr, mem_ptr, (int)len);
1048
1049                         Win32GlobalUnlock(mem);
1050                         Win32GlobalUnlock(dup);
1051
1052                         return dup;
1053                 }
1054                 
1055                 private int GetSystemParametersInfoInt (SPIAction spi)
1056                 {
1057                         int value = 0;
1058                         
1059                         Win32SystemParametersInfo (spi, 0, ref value, 0);
1060                         
1061                         return value;
1062                 }
1063
1064                 private bool GetSystemParametersInfoBool (SPIAction spi)
1065                 {
1066                         bool value = false;
1067                         
1068                         Win32SystemParametersInfo (spi, 0, ref value, 0);
1069
1070                         return value;
1071                 }
1072                 #endregion      // Private Support Methods
1073
1074                 #region Static Properties
1075                 internal override int ActiveWindowTrackingDelay {
1076                         get { return GetSystemParametersInfoInt (SPIAction.SPI_GETACTIVEWNDTRKTIMEOUT); }
1077                 }
1078
1079                 internal override int CaretWidth {
1080                         get { 
1081                                 // Supported on 2k, XP, 2k3 +
1082                                 if (Environment.OSVersion.Version.Major < 5)
1083                                         throw new NotSupportedException ();
1084                                         
1085                                 return GetSystemParametersInfoInt (SPIAction.SPI_GETCARETWIDTH);
1086                         }
1087                 }
1088
1089                 internal override int FontSmoothingContrast {
1090                         get {
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 ();
1094                                         
1095                                 return GetSystemParametersInfoInt (SPIAction.SPI_GETFONTSMOOTHINGCONTRAST);
1096                         }
1097                 }
1098
1099                 internal override int FontSmoothingType {
1100                         get {
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 ();
1104
1105                                 return GetSystemParametersInfoInt (SPIAction.SPI_GETFONTSMOOTHINGTYPE);
1106                         }
1107                 }
1108
1109                 internal override int HorizontalResizeBorderThickness {
1110                         get { return Win32GetSystemMetrics (SystemMetrics.SM_CXSIZEFRAME); }
1111                 }
1112
1113                 internal override bool IsActiveWindowTrackingEnabled {
1114                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETACTIVEWINDOWTRACKING); }
1115                 }
1116
1117                 internal override bool IsComboBoxAnimationEnabled {
1118                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETCOMBOBOXANIMATION); }
1119                 }
1120
1121                 internal override bool IsDropShadowEnabled {
1122                         get {
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 ();
1126
1127                                 return GetSystemParametersInfoBool (SPIAction.SPI_GETDROPSHADOW);
1128                         }
1129                 }
1130
1131                 internal override bool IsFontSmoothingEnabled {
1132                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETFONTSMOOTHING); }
1133                 }
1134
1135                 internal override bool IsHotTrackingEnabled {
1136                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETHOTTRACKING); }
1137                 }
1138
1139                 internal override bool IsIconTitleWrappingEnabled {
1140                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETICONTITLEWRAP); }
1141                 }
1142
1143                 internal override bool IsKeyboardPreferred {
1144                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETKEYBOARDPREF); }
1145                 }
1146
1147                 internal override bool IsListBoxSmoothScrollingEnabled {
1148                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETLISTBOXSMOOTHSCROLLING); }
1149                 }
1150
1151                 internal override bool IsMenuAnimationEnabled {
1152                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETMENUANIMATION); }
1153                 }
1154
1155                 internal override bool IsMenuFadeEnabled {
1156                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETMENUFADE); }
1157                 }
1158
1159                 internal override bool IsMinimizeRestoreAnimationEnabled {
1160                         get {
1161                                 ANIMATIONINFO ai = new ANIMATIONINFO ();
1162                                 ai.cbSize = (uint)Marshal.SizeOf (ai);
1163                                 
1164                                 Win32SystemParametersInfo (SPIAction.SPI_GETANIMATION, 0, ref ai, 0);
1165                                 return ai.iMinAnimate == 0 ? false : true;
1166                         }
1167                 }
1168
1169                 internal override bool IsSelectionFadeEnabled {
1170                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETSELECTIONFADE); }
1171                 }
1172
1173                 internal override bool IsSnapToDefaultEnabled {
1174                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETSNAPTODEFBUTTON); }
1175                 }
1176
1177                 internal override bool IsTitleBarGradientEnabled {
1178                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETGRADIENTCAPTIONS); }
1179                 }
1180
1181                 internal override bool IsToolTipAnimationEnabled {
1182                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETTOOLTIPANIMATION); }
1183                 }
1184
1185                 internal override Size MenuBarButtonSize {
1186                         get {
1187                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXMENUSIZE),
1188                                         Win32GetSystemMetrics (SystemMetrics.SM_CYMENUSIZE));
1189                         }
1190                 }
1191
1192                 public override Size MenuButtonSize {
1193                         get {
1194                                 return new Size (
1195                                         Win32GetSystemMetrics (SystemMetrics.SM_CXMENUSIZE),
1196                                         Win32GetSystemMetrics (SystemMetrics.SM_CYMENUSIZE));
1197                         }
1198                 }
1199
1200                 internal override int MenuShowDelay {
1201                         get { return GetSystemParametersInfoInt (SPIAction.SPI_GETMENUSHOWDELAY); }
1202                 }
1203
1204                 internal override int MouseSpeed {
1205                         get { return GetSystemParametersInfoInt (SPIAction.SPI_GETMOUSESPEED); }
1206                 }
1207
1208                 internal override LeftRightAlignment PopupMenuAlignment {
1209                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETMENUDROPALIGNMENT) == true ? LeftRightAlignment.Left : LeftRightAlignment.Right; }
1210                 }
1211
1212 #if NET_2_0
1213                 internal override PowerStatus PowerStatus {
1214                         get {
1215                                 SYSTEMPOWERSTATUS p = new SYSTEMPOWERSTATUS ();
1216                                 
1217                                 Win32GetSystemPowerStatus (p);
1218                                 
1219                                 PowerStatus ps = new PowerStatus ((BatteryChargeStatus)p._BatteryFlag, p._BatteryFullLifeTime, (float)p._BatteryLifePercent / 255f, p._BatteryLifeTime, (PowerLineStatus)p._ACLineStatus);
1220                                 
1221                                 return ps;
1222                         }
1223                 }
1224 #endif
1225
1226                 internal override int SizingBorderWidth {
1227                         get { return Win32GetSystemMetrics (SystemMetrics.SM_CXSIZEFRAME); }
1228                 }
1229
1230                 internal override Size SmallCaptionButtonSize {
1231                         get {
1232                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXSMSIZE),
1233                                         Win32GetSystemMetrics (SystemMetrics.SM_CYSMSIZE));
1234                         }
1235                 }
1236
1237                 internal override bool UIEffectsEnabled {
1238                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETUIEFFECTS); }
1239                 }
1240
1241                 internal override int VerticalResizeBorderThickness {
1242                         get { return Win32GetSystemMetrics (SystemMetrics.SM_CYSIZEFRAME); }
1243                 }
1244
1245                 internal override void RaiseIdle (EventArgs e)
1246                 {
1247                         if (Idle != null)
1248                                 Idle (this, e);
1249                 }
1250
1251                 internal override Keys ModifierKeys {
1252                         get {
1253                                 short   state;
1254                                 Keys    key_state;
1255
1256                                 key_state = Keys.None;
1257
1258                                 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
1259                                 if ((state & 0x8000) != 0) {
1260                                         key_state |= Keys.Shift;
1261                                 }
1262                                 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
1263                                 if ((state & 0x8000) != 0) {
1264                                         key_state |= Keys.Control;
1265                                 }
1266
1267                                 state = Win32GetKeyState(VirtualKeys.VK_MENU);
1268                                 if ((state & 0x8000) != 0) {
1269                                         key_state |= Keys.Alt;
1270                                 }
1271
1272                                 return key_state;
1273                         }
1274                 }
1275
1276                 internal override MouseButtons MouseButtons {
1277                         get {
1278                                 return mouse_state;
1279                         }
1280                 }
1281
1282                 internal override Point MousePosition {
1283                         get {
1284                                 return mouse_position;
1285                         }
1286                 }
1287
1288                 internal override Size MouseHoverSize {
1289                         get {
1290                                 int     width = 4;
1291                                 int     height = 4;
1292
1293                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref width, 0);
1294                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref height, 0);
1295                                 return new Size(width, height);
1296                         }
1297                 }
1298
1299                 internal override int MouseHoverTime {
1300                         get {
1301                                 int time = 500;
1302
1303                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERTIME, 0, ref time, 0);
1304                                 return time;
1305                         }
1306                 }
1307
1308                 internal override int MouseWheelScrollDelta {
1309                         get {
1310                                 int delta = 120;
1311                                 Win32SystemParametersInfo(SPIAction.SPI_GETWHEELSCROLLLINES, 0, ref delta, 0);
1312                                 return delta;
1313                         }
1314                 }
1315                 
1316                 internal override int HorizontalScrollBarHeight {
1317                         get {
1318                                 return scroll_height;
1319                         }
1320                 }
1321
1322                 internal override bool UserClipWontExposeParent {
1323                         get {
1324                                 return false;
1325                         }
1326                 }
1327
1328
1329                 internal override int VerticalScrollBarWidth {
1330                         get {
1331                                 return scroll_width;
1332                         }
1333                 }
1334
1335                 internal override int MenuHeight {
1336                         get {
1337                                 return Win32GetSystemMetrics(SystemMetrics.SM_CYMENU);
1338                         }
1339                 }
1340
1341                 internal override Size Border3DSize {
1342                         get {
1343                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXEDGE),
1344                                         Win32GetSystemMetrics (SystemMetrics.SM_CYEDGE));
1345                         }
1346                 }
1347
1348                 internal override Size BorderSize {
1349                         get {
1350                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXBORDER),
1351                                         Win32GetSystemMetrics (SystemMetrics.SM_CYBORDER));
1352                         }
1353                 }
1354
1355                 internal override bool DropTarget {
1356                         get {
1357                                 return false;
1358                         }
1359
1360                         set {
1361                                 if (value) {
1362                                         //throw new NotImplementedException("Need to figure out D'n'D for Win32");
1363                                 }
1364                         }
1365                 }
1366
1367                 internal override Size CaptionButtonSize {
1368                         get {
1369                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXSIZE),
1370                                         Win32GetSystemMetrics (SystemMetrics.SM_CYSIZE));
1371                         }
1372                 }
1373
1374                 internal override int CaptionHeight {
1375                         get {
1376                                 return Win32GetSystemMetrics(SystemMetrics.SM_CYCAPTION);
1377                         }
1378                 }
1379
1380                 internal override Size CursorSize {
1381                         get {
1382                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CYCURSOR));
1383                         }
1384                 }
1385
1386                 internal override bool DragFullWindows {
1387                         get {
1388                                 int full = 0;
1389                                 Win32SystemParametersInfo (SPIAction.SPI_GETDRAGFULLWINDOWS, 0, ref full, 0);
1390                                 return (full != 0);
1391                         }
1392                 }
1393
1394                 internal override Size DragSize {
1395                         get {
1396                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXDRAG), Win32GetSystemMetrics(SystemMetrics.SM_CYDRAG));
1397                         }
1398                 }
1399
1400                 internal override Size DoubleClickSize {
1401                         get {
1402                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXDOUBLECLK),
1403                                         Win32GetSystemMetrics (SystemMetrics.SM_CYDOUBLECLK));
1404                         }
1405                 }
1406
1407                 internal override int DoubleClickTime {
1408                         get {
1409                                 return Win32GetDoubleClickTime ();
1410                         }
1411                 }
1412
1413                 internal override Size FixedFrameBorderSize {
1414                         get {
1415                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXFIXEDFRAME),
1416                                         Win32GetSystemMetrics (SystemMetrics.SM_CYFIXEDFRAME));
1417                         }
1418                 }
1419
1420                 internal override Size FrameBorderSize { 
1421                         get {
1422                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXFRAME), Win32GetSystemMetrics(SystemMetrics.SM_CYFRAME));
1423                         }
1424                 }
1425
1426                 internal override Size IconSize {
1427                         get {
1428                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXICON), Win32GetSystemMetrics(SystemMetrics.SM_CYICON));
1429                         }
1430                 }
1431
1432                 internal override Size MaxWindowTrackSize {
1433                         get {
1434                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMAXTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMAXTRACK));
1435                         }
1436                 }
1437
1438                 internal override bool MenuAccessKeysUnderlined {
1439                         get {
1440                                 int underlined = 0;
1441                                 Win32SystemParametersInfo (SPIAction.SPI_GETKEYBOARDCUES, 0, ref underlined, 0);
1442                                 return (underlined != 0);
1443                         }
1444                 }
1445
1446                 internal override Size MinimizedWindowSize {
1447                         get {
1448                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINIMIZED), Win32GetSystemMetrics(SystemMetrics.SM_CYMINIMIZED));
1449                         }
1450                 }
1451
1452                 internal override Size MinimizedWindowSpacingSize {
1453                         get {
1454                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINSPACING), Win32GetSystemMetrics(SystemMetrics.SM_CYMINSPACING));
1455                         }
1456                 }
1457
1458                 internal override Size MinimumWindowSize {
1459                         get {
1460                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMIN), Win32GetSystemMetrics(SystemMetrics.SM_CYMIN));
1461                         }
1462                 }
1463
1464                 internal override Size MinWindowTrackSize {
1465                         get {
1466                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMINTRACK));
1467                         }
1468                 }
1469
1470                 internal override Size SmallIconSize {
1471                         get {
1472                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXSMICON), Win32GetSystemMetrics(SystemMetrics.SM_CYSMICON));
1473                         }
1474                 }
1475
1476                 internal override int MouseButtonCount {
1477                         get {
1478                                 return Win32GetSystemMetrics(SystemMetrics.SM_CMOUSEBUTTONS);
1479                         }
1480                 }
1481
1482                 internal override bool MouseButtonsSwapped {
1483                         get {
1484                                 return Win32GetSystemMetrics(SystemMetrics.SM_SWAPBUTTON) != 0;
1485                         }
1486                 }
1487
1488                 internal override bool MouseWheelPresent {
1489                         get {
1490                                 return Win32GetSystemMetrics(SystemMetrics.SM_MOUSEWHEELPRESENT) != 0;
1491                         }
1492                 }
1493
1494                 internal override Rectangle VirtualScreen {
1495                         get {
1496                                 return new Rectangle(   Win32GetSystemMetrics(SystemMetrics.SM_XVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_YVIRTUALSCREEN),
1497                                                         Win32GetSystemMetrics(SystemMetrics.SM_CXVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYVIRTUALSCREEN));
1498                         }
1499                 }
1500
1501                 internal override Rectangle WorkingArea {
1502                         get {
1503                                 RECT    rect;
1504
1505                                 rect = new RECT();
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));
1509                         }
1510                 }
1511
1512                 internal override bool ThemesEnabled {
1513                         get {
1514                                 return XplatUIWin32.themes_enabled;
1515                         }
1516                 }
1517
1518                 internal override bool RequiresPositiveClientAreaSize {
1519                         get {
1520                                 return false;
1521                         }
1522                 }
1523
1524                 public override int ToolWindowCaptionHeight {
1525                         get {
1526                                 return Win32GetSystemMetrics (SystemMetrics.SM_CYSMCAPTION);
1527                         }
1528                 }
1529
1530                 public override Size ToolWindowCaptionButtonSize {
1531                         get {
1532                                 return new Size (
1533                                         Win32GetSystemMetrics (SystemMetrics.SM_CXSMSIZE),
1534                                         Win32GetSystemMetrics (SystemMetrics.SM_CYSMSIZE));
1535                         }
1536                 }
1537                 #endregion      // Static Properties
1538
1539                 #region Singleton Specific Code
1540                 public static XplatUIWin32 GetInstance() {
1541                         if (instance==null) {
1542                                 instance=new XplatUIWin32();
1543                         }
1544                         ref_count++;
1545                         return instance;
1546                 }
1547
1548                 public int Reference {
1549                         get {
1550                                 return ref_count;
1551                         }
1552                 }
1553                 #endregion
1554
1555                 #region Public Static Methods
1556                 internal override IntPtr InitializeDriver() {
1557                         return IntPtr.Zero;
1558                 }
1559
1560                 internal override void ShutdownDriver(IntPtr token) {
1561                         Console.WriteLine("XplatUIWin32 ShutdownDriver called");
1562                 }
1563
1564
1565                 internal void Version() {
1566                         Console.WriteLine("Xplat version $revision: $");
1567                 }
1568
1569                 internal override void AudibleAlert() {
1570                         Win32PlaySound("Default", IntPtr.Zero, SndFlags.SND_ALIAS | SndFlags.SND_ASYNC | SndFlags.SND_NOSTOP | SndFlags.SND_NOWAIT);
1571                 }
1572
1573                 internal override void GetDisplaySize(out Size size) {
1574                         RECT    rect;
1575
1576                         Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
1577
1578                         size = new Size(rect.right - rect.left, rect.bottom - rect.top);
1579                 }
1580
1581                 internal override void EnableThemes() {
1582                         themes_enabled=true;
1583                 }
1584
1585                 internal override IntPtr CreateWindow(CreateParams cp) {
1586                         IntPtr  WindowHandle;
1587                         IntPtr  ParentHandle;
1588                         Hwnd    hwnd;
1589
1590                         hwnd = new Hwnd();
1591
1592                         ParentHandle=cp.Parent;
1593
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;
1597                         }
1598
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;
1603                         }
1604
1605                         Point location;
1606                         if (cp.HasWindowManager) {
1607                                 location = Hwnd.GetNextStackedFormLocation (cp, Hwnd.ObjectFromHandle (cp.Parent));
1608                         } else {
1609                                 location = new Point (cp.X, cp.Y);
1610                         }
1611
1612                         string class_name = RegisterWindowClass (cp.ClassStyle);
1613                         HwndCreating = hwnd;
1614
1615                         // We cannot actually send the WS_EX_MDICHILD flag to Windows because we
1616                         // are faking MDI, not uses Windows' version.
1617                         if ((cp.WindowExStyle & WindowExStyles.WS_EX_MDICHILD) == WindowExStyles.WS_EX_MDICHILD)
1618                                 cp.WindowExStyle ^= WindowExStyles.WS_EX_MDICHILD;
1619                                 
1620                         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);
1621
1622                         HwndCreating = null;
1623
1624                         if (WindowHandle==IntPtr.Zero) {
1625                                 int error = Marshal.GetLastWin32Error ();
1626
1627                                 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1628                         }
1629
1630                         hwnd.ClientWindow = WindowHandle;
1631                         hwnd.Mapped = true;
1632                         Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1633
1634                         return WindowHandle;
1635                 }
1636
1637                 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1638                         CreateParams create_params = new CreateParams();
1639
1640                         create_params.Caption = "";
1641                         create_params.X = X;
1642                         create_params.Y = Y;
1643                         create_params.Width = Width;
1644                         create_params.Height = Height;
1645
1646                         create_params.ClassName=XplatUI.DefaultClassName;
1647                         create_params.ClassStyle = 0;
1648                         create_params.ExStyle=0;
1649                         create_params.Parent=IntPtr.Zero;
1650                         create_params.Param=0;
1651
1652                         return CreateWindow(create_params);
1653                 }
1654
1655                 internal override void DestroyWindow(IntPtr handle) {
1656                         Hwnd    hwnd;
1657
1658                         hwnd = Hwnd.ObjectFromHandle(handle);
1659                         Win32DestroyWindow(handle);
1660                         hwnd.Dispose();
1661                         return;
1662                 }
1663
1664                 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1665                         // We do nothing, Form has to handle WM_GETMINMAXINFO
1666                 }
1667
1668
1669                 internal override FormWindowState GetWindowState(IntPtr handle) {
1670                         uint style;
1671
1672                         style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1673                         if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1674                                 return FormWindowState.Maximized;
1675                         } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1676                                 return FormWindowState.Minimized;
1677                         }
1678                         return FormWindowState.Normal;
1679                 }
1680
1681                 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1682                         switch(state) {
1683                                 case FormWindowState.Normal: {
1684                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1685                                         return;
1686                                 }
1687
1688                                 case FormWindowState.Minimized: {
1689                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1690                                         return;
1691                                 }
1692
1693                                 case FormWindowState.Maximized: {
1694                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1695                                         return;
1696                                 }
1697                         }
1698                 }
1699
1700                 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1701
1702                         Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1703                         Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1704
1705                         // From MSDN:
1706                         // Certain window data is cached, so changes you make using SetWindowLong
1707                         // will not take effect until you call the SetWindowPos function. Specifically, 
1708                         // if you change any of the frame styles, you must call SetWindowPos with
1709                         // the SWP_FRAMECHANGED flag for the cache to be updated properly.
1710                         if (cp.control is Form)
1711                                 XplatUI.RequestNCRecalc (handle);
1712                 }
1713
1714                 internal override double GetWindowTransparency(IntPtr handle)
1715                 {
1716                         LayeredWindowAttributes lwa;
1717                         COLORREF clrRef;
1718                         byte alpha;
1719
1720                         if (0 == Win32GetLayeredWindowAttributes (handle, out clrRef, out alpha, out lwa))
1721                                 return 1.0;
1722
1723                         return ((double)alpha) / 255.0;
1724                 }
1725
1726                 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1727                         LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1728                         byte opacity = (byte)(transparency*255);
1729                         COLORREF clrRef = new COLORREF();
1730                         if (key != Color.Empty) {
1731                                 clrRef.R = key.R;
1732                                 clrRef.G = key.G;
1733                                 clrRef.B = key.B;
1734                                 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1735                         }
1736                         RECT rc;
1737                         rc.right = 1000;
1738                         rc.bottom = 1000;
1739                         Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1740                 }
1741
1742                 TransparencySupport support;
1743                 bool queried_transparency_support;
1744                 internal override TransparencySupport SupportsTransparency() {
1745                         if (queried_transparency_support)
1746                                 return support;
1747
1748                         bool flag;
1749                         support = TransparencySupport.None;
1750
1751                         flag = true;
1752                         try {
1753                                 Win32SetLayeredWindowAttributes (IntPtr.Zero, new COLORREF (), 255, LayeredWindowAttributes.LWA_ALPHA);
1754                         }
1755                         catch (EntryPointNotFoundException) { flag = false; }
1756                         catch { /* swallow everything else */ }
1757
1758                         if (flag) support |= TransparencySupport.Set;
1759
1760                         flag = true;
1761                         try {
1762                                 LayeredWindowAttributes lwa;
1763                                 COLORREF clrRef;
1764                                 byte alpha;
1765
1766                                 Win32GetLayeredWindowAttributes (IntPtr.Zero, out clrRef, out alpha, out lwa);
1767                         }
1768                         catch (EntryPointNotFoundException) { flag = false; }
1769                         catch { /* swallow everything else */ }
1770
1771                         if (flag) support |= TransparencySupport.Get;
1772
1773                         queried_transparency_support = true;
1774                         return support;
1775                 }
1776
1777                 internal override void UpdateWindow(IntPtr handle) {
1778                         Win32UpdateWindow(handle);
1779                 }
1780
1781                 internal override PaintEventArgs PaintEventStart(ref Message msg, IntPtr handle, bool client) {
1782                         IntPtr          hdc;
1783                         PAINTSTRUCT     ps;
1784                         PaintEventArgs  paint_event;
1785                         RECT            rect;
1786                         Rectangle       clip_rect;
1787                         Hwnd            hwnd;
1788
1789                         clip_rect = new Rectangle();
1790                         rect = new RECT();
1791                         ps = new PAINTSTRUCT();
1792
1793                         hwnd = Hwnd.ObjectFromHandle(msg.HWnd);
1794                         
1795                         if (client) {
1796                                 if (Win32GetUpdateRect(msg.HWnd, ref rect, false)) {
1797                                         if (handle != msg.HWnd) {
1798                                                 // We need to validate the window where the paint message
1799                                                 // was generated, otherwise we'll never stop getting paint 
1800                                                 // messages.
1801                                                 Win32GetClientRect (msg.HWnd, out rect);
1802                                                 Win32ValidateRect (msg.HWnd, ref rect);
1803                                                 hdc = Win32GetDC (handle);
1804                                         } else {
1805                                                 hdc = Win32BeginPaint (handle, ref ps);
1806                                                 rect = ps.rcPaint;
1807                                         }
1808                                 } else {
1809                                         hdc = Win32GetDC(handle);
1810                                 }
1811                                 clip_rect = rect.ToRectangle ();
1812                         } else {
1813                                 hdc = Win32GetWindowDC (handle);
1814
1815                                 // HACK this in for now
1816                                 Win32GetWindowRect (handle, out rect);
1817                                 clip_rect = new Rectangle (0, 0, rect.Width, rect.Height);
1818                         }
1819
1820                         // If we called BeginPaint, store the PAINTSTRUCT,
1821                         // otherwise store hdc, so that PaintEventEnd can know
1822                         // whether to call EndPaint or ReleaseDC.
1823                         if (ps.hdc != IntPtr.Zero) {
1824                                 hwnd.drawing_stack.Push (ps);
1825                         } else {
1826                                 hwnd.drawing_stack.Push (hdc);
1827                         }
1828                         
1829                         Graphics dc = Graphics.FromHdc(hdc);
1830                         hwnd.drawing_stack.Push (dc);
1831
1832                         paint_event = new PaintEventArgs(dc, clip_rect);
1833
1834                         return paint_event;
1835                 }
1836
1837                 internal override void PaintEventEnd(ref Message m, IntPtr handle, bool client) {
1838                         Hwnd            hwnd;
1839
1840                         hwnd = Hwnd.ObjectFromHandle(m.HWnd);
1841
1842                         Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1843                         dc.Dispose ();
1844
1845                         object o = hwnd.drawing_stack.Pop();
1846                         if (o is IntPtr) {
1847                                 IntPtr hdc = (IntPtr) o;
1848                                 Win32ReleaseDC (handle, hdc);
1849                         } else if (o is PAINTSTRUCT) {
1850                                 PAINTSTRUCT ps = (PAINTSTRUCT) o;
1851                                 Win32EndPaint (handle, ref ps);
1852                         }
1853                 }
1854
1855
1856                 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1857                         Win32MoveWindow(handle, x, y, width, height, true);
1858                         return;
1859                 }
1860
1861                 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) {
1862                         IntPtr  parent;
1863                         RECT    rect;
1864                         POINT   pt;
1865
1866                         Win32GetWindowRect(handle, out rect);
1867                         width = rect.right - rect.left;
1868                         height = rect.bottom - rect.top;
1869
1870                         pt.x=rect.left;
1871                         pt.y=rect.top;
1872
1873                         parent = Win32GetAncestor (handle, AncestorType.GA_PARENT);
1874                         if (parent != IntPtr.Zero && parent != Win32GetDesktopWindow ())
1875                                 Win32ScreenToClient(parent, ref pt);
1876
1877                         x = pt.x;
1878                         y = pt.y;
1879
1880                         Win32GetClientRect(handle, out rect);
1881                         client_width = rect.right - rect.left;
1882                         client_height = rect.bottom - rect.top;
1883                         return;
1884                 }
1885
1886                 internal override void Activate(IntPtr handle) {
1887                         Win32SetActiveWindow(handle);
1888                         // delayed timer enabled
1889                         lock (timer_list) {
1890                                 foreach (Timer t in timer_list.Values) {
1891                                         if (t.Enabled && t.window == IntPtr.Zero) {
1892                                                 t.window = handle;
1893                                                 int id = t.GetHashCode ();
1894                                                 Win32SetTimer(handle, id, (uint)t.Interval, IntPtr.Zero);
1895                                         }
1896                                 }
1897                         }
1898                 }
1899
1900                 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1901                         RECT rect;
1902
1903                         rect.left=rc.Left;
1904                         rect.top=rc.Top;
1905                         rect.right=rc.Right;
1906                         rect.bottom=rc.Bottom;
1907                         Win32InvalidateRect(handle, ref rect, clear);
1908                 }
1909
1910
1911                 internal override void InvalidateNC (IntPtr handle)
1912                 {
1913                         // found this gem at
1914                         // http://www.dotnet247.com/247reference/msgs/58/292037.aspx
1915                         Win32SetWindowPos(handle, IntPtr.Zero,
1916                                           0, 0, 0, 0,
1917                                           SetWindowPosFlags.SWP_NOMOVE |
1918                                           SetWindowPosFlags.SWP_NOSIZE |
1919                                           SetWindowPosFlags.SWP_NOZORDER |
1920                                           SetWindowPosFlags.SWP_NOACTIVATE |
1921                                           SetWindowPosFlags.SWP_DRAWFRAME);
1922                 }
1923
1924                 private IntPtr InternalWndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
1925                 {
1926                         if (HwndCreating != null && HwndCreating.ClientWindow == IntPtr.Zero)
1927                                 HwndCreating.ClientWindow = hWnd;
1928                         return NativeWindow.WndProc (hWnd, msg, wParam, lParam);
1929                 }
1930
1931                 internal override IntPtr DefWndProc(ref Message msg) {
1932                         msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1933                         return msg.Result;
1934                 }
1935
1936                 internal override void HandleException(Exception e) {
1937                         StackTrace st = new StackTrace(e);
1938                         Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1939                         Console.WriteLine("{0}{1}", e.Message, st.ToString());
1940                 }
1941
1942                 internal override void DoEvents() {
1943                         MSG msg = new MSG();
1944
1945                         if (override_cursor != IntPtr.Zero) {
1946                                 Cursor.Current = null;
1947                         }
1948
1949                         while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1950                                 XplatUI.TranslateMessage(ref msg);
1951                                 XplatUI.DispatchMessage(ref msg);
1952                         }
1953                 }
1954
1955                 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1956                         return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1957                 }
1958
1959                 internal override void PostQuitMessage(int exitCode) {
1960                         Win32PostQuitMessage(exitCode);
1961                 }
1962
1963                 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
1964                 {
1965                         if (wm_nc_registered == null)
1966                                 wm_nc_registered = new Hashtable ();
1967                                 
1968                         TMEFlags flags = TMEFlags.TME_NONCLIENT;
1969                         if (hover)
1970                                 flags |= TMEFlags.TME_HOVER;
1971                         if (leave)
1972                                 flags |= TMEFlags.TME_LEAVE;
1973
1974                         if (flags == TMEFlags.TME_NONCLIENT) {
1975                                 if (wm_nc_registered.Contains (hwnd)) {
1976                                         wm_nc_registered.Remove (hwnd);
1977                                 }
1978                         } else {
1979                                 if (!wm_nc_registered.Contains (hwnd)) {
1980                                         wm_nc_registered.Add (hwnd, flags);
1981                                 } else {
1982                                         wm_nc_registered [hwnd] = flags;
1983                                 }
1984                         }
1985                 }
1986
1987                 internal override void RequestNCRecalc(IntPtr handle) {
1988                         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);
1989                 }
1990
1991                 internal override void ResetMouseHover(IntPtr handle) {
1992                         TRACKMOUSEEVENT tme;
1993
1994                         tme = new TRACKMOUSEEVENT();
1995                         tme.size = Marshal.SizeOf(tme);
1996                         tme.hWnd = handle;
1997                         tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1998                         Win32TrackMouseEvent(ref tme);
1999                 }
2000
2001
2002                 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
2003                         return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
2004                 }
2005
2006                 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
2007                         bool            result;
2008
2009                         msg.refobject = 0;
2010                         if (RetrieveMessage(ref msg)) {
2011                                 return true;
2012                         }
2013
2014                         if (blocking) {
2015                                 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
2016                         } else {
2017                                 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
2018                                 if (!result) {
2019                                         return false;
2020                                 }
2021                         }
2022
2023                         // We need to fake WM_MOUSE_ENTER
2024                         switch (msg.message) {
2025                                 case Msg.WM_LBUTTONDOWN: {
2026                                         mouse_state |= MouseButtons.Left;
2027                                         break;
2028                                 }
2029
2030                                 case Msg.WM_MBUTTONDOWN: {
2031                                         mouse_state |= MouseButtons.Middle;
2032                                         break;
2033                                 }
2034
2035                                 case Msg.WM_RBUTTONDOWN: {
2036                                         mouse_state |= MouseButtons.Right;
2037                                         break;
2038                                 }
2039
2040                                 case Msg.WM_LBUTTONUP: {
2041                                         mouse_state &= ~MouseButtons.Left;
2042                                         break;
2043                                 }
2044
2045                                 case Msg.WM_MBUTTONUP: {
2046                                         mouse_state &= ~MouseButtons.Middle;
2047                                         break;
2048                                 }
2049
2050                                 case Msg.WM_RBUTTONUP: {
2051                                         mouse_state &= ~MouseButtons.Right;
2052                                         break;
2053                                 }
2054
2055                                 case Msg.WM_ASYNC_MESSAGE: {
2056                                         XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
2057                                         break;
2058                                 }
2059
2060                                 case Msg.WM_MOUSEMOVE: {
2061                                         if (msg.hwnd != prev_mouse_hwnd) {
2062                                                 TRACKMOUSEEVENT tme;
2063
2064                                                 // The current message will be sent out next time around
2065                                                 StoreMessage(ref msg);
2066
2067                                                 // This is the message we want to send at this point
2068                                                 msg.message = Msg.WM_MOUSE_ENTER;
2069
2070                                                 prev_mouse_hwnd = msg.hwnd;
2071
2072                                                 tme = new TRACKMOUSEEVENT();
2073                                                 tme.size = Marshal.SizeOf(tme);
2074                                                 tme.hWnd = msg.hwnd;
2075                                                 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
2076                                                 Win32TrackMouseEvent(ref tme);
2077                                                 return result;
2078                                         }
2079                                         break;
2080                                 }
2081
2082                                 case Msg.WM_NCMOUSEMOVE: {
2083                                         if (wm_nc_registered == null || !wm_nc_registered.Contains (msg.hwnd))
2084                                                 break;
2085                                                 
2086                                         TRACKMOUSEEVENT tme;
2087
2088                                         tme = new TRACKMOUSEEVENT ();
2089                                         tme.size = Marshal.SizeOf(tme);
2090                                         tme.hWnd = msg.hwnd;
2091                                         tme.dwFlags = (TMEFlags)wm_nc_registered[msg.hwnd];
2092                                         Win32TrackMouseEvent (ref tme);
2093                                         return result;
2094                                 }
2095
2096                                 case Msg.WM_DROPFILES: {
2097                                         return Win32DnD.HandleWMDropFiles(ref msg);
2098                                 }
2099
2100                                 case Msg.WM_MOUSELEAVE: {
2101                                         prev_mouse_hwnd = IntPtr.Zero;
2102                                         break;
2103                                 }
2104
2105                                 case Msg.WM_TIMER: {
2106                                         Timer timer=(Timer)timer_list[(int)msg.wParam];
2107
2108                                         if (timer != null) {
2109                                                 timer.FireTick();
2110                                         }
2111                                         break;
2112                                 }
2113                         }
2114
2115                         return result;
2116                 }
2117
2118                 internal override bool TranslateMessage(ref MSG msg) {
2119                         return Win32TranslateMessage(ref msg);
2120                 }
2121
2122                 internal override IntPtr DispatchMessage(ref MSG msg) {
2123                         return Win32DispatchMessage(ref msg);
2124                 }
2125
2126                 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
2127                         if (Top) {
2128                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2129                                 return true;
2130                         } else if (!Bottom) {
2131                                 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2132                         } else {
2133                                 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2134                                 return true;
2135                         }
2136                         return false;
2137                 }
2138
2139                 internal override bool SetTopmost(IntPtr hWnd, bool Enabled) {
2140                         if (Enabled) {
2141                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
2142                                 return true;
2143                         } else {
2144                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
2145                                 return true;
2146                         }
2147                 }
2148                 
2149                 internal override bool SetOwner(IntPtr hWnd, IntPtr hWndOwner) {
2150                         Win32SetWindowLong(hWnd, WindowLong.GWL_HWNDPARENT, (uint) hWndOwner);
2151                         return true;
2152                 }
2153
2154                 internal override bool Text(IntPtr handle, string text) {
2155                         Win32SetWindowText(handle, text);
2156                         return true;
2157                 }
2158
2159                 internal override bool GetText(IntPtr handle, out string text) {
2160                         StringBuilder sb;
2161
2162                         sb = new StringBuilder(256);
2163                         Win32GetWindowText(handle, sb, sb.Capacity);
2164                         text = sb.ToString();
2165                         return true;
2166                 }
2167
2168                 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
2169                 {
2170                         if (visible) {
2171                                 Control c = Control.FromHandle (handle);
2172                                 if (c is Form) {
2173                                         Form f;
2174
2175                                         f = (Form)Control.FromHandle (handle);
2176                                         WindowPlacementFlags flags = WindowPlacementFlags.SW_SHOWNORMAL;
2177                                         switch (f.WindowState) {
2178                                                 case FormWindowState.Normal: flags = WindowPlacementFlags.SW_SHOWNORMAL; break;
2179                                                 case FormWindowState.Minimized: flags = WindowPlacementFlags.SW_MINIMIZE; break;
2180                                                 case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
2181                                         }
2182                                         
2183                                         if (!f.ActivateOnShow)
2184                                                 flags = WindowPlacementFlags.SW_SHOWNOACTIVATE;
2185                                                 
2186                                         Win32ShowWindow (handle, flags);
2187                                 }
2188                                 else {
2189                                         if (c.ActivateOnShow)
2190                                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
2191                                         else
2192                                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
2193                                 }
2194                         }
2195                         else {
2196                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_HIDE);
2197                         }
2198                         return true;
2199                 }
2200
2201                 internal override bool IsEnabled(IntPtr handle) {
2202                         return IsWindowEnabled (handle);
2203                 }
2204
2205                 internal override bool IsKeyLocked (VirtualKeys key)
2206                 {
2207                         return (Win32GetKeyState (key) & 1) == 1;
2208                 }
2209                 
2210                 internal override bool IsVisible(IntPtr handle) {
2211                         return IsWindowVisible (handle);
2212                 }
2213
2214                 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
2215                         Control c = Control.FromHandle (handle);
2216                         if (parent == IntPtr.Zero) {
2217                                 if (!(c is Form)) {
2218                                         Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
2219                                 }
2220                         } else {
2221                                 if (!(c is Form)) {
2222                                         SetVisible (handle, c.is_visible, true);
2223                                 }
2224                         }       
2225                         // The Win32SetParent is lame, it can very well move the window
2226                         // ref: http://groups.google.com/group/microsoft.public.vb.winapi/browse_thread/thread/1b82ccc54231ecee/afa82835bfc0422a%23afa82835bfc0422a
2227                         // Here we save the position before changing the parent, and if it has changed afterwards restore it.
2228                         // Another possibility would be to intercept WM_WINDOWPOSCHANGING and restore the coords there, but this would require plumbing in weird places
2229                         // (either inside Control or add handling to InternalWndProc)
2230                         // We also need to remove WS_CHILD if making the window parent-less, and add it if we're parenting it.
2231                         RECT rect, rect2;
2232                         IntPtr result;
2233                         WindowStyles style, new_style;
2234                         
2235                         Win32GetWindowRect (handle, out rect);
2236                         style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
2237                         
2238                         if (parent == IntPtr.Zero) {
2239                                 new_style = style & ~WindowStyles.WS_CHILD;
2240                                 result = Win32SetParent (handle, FosterParent);
2241                         } else {
2242                                 new_style = style | WindowStyles.WS_CHILD;
2243                                 result = Win32SetParent (handle, parent);
2244                         }
2245                         if (style != new_style && c is Form) {
2246                                 Win32SetWindowLong (handle, WindowLong.GWL_STYLE, (uint) new_style);
2247                         }
2248                         Win32GetWindowRect (handle, out rect2);
2249                         if (rect.top != rect2.top && rect.left != rect2.left && c is Form) {
2250                                 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);
2251                         }
2252                         return result;
2253                 }
2254
2255                 // If we ever start using this, we should probably replace FosterParent with IntPtr.Zero
2256                 internal override IntPtr GetParent(IntPtr handle) {
2257                         return Win32GetParent(handle);
2258                 }
2259
2260                 // This is a nop on win32 and x11
2261                 internal override IntPtr GetPreviousWindow(IntPtr handle) {
2262                         return handle;
2263                 }
2264
2265                 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
2266                         grab_hwnd = hWnd;
2267                         Win32SetCapture(hWnd);
2268                         
2269                         if (ConfineToHwnd != IntPtr.Zero) {
2270                                 RECT window_rect;
2271                                 Win32GetWindowRect (ConfineToHwnd, out window_rect);
2272                                 Win32GetClipCursor (out clipped_cursor_rect);
2273                                 Win32ClipCursor (ref window_rect);
2274                         }
2275                 }
2276
2277                 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
2278                         hWnd = grab_hwnd;
2279                         GrabConfined = grab_confined;
2280                         GrabArea = grab_area;
2281                 }
2282
2283                 internal override void UngrabWindow(IntPtr hWnd) {
2284                         if (!(clipped_cursor_rect.top == 0 && clipped_cursor_rect.bottom == 0 && clipped_cursor_rect.left == 0 && clipped_cursor_rect.right == 0)) {
2285                                 Win32ClipCursor (ref clipped_cursor_rect);
2286                                 clipped_cursor_rect = new RECT ();
2287                         }
2288                         
2289                         Win32ReleaseCapture();
2290                         grab_hwnd = IntPtr.Zero;
2291                 }
2292
2293                 internal override bool CalculateWindowRect(ref Rectangle ClientRect, CreateParams cp, Menu menu, out Rectangle WindowRect) {
2294                         RECT    rect;
2295
2296                         rect.left=ClientRect.Left;
2297                         rect.top=ClientRect.Top;
2298                         rect.right=ClientRect.Right;
2299                         rect.bottom=ClientRect.Bottom;
2300
2301                         if (!Win32AdjustWindowRectEx(ref rect, cp.Style, menu != null, cp.ExStyle)) {
2302                                 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
2303                                 return false;
2304                         }
2305
2306                         WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
2307                         return true;
2308                 }
2309
2310                 internal override void SetCursor(IntPtr window, IntPtr cursor) {
2311                         Win32SetCursor(cursor);
2312                         return;
2313                 }
2314
2315                 internal override void ShowCursor(bool show) {
2316                         Win32ShowCursor(show);
2317                 }
2318
2319                 internal override void OverrideCursor(IntPtr cursor) {
2320                         Win32SetCursor(cursor);
2321                 }
2322
2323                 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
2324                         IntPtr  cursor;
2325                         Bitmap  cursor_bitmap;
2326                         Bitmap  cursor_mask;
2327                         Byte[]  cursor_bits;
2328                         Byte[]  mask_bits;
2329                         Color   pixel;
2330                         int     width;
2331                         int     height;
2332
2333                         // Win32 only allows creation cursors of a certain size
2334                         if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
2335                                 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
2336                                 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
2337                         } else {
2338                                 cursor_bitmap = bitmap;
2339                                 cursor_mask = mask;
2340                         }
2341
2342                         width = cursor_bitmap.Width;
2343                         height = cursor_bitmap.Height;
2344
2345                         cursor_bits = new Byte[(width / 8) * height];
2346                         mask_bits = new Byte[(width / 8) * height];
2347
2348                         for (int y = 0; y < height; y++) {
2349                                 for (int x = 0; x < width; x++) {
2350                                         pixel = cursor_bitmap.GetPixel(x, y);
2351
2352                                         if (pixel == cursor_pixel) {
2353                                                 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
2354                                         }
2355
2356                                         pixel = cursor_mask.GetPixel(x, y);
2357
2358                                         if (pixel == mask_pixel) {
2359                                                 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
2360                                         }
2361                                 }
2362                         }
2363
2364                         cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
2365
2366                         return cursor;
2367                 }
2368
2369                 internal override Bitmap DefineStdCursorBitmap (StdCursor id)
2370                 {
2371                         // We load the cursor, create a bitmap, draw the cursor onto the bitmap and return the bitmap.
2372                         IntPtr cursor = DefineStdCursor (id);
2373                         // Windows only have one possible cursor size!
2374                         int width = Win32GetSystemMetrics (SystemMetrics.SM_CXCURSOR);
2375                         int height = Win32GetSystemMetrics (SystemMetrics.SM_CYCURSOR);
2376                         Bitmap bmp = new Bitmap (width, height);
2377                         Graphics gc = Graphics.FromImage (bmp);
2378                         IntPtr hdc = gc.GetHdc ();
2379                         Win32DrawIcon (hdc, 0, 0, cursor);
2380                         gc.ReleaseHdc (hdc);
2381                         gc.Dispose ();
2382                         return bmp;
2383                 }
2384
2385                 [MonoTODO("Define the missing cursors")]
2386                 internal override IntPtr DefineStdCursor(StdCursor id) {
2387                         switch(id) {
2388                                 case StdCursor.AppStarting:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
2389                                 case StdCursor.Arrow:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2390                                 case StdCursor.Cross:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
2391                                 case StdCursor.Default:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2392                                 case StdCursor.Hand:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
2393                                 case StdCursor.Help:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
2394                                 case StdCursor.HSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2395                                 case StdCursor.IBeam:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
2396                                 case StdCursor.No:              return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
2397                                 case StdCursor.NoMove2D:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2398                                 case StdCursor.NoMoveHoriz:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2399                                 case StdCursor.NoMoveVert:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2400                                 case StdCursor.PanEast:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2401                                 case StdCursor.PanNE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2402                                 case StdCursor.PanNorth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2403                                 case StdCursor.PanNW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2404                                 case StdCursor.PanSE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2405                                 case StdCursor.PanSouth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2406                                 case StdCursor.PanSW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2407                                 case StdCursor.PanWest:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2408                                 case StdCursor.SizeAll:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
2409                                 case StdCursor.SizeNESW:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
2410                                 case StdCursor.SizeNS:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
2411                                 case StdCursor.SizeNWSE:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
2412                                 case StdCursor.SizeWE:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
2413                                 case StdCursor.UpArrow:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
2414                                 case StdCursor.VSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2415                                 case StdCursor.WaitCursor:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
2416                         }
2417                         throw new NotImplementedException ();
2418                 }
2419
2420                 internal override void DestroyCursor(IntPtr cursor) {
2421                         if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
2422                                 Win32DestroyCursor(cursor);
2423                         }
2424                 }
2425
2426                 [MonoTODO]
2427                 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
2428                         ICONINFO ii = new ICONINFO ();
2429                         
2430                         if (!Win32GetIconInfo (cursor, out ii))
2431                                 throw new Win32Exception ();
2432                                 
2433                         width = 20;
2434                         height = 20;
2435                         hotspot_x = ii.xHotspot;
2436                         hotspot_y = ii.yHotspot;
2437                 }
2438
2439                 internal override void SetCursorPos(IntPtr handle, int x, int y) {
2440                         Win32SetCursorPos(x, y);
2441                 }
2442
2443                 internal override Region GetClipRegion(IntPtr hwnd) {
2444                         Region region;
2445
2446                         region = new Region();
2447
2448                         Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
2449
2450                         return region;
2451                 }
2452
2453                 internal override void SetClipRegion(IntPtr hwnd, Region region) {
2454                         if (region == null)
2455                                 Win32SetWindowRgn (hwnd, IntPtr.Zero, true);
2456                         else
2457                                 Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
2458                 }
2459
2460                 internal override void EnableWindow(IntPtr handle, bool Enable) {
2461                         Win32EnableWindow(handle, Enable);
2462                 }
2463
2464                 internal override void EndLoop(System.Threading.Thread thread) {
2465                         // Nothing to do
2466                 }
2467
2468                 internal override object StartLoop(System.Threading.Thread thread) {
2469                         return null;
2470                 }
2471
2472                 internal override void SetModal(IntPtr handle, bool Modal) {
2473                         // we do nothing on Win32
2474                 }
2475
2476                 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
2477                         POINT   pt;
2478
2479                         Win32GetCursorPos(out pt);
2480
2481                         if (handle!=IntPtr.Zero) {
2482                                 Win32ScreenToClient(handle, ref pt);
2483                         }
2484
2485                         x=pt.x;
2486                         y=pt.y;
2487                 }
2488
2489                 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
2490                 {
2491                         POINT pnt = new POINT();                        
2492
2493                         pnt.x = x;
2494                         pnt.y = y;
2495                         Win32ScreenToClient (handle, ref pnt);
2496
2497                         x = pnt.x;
2498                         y = pnt.y;
2499                 }
2500
2501                 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {                    
2502                         POINT   pnt = new POINT();                      
2503
2504                         pnt.x = x;
2505                         pnt.y = y;
2506
2507                         Win32ClientToScreen(handle, ref pnt);
2508
2509                         x = pnt.x;
2510                         y = pnt.y;
2511                 }
2512
2513                 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
2514                         RECT    rect;
2515  
2516                         Win32GetWindowRect(handle, out rect);
2517                         x -= rect.left + SystemInformation.FrameBorderSize.Width;
2518                         y -= rect.top + SystemInformation.FrameBorderSize.Height;
2519
2520                         WindowStyles style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
2521                         if (CreateParams.IsSet (style, WindowStyles.WS_CAPTION)) {
2522                                 y -= ThemeEngine.Current.CaptionHeight;
2523                         }
2524                 }
2525   
2526                 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {                      
2527                         RECT    rect;
2528  
2529                         Win32GetWindowRect(handle, out rect);
2530                         x += rect.left + SystemInformation.FrameBorderSize.Width;
2531                         y += rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
2532                         return;
2533                 }
2534
2535                 internal override void SendAsyncMethod (AsyncMethodData method)
2536                 {
2537                         Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
2538                 }
2539
2540                 internal override void SetTimer (Timer timer)
2541                 {
2542                         int     index;
2543
2544                         index = timer.GetHashCode();
2545
2546                         lock (timer_list) {
2547                                 timer_list[index]=timer;
2548                         }
2549
2550                         if (Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero) != IntPtr.Zero)
2551                                 timer.window = FosterParent;
2552                         else
2553                                 timer.window = IntPtr.Zero;
2554                 }
2555
2556                 internal override void KillTimer (Timer timer)
2557                 {
2558                         int     index;
2559
2560                         index = timer.GetHashCode();
2561
2562                         Win32KillTimer(timer.window, index);
2563
2564                         lock (timer_list) {
2565                                 timer_list.Remove(index);
2566                         }
2567                 }
2568                 
2569                 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
2570                         Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
2571                         caret_visible = false;
2572                 }
2573
2574                 internal override void DestroyCaret(IntPtr hwnd) {
2575                         Win32DestroyCaret();
2576                 }
2577
2578                 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
2579                         Win32SetCaretPos(x, y);
2580                 }
2581
2582                 internal override void CaretVisible(IntPtr hwnd, bool visible) {
2583                         if (visible) {
2584                                 if (!caret_visible) {
2585                                         Win32ShowCaret(hwnd);
2586                                         caret_visible = true;
2587                                 }
2588                         } else {
2589                                 if (caret_visible) {
2590                                         Win32HideCaret(hwnd);
2591                                         caret_visible = false;
2592                                 }
2593                         }
2594                 }
2595
2596                 internal override IntPtr GetFocus() {
2597                         return Win32GetFocus();
2598                 }
2599
2600                 internal override void SetFocus(IntPtr hwnd) {
2601                         Win32SetFocus(hwnd);
2602                 }
2603
2604                 internal override IntPtr GetActive() {
2605                         return Win32GetActiveWindow();
2606                 }
2607
2608                 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
2609                         IntPtr          dc;
2610                         IntPtr          prevobj;
2611                         TEXTMETRIC      tm;
2612
2613                         tm = new TEXTMETRIC();
2614
2615                         dc = Win32GetDC (IntPtr.Zero);
2616                         prevobj = Win32SelectObject (dc, font.ToHfont ());
2617                         
2618                         if (Win32GetTextMetrics (dc, ref tm) == false) {
2619                                 prevobj = Win32SelectObject (dc, prevobj);
2620                                 Win32DeleteObject (prevobj);
2621                                 Win32ReleaseDC (IntPtr.Zero, dc);
2622                                 ascent = 0;
2623                                 descent = 0;
2624                                 return false;
2625                         }
2626                         prevobj = Win32SelectObject (dc, prevobj);
2627                         Win32DeleteObject (prevobj);
2628                         Win32ReleaseDC (IntPtr.Zero, dc);
2629
2630                         ascent = tm.tmAscent;
2631                         descent = tm.tmDescent;
2632
2633                         return true;
2634                 }
2635
2636                 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
2637                         RECT    rect;
2638
2639                         rect = new RECT();
2640                         rect.left = rectangle.X;
2641                         rect.top = rectangle.Y;
2642                         rect.right = rectangle.Right;
2643                         rect.bottom = rectangle.Bottom;
2644
2645                         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));
2646                         Win32UpdateWindow(hwnd);
2647                 }
2648
2649                 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
2650                         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));
2651                 }
2652
2653                 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
2654                         NOTIFYICONDATA  nid;
2655
2656                         nid = new NOTIFYICONDATA();
2657
2658                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2659                         nid.hWnd = hwnd;
2660                         nid.uID = 1;
2661                         nid.uCallbackMessage = (uint)Msg.WM_USER;
2662                         nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2663
2664                         if (tip != null) {       
2665                                 nid.szTip = tip;
2666                                 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2667                         }
2668
2669                         if (icon != null) {
2670                                 nid.hIcon = icon.Handle;
2671                                 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2672                         }
2673
2674                         tt = null;
2675
2676                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
2677                 }
2678
2679                 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
2680                         NOTIFYICONDATA  nid;
2681
2682                         nid = new NOTIFYICONDATA();
2683
2684                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2685                         nid.hIcon = icon.Handle;
2686                         nid.hWnd = hwnd;
2687                         nid.uID = 1;
2688                         nid.uCallbackMessage = (uint)Msg.WM_USER;
2689                         nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2690
2691                         if (tip != null) {
2692                                 nid.szTip = tip;
2693                                 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2694                         }
2695
2696                         if (icon != null) {
2697                                 nid.hIcon = icon.Handle;
2698                                 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2699                         }
2700
2701                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2702                 }
2703
2704                 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
2705                         NOTIFYICONDATA  nid;
2706
2707                         nid = new NOTIFYICONDATA();
2708
2709                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2710                         nid.hWnd = hwnd;
2711                         nid.uID = 1;
2712                         nid.uFlags = 0;
2713
2714                         Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
2715                 }
2716
2717 #if NET_2_0
2718                 internal override void SystrayBalloon(IntPtr hwnd, int timeout, string title, string text, ToolTipIcon icon)
2719                 {
2720                         NOTIFYICONDATA  nid;
2721
2722                         nid = new NOTIFYICONDATA();
2723
2724                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2725                         nid.hWnd = hwnd;
2726                         nid.uID = 1;
2727                         nid.uFlags = NotifyIconFlags.NIF_INFO;
2728                         nid.uTimeoutOrVersion = timeout;
2729                         nid.szInfoTitle = title;
2730                         nid.szInfo = text;
2731                         nid.dwInfoFlags = icon;
2732                         
2733                         Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2734                 }
2735 #endif
2736
2737                 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
2738                         // Nothing to do on Win32
2739                 }
2740
2741                 internal override void SetMenu(IntPtr handle, Menu menu) {
2742                         // Trigger WM_NCCALC
2743                         Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2744                 }
2745
2746                 internal override Point GetMenuOrigin(IntPtr handle) {
2747                         Form form = Control.FromHandle (handle) as Form;
2748                         
2749                         if (form != null) {
2750                                 if (form.FormBorderStyle == FormBorderStyle.None)
2751                                         return Point.Empty;
2752                                         
2753                                 int bordersize = (form.Width - form.ClientSize.Width) / 2;
2754                                         
2755                                 if (form.FormBorderStyle == FormBorderStyle.FixedToolWindow || form.FormBorderStyle == FormBorderStyle.SizableToolWindow)
2756                                         return new Point (bordersize, bordersize + SystemInformation.ToolWindowCaptionHeight);
2757                                 else
2758                                         return new Point (bordersize, bordersize + SystemInformation.CaptionHeight);
2759                         }
2760                         
2761                         return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2762                 }
2763
2764                 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2765                         Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : icon.Handle);    // 1 = large icon (0 would be small)
2766                 }
2767
2768                 internal override void ClipboardClose(IntPtr handle) {
2769                         if (handle != clip_magic) {
2770                                 throw new ArgumentException("handle is not a valid clipboard handle");
2771                         }
2772                         Win32CloseClipboard();
2773                 }
2774
2775                 internal override int ClipboardGetID(IntPtr handle, string format) {
2776                         if (handle != clip_magic) {
2777                                 throw new ArgumentException("handle is not a valid clipboard handle");
2778                         }
2779                         if (format == "Text" ) return 1;
2780                         else if (format == "Bitmap" ) return 2;
2781                         else if (format == "MetaFilePict" ) return 3;
2782                         else if (format == "SymbolicLink" ) return 4;
2783                         else if (format == "DataInterchangeFormat" ) return 5;
2784                         else if (format == "Tiff" ) return 6;
2785                         else if (format == "OEMText" ) return 7;
2786                         else if (format == "DeviceIndependentBitmap" ) return 8;
2787                         else if (format == "Palette" ) return 9;
2788                         else if (format == "PenData" ) return 10;
2789                         else if (format == "RiffAudio" ) return 11;
2790                         else if (format == "WaveAudio" ) return 12;
2791                         else if (format == "UnicodeText" ) return 13;
2792                         else if (format == "EnhancedMetafile" ) return 14;
2793                         else if (format == "FileDrop" ) return 15;
2794                         else if (format == "Locale" ) return 16;
2795
2796                         return (int)Win32RegisterClipboardFormat(format);
2797                 }
2798
2799                 internal override IntPtr ClipboardOpen(bool primary_selection) {
2800                         // Win32 does not have primary selection
2801                         Win32OpenClipboard(FosterParent);
2802                         return clip_magic;
2803                 }
2804
2805                 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2806                         uint    format;
2807                         int[]   result;
2808                         int     count;
2809
2810                         if (handle != clip_magic) {
2811                                 return null;
2812                         }
2813
2814                         // Count first
2815                         count = 0;
2816                         format = 0;
2817                         do {
2818                                 format = Win32EnumClipboardFormats(format);
2819                                 if (format != 0) {
2820                                         count++;
2821                                 }
2822                         } while (format != 0);
2823
2824                         // Now assign
2825                         result = new int[count];
2826                         count = 0;
2827                         format = 0;
2828                         do {
2829                                 format = Win32EnumClipboardFormats(format);
2830                                 if (format != 0) {
2831                                         result[count++] = (int)format;
2832                                 }
2833                         } while (format != 0);
2834
2835                         return result;
2836                 }
2837
2838
2839                 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2840                         IntPtr  hmem;
2841                         IntPtr  data;
2842                         object  obj;
2843
2844                         if (handle != clip_magic) {
2845                                 throw new ArgumentException("handle is not a valid clipboard handle");
2846                         }
2847
2848                         hmem = Win32GetClipboardData((uint)type);
2849                         if (hmem == IntPtr.Zero) {
2850                                 return null;
2851                         }
2852
2853                         data = Win32GlobalLock(hmem);
2854                         if (data == IntPtr.Zero) {
2855                                 uint error = Win32GetLastError();
2856                                 Console.WriteLine("Error: {0}", error);
2857                                 return null;
2858                         }
2859
2860                         obj = null;
2861
2862                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2863                                 obj = AnsiToString(data);
2864                         } else switch ((ClipboardFormats)type) {
2865                                 case ClipboardFormats.CF_TEXT: {
2866                                         obj = AnsiToString(data);
2867                                         break;
2868                                 }
2869
2870                                 case ClipboardFormats.CF_DIB: {
2871                                         obj = DIBtoImage(data);
2872                                         break;
2873                                 }
2874
2875                                 case ClipboardFormats.CF_UNICODETEXT: {
2876                                         obj = UnicodeToString(data);
2877                                         break;
2878                                 }
2879
2880                                 default: {
2881                                         if (converter != null && !converter(type, data, out obj)) {
2882                                                 obj = null;
2883                                         }
2884                                         break;
2885                                 }
2886                         }
2887                         Win32GlobalUnlock(hmem);
2888
2889                         return obj;
2890
2891                 }
2892
2893                 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
2894                         byte[]  data = null;
2895
2896                         if (handle != clip_magic) {
2897                                 throw new ArgumentException("handle is not a valid clipboard handle");
2898                         }
2899
2900                         if (obj == null) {
2901                                 // Just clear it
2902                                 if (!Win32EmptyClipboard())
2903                                         throw new ExternalException("Win32EmptyClipboard");
2904                                 return;
2905                         }
2906
2907                         if (type == -1) {
2908                                 if (obj is string) {
2909                                         type = (int)ClipboardFormats.CF_UNICODETEXT;
2910                                 } else if (obj is Image) {
2911                                         type = (int)ClipboardFormats.CF_DIB;
2912                                 }
2913                         }
2914
2915                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2916                                 data = StringToAnsi ((string)obj);
2917                         } else switch((ClipboardFormats)type) {
2918                                 case ClipboardFormats.CF_UNICODETEXT: {
2919                                         data = StringToUnicode ((string)obj);
2920                                         break;
2921                                 }
2922
2923                                 case ClipboardFormats.CF_TEXT: {
2924                                         data = StringToAnsi ((string)obj);
2925                                         break;
2926                                 }
2927
2928                                 case ClipboardFormats.CF_BITMAP:
2929                                 case ClipboardFormats.CF_DIB: {
2930                                         data = ImageToDIB ((Image)obj);
2931                                         type = (int)ClipboardFormats.CF_DIB;
2932                                         break;
2933                                 }
2934
2935                                 default: {
2936                                         if (converter != null && !converter(ref type, obj, out data)) {
2937                                                 data = null; // ensure that a failed conversion leaves null.
2938                                         }
2939                                         break;
2940                                 }
2941                         }
2942                         if (data != null) {
2943                                 SetClipboardData ((uint)type, data);
2944                         }
2945                 }
2946
2947                 internal static byte[] StringToUnicode (string text)
2948                 {
2949                         return Encoding.Unicode.GetBytes (text + "\0");
2950                 }
2951
2952                 internal static byte[] StringToAnsi (string text)
2953                 {
2954                         // FIXME, follow the behaviour of the previous code using UTF-8,
2955                         // but this should be 'ANSI' on Windows, i.e. the current code page.
2956                         // Does Encoding.Default work on Windows?
2957                         return Encoding.UTF8.GetBytes (text + "\0");
2958                 }
2959
2960                 private void SetClipboardData (uint type, byte[] data)
2961                 {
2962                         if (data.Length == 0)
2963                                 // Shouldn't call Win32SetClipboard with NULL, as, from MSDN:
2964                                 // "This parameter can be NULL, indicating that the window provides data 
2965                                 //  in the specified clipboard format (renders the format) upon request."
2966                                 // and I don't think we support that...
2967                                 // Note this is unrelated to the fact that passing a null obj to 
2968                                 // ClipboardStore is actually a request to empty the clipboard!
2969                                 return;
2970                         IntPtr hmem = CopyToMoveableMemory (data);
2971                         if (hmem == IntPtr.Zero)
2972                                 // As above, should not call with null.
2973                                 // (Not that CopyToMoveableMemory should ever return null!)
2974                                 throw new ExternalException ("CopyToMoveableMemory failed.");
2975                         if (Win32SetClipboardData (type, hmem) == IntPtr.Zero)
2976                                 throw new ExternalException ("Win32SetClipboardData");
2977                 }
2978
2979                 /// <summary>
2980                 /// Creates a memory block with GlobalAlloc(GMEM_MOVEABLE), copies the data 
2981                 /// into it, and returns the handle to the memory.
2982                 /// </summary>
2983                 /// -
2984                 /// <param name="data">The data.  Must not be null or zero-length &#x2014; 
2985                 /// see the exception notes.</param>
2986                 /// -
2987                 /// <returns>The *handle* to the allocated GMEM_MOVEABLE block.</returns>
2988                 /// -
2989                 /// <exception cref="T:System.ArgumentException">The data was null or zero 
2990                 /// length.  This is disallowed since a zero length allocation can't be made
2991                 /// </exception>
2992                 /// <exception cref="T:System.ComponentModel.Win32Exception">The allocation, 
2993                 /// or locking (handle->pointer) failed.
2994                 /// Either out of memory or the handle table is full (256 max currently).
2995                 /// Note Win32Exception is a subclass of ExternalException so this is OK in 
2996                 /// the documented Clipboard interface.
2997                 /// </exception>
2998                 internal static IntPtr CopyToMoveableMemory (byte[] data)
2999                 {
3000                         if (data == null || data.Length == 0)
3001                                 // detect this before GlobalAlloc does.
3002                                 throw new ArgumentException ("Can't create a zero length memory block.");
3003
3004                         IntPtr hmem = Win32GlobalAlloc (GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
3005                         if (hmem == IntPtr.Zero)
3006                                 throw new Win32Exception ();
3007                         IntPtr hmem_ptr = Win32GlobalLock (hmem);
3008                         if (hmem_ptr == IntPtr.Zero) // If the allocation was valid this shouldn't occur.
3009                                 throw new Win32Exception ();
3010                         Marshal.Copy (data, 0, hmem_ptr, data.Length);
3011                         Win32GlobalUnlock (hmem);
3012                         return hmem;
3013                 }
3014
3015
3016                 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
3017                         if (allowed) {
3018                                 Win32DnD.RegisterDropTarget(hwnd);
3019                         } else {
3020                                 Win32DnD.UnregisterDropTarget(hwnd);
3021                         }
3022                 }
3023
3024                 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
3025                         return Win32DnD.StartDrag(hwnd, data, allowedEffects);
3026                 }
3027
3028                 // XXX this doesn't work at all for FrameStyle.Dashed - it draws like Thick, and in the Thick case
3029                 // the corners are drawn incorrectly.
3030                 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
3031                         IntPtr          hdc;
3032                         IntPtr          pen;
3033                         IntPtr          oldpen;
3034                         COLORREF        clrRef = new COLORREF();
3035
3036                         // If we want the standard hatch pattern we would
3037                         // need to create a brush
3038
3039                         clrRef.R = backColor.R;
3040                         clrRef.G = backColor.G;
3041                         clrRef.B = backColor.B;
3042
3043                         // Grab a pen
3044                         pen = Win32CreatePen (style == FrameStyle.Thick ? PenStyle.PS_SOLID : PenStyle.PS_DASH,
3045                                               style == FrameStyle.Thick ? 4 : 2, ref clrRef);
3046
3047                         hdc = Win32GetDC(IntPtr.Zero);
3048                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
3049                         oldpen = Win32SelectObject(hdc, pen);
3050
3051                         Win32MoveToEx(hdc, rectangle.Left, rectangle.Top, IntPtr.Zero);
3052                         if ((rectangle.Width > 0) && (rectangle.Height > 0)) {
3053                                 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
3054                                 Win32LineTo(hdc, rectangle.Right, rectangle.Bottom);
3055                                 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
3056                                 Win32LineTo(hdc, rectangle.Left, rectangle.Top);
3057                         } else {
3058                                 if (rectangle.Width > 0) {
3059                                         Win32LineTo(hdc, rectangle.Right, rectangle.Top);
3060                                 } else {
3061                                         Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
3062                                 }
3063                         }
3064
3065                         Win32SelectObject(hdc, oldpen);
3066                         Win32DeleteObject(pen);
3067
3068                         Win32ReleaseDC(IntPtr.Zero, hdc);
3069                 }
3070
3071                 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
3072                         IntPtr          hdc;
3073                         IntPtr          pen;
3074                         IntPtr          oldpen;
3075                         POINT           pt;
3076                         COLORREF        clrRef = new COLORREF();
3077
3078                         pt = new POINT();
3079                         pt.x = 0;
3080                         pt.y = 0;
3081                         Win32ClientToScreen(IntPtr.Zero, ref pt);
3082
3083                         // If we want the standard hatch pattern we would
3084                         // need to create a brush
3085
3086                         clrRef.R = backColor.R;
3087                         clrRef.G = backColor.G;
3088                         clrRef.B = backColor.B;
3089
3090                         // Grab a pen
3091                         pen = Win32CreatePen(PenStyle.PS_SOLID, 1, ref clrRef);
3092
3093                         hdc = Win32GetDC(IntPtr.Zero);
3094                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
3095                         oldpen = Win32SelectObject(hdc, pen);
3096
3097                         Win32MoveToEx(hdc, pt.x + start.X, pt.y + start.Y, IntPtr.Zero);
3098                         Win32LineTo(hdc, pt.x + end.X, pt.y + end.Y);
3099
3100                         Win32SelectObject(hdc, oldpen);
3101                         Win32DeleteObject(pen);
3102
3103                         Win32ReleaseDC(IntPtr.Zero, hdc);
3104                 }
3105
3106                 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
3107                 {
3108                         RECT    rect;
3109
3110                         rect = new RECT();
3111                         rect.left = rectangle.Left;
3112                         rect.top = rectangle.Top;
3113                         rect.right = rectangle.Right;
3114                         rect.bottom = rectangle.Bottom;
3115
3116                         IntPtr          hdc;
3117                         IntPtr          brush;
3118                         IntPtr          oldbrush;
3119                         COLORREF        clrRef = new COLORREF();
3120
3121                         clrRef.R = backColor.R;
3122                         clrRef.G = backColor.G;
3123                         clrRef.B = backColor.B;
3124
3125                         // Grab a brush
3126                         brush = Win32CreateSolidBrush (clrRef);
3127
3128                         hdc = Win32GetDC(IntPtr.Zero);
3129                         oldbrush = Win32SelectObject(hdc, brush);
3130
3131                         Win32PatBlt (hdc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, PatBltRop.DSTINVERT);
3132
3133                         Win32SelectObject(hdc, oldbrush);
3134                         Win32DeleteObject(brush);
3135
3136                         Win32ReleaseDC(IntPtr.Zero, hdc);
3137                 }
3138
3139                 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
3140                         IntPtr          hdc;
3141                         IntPtr          pen;
3142                         IntPtr          oldpen;
3143                         POINT           pt;
3144
3145                         pt = new POINT();
3146                         pt.x = 0;
3147                         pt.y = 0;
3148                         Win32ClientToScreen(handle, ref pt);
3149
3150                         // If we want the standard hatch pattern we would
3151                         // need to create a brush
3152
3153                         // Grab a pen
3154                         pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
3155
3156                         hdc = Win32GetDC(IntPtr.Zero);
3157                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
3158                         oldpen = Win32SelectObject(hdc, pen);
3159
3160                         Control c = Control.FromHandle (handle);
3161                         if (c != null) {
3162                                 RECT window_rect;
3163                                 Win32GetWindowRect (c.Handle, out window_rect);
3164                                 Region r = new Region (new Rectangle(window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top));
3165                                 Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
3166                         }
3167
3168                         Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
3169                         if ((rect.Width > 0) && (rect.Height > 0)) {
3170                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
3171                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
3172                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
3173                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
3174                         } else {
3175                                 if (rect.Width > 0) {
3176                                         Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
3177                                 } else {
3178                                         Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
3179                                 }
3180                         }
3181
3182                         Win32SelectObject(hdc, oldpen);
3183                         Win32DeleteObject(pen);
3184                         if (c != null)
3185                                 Win32ExtSelectClipRgn(hdc, IntPtr.Zero, (int) ClipCombineMode.RGN_COPY);
3186
3187                         Win32ReleaseDC(IntPtr.Zero, hdc);
3188                 }
3189
3190                 internal override SizeF GetAutoScaleSize(Font font) {
3191                         Graphics        g;
3192                         float           width;
3193                         string          magic_string = "The quick brown fox jumped over the lazy dog.";
3194                         double          magic_number = 44.549996948242189;
3195
3196                         g = Graphics.FromHwnd(FosterParent);
3197
3198                         width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
3199                         return new SizeF(width, font.Height);
3200                 }
3201
3202                 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
3203                         return Win32SendMessage(hwnd, message, wParam, lParam);
3204                 }
3205
3206                 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
3207                         return Win32PostMessage(hwnd, message, wParam, lParam);
3208                 }
3209
3210                 internal override int SendInput (IntPtr hwnd, Queue keys) {
3211                         INPUT[] inputs = new INPUT[keys.Count];
3212                         const Int32 INPUT_KEYBOARD = 1;
3213                         uint returns = 0;
3214                         int i = 0;
3215                         while (keys.Count > 0) {
3216                                 MSG msg = (MSG)keys.Dequeue();
3217
3218                                 
3219                                 inputs[i].ki.wScan = 0;
3220                                 inputs[i].ki.time = 0;
3221                                 inputs[i].ki.dwFlags = (Int32)(msg.message == Msg.WM_KEYUP ? InputFlags.KEYEVENTF_KEYUP : 0);
3222                                 inputs[i].ki.wVk = (short)msg.wParam.ToInt32();
3223                                 inputs[i].type = INPUT_KEYBOARD;
3224                                 i++;
3225                         }
3226                         returns = Win32SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
3227
3228                         return (int) returns;
3229                 }
3230
3231                 internal override int KeyboardSpeed {
3232                         get {
3233                                 int speed = 0;
3234                                 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDSPEED, 0, ref speed, 0);
3235                                 //
3236                                 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
3237                                 //
3238                                 return speed;
3239                         }
3240                 }
3241
3242                 internal override int KeyboardDelay {
3243                         get {
3244                                 int delay = 1;
3245                                 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDDELAY, 0, ref delay, 0);
3246                                 //
3247                                 // Return values must range from 0 to 4, 0 meaning 250ms,
3248                                 // and 4 meaning 1000 ms.
3249                                 //
3250                                 return delay;
3251                         }
3252                 }
3253
3254                 private class WinBuffer
3255                 {
3256                         public IntPtr hdc;
3257                         public IntPtr bitmap;
3258
3259                         public WinBuffer (IntPtr hdc, IntPtr bitmap)
3260                         {
3261                                 this.hdc = hdc;
3262                                 this.bitmap = bitmap;
3263                         }
3264                 }
3265
3266                 internal override void CreateOffscreenDrawable (IntPtr handle, int width, int height, out object offscreen_drawable)
3267                 {
3268                         Graphics destG = Graphics.FromHwnd (handle);
3269                         IntPtr destHdc = destG.GetHdc ();
3270
3271                         IntPtr srcHdc = Win32CreateCompatibleDC (destHdc);
3272                         IntPtr srcBmp = Win32CreateCompatibleBitmap (destHdc, width, height);
3273                         Win32SelectObject (srcHdc, srcBmp);
3274
3275                         offscreen_drawable = new WinBuffer (srcHdc, srcBmp);
3276
3277                         destG.ReleaseHdc (destHdc);
3278                 }
3279
3280                 internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
3281                 {
3282                         return Graphics.FromHdc (((WinBuffer)offscreen_drawable).hdc);
3283                 }
3284
3285                 internal override void BlitFromOffscreen (IntPtr dest_handle, Graphics dest_dc, object offscreen_drawable, Graphics offscreen_dc, Rectangle r)
3286                 {
3287                         WinBuffer wb = (WinBuffer)offscreen_drawable;
3288
3289                         IntPtr destHdc = dest_dc.GetHdc ();
3290                         Win32BitBlt (destHdc, r.Left, r.Top, r.Width, r.Height, wb.hdc, r.Left, r.Top, TernaryRasterOperations.SRCCOPY);
3291                         dest_dc.ReleaseHdc (destHdc);
3292                 }
3293
3294                 internal override void DestroyOffscreenDrawable (object offscreen_drawable)
3295                 {
3296                         WinBuffer wb = (WinBuffer)offscreen_drawable;
3297
3298                         Win32DeleteObject (wb.bitmap);
3299                         Win32DeleteDC (wb.hdc);
3300                 }
3301
3302                 internal override void SetForegroundWindow (IntPtr handle)
3303                 {
3304                         Win32SetForegroundWindow(handle);
3305                 }
3306
3307                 internal override event EventHandler Idle;
3308                 #endregion      // Public Static Methods
3309
3310                 #region Win32 Imports
3311                 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
3312                 private extern static uint Win32GetLastError();
3313
3314                 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3315                 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);
3316
3317                 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
3318                 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
3319
3320                 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3321                 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
3322
3323                 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3324                 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
3325
3326                 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
3327                 internal extern static bool Win32TranslateMessage(ref MSG msg);
3328
3329                 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3330                 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
3331
3332                 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
3333                 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
3334
3335                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
3336                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
3337
3338                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
3339                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
3340
3341                 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3342                 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
3343
3344                 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3345                 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
3346
3347                 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
3348                 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
3349
3350                 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3351                 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
3352
3353                 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3354                 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
3355
3356                 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
3357                 private extern static IntPtr Win32ShowCursor(bool bShow);
3358
3359                 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
3360                 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
3361
3362                 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
3363                 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
3364
3365                 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
3366                 private extern static bool Win32DestroyCursor(IntPtr hCursor);
3367
3368                 [DllImport ("user32.dll", EntryPoint = "DrawIcon", CallingConvention = CallingConvention.StdCall)]
3369                 private extern static bool Win32DrawIcon (IntPtr hDC, int X, int Y, IntPtr hIcon);
3370                 
3371                 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3372                 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
3373
3374                 //[DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3375                 //private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
3376
3377                 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
3378                 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
3379
3380                 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
3381                 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
3382
3383                 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
3384                 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
3385
3386                 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
3387                 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
3388
3389                 [DllImport ("user32.dll", EntryPoint = "ValidateRect", CallingConvention = CallingConvention.StdCall)]
3390                 private extern static IntPtr Win32ValidateRect (IntPtr hWnd, ref RECT rect);
3391                 
3392                 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
3393                 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
3394
3395                 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
3396                 private extern static IntPtr Win32GetDC(IntPtr hWnd);
3397
3398                 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
3399                 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
3400
3401                 //[DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
3402                 //private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
3403
3404                 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
3405                 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
3406
3407                 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3408                 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
3409
3410                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
3411                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
3412
3413                 //[DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
3414                 //private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
3415
3416                 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
3417                 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
3418
3419                 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
3420                 private extern static IntPtr Win32ReleaseCapture();
3421
3422                 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
3423                 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
3424
3425                 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
3426                 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
3427
3428                 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
3429                 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
3430
3431                 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
3432                 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
3433
3434                 // This function returns the parent OR THE OWNER!
3435                 // Use GetAncestor to only get the parent.
3436                 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
3437                 private extern static IntPtr Win32GetParent(IntPtr hWnd);
3438
3439                 [DllImport ("user32.dll", EntryPoint = "GetAncestor", CallingConvention = CallingConvention.StdCall)]
3440                 private extern static IntPtr Win32GetAncestor (IntPtr hWnd, AncestorType flags);
3441
3442                 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3443                 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
3444
3445                 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
3446                 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
3447
3448                 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
3449                 private extern static bool Win32GetCursorPos(out POINT lpPoint);
3450
3451                 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
3452                 private extern static bool Win32SetCursorPos(int x, int y);
3453
3454                 //[DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
3455                 //private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
3456
3457                 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
3458                 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
3459
3460                 //[DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
3461                 //private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
3462
3463                 [DllImport ("gdi32.dll", EntryPoint="CreateSolidBrush", CallingConvention=CallingConvention.StdCall)]
3464                 private extern static IntPtr Win32CreateSolidBrush(COLORREF clrRef);
3465
3466                 [DllImport ("gdi32.dll", EntryPoint="PatBlt", CallingConvention=CallingConvention.StdCall)]
3467                 private extern static int Win32PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, PatBltRop dwRop);
3468
3469                 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
3470                 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
3471
3472                 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
3473                 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
3474
3475                 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
3476                 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
3477
3478                 [DllImport ("user32.dll", EntryPoint="GetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
3479                 private extern static uint Win32GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out LayeredWindowAttributes pwdFlags);
3480
3481                 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
3482                 public extern static bool Win32DeleteObject(IntPtr o);
3483
3484                 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
3485                 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
3486
3487                 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
3488                 private extern static IntPtr Win32GetDesktopWindow();
3489
3490                 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
3491                 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
3492
3493                 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
3494                 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
3495
3496                 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
3497                 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
3498
3499                 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
3500                 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
3501
3502                 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
3503                 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
3504
3505                 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
3506                 internal extern static IntPtr Win32GetFocus();
3507
3508                 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
3509                 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
3510
3511                 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
3512                 private  extern static bool Win32DestroyCaret();
3513
3514                 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
3515                 private  extern static bool Win32ShowCaret(IntPtr hwnd);
3516
3517                 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
3518                 private  extern static bool Win32HideCaret(IntPtr hwnd);
3519
3520                 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
3521                 private  extern static bool Win32SetCaretPos(int X, int Y);
3522
3523                 //[DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
3524                 //private  extern static uint Win32GetCaretBlinkTime();
3525
3526                 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3527                 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
3528
3529                 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
3530                 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
3531
3532                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3533                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3534
3535                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3536                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3537
3538                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3539                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3540
3541                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3542                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3543
3544                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3545                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3546
3547                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3548                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3549
3550                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3551                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3552
3553                 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3554                 private extern static IntPtr Win32GetActiveWindow();
3555
3556                 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
3557                 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
3558
3559                 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3560                 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
3561
3562                 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
3563                 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
3564
3565                 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
3566                 private extern static bool IsWindowEnabled(IntPtr hwnd);
3567
3568                 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
3569                 private extern static bool IsWindowVisible(IntPtr hwnd);
3570
3571                 //[DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
3572                 //private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
3573
3574                 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3575                 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3576
3577                 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3578                 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3579
3580                 [DllImport ("user32.dll", EntryPoint="SendInput", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3581                 private extern static UInt32 Win32SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray)] INPUT[] inputs, Int32 cbSize);
3582
3583                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3584                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
3585                 
3586                 //[DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3587                 //private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
3588
3589                 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3590                 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
3591
3592                 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3593                 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref bool value, uint fWinIni);
3594
3595                 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3596                 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref ANIMATIONINFO value, uint fWinIni);
3597
3598                 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
3599                 private extern static bool Win32OpenClipboard(IntPtr hwnd);
3600
3601                 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
3602                 private extern static bool Win32EmptyClipboard();
3603
3604                 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3605                 private extern static uint Win32RegisterClipboardFormat(string format);
3606
3607                 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
3608                 private extern static bool Win32CloseClipboard();
3609
3610                 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
3611                 private extern static uint Win32EnumClipboardFormats(uint format);
3612
3613                 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
3614                 private extern static IntPtr Win32GetClipboardData(uint format);
3615
3616                 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
3617                 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
3618
3619                 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
3620                 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
3621
3622                 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
3623                 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
3624
3625                 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
3626                 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
3627
3628                 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
3629                 internal extern static uint Win32GlobalSize(IntPtr hMem);
3630
3631                 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
3632                 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
3633
3634                 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
3635                 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
3636
3637                 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
3638                 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
3639
3640                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3641                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
3642
3643                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3644                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
3645
3646                 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
3647                 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
3648
3649                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3650                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
3651
3652                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3653                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
3654
3655                 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
3656                 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
3657
3658                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3659                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
3660
3661                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3662                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
3663
3664                 [DllImport("gdi32.dll", EntryPoint = "ExcludeClipRect", CallingConvention = CallingConvention.StdCall)]
3665                 internal extern static int Win32ExcludeClipRect (IntPtr hdc, int left, int top,  int right, int bottom);
3666
3667                 [DllImport ("gdi32.dll", EntryPoint="ExtSelectClipRgn", CallingConvention=CallingConvention.StdCall)]
3668                 internal extern static int Win32ExtSelectClipRgn(IntPtr hdc, IntPtr hrgn, int mode);
3669
3670                 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3671                 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
3672
3673                 [DllImport ("user32.dll", EntryPoint="GetDoubleClickTime", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3674                 private extern static int Win32GetDoubleClickTime ();
3675
3676                 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3677                 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
3678
3679                 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3680                 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
3681
3682                 [DllImport ("user32.dll", EntryPoint="ClipCursor", CallingConvention=CallingConvention.StdCall)]
3683                 internal extern static bool Win32ClipCursor (ref RECT lpRect);
3684
3685                 [DllImport ("user32.dll", EntryPoint="GetClipCursor", CallingConvention=CallingConvention.StdCall)]
3686                 internal extern static bool Win32GetClipCursor (out RECT lpRect);
3687
3688                 [DllImport ("gdi32.dll", EntryPoint="BitBlt", CallingConvention=CallingConvention.StdCall)]
3689                 internal static extern bool Win32BitBlt (IntPtr hObject, int nXDest, int nYDest, int nWidth,
3690                    int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
3691
3692                 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3693                 internal static extern IntPtr Win32CreateCompatibleDC (IntPtr hdc);
3694
3695                 [DllImport ("gdi32.dll", EntryPoint="DeleteDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3696                 internal static extern bool Win32DeleteDC (IntPtr hdc);
3697
3698                 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleBitmap", CallingConvention=CallingConvention.StdCall)]
3699                 internal static extern IntPtr Win32CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight);
3700
3701                 [DllImport ("kernel32.dll", EntryPoint = "GetSystemPowerStatus", CallingConvention = CallingConvention.StdCall)]
3702                 internal static extern Boolean Win32GetSystemPowerStatus (SYSTEMPOWERSTATUS sps);
3703
3704                 [DllImport ("user32.dll", EntryPoint = "GetIconInfo", CallingConvention = CallingConvention.StdCall)]
3705                 internal static extern bool Win32GetIconInfo (IntPtr hIcon, out ICONINFO piconinfo);
3706
3707                 [DllImport ("user32.dll", EntryPoint="SetForegroundWindow", CallingConvention=CallingConvention.StdCall)]
3708                 extern static bool Win32SetForegroundWindow(IntPtr hWnd);
3709                 #endregion
3710         }
3711 }