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