svn path=/trunk/mcs/; revision=104772
[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                 internal override int MenuShowDelay {
1193                         get { return GetSystemParametersInfoInt (SPIAction.SPI_GETMENUSHOWDELAY); }
1194                 }
1195
1196                 internal override int MouseSpeed {
1197                         get { return GetSystemParametersInfoInt (SPIAction.SPI_GETMOUSESPEED); }
1198                 }
1199
1200                 internal override LeftRightAlignment PopupMenuAlignment {
1201                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETMENUDROPALIGNMENT) == true ? LeftRightAlignment.Left : LeftRightAlignment.Right; }
1202                 }
1203
1204 #if NET_2_0
1205                 internal override PowerStatus PowerStatus {
1206                         get {
1207                                 SYSTEMPOWERSTATUS p = new SYSTEMPOWERSTATUS ();
1208                                 
1209                                 Win32GetSystemPowerStatus (p);
1210                                 
1211                                 PowerStatus ps = new PowerStatus ((BatteryChargeStatus)p._BatteryFlag, p._BatteryFullLifeTime, (float)p._BatteryLifePercent / 255f, p._BatteryLifeTime, (PowerLineStatus)p._ACLineStatus);
1212                                 
1213                                 return ps;
1214                         }
1215                 }
1216 #endif
1217
1218                 internal override int SizingBorderWidth {
1219                         get { return Win32GetSystemMetrics (SystemMetrics.SM_CXSIZEFRAME); }
1220                 }
1221
1222                 internal override Size SmallCaptionButtonSize {
1223                         get {
1224                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXSMSIZE),
1225                                         Win32GetSystemMetrics (SystemMetrics.SM_CYSMSIZE));
1226                         }
1227                 }
1228
1229                 internal override bool UIEffectsEnabled {
1230                         get { return GetSystemParametersInfoBool (SPIAction.SPI_GETUIEFFECTS); }
1231                 }
1232
1233                 internal override int VerticalResizeBorderThickness {
1234                         get { return Win32GetSystemMetrics (SystemMetrics.SM_CYSIZEFRAME); }
1235                 }
1236
1237                 internal override void RaiseIdle (EventArgs e)
1238                 {
1239                         if (Idle != null)
1240                                 Idle (this, e);
1241                 }
1242
1243                 internal override Keys ModifierKeys {
1244                         get {
1245                                 short   state;
1246                                 Keys    key_state;
1247
1248                                 key_state = Keys.None;
1249
1250                                 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
1251                                 if ((state & 0x8000) != 0) {
1252                                         key_state |= Keys.Shift;
1253                                 }
1254                                 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
1255                                 if ((state & 0x8000) != 0) {
1256                                         key_state |= Keys.Control;
1257                                 }
1258
1259                                 state = Win32GetKeyState(VirtualKeys.VK_MENU);
1260                                 if ((state & 0x8000) != 0) {
1261                                         key_state |= Keys.Alt;
1262                                 }
1263
1264                                 return key_state;
1265                         }
1266                 }
1267
1268                 internal override MouseButtons MouseButtons {
1269                         get {
1270                                 return mouse_state;
1271                         }
1272                 }
1273
1274                 internal override Point MousePosition {
1275                         get {
1276                                 return mouse_position;
1277                         }
1278                 }
1279
1280                 internal override Size MouseHoverSize {
1281                         get {
1282                                 int     width = 4;
1283                                 int     height = 4;
1284
1285                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref width, 0);
1286                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref height, 0);
1287                                 return new Size(width, height);
1288                         }
1289                 }
1290
1291                 internal override int MouseHoverTime {
1292                         get {
1293                                 int time = 500;
1294
1295                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERTIME, 0, ref time, 0);
1296                                 return time;
1297                         }
1298                 }
1299
1300                 internal override int MouseWheelScrollDelta {
1301                         get {
1302                                 int delta = 120;
1303                                 Win32SystemParametersInfo(SPIAction.SPI_GETWHEELSCROLLLINES, 0, ref delta, 0);
1304                                 return delta;
1305                         }
1306                 }
1307                 
1308                 internal override int HorizontalScrollBarHeight {
1309                         get {
1310                                 return scroll_height;
1311                         }
1312                 }
1313
1314                 internal override bool UserClipWontExposeParent {
1315                         get {
1316                                 return false;
1317                         }
1318                 }
1319
1320
1321                 internal override int VerticalScrollBarWidth {
1322                         get {
1323                                 return scroll_width;
1324                         }
1325                 }
1326
1327                 internal override int MenuHeight {
1328                         get {
1329                                 return Win32GetSystemMetrics(SystemMetrics.SM_CYMENU);
1330                         }
1331                 }
1332
1333                 internal override Size Border3DSize {
1334                         get {
1335                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXEDGE),
1336                                         Win32GetSystemMetrics (SystemMetrics.SM_CYEDGE));
1337                         }
1338                 }
1339
1340                 internal override Size BorderSize {
1341                         get {
1342                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXBORDER),
1343                                         Win32GetSystemMetrics (SystemMetrics.SM_CYBORDER));
1344                         }
1345                 }
1346
1347                 internal override bool DropTarget {
1348                         get {
1349                                 return false;
1350                         }
1351
1352                         set {
1353                                 if (value) {
1354                                         //throw new NotImplementedException("Need to figure out D'n'D for Win32");
1355                                 }
1356                         }
1357                 }
1358
1359                 internal override Size CaptionButtonSize {
1360                         get {
1361                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXSIZE),
1362                                         Win32GetSystemMetrics (SystemMetrics.SM_CYSIZE));
1363                         }
1364                 }
1365
1366                 internal override int CaptionHeight {
1367                         get {
1368                                 return Win32GetSystemMetrics(SystemMetrics.SM_CYCAPTION);
1369                         }
1370                 }
1371
1372                 internal override Size CursorSize {
1373                         get {
1374                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CYCURSOR));
1375                         }
1376                 }
1377
1378                 internal override bool DragFullWindows {
1379                         get {
1380                                 int full = 0;
1381                                 Win32SystemParametersInfo (SPIAction.SPI_GETDRAGFULLWINDOWS, 0, ref full, 0);
1382                                 return (full != 0);
1383                         }
1384                 }
1385
1386                 internal override Size DragSize {
1387                         get {
1388                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXDRAG), Win32GetSystemMetrics(SystemMetrics.SM_CYDRAG));
1389                         }
1390                 }
1391
1392                 internal override Size DoubleClickSize {
1393                         get {
1394                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXDOUBLECLK),
1395                                         Win32GetSystemMetrics (SystemMetrics.SM_CYDOUBLECLK));
1396                         }
1397                 }
1398
1399                 internal override int DoubleClickTime {
1400                         get {
1401                                 return Win32GetDoubleClickTime ();
1402                         }
1403                 }
1404
1405                 internal override Size FixedFrameBorderSize {
1406                         get {
1407                                 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXFIXEDFRAME),
1408                                         Win32GetSystemMetrics (SystemMetrics.SM_CYFIXEDFRAME));
1409                         }
1410                 }
1411
1412                 internal override Size FrameBorderSize { 
1413                         get {
1414                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXFRAME), Win32GetSystemMetrics(SystemMetrics.SM_CYFRAME));
1415                         }
1416                 }
1417
1418                 internal override Size IconSize {
1419                         get {
1420                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXICON), Win32GetSystemMetrics(SystemMetrics.SM_CYICON));
1421                         }
1422                 }
1423
1424                 internal override Size MaxWindowTrackSize {
1425                         get {
1426                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMAXTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMAXTRACK));
1427                         }
1428                 }
1429
1430                 internal override bool MenuAccessKeysUnderlined {
1431                         get {
1432                                 int underlined = 0;
1433                                 Win32SystemParametersInfo (SPIAction.SPI_GETKEYBOARDCUES, 0, ref underlined, 0);
1434                                 return (underlined != 0);
1435                         }
1436                 }
1437
1438                 internal override Size MinimizedWindowSize {
1439                         get {
1440                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINIMIZED), Win32GetSystemMetrics(SystemMetrics.SM_CYMINIMIZED));
1441                         }
1442                 }
1443
1444                 internal override Size MinimizedWindowSpacingSize {
1445                         get {
1446                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINSPACING), Win32GetSystemMetrics(SystemMetrics.SM_CYMINSPACING));
1447                         }
1448                 }
1449
1450                 internal override Size MinimumWindowSize {
1451                         get {
1452                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMIN), Win32GetSystemMetrics(SystemMetrics.SM_CYMIN));
1453                         }
1454                 }
1455
1456                 internal override Size MinWindowTrackSize {
1457                         get {
1458                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMINTRACK));
1459                         }
1460                 }
1461
1462                 internal override Size SmallIconSize {
1463                         get {
1464                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXSMICON), Win32GetSystemMetrics(SystemMetrics.SM_CYSMICON));
1465                         }
1466                 }
1467
1468                 internal override int MouseButtonCount {
1469                         get {
1470                                 return Win32GetSystemMetrics(SystemMetrics.SM_CMOUSEBUTTONS);
1471                         }
1472                 }
1473
1474                 internal override bool MouseButtonsSwapped {
1475                         get {
1476                                 return Win32GetSystemMetrics(SystemMetrics.SM_SWAPBUTTON) != 0;
1477                         }
1478                 }
1479
1480                 internal override bool MouseWheelPresent {
1481                         get {
1482                                 return Win32GetSystemMetrics(SystemMetrics.SM_MOUSEWHEELPRESENT) != 0;
1483                         }
1484                 }
1485
1486                 internal override Rectangle VirtualScreen {
1487                         get {
1488                                 return new Rectangle(   Win32GetSystemMetrics(SystemMetrics.SM_XVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_YVIRTUALSCREEN),
1489                                                         Win32GetSystemMetrics(SystemMetrics.SM_CXVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYVIRTUALSCREEN));
1490                         }
1491                 }
1492
1493                 internal override Rectangle WorkingArea {
1494                         get {
1495                                 RECT    rect;
1496
1497                                 rect = new RECT();
1498                                 Win32SystemParametersInfo(SPIAction.SPI_GETWORKAREA, 0, ref rect, 0);
1499                                 return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
1500                                 //return new Rectangle(0, 0, Win32GetSystemMetrics(SystemMetrics.SM.SM_CXSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYSCREEN));
1501                         }
1502                 }
1503
1504                 internal override bool ThemesEnabled {
1505                         get {
1506                                 return XplatUIWin32.themes_enabled;
1507                         }
1508                 }
1509  
1510
1511                 #endregion      // Static Properties
1512
1513                 #region Singleton Specific Code
1514                 public static XplatUIWin32 GetInstance() {
1515                         if (instance==null) {
1516                                 instance=new XplatUIWin32();
1517                         }
1518                         ref_count++;
1519                         return instance;
1520                 }
1521
1522                 public int Reference {
1523                         get {
1524                                 return ref_count;
1525                         }
1526                 }
1527                 #endregion
1528
1529                 #region Public Static Methods
1530                 internal override IntPtr InitializeDriver() {
1531                         return IntPtr.Zero;
1532                 }
1533
1534                 internal override void ShutdownDriver(IntPtr token) {
1535                         Console.WriteLine("XplatUIWin32 ShutdownDriver called");
1536                 }
1537
1538
1539                 internal void Version() {
1540                         Console.WriteLine("Xplat version $revision: $");
1541                 }
1542
1543                 internal override void AudibleAlert() {
1544                         Win32PlaySound("Default", IntPtr.Zero, SndFlags.SND_ALIAS | SndFlags.SND_ASYNC | SndFlags.SND_NOSTOP | SndFlags.SND_NOWAIT);
1545                 }
1546
1547                 internal override void GetDisplaySize(out Size size) {
1548                         RECT    rect;
1549
1550                         Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
1551
1552                         size = new Size(rect.right - rect.left, rect.bottom - rect.top);
1553                 }
1554
1555                 internal override void EnableThemes() {
1556                         themes_enabled=true;
1557                 }
1558
1559                 internal override IntPtr CreateWindow(CreateParams cp) {
1560                         IntPtr  WindowHandle;
1561                         IntPtr  ParentHandle;
1562                         Hwnd    hwnd;
1563
1564                         hwnd = new Hwnd();
1565
1566                         ParentHandle=cp.Parent;
1567
1568                         if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
1569                                 // We need to use our foster parent window until this poor child gets it's parent assigned
1570                                 ParentHandle = FosterParent;
1571                         }
1572
1573                         if ( ((cp.Style & (int)(WindowStyles.WS_CHILD | WindowStyles.WS_POPUP))==0) && ((cp.ExStyle & (int)WindowExStyles.WS_EX_APPWINDOW) == 0)) {
1574                                 // If we want to be hidden from the taskbar we need to be 'owned' by 
1575                                 // something not on the taskbar. FosterParent is just that
1576                                 ParentHandle = FosterParent;
1577                         }
1578
1579                         Point location;
1580                         if (cp.HasWindowManager) {
1581                                 location = Hwnd.GetNextStackedFormLocation (cp, Hwnd.ObjectFromHandle (cp.Parent));
1582                         } else {
1583                                 location = new Point (cp.X, cp.Y);
1584                         }
1585
1586                         string class_name = RegisterWindowClass (cp.ClassStyle);
1587                         HwndCreating = hwnd;
1588
1589                         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);
1590
1591                         HwndCreating = null;
1592
1593                         if (WindowHandle==IntPtr.Zero) {
1594                                 int error = Marshal.GetLastWin32Error ();
1595
1596                                 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1597                         }
1598
1599                         hwnd.ClientWindow = WindowHandle;
1600                         hwnd.Mapped = true;
1601                         Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1602
1603                         return WindowHandle;
1604                 }
1605
1606                 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1607                         CreateParams create_params = new CreateParams();
1608
1609                         create_params.Caption = "";
1610                         create_params.X = X;
1611                         create_params.Y = Y;
1612                         create_params.Width = Width;
1613                         create_params.Height = Height;
1614
1615                         create_params.ClassName=XplatUI.DefaultClassName;
1616                         create_params.ClassStyle = 0;
1617                         create_params.ExStyle=0;
1618                         create_params.Parent=IntPtr.Zero;
1619                         create_params.Param=0;
1620
1621                         return CreateWindow(create_params);
1622                 }
1623
1624                 internal override void DestroyWindow(IntPtr handle) {
1625                         Hwnd    hwnd;
1626
1627                         hwnd = Hwnd.ObjectFromHandle(handle);
1628                         Win32DestroyWindow(handle);
1629                         hwnd.Dispose();
1630                         return;
1631                 }
1632
1633                 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1634                         // We do nothing, Form has to handle WM_GETMINMAXINFO
1635                 }
1636
1637
1638                 internal override FormWindowState GetWindowState(IntPtr handle) {
1639                         uint style;
1640
1641                         style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1642                         if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1643                                 return FormWindowState.Maximized;
1644                         } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1645                                 return FormWindowState.Minimized;
1646                         }
1647                         return FormWindowState.Normal;
1648                 }
1649
1650                 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1651                         switch(state) {
1652                                 case FormWindowState.Normal: {
1653                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1654                                         return;
1655                                 }
1656
1657                                 case FormWindowState.Minimized: {
1658                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1659                                         return;
1660                                 }
1661
1662                                 case FormWindowState.Maximized: {
1663                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1664                                         return;
1665                                 }
1666                         }
1667                 }
1668
1669                 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1670
1671                         Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1672                         Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1673
1674                         // From MSDN:
1675                         // Certain window data is cached, so changes you make using SetWindowLong
1676                         // will not take effect until you call the SetWindowPos function. Specifically, 
1677                         // if you change any of the frame styles, you must call SetWindowPos with
1678                         // the SWP_FRAMECHANGED flag for the cache to be updated properly.
1679                         if (cp.control is Form)
1680                                 XplatUI.RequestNCRecalc (handle);
1681                 }
1682
1683                 internal override double GetWindowTransparency(IntPtr handle)
1684                 {
1685                         LayeredWindowAttributes lwa;
1686                         COLORREF clrRef;
1687                         byte alpha;
1688
1689                         if (0 == Win32GetLayeredWindowAttributes (handle, out clrRef, out alpha, out lwa))
1690                                 return 1.0;
1691
1692                         return ((double)alpha) / 255.0;
1693                 }
1694
1695                 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1696                         LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1697                         byte opacity = (byte)(transparency*255);
1698                         COLORREF clrRef = new COLORREF();
1699                         if (key != Color.Empty) {
1700                                 clrRef.R = key.R;
1701                                 clrRef.G = key.G;
1702                                 clrRef.B = key.B;
1703                                 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1704                         }
1705                         RECT rc;
1706                         rc.right = 1000;
1707                         rc.bottom = 1000;
1708                         Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1709                 }
1710
1711                 TransparencySupport support;
1712                 bool queried_transparency_support;
1713                 internal override TransparencySupport SupportsTransparency() {
1714                         if (queried_transparency_support)
1715                                 return support;
1716
1717                         bool flag;
1718                         support = TransparencySupport.None;
1719
1720                         flag = true;
1721                         try {
1722                                 Win32SetLayeredWindowAttributes (IntPtr.Zero, new COLORREF (), 255, LayeredWindowAttributes.LWA_ALPHA);
1723                         }
1724                         catch (EntryPointNotFoundException) { flag = false; }
1725                         catch { /* swallow everything else */ }
1726
1727                         if (flag) support |= TransparencySupport.Set;
1728
1729                         flag = true;
1730                         try {
1731                                 LayeredWindowAttributes lwa;
1732                                 COLORREF clrRef;
1733                                 byte alpha;
1734
1735                                 Win32GetLayeredWindowAttributes (IntPtr.Zero, out clrRef, out alpha, out lwa);
1736                         }
1737                         catch (EntryPointNotFoundException) { flag = false; }
1738                         catch { /* swallow everything else */ }
1739
1740                         if (flag) support |= TransparencySupport.Get;
1741
1742                         queried_transparency_support = true;
1743                         return support;
1744                 }
1745
1746                 internal override void UpdateWindow(IntPtr handle) {
1747                         Win32UpdateWindow(handle);
1748                 }
1749
1750                 internal override PaintEventArgs PaintEventStart(ref Message msg, IntPtr handle, bool client) {
1751                         IntPtr          hdc;
1752                         PAINTSTRUCT     ps;
1753                         PaintEventArgs  paint_event;
1754                         RECT            rect;
1755                         Rectangle       clip_rect;
1756                         Hwnd            hwnd;
1757
1758                         clip_rect = new Rectangle();
1759                         rect = new RECT();
1760                         ps = new PAINTSTRUCT();
1761
1762                         hwnd = Hwnd.ObjectFromHandle(msg.HWnd);
1763                         
1764                         if (client) {
1765                                 if (Win32GetUpdateRect(msg.HWnd, ref rect, false)) {
1766                                         if (handle != msg.HWnd) {
1767                                                 // We need to validate the window where the paint message
1768                                                 // was generated, otherwise we'll never stop getting paint 
1769                                                 // messages.
1770                                                 Win32GetClientRect (msg.HWnd, out rect);
1771                                                 Win32ValidateRect (msg.HWnd, ref rect);
1772                                                 hdc = Win32GetDC (handle);
1773                                         } else {
1774                                                 hdc = Win32BeginPaint (handle, ref ps);
1775                                                 rect = ps.rcPaint;
1776                                         }
1777                                 } else {
1778                                         hdc = Win32GetDC(handle);
1779                                 }
1780                                 clip_rect = rect.ToRectangle ();
1781                         } else {
1782                                 hdc = Win32GetWindowDC (handle);
1783
1784                                 // HACK this in for now
1785                                 Win32GetWindowRect (handle, out rect);
1786                                 clip_rect = new Rectangle (0, 0, rect.Width, rect.Height);
1787                         }
1788
1789                         // If we called BeginPaint, store the PAINTSTRUCT,
1790                         // otherwise store hdc, so that PaintEventEnd can know
1791                         // whether to call EndPaint or ReleaseDC.
1792                         if (ps.hdc != IntPtr.Zero) {
1793                                 hwnd.drawing_stack.Push (ps);
1794                         } else {
1795                                 hwnd.drawing_stack.Push (hdc);
1796                         }
1797                         
1798                         Graphics dc = Graphics.FromHdc(hdc);
1799                         hwnd.drawing_stack.Push (dc);
1800
1801                         paint_event = new PaintEventArgs(dc, clip_rect);
1802
1803                         return paint_event;
1804                 }
1805
1806                 internal override void PaintEventEnd(ref Message m, IntPtr handle, bool client) {
1807                         Hwnd            hwnd;
1808
1809                         hwnd = Hwnd.ObjectFromHandle(m.HWnd);
1810
1811                         Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1812                         dc.Dispose ();
1813
1814                         object o = hwnd.drawing_stack.Pop();
1815                         if (o is IntPtr) {
1816                                 IntPtr hdc = (IntPtr) o;
1817                                 Win32ReleaseDC (handle, hdc);
1818                         } else if (o is PAINTSTRUCT) {
1819                                 PAINTSTRUCT ps = (PAINTSTRUCT) o;
1820                                 Win32EndPaint (handle, ref ps);
1821                         }
1822                 }
1823
1824
1825                 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1826                         Win32MoveWindow(handle, x, y, width, height, true);
1827                         return;
1828                 }
1829
1830                 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) {
1831                         IntPtr  parent;
1832                         RECT    rect;
1833                         POINT   pt;
1834
1835                         Win32GetWindowRect(handle, out rect);
1836                         width = rect.right - rect.left;
1837                         height = rect.bottom - rect.top;
1838
1839                         pt.x=rect.left;
1840                         pt.y=rect.top;
1841
1842                         parent = Win32GetAncestor (handle, AncestorType.GA_PARENT);
1843                         if (parent != IntPtr.Zero && parent != Win32GetDesktopWindow ())
1844                                 Win32ScreenToClient(parent, ref pt);
1845
1846                         x = pt.x;
1847                         y = pt.y;
1848
1849                         Win32GetClientRect(handle, out rect);
1850                         client_width = rect.right - rect.left;
1851                         client_height = rect.bottom - rect.top;
1852                         return;
1853                 }
1854
1855                 internal override void Activate(IntPtr handle) {
1856                         Win32SetActiveWindow(handle);
1857                         // delayed timer enabled
1858                         lock (timer_list) {
1859                                 foreach (Timer t in timer_list.Values) {
1860                                         if (t.Enabled && t.window == IntPtr.Zero) {
1861                                                 t.window = handle;
1862                                                 int id = t.GetHashCode ();
1863                                                 Win32SetTimer(handle, id, (uint)t.Interval, IntPtr.Zero);
1864                                         }
1865                                 }
1866                         }
1867                 }
1868
1869                 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1870                         RECT rect;
1871
1872                         rect.left=rc.Left;
1873                         rect.top=rc.Top;
1874                         rect.right=rc.Right;
1875                         rect.bottom=rc.Bottom;
1876                         Win32InvalidateRect(handle, ref rect, clear);
1877                 }
1878
1879
1880                 internal override void InvalidateNC (IntPtr handle)
1881                 {
1882                         // found this gem at
1883                         // http://www.dotnet247.com/247reference/msgs/58/292037.aspx
1884                         Win32SetWindowPos(handle, IntPtr.Zero,
1885                                           0, 0, 0, 0,
1886                                           SetWindowPosFlags.SWP_NOMOVE |
1887                                           SetWindowPosFlags.SWP_NOSIZE |
1888                                           SetWindowPosFlags.SWP_NOZORDER |
1889                                           SetWindowPosFlags.SWP_NOACTIVATE |
1890                                           SetWindowPosFlags.SWP_DRAWFRAME);
1891                 }
1892
1893                 private IntPtr InternalWndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
1894                 {
1895                         if (HwndCreating != null && HwndCreating.ClientWindow == IntPtr.Zero)
1896                                 HwndCreating.ClientWindow = hWnd;
1897                         return NativeWindow.WndProc (hWnd, msg, wParam, lParam);
1898                 }
1899
1900                 internal override IntPtr DefWndProc(ref Message msg) {
1901                         msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1902                         return msg.Result;
1903                 }
1904
1905                 internal override void HandleException(Exception e) {
1906                         StackTrace st = new StackTrace(e);
1907                         Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1908                         Console.WriteLine("{0}{1}", e.Message, st.ToString());
1909                 }
1910
1911                 internal override void DoEvents() {
1912                         MSG msg = new MSG();
1913
1914                         if (override_cursor != IntPtr.Zero) {
1915                                 Cursor.Current = null;
1916                         }
1917
1918                         while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1919                                 XplatUI.TranslateMessage(ref msg);
1920                                 XplatUI.DispatchMessage(ref msg);
1921                         }
1922                 }
1923
1924                 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1925                         return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1926                 }
1927
1928                 internal override void PostQuitMessage(int exitCode) {
1929                         Win32PostQuitMessage(exitCode);
1930                 }
1931
1932                 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
1933                 {
1934                         if (wm_nc_registered == null)
1935                                 wm_nc_registered = new Hashtable ();
1936                                 
1937                         TMEFlags flags = TMEFlags.TME_NONCLIENT;
1938                         if (hover)
1939                                 flags |= TMEFlags.TME_HOVER;
1940                         if (leave)
1941                                 flags |= TMEFlags.TME_LEAVE;
1942
1943                         if (flags == TMEFlags.TME_NONCLIENT) {
1944                                 if (wm_nc_registered.Contains (hwnd)) {
1945                                         wm_nc_registered.Remove (hwnd);
1946                                 }
1947                         } else {
1948                                 if (!wm_nc_registered.Contains (hwnd)) {
1949                                         wm_nc_registered.Add (hwnd, flags);
1950                                 } else {
1951                                         wm_nc_registered [hwnd] = flags;
1952                                 }
1953                         }
1954                 }
1955
1956                 internal override void RequestNCRecalc(IntPtr handle) {
1957                         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);
1958                 }
1959
1960                 internal override void ResetMouseHover(IntPtr handle) {
1961                         TRACKMOUSEEVENT tme;
1962
1963                         tme = new TRACKMOUSEEVENT();
1964                         tme.size = Marshal.SizeOf(tme);
1965                         tme.hWnd = handle;
1966                         tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1967                         Win32TrackMouseEvent(ref tme);
1968                 }
1969
1970
1971                 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1972                         return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
1973                 }
1974
1975                 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
1976                         bool            result;
1977
1978                         msg.refobject = 0;
1979                         if (RetrieveMessage(ref msg)) {
1980                                 return true;
1981                         }
1982
1983                         if (blocking) {
1984                                 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
1985                         } else {
1986                                 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
1987                                 if (!result) {
1988                                         return false;
1989                                 }
1990                         }
1991
1992                         // We need to fake WM_MOUSE_ENTER
1993                         switch (msg.message) {
1994                                 case Msg.WM_LBUTTONDOWN: {
1995                                         mouse_state |= MouseButtons.Left;
1996                                         break;
1997                                 }
1998
1999                                 case Msg.WM_MBUTTONDOWN: {
2000                                         mouse_state |= MouseButtons.Middle;
2001                                         break;
2002                                 }
2003
2004                                 case Msg.WM_RBUTTONDOWN: {
2005                                         mouse_state |= MouseButtons.Right;
2006                                         break;
2007                                 }
2008
2009                                 case Msg.WM_LBUTTONUP: {
2010                                         mouse_state &= ~MouseButtons.Left;
2011                                         break;
2012                                 }
2013
2014                                 case Msg.WM_MBUTTONUP: {
2015                                         mouse_state &= ~MouseButtons.Middle;
2016                                         break;
2017                                 }
2018
2019                                 case Msg.WM_RBUTTONUP: {
2020                                         mouse_state &= ~MouseButtons.Right;
2021                                         break;
2022                                 }
2023
2024                                 case Msg.WM_ASYNC_MESSAGE: {
2025                                         XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
2026                                         break;
2027                                 }
2028
2029                                 case Msg.WM_MOUSEMOVE: {
2030                                         if (msg.hwnd != prev_mouse_hwnd) {
2031                                                 TRACKMOUSEEVENT tme;
2032
2033                                                 // The current message will be sent out next time around
2034                                                 StoreMessage(ref msg);
2035
2036                                                 // This is the message we want to send at this point
2037                                                 msg.message = Msg.WM_MOUSE_ENTER;
2038
2039                                                 prev_mouse_hwnd = msg.hwnd;
2040
2041                                                 tme = new TRACKMOUSEEVENT();
2042                                                 tme.size = Marshal.SizeOf(tme);
2043                                                 tme.hWnd = msg.hwnd;
2044                                                 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
2045                                                 Win32TrackMouseEvent(ref tme);
2046                                                 return result;
2047                                         }
2048                                         break;
2049                                 }
2050
2051                                 case Msg.WM_NCMOUSEMOVE: {
2052                                         if (wm_nc_registered == null || !wm_nc_registered.Contains (msg.hwnd))
2053                                                 break;
2054                                                 
2055                                         TRACKMOUSEEVENT tme;
2056
2057                                         tme = new TRACKMOUSEEVENT ();
2058                                         tme.size = Marshal.SizeOf(tme);
2059                                         tme.hWnd = msg.hwnd;
2060                                         tme.dwFlags = (TMEFlags)wm_nc_registered[msg.hwnd];
2061                                         Win32TrackMouseEvent (ref tme);
2062                                         return result;
2063                                 }
2064
2065                                 case Msg.WM_DROPFILES: {
2066                                         return Win32DnD.HandleWMDropFiles(ref msg);
2067                                 }
2068
2069                                 case Msg.WM_MOUSELEAVE: {
2070                                         prev_mouse_hwnd = IntPtr.Zero;
2071                                         break;
2072                                 }
2073
2074                                 case Msg.WM_TIMER: {
2075                                         Timer timer=(Timer)timer_list[(int)msg.wParam];
2076
2077                                         if (timer != null) {
2078                                                 timer.FireTick();
2079                                         }
2080                                         break;
2081                                 }
2082                         }
2083
2084                         return result;
2085                 }
2086
2087                 internal override bool TranslateMessage(ref MSG msg) {
2088                         return Win32TranslateMessage(ref msg);
2089                 }
2090
2091                 internal override IntPtr DispatchMessage(ref MSG msg) {
2092                         return Win32DispatchMessage(ref msg);
2093                 }
2094
2095                 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
2096                         if (Top) {
2097                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2098                                 return true;
2099                         } else if (!Bottom) {
2100                                 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2101                         } else {
2102                                 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2103                                 return true;
2104                         }
2105                         return false;
2106                 }
2107
2108                 internal override bool SetTopmost(IntPtr hWnd, bool Enabled) {
2109                         if (Enabled) {
2110                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
2111                                 return true;
2112                         } else {
2113                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
2114                                 return true;
2115                         }
2116                 }
2117                 
2118                 internal override bool SetOwner(IntPtr hWnd, IntPtr hWndOwner) {
2119                         Win32SetWindowLong(hWnd, WindowLong.GWL_HWNDPARENT, (uint) hWndOwner);
2120                         return true;
2121                 }
2122
2123                 internal override bool Text(IntPtr handle, string text) {
2124                         Win32SetWindowText(handle, text);
2125                         return true;
2126                 }
2127
2128                 internal override bool GetText(IntPtr handle, out string text) {
2129                         StringBuilder sb;
2130
2131                         sb = new StringBuilder(256);
2132                         Win32GetWindowText(handle, sb, sb.Capacity);
2133                         text = sb.ToString();
2134                         return true;
2135                 }
2136
2137                 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
2138                 {
2139                         if (visible) {
2140                                 Control c = Control.FromHandle (handle);
2141                                 if (c is Form) {
2142                                         Form f;
2143
2144                                         f = (Form)Control.FromHandle (handle);
2145                                         WindowPlacementFlags flags = WindowPlacementFlags.SW_SHOWNORMAL;
2146                                         switch (f.WindowState) {
2147                                                 case FormWindowState.Normal: flags = WindowPlacementFlags.SW_SHOWNORMAL; break;
2148                                                 case FormWindowState.Minimized: flags = WindowPlacementFlags.SW_MINIMIZE; break;
2149                                                 case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
2150                                         }
2151                                         
2152                                         if (!f.ActivateOnShow)
2153                                                 flags = WindowPlacementFlags.SW_SHOWNOACTIVATE;
2154                                                 
2155                                         Win32ShowWindow (handle, flags);
2156                                 }
2157                                 else {
2158                                         if (c.ActivateOnShow)
2159                                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
2160                                         else
2161                                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
2162                                 }
2163                         }
2164                         else {
2165                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_HIDE);
2166                         }
2167                         return true;
2168                 }
2169
2170                 internal override bool IsEnabled(IntPtr handle) {
2171                         return IsWindowEnabled (handle);
2172                 }
2173
2174                 internal override bool IsKeyLocked (VirtualKeys key)
2175                 {
2176                         return (Win32GetKeyState (key) & 1) == 1;
2177                 }
2178                 
2179                 internal override bool IsVisible(IntPtr handle) {
2180                         return IsWindowVisible (handle);
2181                 }
2182
2183                 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
2184                         Control c = Control.FromHandle (handle);
2185                         if (parent == IntPtr.Zero) {
2186                                 if (!(c is Form)) {
2187                                         Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
2188                                 }
2189                         } else {
2190                                 if (!(c is Form)) {
2191                                         SetVisible (handle, c.is_visible, true);
2192                                 }
2193                         }       
2194                         // The Win32SetParent is lame, it can very well move the window
2195                         // ref: http://groups.google.com/group/microsoft.public.vb.winapi/browse_thread/thread/1b82ccc54231ecee/afa82835bfc0422a%23afa82835bfc0422a
2196                         // Here we save the position before changing the parent, and if it has changed afterwards restore it.
2197                         // Another possibility would be to intercept WM_WINDOWPOSCHANGING and restore the coords there, but this would require plumbing in weird places
2198                         // (either inside Control or add handling to InternalWndProc)
2199                         // We also need to remove WS_CHILD if making the window parent-less, and add it if we're parenting it.
2200                         RECT rect, rect2;
2201                         IntPtr result;
2202                         WindowStyles style, new_style;
2203                         
2204                         Win32GetWindowRect (handle, out rect);
2205                         style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
2206                         
2207                         if (parent == IntPtr.Zero) {
2208                                 new_style = style & ~WindowStyles.WS_CHILD;
2209                                 result = Win32SetParent (handle, FosterParent);
2210                         } else {
2211                                 new_style = style | WindowStyles.WS_CHILD;
2212                                 result = Win32SetParent (handle, parent);
2213                         }
2214                         if (style != new_style && c is Form) {
2215                                 Win32SetWindowLong (handle, WindowLong.GWL_STYLE, (uint) new_style);
2216                         }
2217                         Win32GetWindowRect (handle, out rect2);
2218                         if (rect.top != rect2.top && rect.left != rect2.left && c is Form) {
2219                                 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);
2220                         }
2221                         return result;
2222                 }
2223
2224                 // If we ever start using this, we should probably replace FosterParent with IntPtr.Zero
2225                 internal override IntPtr GetParent(IntPtr handle) {
2226                         return Win32GetParent(handle);
2227                 }
2228
2229                 // This is a nop on win32 and x11
2230                 internal override IntPtr GetPreviousWindow(IntPtr handle) {
2231                         return handle;
2232                 }
2233
2234                 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
2235                         grab_hwnd = hWnd;
2236                         Win32SetCapture(hWnd);
2237                         
2238                         if (ConfineToHwnd != IntPtr.Zero) {
2239                                 RECT window_rect;
2240                                 Win32GetWindowRect (ConfineToHwnd, out window_rect);
2241                                 Win32GetClipCursor (out clipped_cursor_rect);
2242                                 Win32ClipCursor (ref window_rect);
2243                         }
2244                 }
2245
2246                 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
2247                         hWnd = grab_hwnd;
2248                         GrabConfined = grab_confined;
2249                         GrabArea = grab_area;
2250                 }
2251
2252                 internal override void UngrabWindow(IntPtr hWnd) {
2253                         if (!(clipped_cursor_rect.top == 0 && clipped_cursor_rect.bottom == 0 && clipped_cursor_rect.left == 0 && clipped_cursor_rect.right == 0)) {
2254                                 Win32ClipCursor (ref clipped_cursor_rect);
2255                                 clipped_cursor_rect = new RECT ();
2256                         }
2257                         
2258                         Win32ReleaseCapture();
2259                         grab_hwnd = IntPtr.Zero;
2260                 }
2261
2262                 internal override bool CalculateWindowRect(ref Rectangle ClientRect, CreateParams cp, Menu menu, out Rectangle WindowRect) {
2263                         RECT    rect;
2264
2265                         rect.left=ClientRect.Left;
2266                         rect.top=ClientRect.Top;
2267                         rect.right=ClientRect.Right;
2268                         rect.bottom=ClientRect.Bottom;
2269
2270                         if (!Win32AdjustWindowRectEx(ref rect, cp.Style, menu != null, cp.ExStyle)) {
2271                                 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
2272                                 return false;
2273                         }
2274
2275                         WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
2276                         return true;
2277                 }
2278
2279                 internal override void SetCursor(IntPtr window, IntPtr cursor) {
2280                         Win32SetCursor(cursor);
2281                         return;
2282                 }
2283
2284                 internal override void ShowCursor(bool show) {
2285                         Win32ShowCursor(show);
2286                 }
2287
2288                 internal override void OverrideCursor(IntPtr cursor) {
2289                         Win32SetCursor(cursor);
2290                 }
2291
2292                 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
2293                         IntPtr  cursor;
2294                         Bitmap  cursor_bitmap;
2295                         Bitmap  cursor_mask;
2296                         Byte[]  cursor_bits;
2297                         Byte[]  mask_bits;
2298                         Color   pixel;
2299                         int     width;
2300                         int     height;
2301
2302                         // Win32 only allows creation cursors of a certain size
2303                         if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
2304                                 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
2305                                 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
2306                         } else {
2307                                 cursor_bitmap = bitmap;
2308                                 cursor_mask = mask;
2309                         }
2310
2311                         width = cursor_bitmap.Width;
2312                         height = cursor_bitmap.Height;
2313
2314                         cursor_bits = new Byte[(width / 8) * height];
2315                         mask_bits = new Byte[(width / 8) * height];
2316
2317                         for (int y = 0; y < height; y++) {
2318                                 for (int x = 0; x < width; x++) {
2319                                         pixel = cursor_bitmap.GetPixel(x, y);
2320
2321                                         if (pixel == cursor_pixel) {
2322                                                 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
2323                                         }
2324
2325                                         pixel = cursor_mask.GetPixel(x, y);
2326
2327                                         if (pixel == mask_pixel) {
2328                                                 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
2329                                         }
2330                                 }
2331                         }
2332
2333                         cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
2334
2335                         return cursor;
2336                 }
2337
2338                 internal override Bitmap DefineStdCursorBitmap (StdCursor id)
2339                 {
2340                         // We load the cursor, create a bitmap, draw the cursor onto the bitmap and return the bitmap.
2341                         IntPtr cursor = DefineStdCursor (id);
2342                         // Windows only have one possible cursor size!
2343                         int width = Win32GetSystemMetrics (SystemMetrics.SM_CXCURSOR);
2344                         int height = Win32GetSystemMetrics (SystemMetrics.SM_CYCURSOR);
2345                         Bitmap bmp = new Bitmap (width, height);
2346                         Graphics gc = Graphics.FromImage (bmp);
2347                         IntPtr hdc = gc.GetHdc ();
2348                         Win32DrawIcon (hdc, 0, 0, cursor);
2349                         gc.ReleaseHdc (hdc);
2350                         gc.Dispose ();
2351                         return bmp;
2352                 }
2353
2354                 [MonoTODO("Define the missing cursors")]
2355                 internal override IntPtr DefineStdCursor(StdCursor id) {
2356                         switch(id) {
2357                                 case StdCursor.AppStarting:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
2358                                 case StdCursor.Arrow:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2359                                 case StdCursor.Cross:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
2360                                 case StdCursor.Default:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2361                                 case StdCursor.Hand:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
2362                                 case StdCursor.Help:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
2363                                 case StdCursor.HSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2364                                 case StdCursor.IBeam:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
2365                                 case StdCursor.No:              return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
2366                                 case StdCursor.NoMove2D:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2367                                 case StdCursor.NoMoveHoriz:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2368                                 case StdCursor.NoMoveVert:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2369                                 case StdCursor.PanEast:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2370                                 case StdCursor.PanNE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2371                                 case StdCursor.PanNorth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2372                                 case StdCursor.PanNW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2373                                 case StdCursor.PanSE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2374                                 case StdCursor.PanSouth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2375                                 case StdCursor.PanSW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2376                                 case StdCursor.PanWest:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2377                                 case StdCursor.SizeAll:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
2378                                 case StdCursor.SizeNESW:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
2379                                 case StdCursor.SizeNS:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
2380                                 case StdCursor.SizeNWSE:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
2381                                 case StdCursor.SizeWE:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
2382                                 case StdCursor.UpArrow:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
2383                                 case StdCursor.VSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2384                                 case StdCursor.WaitCursor:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
2385                         }
2386                         throw new NotImplementedException ();
2387                 }
2388
2389                 internal override void DestroyCursor(IntPtr cursor) {
2390                         if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
2391                                 Win32DestroyCursor(cursor);
2392                         }
2393                 }
2394
2395                 [MonoTODO]
2396                 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
2397                         ICONINFO ii = new ICONINFO ();
2398                         
2399                         if (!Win32GetIconInfo (cursor, out ii))
2400                                 throw new Win32Exception ();
2401                                 
2402                         width = 20;
2403                         height = 20;
2404                         hotspot_x = ii.xHotspot;
2405                         hotspot_y = ii.yHotspot;
2406                 }
2407
2408                 internal override void SetCursorPos(IntPtr handle, int x, int y) {
2409                         Win32SetCursorPos(x, y);
2410                 }
2411
2412                 internal override Region GetClipRegion(IntPtr hwnd) {
2413                         Region region;
2414
2415                         region = new Region();
2416
2417                         Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
2418
2419                         return region;
2420                 }
2421
2422                 internal override void SetClipRegion(IntPtr hwnd, Region region) {
2423                         if (region == null)
2424                                 Win32SetWindowRgn (hwnd, IntPtr.Zero, true);
2425                         else
2426                                 Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
2427                 }
2428
2429                 internal override void EnableWindow(IntPtr handle, bool Enable) {
2430                         Win32EnableWindow(handle, Enable);
2431                 }
2432
2433                 internal override void EndLoop(System.Threading.Thread thread) {
2434                         // Nothing to do
2435                 }
2436
2437                 internal override object StartLoop(System.Threading.Thread thread) {
2438                         return null;
2439                 }
2440
2441                 internal override void SetModal(IntPtr handle, bool Modal) {
2442                         // we do nothing on Win32
2443                 }
2444
2445                 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
2446                         POINT   pt;
2447
2448                         Win32GetCursorPos(out pt);
2449
2450                         if (handle!=IntPtr.Zero) {
2451                                 Win32ScreenToClient(handle, ref pt);
2452                         }
2453
2454                         x=pt.x;
2455                         y=pt.y;
2456                 }
2457
2458                 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
2459                 {
2460                         POINT pnt = new POINT();                        
2461
2462                         pnt.x = x;
2463                         pnt.y = y;
2464                         Win32ScreenToClient (handle, ref pnt);
2465
2466                         x = pnt.x;
2467                         y = pnt.y;
2468                 }
2469
2470                 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {                    
2471                         POINT   pnt = new POINT();                      
2472
2473                         pnt.x = x;
2474                         pnt.y = y;
2475
2476                         Win32ClientToScreen(handle, ref pnt);
2477
2478                         x = pnt.x;
2479                         y = pnt.y;
2480                 }
2481
2482                 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
2483                         RECT    rect;
2484  
2485                         Win32GetWindowRect(handle, out rect);
2486                         x -= rect.left + SystemInformation.FrameBorderSize.Width;
2487                         y -= rect.top + SystemInformation.FrameBorderSize.Height;
2488
2489                         WindowStyles style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
2490                         if (CreateParams.IsSet (style, WindowStyles.WS_CAPTION)) {
2491                                 y -= ThemeEngine.Current.CaptionHeight;
2492                         }
2493                 }
2494   
2495                 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {                      
2496                         RECT    rect;
2497  
2498                         Win32GetWindowRect(handle, out rect);
2499                         x += rect.left + SystemInformation.FrameBorderSize.Width;
2500                         y += rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
2501                         return;
2502                 }
2503
2504                 internal override void SendAsyncMethod (AsyncMethodData method)
2505                 {
2506                         Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
2507                 }
2508
2509                 internal override void SetTimer (Timer timer)
2510                 {
2511                         int     index;
2512
2513                         index = timer.GetHashCode();
2514
2515                         lock (timer_list) {
2516                                 timer_list[index]=timer;
2517                         }
2518
2519                         if (Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero) == IntPtr.Zero)
2520                                 timer.window = FosterParent;
2521                         else
2522                                 timer.window = IntPtr.Zero;
2523                 }
2524
2525                 internal override void KillTimer (Timer timer)
2526                 {
2527                         int     index;
2528
2529                         index = timer.GetHashCode();
2530
2531                         Win32KillTimer(timer.window, index);
2532
2533                         lock (timer_list) {
2534                                 timer_list.Remove(index);
2535                         }
2536                 }
2537                 
2538                 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
2539                         Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
2540                         caret_visible = false;
2541                 }
2542
2543                 internal override void DestroyCaret(IntPtr hwnd) {
2544                         Win32DestroyCaret();
2545                 }
2546
2547                 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
2548                         Win32SetCaretPos(x, y);
2549                 }
2550
2551                 internal override void CaretVisible(IntPtr hwnd, bool visible) {
2552                         if (visible) {
2553                                 if (!caret_visible) {
2554                                         Win32ShowCaret(hwnd);
2555                                         caret_visible = true;
2556                                 }
2557                         } else {
2558                                 if (caret_visible) {
2559                                         Win32HideCaret(hwnd);
2560                                         caret_visible = false;
2561                                 }
2562                         }
2563                 }
2564
2565                 internal override IntPtr GetFocus() {
2566                         return Win32GetFocus();
2567                 }
2568
2569                 internal override void SetFocus(IntPtr hwnd) {
2570                         Win32SetFocus(hwnd);
2571                 }
2572
2573                 internal override IntPtr GetActive() {
2574                         return Win32GetActiveWindow();
2575                 }
2576
2577                 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
2578                         IntPtr          dc;
2579                         IntPtr          prevobj;
2580                         TEXTMETRIC      tm;
2581
2582                         tm = new TEXTMETRIC();
2583
2584                         dc = Win32GetDC (IntPtr.Zero);
2585                         prevobj = Win32SelectObject (dc, font.ToHfont ());
2586                         
2587                         if (Win32GetTextMetrics (dc, ref tm) == false) {
2588                                 prevobj = Win32SelectObject (dc, prevobj);
2589                                 Win32DeleteObject (prevobj);
2590                                 Win32ReleaseDC (IntPtr.Zero, dc);
2591                                 ascent = 0;
2592                                 descent = 0;
2593                                 return false;
2594                         }
2595                         prevobj = Win32SelectObject (dc, prevobj);
2596                         Win32DeleteObject (prevobj);
2597                         Win32ReleaseDC (IntPtr.Zero, dc);
2598
2599                         ascent = tm.tmAscent;
2600                         descent = tm.tmDescent;
2601
2602                         return true;
2603                 }
2604
2605                 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
2606                         RECT    rect;
2607
2608                         rect = new RECT();
2609                         rect.left = rectangle.X;
2610                         rect.top = rectangle.Y;
2611                         rect.right = rectangle.Right;
2612                         rect.bottom = rectangle.Bottom;
2613
2614                         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));
2615                         Win32UpdateWindow(hwnd);
2616                 }
2617
2618                 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
2619                         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));
2620                 }
2621
2622                 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
2623                         NOTIFYICONDATA  nid;
2624
2625                         nid = new NOTIFYICONDATA();
2626
2627                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2628                         nid.hWnd = hwnd;
2629                         nid.uID = 1;
2630                         nid.uCallbackMessage = (uint)Msg.WM_USER;
2631                         nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2632
2633                         if (tip != null) {       
2634                                 nid.szTip = tip;
2635                                 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2636                         }
2637
2638                         if (icon != null) {
2639                                 nid.hIcon = icon.Handle;
2640                                 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2641                         }
2642
2643                         tt = null;
2644
2645                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
2646                 }
2647
2648                 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
2649                         NOTIFYICONDATA  nid;
2650
2651                         nid = new NOTIFYICONDATA();
2652
2653                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2654                         nid.hIcon = icon.Handle;
2655                         nid.hWnd = hwnd;
2656                         nid.uID = 1;
2657                         nid.uCallbackMessage = (uint)Msg.WM_USER;
2658                         nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2659
2660                         if (tip != null) {
2661                                 nid.szTip = tip;
2662                                 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2663                         }
2664
2665                         if (icon != null) {
2666                                 nid.hIcon = icon.Handle;
2667                                 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2668                         }
2669
2670                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2671                 }
2672
2673                 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
2674                         NOTIFYICONDATA  nid;
2675
2676                         nid = new NOTIFYICONDATA();
2677
2678                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2679                         nid.hWnd = hwnd;
2680                         nid.uID = 1;
2681                         nid.uFlags = 0;
2682
2683                         Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
2684                 }
2685
2686 #if NET_2_0
2687                 internal override void SystrayBalloon(IntPtr hwnd, int timeout, string title, string text, ToolTipIcon icon)
2688                 {
2689                         NOTIFYICONDATA  nid;
2690
2691                         nid = new NOTIFYICONDATA();
2692
2693                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2694                         nid.hWnd = hwnd;
2695                         nid.uID = 1;
2696                         nid.uFlags = NotifyIconFlags.NIF_INFO;
2697                         nid.uTimeoutOrVersion = timeout;
2698                         nid.szInfoTitle = title;
2699                         nid.szInfo = text;
2700                         nid.dwInfoFlags = icon;
2701                         
2702                         Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2703                 }
2704 #endif
2705
2706                 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
2707                         // Nothing to do on Win32
2708                 }
2709
2710                 internal override void SetMenu(IntPtr handle, Menu menu) {
2711                         // Trigger WM_NCCALC
2712                         Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2713                 }
2714
2715                 internal override Point GetMenuOrigin(IntPtr handle) {
2716                         Form form = Control.FromHandle (handle) as Form;
2717                         if (form != null) {
2718                                 Hwnd.Borders borders = Hwnd.GetBorders (form.GetCreateParams (), null);
2719                                 return new Point(borders.left, borders.top);
2720                         }
2721                         return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2722                 }
2723
2724                 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2725                         Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : icon.Handle);    // 1 = large icon (0 would be small)
2726                 }
2727
2728                 internal override void ClipboardClose(IntPtr handle) {
2729                         if (handle != clip_magic) {
2730                                 throw new ArgumentException("handle is not a valid clipboard handle");
2731                         }
2732                         Win32CloseClipboard();
2733                 }
2734
2735                 internal override int ClipboardGetID(IntPtr handle, string format) {
2736                         if (handle != clip_magic) {
2737                                 throw new ArgumentException("handle is not a valid clipboard handle");
2738                         }
2739                         if (format == "Text" ) return 1;
2740                         else if (format == "Bitmap" ) return 2;
2741                         else if (format == "MetaFilePict" ) return 3;
2742                         else if (format == "SymbolicLink" ) return 4;
2743                         else if (format == "DataInterchangeFormat" ) return 5;
2744                         else if (format == "Tiff" ) return 6;
2745                         else if (format == "OEMText" ) return 7;
2746                         else if (format == "DeviceIndependentBitmap" ) return 8;
2747                         else if (format == "Palette" ) return 9;
2748                         else if (format == "PenData" ) return 10;
2749                         else if (format == "RiffAudio" ) return 11;
2750                         else if (format == "WaveAudio" ) return 12;
2751                         else if (format == "UnicodeText" ) return 13;
2752                         else if (format == "EnhancedMetafile" ) return 14;
2753                         else if (format == "FileDrop" ) return 15;
2754                         else if (format == "Locale" ) return 16;
2755
2756                         return (int)Win32RegisterClipboardFormat(format);
2757                 }
2758
2759                 internal override IntPtr ClipboardOpen(bool primary_selection) {
2760                         // Win32 does not have primary selection
2761                         Win32OpenClipboard(FosterParent);
2762                         return clip_magic;
2763                 }
2764
2765                 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2766                         uint    format;
2767                         int[]   result;
2768                         int     count;
2769
2770                         if (handle != clip_magic) {
2771                                 return null;
2772                         }
2773
2774                         // Count first
2775                         count = 0;
2776                         format = 0;
2777                         do {
2778                                 format = Win32EnumClipboardFormats(format);
2779                                 if (format != 0) {
2780                                         count++;
2781                                 }
2782                         } while (format != 0);
2783
2784                         // Now assign
2785                         result = new int[count];
2786                         count = 0;
2787                         format = 0;
2788                         do {
2789                                 format = Win32EnumClipboardFormats(format);
2790                                 if (format != 0) {
2791                                         result[count++] = (int)format;
2792                                 }
2793                         } while (format != 0);
2794
2795                         return result;
2796                 }
2797
2798
2799                 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2800                         IntPtr  hmem;
2801                         IntPtr  data;
2802                         object  obj;
2803
2804                         if (handle != clip_magic) {
2805                                 throw new ArgumentException("handle is not a valid clipboard handle");
2806                         }
2807
2808                         hmem = Win32GetClipboardData((uint)type);
2809                         if (hmem == IntPtr.Zero) {
2810                                 return null;
2811                         }
2812
2813                         data = Win32GlobalLock(hmem);
2814                         if (data == IntPtr.Zero) {
2815                                 uint error = Win32GetLastError();
2816                                 Console.WriteLine("Error: {0}", error);
2817                                 return null;
2818                         }
2819
2820                         obj = null;
2821
2822                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2823                                 obj = AnsiToString(data);
2824                         } else switch ((ClipboardFormats)type) {
2825                                 case ClipboardFormats.CF_TEXT: {
2826                                         obj = AnsiToString(data);
2827                                         break;
2828                                 }
2829
2830                                 case ClipboardFormats.CF_DIB: {
2831                                         obj = DIBtoImage(data);
2832                                         break;
2833                                 }
2834
2835                                 case ClipboardFormats.CF_UNICODETEXT: {
2836                                         obj = UnicodeToString(data);
2837                                         break;
2838                                 }
2839
2840                                 default: {
2841                                         if (converter != null && !converter(type, data, out obj)) {
2842                                                 obj = null;
2843                                         }
2844                                         break;
2845                                 }
2846                         }
2847                         Win32GlobalUnlock(hmem);
2848
2849                         return obj;
2850
2851                 }
2852
2853                 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
2854                         byte[]  data;
2855                         IntPtr  hmem;
2856                         IntPtr  hmem_ptr;
2857
2858                         if (handle != clip_magic) {
2859                                 throw new ArgumentException("handle is not a valid clipboard handle");
2860                         }
2861
2862                         if (obj == null) {
2863                                 // Just clear it
2864                                 if (!Win32EmptyClipboard())
2865                                         throw new ExternalException("Win32EmptyClipboard");
2866                                 return;
2867                         }
2868
2869                         if (type == -1) {
2870                                 if (obj is string) {
2871                                         type = (int)ClipboardFormats.CF_UNICODETEXT;
2872                                 } else if (obj is Image) {
2873                                         type = (int)ClipboardFormats.CF_DIB;
2874                                 }
2875                         }
2876
2877                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2878                                 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2879                                 if (Win32SetClipboardData((uint)type, hmem) == IntPtr.Zero )
2880                                         throw new ExternalException("Win32SetClipboardData");
2881                                 return;
2882                         } else switch((ClipboardFormats)type) {
2883                                 case ClipboardFormats.CF_UNICODETEXT: {
2884                                         hmem = Marshal.StringToHGlobalUni((string)obj);
2885                                         if (Win32SetClipboardData((uint)type, hmem) == IntPtr.Zero)
2886                                                 throw new ExternalException("Win32SetClipboardData");
2887                                         return;
2888                                 }
2889
2890                                 case ClipboardFormats.CF_TEXT: {
2891                                         hmem = Marshal.StringToHGlobalAnsi((string)obj);
2892                                         if (Win32SetClipboardData((uint)type, hmem) == IntPtr.Zero)
2893                                                 throw new ExternalException("Win32SetClipboardData");
2894                                         return;
2895                                 }
2896
2897                                 case ClipboardFormats.CF_BITMAP:
2898                                 case ClipboardFormats.CF_DIB: {
2899                                         data = ImageToDIB((Image)obj);
2900
2901                                         hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2902                                         hmem_ptr = Win32GlobalLock(hmem);
2903                                         Marshal.Copy(data, 0, hmem_ptr, data.Length);
2904                                         Win32GlobalUnlock(hmem);
2905                                         if (Win32SetClipboardData((uint)ClipboardFormats.CF_DIB, hmem) == IntPtr.Zero)
2906                                                 throw new ExternalException("Win32SetClipboardData");
2907                                         return;
2908                                 }
2909
2910                                 default: {
2911                                         if (converter != null && converter(ref type, obj, out data)) {
2912                                                 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2913                                                 hmem_ptr = Win32GlobalLock(hmem);
2914                                                 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2915                                                 Win32GlobalUnlock(hmem);
2916                                                 if (Win32SetClipboardData((uint)type, hmem) == IntPtr.Zero)
2917                                                         throw new ExternalException("Win32SetClipboardData");
2918                                         }
2919                                         return;
2920                                 }
2921                         }
2922                 }
2923
2924                 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
2925                         if (allowed) {
2926                                 Win32DnD.RegisterDropTarget(hwnd);
2927                         } else {
2928                                 Win32DnD.UnregisterDropTarget(hwnd);
2929                         }
2930                 }
2931
2932                 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
2933                         return Win32DnD.StartDrag(hwnd, data, allowedEffects);
2934                 }
2935
2936                 // XXX this doesn't work at all for FrameStyle.Dashed - it draws like Thick, and in the Thick case
2937                 // the corners are drawn incorrectly.
2938                 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
2939                         IntPtr          hdc;
2940                         IntPtr          pen;
2941                         IntPtr          oldpen;
2942                         COLORREF        clrRef = new COLORREF();
2943
2944                         // If we want the standard hatch pattern we would
2945                         // need to create a brush
2946
2947                         clrRef.R = backColor.R;
2948                         clrRef.G = backColor.G;
2949                         clrRef.B = backColor.B;
2950
2951                         // Grab a pen
2952                         pen = Win32CreatePen (style == FrameStyle.Thick ? PenStyle.PS_SOLID : PenStyle.PS_DASH,
2953                                               style == FrameStyle.Thick ? 4 : 2, ref clrRef);
2954
2955                         hdc = Win32GetDC(IntPtr.Zero);
2956                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2957                         oldpen = Win32SelectObject(hdc, pen);
2958
2959                         Win32MoveToEx(hdc, rectangle.Left, rectangle.Top, IntPtr.Zero);
2960                         if ((rectangle.Width > 0) && (rectangle.Height > 0)) {
2961                                 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2962                                 Win32LineTo(hdc, rectangle.Right, rectangle.Bottom);
2963                                 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2964                                 Win32LineTo(hdc, rectangle.Left, rectangle.Top);
2965                         } else {
2966                                 if (rectangle.Width > 0) {
2967                                         Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2968                                 } else {
2969                                         Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2970                                 }
2971                         }
2972
2973                         Win32SelectObject(hdc, oldpen);
2974                         Win32DeleteObject(pen);
2975
2976                         Win32ReleaseDC(IntPtr.Zero, hdc);
2977                 }
2978
2979                 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
2980                         IntPtr          hdc;
2981                         IntPtr          pen;
2982                         IntPtr          oldpen;
2983                         POINT           pt;
2984                         COLORREF        clrRef = new COLORREF();
2985
2986                         pt = new POINT();
2987                         pt.x = 0;
2988                         pt.y = 0;
2989                         Win32ClientToScreen(IntPtr.Zero, ref pt);
2990
2991                         // If we want the standard hatch pattern we would
2992                         // need to create a brush
2993
2994                         clrRef.R = backColor.R;
2995                         clrRef.G = backColor.G;
2996                         clrRef.B = backColor.B;
2997
2998                         // Grab a pen
2999                         pen = Win32CreatePen(PenStyle.PS_SOLID, 1, ref clrRef);
3000
3001                         hdc = Win32GetDC(IntPtr.Zero);
3002                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
3003                         oldpen = Win32SelectObject(hdc, pen);
3004
3005                         Win32MoveToEx(hdc, pt.x + start.X, pt.y + start.Y, IntPtr.Zero);
3006                         Win32LineTo(hdc, pt.x + end.X, pt.y + end.Y);
3007
3008                         Win32SelectObject(hdc, oldpen);
3009                         Win32DeleteObject(pen);
3010
3011                         Win32ReleaseDC(IntPtr.Zero, hdc);
3012                 }
3013
3014                 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
3015                 {
3016                         RECT    rect;
3017
3018                         rect = new RECT();
3019                         rect.left = rectangle.Left;
3020                         rect.top = rectangle.Top;
3021                         rect.right = rectangle.Right;
3022                         rect.bottom = rectangle.Bottom;
3023
3024                         IntPtr          hdc;
3025                         IntPtr          brush;
3026                         IntPtr          oldbrush;
3027                         COLORREF        clrRef = new COLORREF();
3028
3029                         clrRef.R = backColor.R;
3030                         clrRef.G = backColor.G;
3031                         clrRef.B = backColor.B;
3032
3033                         // Grab a brush
3034                         brush = Win32CreateSolidBrush (clrRef);
3035
3036                         hdc = Win32GetDC(IntPtr.Zero);
3037                         oldbrush = Win32SelectObject(hdc, brush);
3038
3039                         Win32PatBlt (hdc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, PatBltRop.DSTINVERT);
3040
3041                         Win32SelectObject(hdc, oldbrush);
3042                         Win32DeleteObject(brush);
3043
3044                         Win32ReleaseDC(IntPtr.Zero, hdc);
3045                 }
3046
3047                 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
3048                         IntPtr          hdc;
3049                         IntPtr          pen;
3050                         IntPtr          oldpen;
3051                         POINT           pt;
3052
3053                         pt = new POINT();
3054                         pt.x = 0;
3055                         pt.y = 0;
3056                         Win32ClientToScreen(handle, ref pt);
3057
3058                         // If we want the standard hatch pattern we would
3059                         // need to create a brush
3060
3061                         // Grab a pen
3062                         pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
3063
3064                         hdc = Win32GetDC(IntPtr.Zero);
3065                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
3066                         oldpen = Win32SelectObject(hdc, pen);
3067
3068                         Control c = Control.FromHandle (handle);
3069                         if (c != null) {
3070                                 RECT window_rect;
3071                                 Win32GetWindowRect (c.Handle, out window_rect);
3072                                 Region r = new Region (new Rectangle(window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top));
3073                                 Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
3074                         }
3075
3076                         Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
3077                         if ((rect.Width > 0) && (rect.Height > 0)) {
3078                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
3079                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
3080                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
3081                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
3082                         } else {
3083                                 if (rect.Width > 0) {
3084                                         Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
3085                                 } else {
3086                                         Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
3087                                 }
3088                         }
3089
3090                         Win32SelectObject(hdc, oldpen);
3091                         Win32DeleteObject(pen);
3092                         if (c != null)
3093                                 Win32ExtSelectClipRgn(hdc, IntPtr.Zero, (int) ClipCombineMode.RGN_COPY);
3094
3095                         Win32ReleaseDC(IntPtr.Zero, hdc);
3096                 }
3097
3098                 internal override SizeF GetAutoScaleSize(Font font) {
3099                         Graphics        g;
3100                         float           width;
3101                         string          magic_string = "The quick brown fox jumped over the lazy dog.";
3102                         double          magic_number = 44.549996948242189;
3103
3104                         g = Graphics.FromHwnd(FosterParent);
3105
3106                         width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
3107                         return new SizeF(width, font.Height);
3108                 }
3109
3110                 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
3111                         return Win32SendMessage(hwnd, message, wParam, lParam);
3112                 }
3113
3114                 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
3115                         return Win32PostMessage(hwnd, message, wParam, lParam);
3116                 }
3117
3118                 internal override int SendInput (IntPtr hwnd, Queue keys) {
3119                         INPUT[] inputs = new INPUT[keys.Count];
3120                         const Int32 INPUT_KEYBOARD = 1;
3121                         uint returns = 0;
3122                         int i = 0;
3123                         while (keys.Count > 0) {
3124                                 MSG msg = (MSG)keys.Dequeue();
3125
3126                                 
3127                                 inputs[i].ki.wScan = 0;
3128                                 inputs[i].ki.time = 0;
3129                                 inputs[i].ki.dwFlags = (Int32)(msg.message == Msg.WM_KEYUP ? InputFlags.KEYEVENTF_KEYUP : 0);
3130                                 inputs[i].ki.wVk = (short)msg.wParam.ToInt32();
3131                                 inputs[i].type = INPUT_KEYBOARD;
3132                                 i++;
3133                         }
3134                         returns = Win32SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
3135
3136                         return (int) returns;
3137                 }
3138
3139                 internal override int KeyboardSpeed {
3140                         get {
3141                                 int speed = 0;
3142                                 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDSPEED, 0, ref speed, 0);
3143                                 //
3144                                 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
3145                                 //
3146                                 return speed;
3147                         }
3148                 }
3149
3150                 internal override int KeyboardDelay {
3151                         get {
3152                                 int delay = 1;
3153                                 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDDELAY, 0, ref delay, 0);
3154                                 //
3155                                 // Return values must range from 0 to 4, 0 meaning 250ms,
3156                                 // and 4 meaning 1000 ms.
3157                                 //
3158                                 return delay;
3159                         }
3160                 }
3161
3162                 private class WinBuffer
3163                 {
3164                         public IntPtr hdc;
3165                         public IntPtr bitmap;
3166
3167                         public WinBuffer (IntPtr hdc, IntPtr bitmap)
3168                         {
3169                                 this.hdc = hdc;
3170                                 this.bitmap = bitmap;
3171                         }
3172                 }
3173
3174                 internal override void CreateOffscreenDrawable (IntPtr handle, int width, int height, out object offscreen_drawable)
3175                 {
3176                         Graphics destG = Graphics.FromHwnd (handle);
3177                         IntPtr destHdc = destG.GetHdc ();
3178
3179                         IntPtr srcHdc = Win32CreateCompatibleDC (destHdc);
3180                         IntPtr srcBmp = Win32CreateCompatibleBitmap (destHdc, width, height);
3181                         Win32SelectObject (srcHdc, srcBmp);
3182
3183                         offscreen_drawable = new WinBuffer (srcHdc, srcBmp);
3184
3185                         destG.ReleaseHdc (destHdc);
3186                 }
3187
3188                 internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
3189                 {
3190                         return Graphics.FromHdc (((WinBuffer)offscreen_drawable).hdc);
3191                 }
3192
3193                 internal override void BlitFromOffscreen (IntPtr dest_handle, Graphics dest_dc, object offscreen_drawable, Graphics offscreen_dc, Rectangle r)
3194                 {
3195                         WinBuffer wb = (WinBuffer)offscreen_drawable;
3196
3197                         IntPtr destHdc = dest_dc.GetHdc ();
3198                         Win32BitBlt (destHdc, r.Left, r.Top, r.Width, r.Height, wb.hdc, r.Left, r.Top, TernaryRasterOperations.SRCCOPY);
3199                         dest_dc.ReleaseHdc (destHdc);
3200                 }
3201
3202                 internal override void DestroyOffscreenDrawable (object offscreen_drawable)
3203                 {
3204                         WinBuffer wb = (WinBuffer)offscreen_drawable;
3205
3206                         Win32DeleteObject (wb.bitmap);
3207                         Win32DeleteDC (wb.hdc);
3208                 }
3209
3210                 internal override void SetForegroundWindow (IntPtr handle)
3211                 {
3212                         Win32SetForegroundWindow(handle);
3213                 }
3214
3215                 internal override event EventHandler Idle;
3216                 #endregion      // Public Static Methods
3217
3218                 #region Win32 Imports
3219                 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
3220                 private extern static uint Win32GetLastError();
3221
3222                 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3223                 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);
3224
3225                 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
3226                 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
3227
3228                 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3229                 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
3230
3231                 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3232                 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
3233
3234                 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
3235                 internal extern static bool Win32TranslateMessage(ref MSG msg);
3236
3237                 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3238                 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
3239
3240                 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
3241                 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
3242
3243                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
3244                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
3245
3246                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
3247                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
3248
3249                 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3250                 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
3251
3252                 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3253                 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
3254
3255                 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
3256                 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
3257
3258                 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3259                 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
3260
3261                 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3262                 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
3263
3264                 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
3265                 private extern static IntPtr Win32ShowCursor(bool bShow);
3266
3267                 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
3268                 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
3269
3270                 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
3271                 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
3272
3273                 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
3274                 private extern static bool Win32DestroyCursor(IntPtr hCursor);
3275
3276                 [DllImport ("user32.dll", EntryPoint = "DrawIcon", CallingConvention = CallingConvention.StdCall)]
3277                 private extern static bool Win32DrawIcon (IntPtr hDC, int X, int Y, IntPtr hIcon);
3278                 
3279                 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3280                 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
3281
3282                 //[DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3283                 //private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
3284
3285                 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
3286                 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
3287
3288                 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
3289                 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
3290
3291                 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
3292                 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
3293
3294                 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
3295                 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
3296
3297                 [DllImport ("user32.dll", EntryPoint = "ValidateRect", CallingConvention = CallingConvention.StdCall)]
3298                 private extern static IntPtr Win32ValidateRect (IntPtr hWnd, ref RECT rect);
3299                 
3300                 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
3301                 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
3302
3303                 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
3304                 private extern static IntPtr Win32GetDC(IntPtr hWnd);
3305
3306                 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
3307                 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
3308
3309                 //[DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
3310                 //private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
3311
3312                 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
3313                 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
3314
3315                 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3316                 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
3317
3318                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
3319                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
3320
3321                 //[DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
3322                 //private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
3323
3324                 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
3325                 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
3326
3327                 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
3328                 private extern static IntPtr Win32ReleaseCapture();
3329
3330                 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
3331                 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
3332
3333                 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
3334                 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
3335
3336                 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
3337                 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
3338
3339                 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
3340                 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
3341
3342                 // This function returns the parent OR THE OWNER!
3343                 // Use GetAncestor to only get the parent.
3344                 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
3345                 private extern static IntPtr Win32GetParent(IntPtr hWnd);
3346
3347                 [DllImport ("user32.dll", EntryPoint = "GetAncestor", CallingConvention = CallingConvention.StdCall)]
3348                 private extern static IntPtr Win32GetAncestor (IntPtr hWnd, AncestorType flags);
3349
3350                 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3351                 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
3352
3353                 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
3354                 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
3355
3356                 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
3357                 private extern static bool Win32GetCursorPos(out POINT lpPoint);
3358
3359                 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
3360                 private extern static bool Win32SetCursorPos(int x, int y);
3361
3362                 //[DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
3363                 //private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
3364
3365                 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
3366                 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
3367
3368                 //[DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
3369                 //private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
3370
3371                 [DllImport ("gdi32.dll", EntryPoint="CreateSolidBrush", CallingConvention=CallingConvention.StdCall)]
3372                 private extern static IntPtr Win32CreateSolidBrush(COLORREF clrRef);
3373
3374                 [DllImport ("gdi32.dll", EntryPoint="PatBlt", CallingConvention=CallingConvention.StdCall)]
3375                 private extern static int Win32PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, PatBltRop dwRop);
3376
3377                 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
3378                 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
3379
3380                 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
3381                 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
3382
3383                 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
3384                 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
3385
3386                 [DllImport ("user32.dll", EntryPoint="GetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
3387                 private extern static uint Win32GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out LayeredWindowAttributes pwdFlags);
3388
3389                 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
3390                 public extern static bool Win32DeleteObject(IntPtr o);
3391
3392                 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
3393                 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
3394
3395                 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
3396                 private extern static IntPtr Win32GetDesktopWindow();
3397
3398                 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
3399                 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
3400
3401                 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
3402                 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
3403
3404                 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
3405                 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
3406
3407                 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
3408                 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
3409
3410                 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
3411                 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
3412
3413                 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
3414                 internal extern static IntPtr Win32GetFocus();
3415
3416                 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
3417                 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
3418
3419                 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
3420                 private  extern static bool Win32DestroyCaret();
3421
3422                 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
3423                 private  extern static bool Win32ShowCaret(IntPtr hwnd);
3424
3425                 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
3426                 private  extern static bool Win32HideCaret(IntPtr hwnd);
3427
3428                 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
3429                 private  extern static bool Win32SetCaretPos(int X, int Y);
3430
3431                 //[DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
3432                 //private  extern static uint Win32GetCaretBlinkTime();
3433
3434                 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3435                 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
3436
3437                 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
3438                 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
3439
3440                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3441                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3442
3443                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3444                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3445
3446                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3447                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3448
3449                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3450                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3451
3452                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3453                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3454
3455                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3456                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3457
3458                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3459                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3460
3461                 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3462                 private extern static IntPtr Win32GetActiveWindow();
3463
3464                 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
3465                 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
3466
3467                 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3468                 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
3469
3470                 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
3471                 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
3472
3473                 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
3474                 private extern static bool IsWindowEnabled(IntPtr hwnd);
3475
3476                 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
3477                 private extern static bool IsWindowVisible(IntPtr hwnd);
3478
3479                 //[DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
3480                 //private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
3481
3482                 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3483                 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3484
3485                 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3486                 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3487
3488                 [DllImport ("user32.dll", EntryPoint="SendInput", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3489                 private extern static UInt32 Win32SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray)] INPUT[] inputs, Int32 cbSize);
3490
3491                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3492                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
3493                 
3494                 //[DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3495                 //private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
3496
3497                 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3498                 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
3499
3500                 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3501                 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref bool value, uint fWinIni);
3502
3503                 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3504                 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref ANIMATIONINFO value, uint fWinIni);
3505
3506                 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
3507                 private extern static bool Win32OpenClipboard(IntPtr hwnd);
3508
3509                 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
3510                 private extern static bool Win32EmptyClipboard();
3511
3512                 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3513                 private extern static uint Win32RegisterClipboardFormat(string format);
3514
3515                 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
3516                 private extern static bool Win32CloseClipboard();
3517
3518                 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
3519                 private extern static uint Win32EnumClipboardFormats(uint format);
3520
3521                 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
3522                 private extern static IntPtr Win32GetClipboardData(uint format);
3523
3524                 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
3525                 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
3526
3527                 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
3528                 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
3529
3530                 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
3531                 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
3532
3533                 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
3534                 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
3535
3536                 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
3537                 internal extern static uint Win32GlobalSize(IntPtr hMem);
3538
3539                 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
3540                 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
3541
3542                 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
3543                 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
3544
3545                 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
3546                 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
3547
3548                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3549                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
3550
3551                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3552                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
3553
3554                 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
3555                 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
3556
3557                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3558                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
3559
3560                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3561                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
3562
3563                 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
3564                 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
3565
3566                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3567                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
3568
3569                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3570                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
3571
3572                 [DllImport("gdi32.dll", EntryPoint = "ExcludeClipRect", CallingConvention = CallingConvention.StdCall)]
3573                 internal extern static int Win32ExcludeClipRect (IntPtr hdc, int left, int top,  int right, int bottom);
3574
3575                 [DllImport ("gdi32.dll", EntryPoint="ExtSelectClipRgn", CallingConvention=CallingConvention.StdCall)]
3576                 internal extern static int Win32ExtSelectClipRgn(IntPtr hdc, IntPtr hrgn, int mode);
3577
3578                 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3579                 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
3580
3581                 [DllImport ("user32.dll", EntryPoint="GetDoubleClickTime", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3582                 private extern static int Win32GetDoubleClickTime ();
3583
3584                 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3585                 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
3586
3587                 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3588                 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
3589
3590                 [DllImport ("user32.dll", EntryPoint="ClipCursor", CallingConvention=CallingConvention.StdCall)]
3591                 internal extern static bool Win32ClipCursor (ref RECT lpRect);
3592
3593                 [DllImport ("user32.dll", EntryPoint="GetClipCursor", CallingConvention=CallingConvention.StdCall)]
3594                 internal extern static bool Win32GetClipCursor (out RECT lpRect);
3595
3596                 [DllImport ("gdi32.dll", EntryPoint="BitBlt", CallingConvention=CallingConvention.StdCall)]
3597                 internal static extern bool Win32BitBlt (IntPtr hObject, int nXDest, int nYDest, int nWidth,
3598                    int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
3599
3600                 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3601                 internal static extern IntPtr Win32CreateCompatibleDC (IntPtr hdc);
3602
3603                 [DllImport ("gdi32.dll", EntryPoint="DeleteDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3604                 internal static extern bool Win32DeleteDC (IntPtr hdc);
3605
3606                 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleBitmap", CallingConvention=CallingConvention.StdCall)]
3607                 internal static extern IntPtr Win32CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight);
3608
3609                 [DllImport ("kernel32.dll", EntryPoint = "GetSystemPowerStatus", CallingConvention = CallingConvention.StdCall)]
3610                 internal static extern Boolean Win32GetSystemPowerStatus (SYSTEMPOWERSTATUS sps);
3611
3612                 [DllImport ("user32.dll", EntryPoint = "GetIconInfo", CallingConvention = CallingConvention.StdCall)]
3613                 internal static extern bool Win32GetIconInfo (IntPtr hIcon, out ICONINFO piconinfo);
3614
3615                 [DllImport ("user32.dll", EntryPoint="SetForegroundWindow", CallingConvention=CallingConvention.StdCall)]
3616                 extern static bool Win32SetForegroundWindow(IntPtr hWnd);
3617                 #endregion
3618         }
3619 }