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