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