* XplatUIWin32.cs: Use the previous change in SetParent for forms only.
[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                         NIF_STATE                       = 0x00000008,
590                         NIF_INFO                        = 0x00000010                    
591                 }
592
593                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
594                 internal struct NOTIFYICONDATA {
595                         internal uint                           cbSize;
596                         internal IntPtr                         hWnd;
597                         internal uint                           uID;
598                         internal NotifyIconFlags        uFlags;
599                         internal uint                           uCallbackMessage;
600                         internal IntPtr                         hIcon;
601                         [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
602                         internal string                         szTip;
603                         internal int                            dwState;
604                         internal int                            dwStateMask;
605                         [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
606                         internal string                         szInfo;
607                         internal int                            uTimeoutOrVersion;
608                         [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
609                         internal string                         szInfoTitle;
610 #if NET_2_0
611                         internal ToolTipIcon            dwInfoFlags;
612 #else
613                         internal int                            dwInfoFlags;
614 #endif
615                 }
616
617                 [Flags]
618                 internal enum DCExFlags {
619                         DCX_WINDOW                      = 0x00000001,
620                         DCX_CACHE                       = 0x00000002,
621                         DCX_NORESETATTRS                = 0x00000004,
622                         DCX_CLIPCHILDREN                = 0x00000008,
623                         DCX_CLIPSIBLINGS                = 0x00000010,
624                         DCX_PARENTCLIP                  = 0x00000020,
625                         DCX_EXCLUDERGN                  = 0x00000040,
626                         DCX_INTERSECTRGN                = 0x00000080,
627                         DCX_EXCLUDEUPDATE               = 0x00000100,
628                         DCX_INTERSECTUPDATE             = 0x00000200,
629                         DCX_LOCKWINDOWUPDATE            = 0x00000400,
630                         DCX_USESTYLE                    = 0x00010000,
631                         DCX_VALIDATE                    = 0x00200000
632                 }
633
634                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
635                 internal struct CLIENTCREATESTRUCT {
636                         internal IntPtr                 hWindowMenu;
637                         internal uint                   idFirstChild;
638                 }
639
640                 private enum ClassLong : int {
641                         GCL_MENUNAME                    = -8,
642                         GCL_HBRBACKGROUND               = -10,
643                         GCL_HCURSOR                     = -12,
644                         GCL_HICON                       = -14,
645                         GCL_HMODULE                     = -16,
646                         GCL_CBWNDEXTRA                  = -18,
647                         GCL_CBCLSEXTRA                  = -20,
648                         GCL_WNDPROC                     = -24,
649                         GCL_STYLE                       = -26,
650                         GCW_ATOM                        = -32,
651                         GCL_HICONSM                     = -34
652                 }
653
654                 [Flags]
655                 internal enum GAllocFlags : uint {
656                         GMEM_FIXED                      = 0x0000,
657                         GMEM_MOVEABLE                   = 0x0002,
658                         GMEM_NOCOMPACT                  = 0x0010,
659                         GMEM_NODISCARD                  = 0x0020,
660                         GMEM_ZEROINIT                   = 0x0040,
661                         GMEM_MODIFY                     = 0x0080,
662                         GMEM_DISCARDABLE                = 0x0100,
663                         GMEM_NOT_BANKED                 = 0x1000,
664                         GMEM_SHARE                      = 0x2000,
665                         GMEM_DDESHARE                   = 0x2000,
666                         GMEM_NOTIFY                     = 0x4000,
667                         GMEM_LOWER                      = GMEM_NOT_BANKED,
668                         GMEM_VALID_FLAGS                = 0x7F72,
669                         GMEM_INVALID_HANDLE             = 0x8000,
670                         GHND                            = (GMEM_MOVEABLE | GMEM_ZEROINIT),
671                         GPTR                            = (GMEM_FIXED | GMEM_ZEROINIT)
672                 }
673
674                 internal enum ROP2DrawMode : int {
675                         R2_BLACK                        = 1,
676                         R2_NOTMERGEPEN                  = 2,
677                         R2_MASKNOTPEN                   = 3,
678                         R2_NOTCOPYPEN                   = 4,
679                         R2_MASKPENNOT                   = 5,
680                         R2_NOT                          = 6,
681                         R2_XORPEN                       = 7,
682                         R2_NOTMASKPEN                   = 8,
683                         R2_MASKPEN                      = 9,
684                         R2_NOTXORPEN                    = 10,
685                         R2_NOP                          = 11,
686                         R2_MERGENOTPEN                  = 12,
687                         R2_COPYPEN                      = 13,
688                         R2_MERGEPENNOT                  = 14,
689                         R2_MERGEPEN                     = 15,
690                         R2_WHITE                        = 16,
691                         R2_LAST                         = 16
692                 }
693
694                 internal enum PenStyle : int {
695                         PS_SOLID                        = 0,
696                         PS_DASH                         = 1,
697                         PS_DOT                          = 2,
698                         PS_DASHDOT                      = 3,
699                         PS_DASHDOTDOT                   = 4,
700                         PS_NULL                         = 5,
701                         PS_INSIDEFRAME                  = 6,
702                         PS_USERSTYLE                    = 7,
703                         PS_ALTERNATE                    = 8
704                 }
705
706                 internal enum PatBltRop : int {
707                         PATCOPY   = 0xf00021,
708                         PATINVERT = 0x5a0049,
709                         DSTINVERT = 0x550009,
710                         BLACKNESS = 0x000042,
711                         WHITENESS = 0xff0062,
712                 }
713
714                 internal enum StockObject : int {
715                         WHITE_BRUSH                     = 0,
716                         LTGRAY_BRUSH                    = 1,
717                         GRAY_BRUSH                      = 2,
718                         DKGRAY_BRUSH                    = 3,
719                         BLACK_BRUSH                     = 4,
720                         NULL_BRUSH                      = 5,
721                         HOLLOW_BRUSH                    = NULL_BRUSH,
722                         WHITE_PEN                       = 6,
723                         BLACK_PEN                       = 7,
724                         NULL_PEN                        = 8,
725                         OEM_FIXED_FONT                  = 10,
726                         ANSI_FIXED_FONT                 = 11,
727                         ANSI_VAR_FONT                   = 12,
728                         SYSTEM_FONT                     = 13,
729                         DEVICE_DEFAULT_FONT             = 14,
730                         DEFAULT_PALETTE                 = 15,
731                         SYSTEM_FIXED_FONT               = 16
732                 }
733
734                 internal enum HatchStyle : int {
735                         HS_HORIZONTAL                   = 0,
736                         HS_VERTICAL                     = 1,
737                         HS_FDIAGONAL                    = 2,
738                         HS_BDIAGONAL                    = 3,
739                         HS_CROSS                        = 4,
740                         HS_DIAGCROSS                    = 5
741                 }
742
743                 [Flags]
744                 internal enum SndFlags : int {
745                         SND_SYNC                        = 0x0000,
746                         SND_ASYNC                       = 0x0001,
747                         SND_NODEFAULT                   = 0x0002,
748                         SND_MEMORY                      = 0x0004,
749                         SND_LOOP                        = 0x0008,
750                         SND_NOSTOP                      = 0x0010,
751                         SND_NOWAIT                      = 0x00002000,
752                         SND_ALIAS                       = 0x00010000,
753                         SND_ALIAS_ID                    = 0x00110000,
754                         SND_FILENAME                    = 0x00020000,
755                         SND_RESOURCE                    = 0x00040004,
756                         SND_PURGE                       = 0x0040,
757                         SND_APPLICATION                 = 0x0080,
758                 }
759
760                 [Flags]
761                 internal enum LayeredWindowAttributes : int {
762                         LWA_COLORKEY            = 0x1,
763                         LWA_ALPHA                       = 0x2,
764                 }
765
766                 
767                 #endregion
768
769                 #region Constructor & Destructor
770                 private XplatUIWin32() {
771                         // Handle singleton stuff first
772                         ref_count=0;
773
774                         mouse_state = MouseButtons.None;
775                         mouse_position = Point.Empty;
776
777                         message_queue = new Queue();
778
779                         themes_enabled = false;
780
781                         wnd_proc = new WndProc(InternalWndProc);
782
783                         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);
784
785                         if (FosterParent==IntPtr.Zero) {
786                                 Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
787                         }
788
789                         scroll_height = Win32GetSystemMetrics(SystemMetrics.SM_CYHSCROLL);
790                         scroll_width = Win32GetSystemMetrics(SystemMetrics.SM_CXVSCROLL);
791
792                         timer_list = new Hashtable ();
793                         registered_classes = new Hashtable ();
794                 }
795                 #endregion      // Constructor & Destructor
796
797                 #region Private Support Methods
798
799                 private string RegisterWindowClass (int classStyle)
800                 {
801                         string class_name;
802
803                         lock (registered_classes) {
804                                 class_name = (string)registered_classes[classStyle];
805
806                                 if (class_name != null)
807                                         return class_name;
808
809                                 class_name = string.Format ("Mono.WinForms.{0}.{1}", System.Threading.Thread.GetDomainID ().ToString (), classStyle);
810
811                                 WNDCLASS wndClass;
812
813                                 wndClass.style = classStyle;
814                                 wndClass.lpfnWndProc = wnd_proc;
815                                 wndClass.cbClsExtra = 0;
816                                 wndClass.cbWndExtra = 0;
817                                 wndClass.hbrBackground = (IntPtr)(GetSysColorIndex.COLOR_WINDOW + 1);
818                                 wndClass.hCursor = Win32LoadCursor (IntPtr.Zero, LoadCursorType.IDC_ARROW);
819                                 wndClass.hIcon = IntPtr.Zero;
820                                 wndClass.hInstance = IntPtr.Zero;
821                                 wndClass.lpszClassName = class_name;
822                                 wndClass.lpszMenuName = "";
823
824                                 bool result = Win32RegisterClass (ref wndClass);
825
826                                 if (result == false)
827                                         Win32MessageBox (IntPtr.Zero, "Could not register the window class, win32 error " + Win32GetLastError ().ToString (), "Oops", 0);
828
829                                 registered_classes[classStyle] = class_name;
830                         }
831                         
832                         return class_name;
833                 }
834
835                 private static bool RetrieveMessage(ref MSG msg) {
836                         MSG     message;
837
838                         if (message_queue.Count == 0) {
839                                 return false;
840                         }
841
842                         message = (MSG)message_queue.Dequeue();
843                         msg = message;
844
845                         return true;
846                 }
847
848                 private static bool StoreMessage(ref MSG msg) {
849                         MSG message = new MSG();
850
851                         message = msg;
852                         message_queue.Enqueue(message);
853
854                         return true;
855                 }
856
857                 internal static String AnsiToString(IntPtr ansi_data) {
858                         return (string)Marshal.PtrToStringAnsi(ansi_data);
859                 }
860
861                 internal static String UnicodeToString(IntPtr unicode_data) {
862                         return (string)Marshal.PtrToStringUni(unicode_data);
863                 }
864
865                 internal static Image DIBtoImage(IntPtr dib_data) {
866                         BITMAPINFOHEADER        bmi;
867                         int                     ncolors;
868                         int                     imagesize;
869                         //int                   palettesize;
870                         Bitmap                  bmp;
871                         BitmapData              bits;
872                         ColorPalette            pal;
873                         int[]                   palette;
874                         byte[]                  imagebits;
875                         int                     bytesPerLine;
876
877                         bmi = (BITMAPINFOHEADER)Marshal.PtrToStructure(dib_data, typeof(BITMAPINFOHEADER));
878
879                         ncolors = (int)bmi.biClrUsed;
880                         if (ncolors == 0) {
881                                 if (bmi.biBitCount < 24) {
882                                         ncolors = (int)(1 << bmi.biBitCount);
883                                 }
884                         }
885                         //palettesize = ncolors * 4;
886
887                         imagesize = (int)bmi.biSizeImage;
888                         if (imagesize == 0) {
889                                 imagesize = (int)(((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);
890                         }
891
892                         switch(bmi.biBitCount) {
893                                 case 1: {       // Monochrome
894                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format1bppIndexed);
895                                         palette = new int[2];
896                                         break;
897                                 }
898
899                                 case 4: {       // 4bpp
900                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format4bppIndexed);
901                                         palette = new int[16];
902                                         break;
903                                 }
904
905                                 case 8: {       // 8bpp
906                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format8bppIndexed);
907                                         palette = new int[256];
908                                         break;
909                                 }
910
911                                 case 24:
912                                 case 32: {      // 32bpp
913                                         bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format32bppArgb);
914                                         palette = new int[0];
915                                         break;
916                                 }
917
918                                 default: {
919                                         throw new Exception("Unexpected number of bits:" + bmi.biBitCount.ToString());
920                                 }
921                         }
922
923                         if (bmi.biBitCount < 24) {
924                                 pal = bmp.Palette;                              // Managed palette
925                                 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER))), palette, 0, palette.Length);
926
927                                 for (int i = 0; i < ncolors; i++) {
928                                         pal.Entries[i] = Color.FromArgb(palette[i] | unchecked((int)0xff000000));
929                                 }
930                                 bmp.Palette = pal;
931                         }
932
933                         bytesPerLine = (int)((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3);
934                         bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
935
936                         imagebits = new byte[bytesPerLine];
937
938                         for (int y = 0; y < bmi.biHeight; y++) {
939                                 // Copy from source to managed
940                                 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER)) + palette.Length * 4 + bytesPerLine * y), imagebits, 0, bytesPerLine);
941
942                                 // Copy from managed to dest
943                                 Marshal.Copy(imagebits, 0, (IntPtr)((int)bits.Scan0 + bits.Stride * (bmi.biHeight - 1 - y)), imagebits.Length);
944                         }
945
946                         bmp.UnlockBits(bits);
947
948                         return bmp;
949                 }
950
951                 internal static byte[] ImageToDIB(Image image) {
952                         MemoryStream    ms;
953                         byte[]          buffer;
954                         byte[]          retbuf;
955
956                         ms = new MemoryStream();
957                         image.Save(ms, ImageFormat.Bmp);
958                         buffer = ms.GetBuffer();
959
960                         // Filter out the file header
961                         retbuf = new byte[buffer.Length];
962                         Array.Copy(buffer, 14, retbuf, 0, buffer.Length - 14);
963                         return retbuf;
964                 }
965
966                 internal static IntPtr DupGlobalMem(IntPtr mem) {
967                         IntPtr  dup;
968                         IntPtr  dup_ptr;
969                         IntPtr  mem_ptr;
970                         uint    len;
971
972                         len = Win32GlobalSize(mem);
973                         mem_ptr = Win32GlobalLock(mem);
974
975                         dup = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE, (int)len);
976                         dup_ptr = Win32GlobalLock(dup);
977
978                         Win32CopyMemory(dup_ptr, mem_ptr, (int)len);
979
980                         Win32GlobalUnlock(mem);
981                         Win32GlobalUnlock(dup);
982
983                         return dup;
984                 }
985                 #endregion      // Private Support Methods
986
987                 #region Static Properties
988                 internal override Keys ModifierKeys {
989                         get {
990                                 short   state;
991                                 Keys    key_state;
992
993                                 key_state = Keys.None;
994
995                                 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
996                                 if ((state & 0x8000) != 0) {
997                                         key_state |= Keys.Shift;
998                                 }
999                                 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
1000                                 if ((state & 0x8000) != 0) {
1001                                         key_state |= Keys.Control;
1002                                 }
1003
1004                                 state = Win32GetKeyState(VirtualKeys.VK_MENU);
1005                                 if ((state & 0x8000) != 0) {
1006                                         key_state |= Keys.Alt;
1007                                 }
1008
1009                                 return key_state;
1010                         }
1011                 }
1012
1013                 internal override MouseButtons MouseButtons {
1014                         get {
1015                                 return mouse_state;
1016                         }
1017                 }
1018
1019                 internal override Point MousePosition {
1020                         get {
1021                                 return mouse_position;
1022                         }
1023                 }
1024
1025                 internal override Size MouseHoverSize {
1026                         get {
1027                                 int     width = 4;
1028                                 int     height = 4;
1029
1030                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref width, 0);
1031                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref height, 0);
1032                                 return new Size(width, height);
1033                         }
1034                 }
1035
1036                 internal override int MouseHoverTime {
1037                         get {
1038                                 int time = 500;
1039
1040                                 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERTIME, 0, ref time, 0);
1041                                 return time;
1042                         }
1043                 }
1044
1045                 internal override int MouseWheelScrollDelta {
1046                         get {
1047                                 int delta = 120;
1048                                 Win32SystemParametersInfo(SPIAction.SPI_GETWHEELSCROLLLINES, 0, ref delta, 0);
1049                                 return delta;
1050                         }
1051                 }
1052                 
1053                 internal override int HorizontalScrollBarHeight {
1054                         get {
1055                                 return scroll_height;
1056                         }
1057                 }
1058
1059                 internal override bool UserClipWontExposeParent {
1060                         get {
1061                                 return false;
1062                         }
1063                 }
1064
1065
1066                 internal override int VerticalScrollBarWidth {
1067                         get {
1068                                 return scroll_width;
1069                         }
1070                 }
1071
1072                 internal override int MenuHeight {
1073                         get {
1074                                 return Win32GetSystemMetrics(SystemMetrics.SM_CYMENU);
1075                         }
1076                 }
1077
1078                 internal override bool DropTarget {
1079                         get {
1080                                 return false;
1081                         }
1082
1083                         set {
1084                                 if (value) {
1085                                         //throw new NotImplementedException("Need to figure out D'n'D for Win32");
1086                                 }
1087                         }
1088                 }
1089
1090                 internal override int Caption {
1091                         get {
1092                                 return Win32GetSystemMetrics(SystemMetrics.SM_CYCAPTION);
1093                         }
1094                 }
1095
1096                 internal override Size CursorSize {
1097                         get {
1098                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CYCURSOR));
1099                         }
1100                 }
1101
1102                 internal override bool DragFullWindows {
1103                         get {
1104                                 return true;
1105                         }
1106                 }
1107
1108                 internal override Size DragSize {
1109                         get {
1110                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXDRAG), Win32GetSystemMetrics(SystemMetrics.SM_CYDRAG));
1111                         }
1112                 }
1113
1114                 internal override Size FrameBorderSize { 
1115                         get {
1116                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXFRAME), Win32GetSystemMetrics(SystemMetrics.SM_CYFRAME));
1117                         }
1118                 }
1119
1120                 internal override Size IconSize {
1121                         get {
1122                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXICON), Win32GetSystemMetrics(SystemMetrics.SM_CYICON));
1123                         }
1124                 }
1125
1126                 internal override Size MaxWindowTrackSize {
1127                         get {
1128                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMAXTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMAXTRACK));
1129                         }
1130                 }
1131
1132                 internal override Size MinimizedWindowSize {
1133                         get {
1134                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINIMIZED), Win32GetSystemMetrics(SystemMetrics.SM_CYMINIMIZED));
1135                         }
1136                 }
1137
1138                 internal override Size MinimizedWindowSpacingSize {
1139                         get {
1140                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINSPACING), Win32GetSystemMetrics(SystemMetrics.SM_CYMINSPACING));
1141                         }
1142                 }
1143
1144                 internal override Size MinimumWindowSize {
1145                         get {
1146                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMIN), Win32GetSystemMetrics(SystemMetrics.SM_CYMIN));
1147                         }
1148                 }
1149
1150                 internal override Size MinWindowTrackSize {
1151                         get {
1152                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMINTRACK));
1153                         }
1154                 }
1155
1156                 internal override Size SmallIconSize {
1157                         get {
1158                                 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXSMICON), Win32GetSystemMetrics(SystemMetrics.SM_CYSMICON));
1159                         }
1160                 }
1161
1162                 internal override int MouseButtonCount {
1163                         get {
1164                                 return Win32GetSystemMetrics(SystemMetrics.SM_CMOUSEBUTTONS);
1165                         }
1166                 }
1167
1168                 internal override bool MouseButtonsSwapped {
1169                         get {
1170                                 return Win32GetSystemMetrics(SystemMetrics.SM_SWAPBUTTON) != 0;
1171                         }
1172                 }
1173
1174                 internal override bool MouseWheelPresent {
1175                         get {
1176                                 return Win32GetSystemMetrics(SystemMetrics.SM_MOUSEWHEELPRESENT) != 0;
1177                         }
1178                 }
1179
1180                 internal override Rectangle VirtualScreen {
1181                         get {
1182                                 return new Rectangle(   Win32GetSystemMetrics(SystemMetrics.SM_XVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_YVIRTUALSCREEN),
1183                                                         Win32GetSystemMetrics(SystemMetrics.SM_CXVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYVIRTUALSCREEN));
1184                         }
1185                 }
1186
1187                 internal override Rectangle WorkingArea {
1188                         get {
1189                                 RECT    rect;
1190
1191                                 rect = new RECT();
1192                                 Win32SystemParametersInfo(SPIAction.SPI_GETWORKAREA, 0, ref rect, 0);
1193                                 return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
1194                                 //return new Rectangle(0, 0, Win32GetSystemMetrics(SystemMetrics.SM.SM_CXSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYSCREEN));
1195                         }
1196                 }
1197
1198                 internal override bool ThemesEnabled {
1199                         get {
1200                                 return XplatUIWin32.themes_enabled;
1201                         }
1202                 }
1203  
1204
1205                 #endregion      // Static Properties
1206
1207                 #region Singleton Specific Code
1208                 public static XplatUIWin32 GetInstance() {
1209                         if (instance==null) {
1210                                 instance=new XplatUIWin32();
1211                         }
1212                         ref_count++;
1213                         return instance;
1214                 }
1215
1216                 public int Reference {
1217                         get {
1218                                 return ref_count;
1219                         }
1220                 }
1221                 #endregion
1222
1223                 #region Public Static Methods
1224                 internal override IntPtr InitializeDriver() {
1225                         return IntPtr.Zero;
1226                 }
1227
1228                 internal override void ShutdownDriver(IntPtr token) {
1229                         Console.WriteLine("XplatUIWin32 ShutdownDriver called");
1230                 }
1231
1232
1233                 internal void Version() {
1234                         Console.WriteLine("Xplat version $revision: $");
1235                 }
1236
1237                 internal override void AudibleAlert() {
1238                         Win32PlaySound("Default", IntPtr.Zero, SndFlags.SND_ALIAS | SndFlags.SND_ASYNC | SndFlags.SND_NOSTOP | SndFlags.SND_NOWAIT);
1239                 }
1240
1241                 internal override void GetDisplaySize(out Size size) {
1242                         RECT    rect;
1243
1244                         Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
1245
1246                         size = new Size(rect.right - rect.left, rect.bottom - rect.top);
1247                 }
1248
1249                 internal override void EnableThemes() {
1250                         themes_enabled=true;
1251                 }
1252
1253                 internal override IntPtr CreateWindow(CreateParams cp) {
1254                         IntPtr  WindowHandle;
1255                         IntPtr  ParentHandle;
1256                         Hwnd    hwnd;
1257
1258                         hwnd = new Hwnd();
1259
1260                         ParentHandle=cp.Parent;
1261
1262                         if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
1263                                 // We need to use our foster parent window until this poor child gets it's parent assigned
1264                                 ParentHandle = FosterParent;
1265                         }
1266
1267                         if ( ((cp.Style & (int)(WindowStyles.WS_CHILD | WindowStyles.WS_POPUP))==0) && ((cp.ExStyle & (int)WindowExStyles.WS_EX_APPWINDOW) == 0)) {
1268                                 // If we want to be hidden from the taskbar we need to be 'owned' by 
1269                                 // something not on the taskbar. FosterParent is just that
1270                                 ParentHandle = FosterParent;
1271                         }
1272
1273                         FakeStyles (cp);
1274
1275                         string class_name = RegisterWindowClass (cp.ClassStyle);
1276                         HwndCreating = hwnd;
1277
1278                         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);
1279
1280                         HwndCreating = null;
1281
1282                         if (WindowHandle==IntPtr.Zero) {
1283                                 uint error = Win32GetLastError();
1284
1285                                 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1286                         }
1287
1288                         hwnd.ClientWindow = WindowHandle;
1289
1290                         Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1291
1292                         return WindowHandle;
1293                 }
1294
1295                 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1296                         CreateParams create_params = new CreateParams();
1297
1298                         create_params.Caption = "";
1299                         create_params.X = X;
1300                         create_params.Y = Y;
1301                         create_params.Width = Width;
1302                         create_params.Height = Height;
1303
1304                         create_params.ClassName=XplatUI.DefaultClassName;
1305                         create_params.ClassStyle = 0;
1306                         create_params.ExStyle=0;
1307                         create_params.Parent=IntPtr.Zero;
1308                         create_params.Param=0;
1309
1310                         return CreateWindow(create_params);
1311                 }
1312
1313                 internal override void DestroyWindow(IntPtr handle) {
1314                         Hwnd    hwnd;
1315
1316                         hwnd = Hwnd.ObjectFromHandle(handle);
1317                         Win32DestroyWindow(handle);
1318                         hwnd.Dispose();
1319                         return;
1320                 }
1321
1322                 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1323                         // We do nothing, Form has to handle WM_GETMINMAXINFO
1324                 }
1325
1326
1327                 internal override FormWindowState GetWindowState(IntPtr handle) {
1328                         uint style;
1329
1330                         style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1331                         if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1332                                 return FormWindowState.Maximized;
1333                         } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1334                                 return FormWindowState.Minimized;
1335                         }
1336                         return FormWindowState.Normal;
1337                 }
1338
1339                 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1340                         switch(state) {
1341                                 case FormWindowState.Normal: {
1342                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1343                                         return;
1344                                 }
1345
1346                                 case FormWindowState.Minimized: {
1347                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1348                                         return;
1349                                 }
1350
1351                                 case FormWindowState.Maximized: {
1352                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1353                                         return;
1354                                 }
1355                         }
1356                 }
1357
1358                 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1359
1360                         FakeStyles (cp);
1361
1362                         Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1363                         Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1364
1365                         if ((cp.ExStyle & (int) WindowExStyles.WS_EX_TOOLWINDOW) > 0 || 
1366                                 ((cp.WindowStyle & (WindowStyles.WS_CAPTION)) != WindowStyles.WS_CAPTION && cp.control is Form))
1367                                 XplatUI.RequestNCRecalc (handle);
1368                 }
1369
1370                 internal override double GetWindowTransparency(IntPtr handle)
1371                 {
1372                         LayeredWindowAttributes lwa;
1373                         COLORREF clrRef;
1374                         byte alpha;
1375
1376                         if (0 == Win32GetLayeredWindowAttributes (handle, out clrRef, out alpha, out lwa))
1377                                 return 1.0;
1378
1379                         return ((double)alpha) / 255.0;
1380                 }
1381
1382                 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1383                         LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1384                         byte opacity = (byte)(transparency*255);
1385                         COLORREF clrRef = new COLORREF();
1386                         if (key != Color.Empty) {
1387                                 clrRef.R = key.R;
1388                                 clrRef.G = key.G;
1389                                 clrRef.B = key.B;
1390                                 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1391                         }
1392                         RECT rc;
1393                         rc.right = 1000;
1394                         rc.bottom = 1000;
1395                         Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1396                 }
1397
1398                 TransparencySupport support;
1399                 bool queried_transparency_support;
1400                 internal override TransparencySupport SupportsTransparency() {
1401                         if (queried_transparency_support)
1402                                 return support;
1403
1404                         bool flag;
1405                         support = TransparencySupport.None;
1406
1407                         flag = true;
1408                         try {
1409                                 Win32SetLayeredWindowAttributes (IntPtr.Zero, new COLORREF (), 255, LayeredWindowAttributes.LWA_ALPHA);
1410                         }
1411                         catch (EntryPointNotFoundException) { flag = false; }
1412                         catch { /* swallow everything else */ }
1413
1414                         if (flag) support |= TransparencySupport.Set;
1415
1416                         flag = true;
1417                         try {
1418                                 LayeredWindowAttributes lwa;
1419                                 COLORREF clrRef;
1420                                 byte alpha;
1421
1422                                 Win32GetLayeredWindowAttributes (IntPtr.Zero, out clrRef, out alpha, out lwa);
1423                         }
1424                         catch (EntryPointNotFoundException) { flag = false; }
1425                         catch { /* swallow everything else */ }
1426
1427                         if (flag) support |= TransparencySupport.Get;
1428
1429                         queried_transparency_support = true;
1430                         return support;
1431                 }
1432
1433                 internal override void UpdateWindow(IntPtr handle) {
1434                         Win32UpdateWindow(handle);
1435                 }
1436
1437                 internal override PaintEventArgs PaintEventStart(IntPtr handle, bool client) {
1438                         IntPtr          hdc;
1439                         PAINTSTRUCT     ps;
1440                         PaintEventArgs  paint_event;
1441                         RECT            rect;
1442                         Rectangle       clip_rect;
1443                         Hwnd            hwnd;
1444
1445                         clip_rect = new Rectangle();
1446                         rect = new RECT();
1447                         ps = new PAINTSTRUCT();
1448
1449                         hwnd = Hwnd.ObjectFromHandle(handle);
1450
1451                         if (client) {
1452                                 if (Win32GetUpdateRect(handle, ref rect, false)) {
1453                                         hdc = Win32BeginPaint(handle, ref ps);
1454
1455                                         hwnd.drawing_stack.Push (ps);
1456
1457                                         clip_rect = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top);
1458                                 } else {
1459                                         hdc = Win32GetDC(handle);
1460                                         // FIXME: Add the DC to internal list
1461
1462                                         hwnd.drawing_stack.Push (null);
1463
1464                                         clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
1465                                 }
1466                         } else {
1467                                 hdc = Win32GetWindowDC (handle);
1468
1469                                 hwnd.drawing_stack.Push (hdc);
1470
1471                                 // HACK this in for now
1472                                 Win32GetWindowRect (handle, out rect);
1473                                 clip_rect = new Rectangle(0, 0, rect.right-rect.left, rect.bottom-rect.top);
1474                         }
1475
1476                         Graphics dc = Graphics.FromHdc(hdc);
1477                         hwnd.drawing_stack.Push (dc);
1478
1479                         paint_event = new PaintEventArgs(dc, clip_rect);
1480
1481                         return paint_event;
1482                 }
1483
1484                 internal override void PaintEventEnd(IntPtr handle, bool client) {
1485                         Hwnd            hwnd;
1486
1487                         hwnd = Hwnd.ObjectFromHandle(handle);
1488
1489                         Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1490                         dc.Dispose ();
1491
1492                         if (client) {
1493                                 object o = hwnd.drawing_stack.Pop();
1494                                 if (o != null) {
1495                                         PAINTSTRUCT ps = (PAINTSTRUCT)o;
1496                                         Win32EndPaint(handle, ref ps);
1497                                 }
1498                         } else {
1499                                 IntPtr hdc = (IntPtr)hwnd.drawing_stack.Pop();
1500                                 Win32ReleaseDC(handle, hdc);
1501                         }
1502                 }
1503
1504
1505                 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1506                         Win32MoveWindow(handle, x, y, width, height, true);
1507                         return;
1508                 }
1509
1510                 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) {
1511                         IntPtr  parent;
1512                         RECT    rect;
1513                         POINT   pt;
1514
1515                         Win32GetWindowRect(handle, out rect);
1516                         width = rect.right - rect.left;
1517                         height = rect.bottom - rect.top;
1518
1519                         pt.x=rect.left;
1520                         pt.y=rect.top;
1521
1522                         parent = Win32GetParent(handle);
1523                         Win32ScreenToClient(parent, ref pt);
1524
1525                         x = pt.x;
1526                         y = pt.y;
1527
1528                         Win32GetClientRect(handle, out rect);
1529                         client_width = rect.right - rect.left;
1530                         client_height = rect.bottom - rect.top;
1531                         return;
1532                 }
1533
1534                 internal override void Activate(IntPtr handle) {
1535                         Win32SetActiveWindow(handle);
1536                 }
1537
1538                 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1539                         RECT rect;
1540
1541                         rect.left=rc.Left;
1542                         rect.top=rc.Top;
1543                         rect.right=rc.Right;
1544                         rect.bottom=rc.Bottom;
1545                         Win32InvalidateRect(handle, ref rect, clear);
1546                 }
1547
1548
1549                 internal override void InvalidateNC (IntPtr handle)
1550                 {
1551                         // found this gem at
1552                         // http://www.dotnet247.com/247reference/msgs/58/292037.aspx
1553                         Win32SetWindowPos(handle, IntPtr.Zero,
1554                                           0, 0, 0, 0,
1555                                           SetWindowPosFlags.SWP_NOMOVE |
1556                                           SetWindowPosFlags.SWP_NOSIZE |
1557                                           SetWindowPosFlags.SWP_NOZORDER |
1558                                           SetWindowPosFlags.SWP_NOACTIVATE |
1559                                           SetWindowPosFlags.SWP_DRAWFRAME);
1560                 }
1561
1562                 private IntPtr InternalWndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
1563                 {
1564                         if (HwndCreating != null && HwndCreating.ClientWindow == IntPtr.Zero)
1565                                 HwndCreating.ClientWindow = hWnd;
1566                         return NativeWindow.WndProc (hWnd, msg, wParam, lParam);
1567                 }
1568
1569                 internal override IntPtr DefWndProc(ref Message msg) {
1570                         msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1571                         return msg.Result;
1572                 }
1573
1574                 internal override void HandleException(Exception e) {
1575                         StackTrace st = new StackTrace(e);
1576                         Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1577                         Console.WriteLine("{0}{1}", e.Message, st.ToString());
1578                 }
1579
1580                 internal override void DoEvents() {
1581                         MSG msg = new MSG();
1582
1583                         if (override_cursor != IntPtr.Zero) {
1584                                 Cursor.Current = null;
1585                         }
1586
1587                         while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1588                                 XplatUI.TranslateMessage(ref msg);
1589                                 XplatUI.DispatchMessage(ref msg);
1590                         }
1591                 }
1592
1593                 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1594                         return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1595                 }
1596
1597                 internal override void PostQuitMessage(int exitCode) {
1598                         Win32PostQuitMessage(exitCode);
1599                 }
1600
1601                 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
1602                 {
1603                         if (wm_nc_registered == null)
1604                                 wm_nc_registered = new Hashtable ();
1605                                 
1606                         TMEFlags flags = TMEFlags.TME_NONCLIENT;
1607                         if (hover)
1608                                 flags |= TMEFlags.TME_HOVER;
1609                         if (leave)
1610                                 flags |= TMEFlags.TME_LEAVE;
1611
1612                         if (flags == TMEFlags.TME_NONCLIENT) {
1613                                 if (wm_nc_registered.Contains (hwnd)) {
1614                                         wm_nc_registered.Remove (hwnd);
1615                                 }
1616                         } else {
1617                                 if (!wm_nc_registered.Contains (hwnd)) {
1618                                         wm_nc_registered.Add (hwnd, flags);
1619                                 } else {
1620                                         wm_nc_registered [hwnd] = flags;
1621                                 }
1622                         }
1623                 }
1624
1625                 internal override void RequestNCRecalc(IntPtr handle) {
1626                         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);
1627                 }
1628
1629                 internal override void ResetMouseHover(IntPtr handle) {
1630                         TRACKMOUSEEVENT tme;
1631
1632                         tme = new TRACKMOUSEEVENT();
1633                         tme.size = Marshal.SizeOf(tme);
1634                         tme.hWnd = handle;
1635                         tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1636                         Win32TrackMouseEvent(ref tme);
1637                 }
1638
1639
1640                 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1641                         return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
1642                 }
1643
1644                 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
1645                         bool            result;
1646
1647                         msg.refobject = 0;
1648                         if (RetrieveMessage(ref msg)) {
1649                                 return true;
1650                         }
1651
1652                         if (blocking) {
1653                                 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
1654                         } else {
1655                                 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
1656                                 if (!result) {
1657                                         return false;
1658                                 }
1659                         }
1660
1661                         // We need to fake WM_MOUSE_ENTER
1662                         switch (msg.message) {
1663                                 case Msg.WM_LBUTTONDOWN: {
1664                                         mouse_state |= MouseButtons.Left;
1665                                         break;
1666                                 }
1667
1668                                 case Msg.WM_MBUTTONDOWN: {
1669                                         mouse_state |= MouseButtons.Middle;
1670                                         break;
1671                                 }
1672
1673                                 case Msg.WM_RBUTTONDOWN: {
1674                                         mouse_state |= MouseButtons.Right;
1675                                         break;
1676                                 }
1677
1678                                 case Msg.WM_LBUTTONUP: {
1679                                         mouse_state &= ~MouseButtons.Left;
1680                                         break;
1681                                 }
1682
1683                                 case Msg.WM_MBUTTONUP: {
1684                                         mouse_state &= ~MouseButtons.Middle;
1685                                         break;
1686                                 }
1687
1688                                 case Msg.WM_RBUTTONUP: {
1689                                         mouse_state &= ~MouseButtons.Right;
1690                                         break;
1691                                 }
1692
1693                                 case Msg.WM_ASYNC_MESSAGE: {
1694                                         XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
1695                                         break;
1696                                 }
1697
1698                                 case Msg.WM_MOUSEMOVE: {
1699                                         if (msg.hwnd != prev_mouse_hwnd) {
1700                                                 TRACKMOUSEEVENT tme;
1701
1702                                                 // The current message will be sent out next time around
1703                                                 StoreMessage(ref msg);
1704
1705                                                 // This is the message we want to send at this point
1706                                                 msg.message = Msg.WM_MOUSE_ENTER;
1707
1708                                                 prev_mouse_hwnd = msg.hwnd;
1709
1710                                                 tme = new TRACKMOUSEEVENT();
1711                                                 tme.size = Marshal.SizeOf(tme);
1712                                                 tme.hWnd = msg.hwnd;
1713                                                 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1714                                                 Win32TrackMouseEvent(ref tme);
1715                                                 return result;
1716                                         }
1717                                         break;
1718                                 }
1719
1720                                 case Msg.WM_NCMOUSEMOVE: {
1721                                         if (wm_nc_registered == null || !wm_nc_registered.Contains (msg.hwnd))
1722                                                 break;
1723                                                 
1724                                         TRACKMOUSEEVENT tme;
1725
1726                                         tme = new TRACKMOUSEEVENT ();
1727                                         tme.size = Marshal.SizeOf(tme);
1728                                         tme.hWnd = msg.hwnd;
1729                                         tme.dwFlags = (TMEFlags)wm_nc_registered[msg.hwnd];
1730                                         Win32TrackMouseEvent (ref tme);
1731                                         return result;
1732                                 }
1733
1734                                 case Msg.WM_DROPFILES: {
1735                                         return Win32DnD.HandleWMDropFiles(ref msg);
1736                                 }
1737
1738                                 case Msg.WM_MOUSELEAVE: {
1739                                         prev_mouse_hwnd = IntPtr.Zero;
1740                                         break;
1741                                 }
1742
1743                                 case Msg.WM_TIMER: {
1744                                         Timer timer=(Timer)timer_list[(int)msg.wParam];
1745
1746                                         if (timer != null) {
1747                                                 timer.FireTick();
1748                                         }
1749                                         break;
1750                                 }
1751                         }
1752
1753                         return result;
1754                 }
1755
1756                 internal override bool TranslateMessage(ref MSG msg) {
1757                         return Win32TranslateMessage(ref msg);
1758                 }
1759
1760                 internal override IntPtr DispatchMessage(ref MSG msg) {
1761                         return Win32DispatchMessage(ref msg);
1762                 }
1763
1764                 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
1765                         if (Top) {
1766                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1767                                 return true;
1768                         } else if (!Bottom) {
1769                                 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1770                         } else {
1771                                 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1772                                 return true;
1773                         }
1774                         return false;
1775                 }
1776
1777                 internal override bool SetTopmost(IntPtr hWnd, bool Enabled) {
1778                         if (Enabled) {
1779                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1780                                 return true;
1781                         } else {
1782                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1783                                 return true;
1784                         }
1785                 }
1786                 
1787                 internal override bool SetOwner(IntPtr hWnd, IntPtr hWndOwner) {
1788                         Win32SetWindowLong(hWnd, WindowLong.GWL_HWNDPARENT, (uint) hWndOwner);
1789                         return true;
1790                 }
1791
1792                 internal override bool Text(IntPtr handle, string text) {
1793                         Win32SetWindowText(handle, text);
1794                         return true;
1795                 }
1796
1797                 internal override bool GetText(IntPtr handle, out string text) {
1798                         StringBuilder sb;
1799
1800                         sb = new StringBuilder(256);
1801                         Win32GetWindowText(handle, sb, sb.Capacity);
1802                         text = sb.ToString();
1803                         return true;
1804                 }
1805
1806                 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
1807                 {
1808                         if (visible) {
1809                                 Control c = Control.FromHandle (handle);
1810                                 if (c is Form) {
1811                                         Form f;
1812
1813                                         f = (Form)Control.FromHandle (handle);
1814                                         WindowPlacementFlags flags = WindowPlacementFlags.SW_SHOWNORMAL;
1815                                         switch (f.WindowState) {
1816                                                 case FormWindowState.Normal: flags = WindowPlacementFlags.SW_SHOWNORMAL; break;
1817                                                 case FormWindowState.Minimized: flags = WindowPlacementFlags.SW_MINIMIZE; break;
1818                                                 case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
1819                                         }
1820                                         
1821                                         if (!f.ActivateOnShow)
1822                                                 flags = WindowPlacementFlags.SW_SHOWNOACTIVATE;
1823                                                 
1824                                         Win32ShowWindow (handle, flags);
1825                                 }
1826                                 else {
1827                                         if (c.ActivateOnShow)
1828                                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
1829                                         else
1830                                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
1831                                 }
1832                         }
1833                         else {
1834                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_HIDE);
1835                         }
1836                         return true;
1837                 }
1838
1839                 internal override bool IsEnabled(IntPtr handle) {
1840                         return IsWindowEnabled (handle);
1841                 }
1842                 
1843                 internal override bool IsVisible(IntPtr handle) {
1844                         return IsWindowVisible (handle);
1845                 }
1846
1847                 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
1848                         Control c = Control.FromHandle (handle);
1849                         if (parent == IntPtr.Zero) {
1850                                 if (!(c is Form)) {
1851                                         Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
1852                                 }
1853                         }
1854                         else
1855                                 SetVisible (handle, c.is_visible, true);
1856                                 
1857                         // The Win32SetParent is lame, it can very well move the window
1858                         // ref: http://groups.google.com/group/microsoft.public.vb.winapi/browse_thread/thread/1b82ccc54231ecee/afa82835bfc0422a%23afa82835bfc0422a
1859                         // Here we save the position before changing the parent, and if it has changed afterwards restore it.
1860                         // Another possibility would be to intercept WM_WINDOWPOSCHANGING and restore the coords there, but this would require plumbing in weird places
1861                         // (either inside Control or add handling to InternalWndProc)
1862                         // We also need to remove WS_CHILD if making the window parent-less, and add it if we're parenting it.
1863                         RECT rect, rect2;
1864                         IntPtr result;
1865                         WindowStyles style, new_style;
1866                         
1867                         Win32GetWindowRect (handle, out rect);
1868                         style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
1869                         
1870                         if (parent == IntPtr.Zero) {
1871                                 new_style = style & ~WindowStyles.WS_CHILD;
1872                                 result = Win32SetParent (handle, FosterParent);
1873                         } else {
1874                                 new_style = style | WindowStyles.WS_CHILD;
1875                                 result = Win32SetParent (handle, parent);
1876                         }
1877                         if (style != new_style && c is Form) {
1878                                 Win32SetWindowLong (handle, WindowLong.GWL_STYLE, (uint) new_style);
1879                         }
1880                         Win32GetWindowRect (handle, out rect2);
1881                         if (rect.top != rect2.top && rect.left != rect2.left && c is Form) {
1882                                 Win32SetWindowPos (handle, IntPtr.Zero, rect.top, rect.left, rect.Width, rect.Height, SetWindowPosFlags.SWP_NOZORDER |  SetWindowPosFlags.SWP_NOREDRAW | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOENDSCHANGING | SetWindowPosFlags.SWP_NOACTIVATE);
1883                         }
1884                         return result;
1885                 }
1886
1887                 // If we ever start using this, we should probably replace FosterParent with IntPtr.Zero
1888                 internal override IntPtr GetParent(IntPtr handle) {
1889                         return Win32GetParent(handle);
1890                 }
1891
1892                 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
1893                         grab_hwnd = hWnd;
1894                         Win32SetCapture(hWnd);
1895                         
1896                         if (ConfineToHwnd != IntPtr.Zero) {
1897                                 RECT window_rect;
1898                                 Win32GetWindowRect (ConfineToHwnd, out window_rect);
1899                                 Win32GetClipCursor (out clipped_cursor_rect);
1900                                 Win32ClipCursor (ref window_rect);
1901                         }
1902                 }
1903
1904                 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
1905                         hWnd = grab_hwnd;
1906                         GrabConfined = grab_confined;
1907                         GrabArea = grab_area;
1908                 }
1909
1910                 internal override void UngrabWindow(IntPtr hWnd) {
1911                         if (!(clipped_cursor_rect.top == 0 && clipped_cursor_rect.bottom == 0 && clipped_cursor_rect.left == 0 && clipped_cursor_rect.right == 0)) {
1912                                 Win32ClipCursor (ref clipped_cursor_rect);
1913                                 clipped_cursor_rect = new RECT ();
1914                         }
1915                         
1916                         Win32ReleaseCapture();
1917                         grab_hwnd = IntPtr.Zero;
1918                 }
1919
1920                 internal override bool CalculateWindowRect(ref Rectangle ClientRect, int Style, int ExStyle, Menu menu, out Rectangle WindowRect) {
1921                         RECT    rect;
1922
1923                         rect.left=ClientRect.Left;
1924                         rect.top=ClientRect.Top;
1925                         rect.right=ClientRect.Right;
1926                         rect.bottom=ClientRect.Bottom;
1927
1928                         if (!Win32AdjustWindowRectEx(ref rect, Style, menu != null, ExStyle)) {
1929                                 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
1930                                 return false;
1931                         }
1932
1933                         WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
1934                         return true;
1935                 }
1936
1937                 internal override void SetCursor(IntPtr window, IntPtr cursor) {
1938                         Win32SetCursor(cursor);
1939                         return;
1940                 }
1941
1942                 internal override void ShowCursor(bool show) {
1943                         Win32ShowCursor(show);
1944                 }
1945
1946                 internal override void OverrideCursor(IntPtr cursor) {
1947                         Win32SetCursor(cursor);
1948                 }
1949
1950                 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
1951                         IntPtr  cursor;
1952                         Bitmap  cursor_bitmap;
1953                         Bitmap  cursor_mask;
1954                         Byte[]  cursor_bits;
1955                         Byte[]  mask_bits;
1956                         Color   pixel;
1957                         int     width;
1958                         int     height;
1959
1960                         // Win32 only allows creation cursors of a certain size
1961                         if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
1962                                 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1963                                 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1964                         } else {
1965                                 cursor_bitmap = bitmap;
1966                                 cursor_mask = mask;
1967                         }
1968
1969                         width = cursor_bitmap.Width;
1970                         height = cursor_bitmap.Height;
1971
1972                         cursor_bits = new Byte[(width / 8) * height];
1973                         mask_bits = new Byte[(width / 8) * height];
1974
1975                         for (int y = 0; y < height; y++) {
1976                                 for (int x = 0; x < width; x++) {
1977                                         pixel = cursor_bitmap.GetPixel(x, y);
1978
1979                                         if (pixel == cursor_pixel) {
1980                                                 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1981                                         }
1982
1983                                         pixel = cursor_mask.GetPixel(x, y);
1984
1985                                         if (pixel == mask_pixel) {
1986                                                 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1987                                         }
1988                                 }
1989                         }
1990
1991                         cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
1992
1993                         return cursor;
1994                 }
1995
1996                 [MonoTODO("Define the missing cursors")]
1997                 internal override IntPtr DefineStdCursor(StdCursor id) {
1998                         switch(id) {
1999                                 case StdCursor.AppStarting:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
2000                                 case StdCursor.Arrow:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2001                                 case StdCursor.Cross:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
2002                                 case StdCursor.Default:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2003                                 case StdCursor.Hand:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
2004                                 case StdCursor.Help:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
2005                                 case StdCursor.HSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2006                                 case StdCursor.IBeam:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
2007                                 case StdCursor.No:              return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
2008                                 case StdCursor.NoMove2D:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2009                                 case StdCursor.NoMoveHoriz:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2010                                 case StdCursor.NoMoveVert:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2011                                 case StdCursor.PanEast:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2012                                 case StdCursor.PanNE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2013                                 case StdCursor.PanNorth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2014                                 case StdCursor.PanNW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2015                                 case StdCursor.PanSE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2016                                 case StdCursor.PanSouth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2017                                 case StdCursor.PanSW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2018                                 case StdCursor.PanWest:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2019                                 case StdCursor.SizeAll:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
2020                                 case StdCursor.SizeNESW:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
2021                                 case StdCursor.SizeNS:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
2022                                 case StdCursor.SizeNWSE:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
2023                                 case StdCursor.SizeWE:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
2024                                 case StdCursor.UpArrow:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
2025                                 case StdCursor.VSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2026                                 case StdCursor.WaitCursor:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
2027                         }
2028                         throw new NotImplementedException ();
2029                 }
2030
2031                 internal override void DestroyCursor(IntPtr cursor) {
2032                         if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
2033                                 Win32DestroyCursor(cursor);
2034                         }
2035                 }
2036
2037                 [MonoTODO]
2038                 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
2039                         width = 20;
2040                         height = 20;
2041                         hotspot_x = 0;
2042                         hotspot_y = 0;
2043                 }
2044
2045                 internal override void SetCursorPos(IntPtr handle, int x, int y) {
2046                         Win32SetCursorPos(x, y);
2047                 }
2048
2049                 internal override Region GetClipRegion(IntPtr hwnd) {
2050                         Region region;
2051
2052                         region = new Region();
2053
2054                         Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
2055
2056                         return region;
2057                 }
2058
2059                 internal override void SetClipRegion(IntPtr hwnd, Region region) {
2060                         Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
2061                 }
2062
2063                 internal override void EnableWindow(IntPtr handle, bool Enable) {
2064                         Win32EnableWindow(handle, Enable);
2065                 }
2066
2067                 internal override void EndLoop(System.Threading.Thread thread) {
2068                         // Nothing to do
2069                 }
2070
2071                 internal override object StartLoop(System.Threading.Thread thread) {
2072                         return null;
2073                 }
2074
2075                 internal override void SetModal(IntPtr handle, bool Modal) {
2076                         // we do nothing on Win32
2077                 }
2078
2079                 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
2080                         POINT   pt;
2081
2082                         Win32GetCursorPos(out pt);
2083
2084                         if (handle!=IntPtr.Zero) {
2085                                 Win32ScreenToClient(handle, ref pt);
2086                         }
2087
2088                         x=pt.x;
2089                         y=pt.y;
2090                 }
2091
2092                 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
2093                 {
2094                         POINT pnt = new POINT();                        
2095
2096                         pnt.x = x;
2097                         pnt.y = y;
2098                         Win32ScreenToClient (handle, ref pnt);
2099
2100                         x = pnt.x;
2101                         y = pnt.y;
2102                 }
2103
2104                 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {                    
2105                         POINT   pnt = new POINT();                      
2106
2107                         pnt.x = x;
2108                         pnt.y = y;
2109
2110                         Win32ClientToScreen(handle, ref pnt);
2111
2112                         x = pnt.x;
2113                         y = pnt.y;
2114                 }
2115
2116                 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
2117                         RECT    rect;
2118  
2119                         Win32GetWindowRect(handle, out rect);
2120                         x -= rect.left + SystemInformation.FrameBorderSize.Width;
2121                         y -= rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
2122                         return;
2123                 }
2124   
2125                 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {                      
2126                         RECT    rect;
2127  
2128                         Win32GetWindowRect(handle, out rect);
2129                         x += rect.left + SystemInformation.FrameBorderSize.Width;
2130                         y += rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
2131                         return;
2132                 }
2133
2134                 internal override void SendAsyncMethod (AsyncMethodData method)
2135                 {
2136                         Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
2137                 }
2138
2139                 internal override void SetTimer (Timer timer)
2140                 {
2141                         int     index;
2142
2143                         index = timer.GetHashCode();
2144
2145                         lock (timer_list) {
2146                                 timer_list[index]=timer;
2147                         }
2148
2149                         Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero);
2150                 }
2151
2152                 internal override void KillTimer (Timer timer)
2153                 {
2154                         int     index;
2155
2156                         index = timer.GetHashCode();
2157
2158                         Win32KillTimer(FosterParent, index);
2159
2160                         lock (timer_list) {
2161                                 timer_list.Remove(index);
2162                         }
2163                 }
2164                 
2165                 private void FakeStyles (CreateParams cp)
2166                 {
2167                         if (cp.HasWindowManager) {
2168                                 // Remove all styles but WS_VISIBLE.
2169                                 cp.WindowStyle &= WindowStyles.WS_VISIBLE;
2170                                 // Set styles that enables us to use the window manager.
2171                                 cp.WindowStyle |= WindowStyles.WS_CHILD | WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS;
2172                                 cp.ExStyle = 0;
2173                         }
2174                 }
2175                 
2176                 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
2177                         Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
2178                         caret_visible = false;
2179                 }
2180
2181                 internal override void DestroyCaret(IntPtr hwnd) {
2182                         Win32DestroyCaret();
2183                 }
2184
2185                 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
2186                         Win32SetCaretPos(x, y);
2187                 }
2188
2189                 internal override void CaretVisible(IntPtr hwnd, bool visible) {
2190                         if (visible) {
2191                                 if (!caret_visible) {
2192                                         Win32ShowCaret(hwnd);
2193                                         caret_visible = true;
2194                                 }
2195                         } else {
2196                                 if (caret_visible) {
2197                                         Win32HideCaret(hwnd);
2198                                         caret_visible = false;
2199                                 }
2200                         }
2201                 }
2202
2203                 internal override IntPtr GetFocus() {
2204                         return Win32GetFocus();
2205                 }
2206
2207                 internal override void SetFocus(IntPtr hwnd) {
2208                         Win32SetFocus(hwnd);
2209                 }
2210
2211                 internal override IntPtr GetActive() {
2212                         return Win32GetActiveWindow();
2213                 }
2214
2215                 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
2216                         IntPtr          dc;
2217                         TEXTMETRIC      tm;
2218
2219                         tm = new TEXTMETRIC();
2220
2221                         dc = Win32GetDC(IntPtr.Zero);
2222                         Win32SelectObject(dc, font.ToHfont());
2223                         if (Win32GetTextMetrics(dc, ref tm) == false) {
2224                                 Win32ReleaseDC(IntPtr.Zero, dc);
2225                                 ascent = 0;
2226                                 descent = 0;
2227                                 return false;
2228                         }
2229                         Win32ReleaseDC(IntPtr.Zero, dc);
2230
2231                         ascent = tm.tmAscent;
2232                         descent = tm.tmDescent;
2233
2234                         return true;
2235                 }
2236
2237                 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
2238                         RECT    rect;
2239
2240                         rect = new RECT();
2241                         rect.left = rectangle.X;
2242                         rect.top = rectangle.Y;
2243                         rect.right = rectangle.Right;
2244                         rect.bottom = rectangle.Bottom;
2245
2246                         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));
2247                         Win32UpdateWindow(hwnd);
2248                 }
2249
2250                 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
2251                         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));
2252                 }
2253
2254                 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
2255                         NOTIFYICONDATA  nid;
2256
2257                         nid = new NOTIFYICONDATA();
2258
2259                         nid.cbSize = (uint)Marshal.SizeOf(nid);
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                         tt = null;
2276
2277                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
2278                 }
2279
2280                 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
2281                         NOTIFYICONDATA  nid;
2282
2283                         nid = new NOTIFYICONDATA();
2284
2285                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2286                         nid.hIcon = icon.Handle;
2287                         nid.hWnd = hwnd;
2288                         nid.uID = 1;
2289                         nid.uCallbackMessage = (uint)Msg.WM_USER;
2290                         nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2291
2292                         if (tip != null) {
2293                                 nid.szTip = tip;
2294                                 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2295                         }
2296
2297                         if (icon != null) {
2298                                 nid.hIcon = icon.Handle;
2299                                 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2300                         }
2301
2302                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2303                 }
2304
2305                 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
2306                         NOTIFYICONDATA  nid;
2307
2308                         nid = new NOTIFYICONDATA();
2309
2310                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2311                         nid.hWnd = hwnd;
2312                         nid.uID = 1;
2313                         nid.uFlags = 0;
2314
2315                         Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
2316                 }
2317
2318 #if NET_2_0
2319                 internal override void SystrayBalloon(IntPtr hwnd, int timeout, string title, string text, ToolTipIcon icon)
2320                 {
2321                         NOTIFYICONDATA  nid;
2322
2323                         nid = new NOTIFYICONDATA();
2324
2325                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2326                         nid.hWnd = hwnd;
2327                         nid.uID = 1;
2328                         nid.uFlags = NotifyIconFlags.NIF_INFO;
2329                         nid.uTimeoutOrVersion = timeout;
2330                         nid.szInfoTitle = title;
2331                         nid.szInfo = text;
2332                         nid.dwInfoFlags = icon;
2333                         
2334                         Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2335                 }
2336 #endif
2337
2338                 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
2339                         // Nothing to do on Win32
2340                 }
2341
2342                 internal override void SetMenu(IntPtr handle, Menu menu) {
2343                         // Trigger WM_NCCALC
2344                         Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2345                 }
2346
2347                 internal override Point GetMenuOrigin(IntPtr handle) {
2348                         return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2349                 }
2350
2351                 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2352                         Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : icon.Handle);    // 1 = large icon (0 would be small)
2353                 }
2354
2355                 internal override void ClipboardClose(IntPtr handle) {
2356                         if (handle != clip_magic) {
2357                                 throw new ArgumentException("handle is not a valid clipboard handle");
2358                         }
2359                         Win32CloseClipboard();
2360                 }
2361
2362                 internal override int ClipboardGetID(IntPtr handle, string format) {
2363                         if (handle != clip_magic) {
2364                                 throw new ArgumentException("handle is not a valid clipboard handle");
2365                         }
2366                         if (format == "Text" ) return 1;
2367                         else if (format == "Bitmap" ) return 2;
2368                         else if (format == "MetaFilePict" ) return 3;
2369                         else if (format == "SymbolicLink" ) return 4;
2370                         else if (format == "DataInterchangeFormat" ) return 5;
2371                         else if (format == "Tiff" ) return 6;
2372                         else if (format == "OEMText" ) return 7;
2373                         else if (format == "DeviceIndependentBitmap" ) return 8;
2374                         else if (format == "Palette" ) return 9;
2375                         else if (format == "PenData" ) return 10;
2376                         else if (format == "RiffAudio" ) return 11;
2377                         else if (format == "WaveAudio" ) return 12;
2378                         else if (format == "UnicodeText" ) return 13;
2379                         else if (format == "EnhancedMetafile" ) return 14;
2380                         else if (format == "FileDrop" ) return 15;
2381                         else if (format == "Locale" ) return 16;
2382
2383                         return (int)Win32RegisterClipboardFormat(format);
2384                 }
2385
2386                 internal override IntPtr ClipboardOpen(bool primary_selection) {
2387                         // Win32 does not have primary selection
2388                         Win32OpenClipboard(FosterParent);
2389                         return clip_magic;
2390                 }
2391
2392                 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2393                         uint    format;
2394                         int[]   result;
2395                         int     count;
2396
2397                         if (handle != clip_magic) {
2398                                 return null;
2399                         }
2400
2401                         // Count first
2402                         count = 0;
2403                         format = 0;
2404                         do {
2405                                 format = Win32EnumClipboardFormats(format);
2406                                 if (format != 0) {
2407                                         count++;
2408                                 }
2409                         } while (format != 0);
2410
2411                         // Now assign
2412                         result = new int[count];
2413                         count = 0;
2414                         format = 0;
2415                         do {
2416                                 format = Win32EnumClipboardFormats(format);
2417                                 if (format != 0) {
2418                                         result[count++] = (int)format;
2419                                 }
2420                         } while (format != 0);
2421
2422                         return result;
2423                 }
2424
2425
2426                 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2427                         IntPtr  hmem;
2428                         IntPtr  data;
2429                         object  obj;
2430
2431                         if (handle != clip_magic) {
2432                                 throw new ArgumentException("handle is not a valid clipboard handle");
2433                         }
2434
2435                         hmem = Win32GetClipboardData((uint)type);
2436                         if (hmem == IntPtr.Zero) {
2437                                 return null;
2438                         }
2439
2440                         data = Win32GlobalLock(hmem);
2441                         if (data == IntPtr.Zero) {
2442                                 uint error = Win32GetLastError();
2443                                 Console.WriteLine("Error: {0}", error);
2444                                 return null;
2445                         }
2446
2447                         obj = null;
2448
2449                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2450                                 obj = AnsiToString(data);
2451                         } else switch ((ClipboardFormats)type) {
2452                                 case ClipboardFormats.CF_TEXT: {
2453                                         obj = AnsiToString(data);
2454                                         break;
2455                                 }
2456
2457                                 case ClipboardFormats.CF_DIB: {
2458                                         obj = DIBtoImage(data);
2459                                         break;
2460                                 }
2461
2462                                 case ClipboardFormats.CF_UNICODETEXT: {
2463                                         obj = UnicodeToString(data);
2464                                         break;
2465                                 }
2466
2467                                 default: {
2468                                         if (converter != null && !converter(type, data, out obj)) {
2469                                                 obj = null;
2470                                         }
2471                                         break;
2472                                 }
2473                         }
2474                         Win32GlobalUnlock(hmem);
2475
2476                         return obj;
2477
2478                 }
2479
2480                 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
2481                         byte[]  data;
2482                         IntPtr  hmem;
2483                         IntPtr  hmem_ptr;
2484
2485                         if (handle != clip_magic) {
2486                                 throw new ArgumentException("handle is not a valid clipboard handle");
2487                         }
2488
2489                         if (obj == null) {
2490                                 // Just clear it
2491                                 Win32EmptyClipboard();
2492                                 return;
2493                         }
2494
2495                         if (type == -1) {
2496                                 if (obj is string) {
2497                                         type = (int)ClipboardFormats.CF_UNICODETEXT;
2498                                 } else if (obj is Image) {
2499                                         type = (int)ClipboardFormats.CF_DIB;
2500                                 }
2501                         }
2502
2503                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2504                                 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2505                                 Win32SetClipboardData((uint)type, hmem);
2506                                 return;
2507                         } else switch((ClipboardFormats)type) {
2508                                 case ClipboardFormats.CF_UNICODETEXT: {
2509                                         hmem = Marshal.StringToHGlobalUni((string)obj);
2510                                         Win32SetClipboardData((uint)type, hmem);
2511                                         return;
2512                                 }
2513
2514                                 case ClipboardFormats.CF_TEXT: {
2515                                         hmem = Marshal.StringToHGlobalAnsi((string)obj);
2516                                         Win32SetClipboardData((uint)type, hmem);
2517                                         return;
2518                                 }
2519
2520                                 case ClipboardFormats.CF_BITMAP:
2521                                 case ClipboardFormats.CF_DIB: {
2522                                         data = ImageToDIB((Image)obj);
2523
2524                                         hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2525                                         hmem_ptr = Win32GlobalLock(hmem);
2526                                         Marshal.Copy(data, 0, hmem_ptr, data.Length);
2527                                         Win32GlobalUnlock(hmem);
2528                                         Win32SetClipboardData((uint)ClipboardFormats.CF_DIB, hmem);
2529                                         return;
2530                                 }
2531
2532                                 default: {
2533                                         if (converter != null && converter(ref type, obj, out data)) {
2534                                                 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2535                                                 hmem_ptr = Win32GlobalLock(hmem);
2536                                                 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2537                                                 Win32GlobalUnlock(hmem);
2538                                                 Win32SetClipboardData((uint)type, hmem);
2539                                         }
2540                                         return;
2541                                 }
2542                         }
2543                 }
2544
2545                 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
2546                         if (allowed) {
2547                                 Win32DnD.RegisterDropTarget(hwnd);
2548                         } else {
2549                                 Win32DnD.UnregisterDropTarget(hwnd);
2550                         }
2551                 }
2552
2553                 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
2554                         return Win32DnD.StartDrag(hwnd, data, allowedEffects);
2555                 }
2556
2557                 // XXX this doesn't work at all for FrameStyle.Dashed - it draws like Thick, and in the Thick case
2558                 // the corners are drawn incorrectly.
2559                 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
2560                         IntPtr          hdc;
2561                         IntPtr          pen;
2562                         IntPtr          oldpen;
2563                         COLORREF        clrRef = new COLORREF();
2564
2565                         // If we want the standard hatch pattern we would
2566                         // need to create a brush
2567
2568                         clrRef.R = backColor.R;
2569                         clrRef.G = backColor.G;
2570                         clrRef.B = backColor.B;
2571
2572                         // Grab a pen
2573                         pen = Win32CreatePen (style == FrameStyle.Thick ? PenStyle.PS_SOLID : PenStyle.PS_DASH,
2574                                               style == FrameStyle.Thick ? 4 : 2, ref clrRef);
2575
2576                         hdc = Win32GetDC(IntPtr.Zero);
2577                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2578                         oldpen = Win32SelectObject(hdc, pen);
2579
2580                         Win32MoveToEx(hdc, rectangle.Left, rectangle.Top, IntPtr.Zero);
2581                         if ((rectangle.Width > 0) && (rectangle.Height > 0)) {
2582                                 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2583                                 Win32LineTo(hdc, rectangle.Right, rectangle.Bottom);
2584                                 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2585                                 Win32LineTo(hdc, rectangle.Left, rectangle.Top);
2586                         } else {
2587                                 if (rectangle.Width > 0) {
2588                                         Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2589                                 } else {
2590                                         Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2591                                 }
2592                         }
2593
2594                         Win32SelectObject(hdc, oldpen);
2595                         Win32DeleteObject(pen);
2596
2597                         Win32ReleaseDC(IntPtr.Zero, hdc);
2598                 }
2599
2600                 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
2601                         IntPtr          hdc;
2602                         IntPtr          pen;
2603                         IntPtr          oldpen;
2604                         POINT           pt;
2605                         COLORREF        clrRef = new COLORREF();
2606
2607                         pt = new POINT();
2608                         pt.x = 0;
2609                         pt.y = 0;
2610                         Win32ClientToScreen(IntPtr.Zero, ref pt);
2611
2612                         // If we want the standard hatch pattern we would
2613                         // need to create a brush
2614
2615                         clrRef.R = backColor.R;
2616                         clrRef.G = backColor.G;
2617                         clrRef.B = backColor.B;
2618
2619                         // Grab a pen
2620                         pen = Win32CreatePen(PenStyle.PS_SOLID, 1, ref clrRef);
2621
2622                         hdc = Win32GetDC(IntPtr.Zero);
2623                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2624                         oldpen = Win32SelectObject(hdc, pen);
2625
2626                         Win32MoveToEx(hdc, pt.x + start.X, pt.y + start.Y, IntPtr.Zero);
2627                         Win32LineTo(hdc, pt.x + end.X, pt.y + end.Y);
2628
2629                         Win32SelectObject(hdc, oldpen);
2630                         Win32DeleteObject(pen);
2631
2632                         Win32ReleaseDC(IntPtr.Zero, hdc);
2633                 }
2634
2635                 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
2636                 {
2637                         RECT    rect;
2638
2639                         rect = new RECT();
2640                         rect.left = rectangle.Left;
2641                         rect.top = rectangle.Top;
2642                         rect.right = rectangle.Right;
2643                         rect.bottom = rectangle.Bottom;
2644
2645                         IntPtr          hdc;
2646                         IntPtr          brush;
2647                         IntPtr          oldbrush;
2648                         COLORREF        clrRef = new COLORREF();
2649
2650                         clrRef.R = backColor.R;
2651                         clrRef.G = backColor.G;
2652                         clrRef.B = backColor.B;
2653
2654                         // Grab a brush
2655                         brush = Win32CreateSolidBrush (clrRef);
2656
2657                         hdc = Win32GetDC(IntPtr.Zero);
2658                         oldbrush = Win32SelectObject(hdc, brush);
2659
2660                         Win32PatBlt (hdc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, PatBltRop.DSTINVERT);
2661
2662                         Win32SelectObject(hdc, oldbrush);
2663                         Win32DeleteObject(brush);
2664
2665                         Win32ReleaseDC(IntPtr.Zero, hdc);
2666                 }
2667
2668                 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
2669                         IntPtr          hdc;
2670                         IntPtr          pen;
2671                         IntPtr          oldpen;
2672                         POINT           pt;
2673
2674                         pt = new POINT();
2675                         pt.x = 0;
2676                         pt.y = 0;
2677                         Win32ClientToScreen(handle, ref pt);
2678
2679                         // If we want the standard hatch pattern we would
2680                         // need to create a brush
2681
2682                         // Grab a pen
2683                         pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
2684
2685                         hdc = Win32GetDC(IntPtr.Zero);
2686                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2687                         oldpen = Win32SelectObject(hdc, pen);
2688
2689                         Control c = Control.FromHandle (handle);
2690                         if (c != null) {
2691                                 RECT window_rect;
2692                                 Win32GetWindowRect (c.Handle, out window_rect);
2693                                 Region r = new Region (new Rectangle(window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top));
2694                                 Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
2695                         }
2696
2697                         Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
2698                         if ((rect.Width > 0) && (rect.Height > 0)) {
2699                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2700                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
2701                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2702                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
2703                         } else {
2704                                 if (rect.Width > 0) {
2705                                         Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2706                                 } else {
2707                                         Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2708                                 }
2709                         }
2710
2711                         Win32SelectObject(hdc, oldpen);
2712                         Win32DeleteObject(pen);
2713                         if (c != null)
2714                                 Win32ExtSelectClipRgn(hdc, IntPtr.Zero, (int) ClipCombineMode.RGN_COPY);
2715
2716                         Win32ReleaseDC(IntPtr.Zero, hdc);
2717                 }
2718
2719                 internal override SizeF GetAutoScaleSize(Font font) {
2720                         Graphics        g;
2721                         float           width;
2722                         string          magic_string = "The quick brown fox jumped over the lazy dog.";
2723                         double          magic_number = 44.549996948242189;
2724
2725                         g = Graphics.FromHwnd(FosterParent);
2726
2727                         width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
2728                         return new SizeF(width, font.Height);
2729                 }
2730
2731                 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2732                         return Win32SendMessage(hwnd, message, wParam, lParam);
2733                 }
2734
2735                 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2736                         return Win32PostMessage(hwnd, message, wParam, lParam);
2737                 }
2738
2739                 internal override int SendInput (IntPtr hwnd, Queue keys) {
2740                         INPUT[] inputs = new INPUT[keys.Count];
2741                         const Int32 INPUT_KEYBOARD = 1;
2742                         uint returns = 0;
2743                         int i = 0;
2744                         while (keys.Count > 0) {
2745                                 MSG msg = (MSG)keys.Dequeue();
2746
2747                                 
2748                                 inputs[i].ki.wScan = 0;
2749                                 inputs[i].ki.time = 0;
2750                                 inputs[i].ki.dwFlags = (Int32)(msg.message == Msg.WM_KEYUP ? InputFlags.KEYEVENTF_KEYUP : 0);
2751                                 inputs[i].ki.wVk = (short)msg.wParam.ToInt32();
2752                                 inputs[i].type = INPUT_KEYBOARD;
2753                                 i++;
2754                         }
2755                         returns = Win32SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
2756
2757                         return (int) returns;
2758                 }
2759
2760                 internal override int KeyboardSpeed {
2761                         get {
2762                                 int speed = 0;
2763                                 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDSPEED, 0, ref speed, 0);
2764                                 //
2765                                 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
2766                                 //
2767                                 return speed;
2768                         }
2769                 }
2770
2771                 internal override int KeyboardDelay {
2772                         get {
2773                                 int delay = 1;
2774                                 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDDELAY, 0, ref delay, 0);
2775                                 //
2776                                 // Return values must range from 0 to 4, 0 meaning 250ms,
2777                                 // and 4 meaning 1000 ms.
2778                                 //
2779                                 return delay;
2780                         }
2781                 }
2782
2783                 private class WinBuffer
2784                 {
2785                         public IntPtr hdc;
2786                         public IntPtr bitmap;
2787
2788                         public WinBuffer (IntPtr hdc, IntPtr bitmap)
2789                         {
2790                                 this.hdc = hdc;
2791                                 this.bitmap = bitmap;
2792                         }
2793                 }
2794
2795                 internal override void CreateOffscreenDrawable (IntPtr handle, int width, int height, out object offscreen_drawable)
2796                 {
2797                         Graphics destG = Graphics.FromHwnd (handle);
2798                         IntPtr destHdc = destG.GetHdc ();
2799
2800                         IntPtr srcHdc = Win32CreateCompatibleDC (destHdc);
2801                         IntPtr srcBmp = Win32CreateCompatibleBitmap (destHdc, width, height);
2802                         Win32SelectObject (srcHdc, srcBmp);
2803
2804                         offscreen_drawable = new WinBuffer (srcHdc, srcBmp);
2805
2806                         destG.ReleaseHdc (destHdc);
2807                 }
2808
2809                 internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
2810                 {
2811                         return Graphics.FromHdc (((WinBuffer)offscreen_drawable).hdc);
2812                 }
2813
2814                 internal override void BlitFromOffscreen (IntPtr dest_handle, Graphics dest_dc, object offscreen_drawable, Graphics offscreen_dc, Rectangle r)
2815                 {
2816                         WinBuffer wb = (WinBuffer)offscreen_drawable;
2817
2818                         IntPtr destHdc = dest_dc.GetHdc ();
2819                         Win32BitBlt (destHdc, r.Left, r.Top, r.Width, r.Height, wb.hdc, r.Left, r.Top, TernaryRasterOperations.SRCCOPY);
2820                         dest_dc.ReleaseHdc (destHdc);
2821                 }
2822
2823                 internal override void DestroyOffscreenDrawable (object offscreen_drawable)
2824                 {
2825                         WinBuffer wb = (WinBuffer)offscreen_drawable;
2826
2827                         Win32DeleteObject (wb.bitmap);
2828                         Win32DeleteDC (wb.hdc);
2829                 }
2830
2831                 internal override event EventHandler Idle;
2832                 #endregion      // Public Static Methods
2833
2834                 #region Win32 Imports
2835                 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
2836                 private extern static uint Win32GetLastError();
2837
2838                 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2839                 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);
2840
2841                 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
2842                 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
2843
2844                 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2845                 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
2846
2847                 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2848                 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
2849
2850                 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
2851                 internal extern static bool Win32TranslateMessage(ref MSG msg);
2852
2853                 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2854                 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
2855
2856                 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
2857                 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
2858
2859                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2860                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2861
2862                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2863                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2864
2865                 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2866                 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
2867
2868                 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2869                 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
2870
2871                 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
2872                 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
2873
2874                 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2875                 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
2876
2877                 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2878                 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
2879
2880                 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
2881                 private extern static IntPtr Win32ShowCursor(bool bShow);
2882
2883                 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
2884                 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
2885
2886                 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
2887                 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
2888
2889                 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
2890                 private extern static bool Win32DestroyCursor(IntPtr hCursor);
2891
2892                 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2893                 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2894
2895                 //[DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2896                 //private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2897
2898                 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
2899                 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
2900
2901                 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
2902                 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
2903
2904                 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
2905                 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
2906
2907                 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
2908                 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2909
2910                 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
2911                 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2912
2913                 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
2914                 private extern static IntPtr Win32GetDC(IntPtr hWnd);
2915
2916                 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
2917                 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
2918
2919                 //[DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
2920                 //private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
2921
2922                 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
2923                 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
2924
2925                 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2926                 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
2927
2928                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2929                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
2930
2931                 //[DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2932                 //private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
2933
2934                 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
2935                 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
2936
2937                 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
2938                 private extern static IntPtr Win32ReleaseCapture();
2939
2940                 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
2941                 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
2942
2943                 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
2944                 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
2945
2946                 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
2947                 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
2948
2949                 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
2950                 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
2951
2952                 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
2953                 private extern static IntPtr Win32GetParent(IntPtr hWnd);
2954
2955                 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
2956                 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
2957
2958                 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
2959                 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
2960
2961                 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
2962                 private extern static bool Win32GetCursorPos(out POINT lpPoint);
2963
2964                 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
2965                 private extern static bool Win32SetCursorPos(int x, int y);
2966
2967                 //[DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
2968                 //private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
2969
2970                 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
2971                 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
2972
2973                 //[DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
2974                 //private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
2975
2976                 [DllImport ("gdi32.dll", EntryPoint="CreateSolidBrush", CallingConvention=CallingConvention.StdCall)]
2977                 private extern static IntPtr Win32CreateSolidBrush(COLORREF clrRef);
2978
2979                 [DllImport ("gdi32.dll", EntryPoint="PatBlt", CallingConvention=CallingConvention.StdCall)]
2980                 private extern static int Win32PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, PatBltRop dwRop);
2981
2982                 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
2983                 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
2984
2985                 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
2986                 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
2987
2988                 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2989                 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
2990
2991                 [DllImport ("user32.dll", EntryPoint="GetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2992                 private extern static uint Win32GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out LayeredWindowAttributes pwdFlags);
2993
2994                 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
2995                 private extern static bool Win32DeleteObject(IntPtr o);
2996
2997                 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
2998                 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
2999
3000                 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
3001                 private extern static IntPtr Win32GetDesktopWindow();
3002
3003                 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
3004                 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
3005
3006                 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
3007                 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
3008
3009                 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
3010                 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
3011
3012                 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
3013                 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
3014
3015                 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
3016                 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
3017
3018                 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
3019                 internal extern static IntPtr Win32GetFocus();
3020
3021                 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
3022                 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
3023
3024                 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
3025                 private  extern static bool Win32DestroyCaret();
3026
3027                 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
3028                 private  extern static bool Win32ShowCaret(IntPtr hwnd);
3029
3030                 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
3031                 private  extern static bool Win32HideCaret(IntPtr hwnd);
3032
3033                 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
3034                 private  extern static bool Win32SetCaretPos(int X, int Y);
3035
3036                 //[DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
3037                 //private  extern static uint Win32GetCaretBlinkTime();
3038
3039                 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3040                 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
3041
3042                 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
3043                 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
3044
3045                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3046                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3047
3048                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3049                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3050
3051                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3052                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3053
3054                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3055                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3056
3057                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3058                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3059
3060                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3061                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3062
3063                 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3064                 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3065
3066                 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3067                 private extern static IntPtr Win32GetActiveWindow();
3068
3069                 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
3070                 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
3071
3072                 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3073                 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
3074
3075                 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
3076                 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
3077
3078                 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
3079                 private extern static bool IsWindowEnabled(IntPtr hwnd);
3080
3081                 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
3082                 private extern static bool IsWindowVisible(IntPtr hwnd);
3083
3084                 //[DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
3085                 //private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
3086
3087                 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3088                 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3089
3090                 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3091                 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3092
3093                 [DllImport ("user32.dll", EntryPoint="SendInput", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3094                 private extern static UInt32 Win32SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray)] INPUT[] inputs, Int32 cbSize);
3095
3096                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3097                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
3098                 
3099                 //[DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3100                 //private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
3101
3102                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3103                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
3104
3105                 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
3106                 private extern static bool Win32OpenClipboard(IntPtr hwnd);
3107
3108                 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
3109                 private extern static bool Win32EmptyClipboard();
3110
3111                 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3112                 private extern static uint Win32RegisterClipboardFormat(string format);
3113
3114                 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
3115                 private extern static bool Win32CloseClipboard();
3116
3117                 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
3118                 private extern static uint Win32EnumClipboardFormats(uint format);
3119
3120                 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
3121                 private extern static IntPtr Win32GetClipboardData(uint format);
3122
3123                 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
3124                 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
3125
3126                 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
3127                 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
3128
3129                 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
3130                 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
3131
3132                 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
3133                 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
3134
3135                 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
3136                 internal extern static uint Win32GlobalSize(IntPtr hMem);
3137
3138                 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
3139                 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
3140
3141                 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
3142                 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
3143
3144                 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
3145                 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
3146
3147                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3148                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
3149
3150                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3151                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
3152
3153                 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
3154                 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
3155
3156                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3157                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
3158
3159                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3160                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
3161
3162                 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
3163                 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
3164
3165                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3166                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
3167
3168                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3169                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
3170
3171                 [DllImport("gdi32.dll", EntryPoint = "ExcludeClipRect", CallingConvention = CallingConvention.StdCall)]
3172                 internal extern static int Win32ExcludeClipRect (IntPtr hdc, int left, int top,  int right, int bottom);
3173
3174                 [DllImport ("gdi32.dll", EntryPoint="ExtSelectClipRgn", CallingConvention=CallingConvention.StdCall)]
3175                 internal extern static int Win32ExtSelectClipRgn(IntPtr hdc, IntPtr hrgn, int mode);
3176
3177                 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3178                 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
3179
3180                 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3181                 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
3182
3183                 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3184                 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
3185
3186                 [DllImport ("user32.dll", EntryPoint="ClipCursor", CallingConvention=CallingConvention.StdCall)]
3187                 internal extern static bool Win32ClipCursor (ref RECT lpRect);
3188
3189                 [DllImport ("user32.dll", EntryPoint="GetClipCursor", CallingConvention=CallingConvention.StdCall)]
3190                 internal extern static bool Win32GetClipCursor (out RECT lpRect);
3191
3192                 [DllImport ("gdi32.dll", EntryPoint="BitBlt", CallingConvention=CallingConvention.StdCall)]
3193                 internal static extern bool Win32BitBlt (IntPtr hObject, int nXDest, int nYDest, int nWidth,
3194                    int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
3195
3196                 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3197                 internal static extern IntPtr Win32CreateCompatibleDC (IntPtr hdc);
3198
3199                 [DllImport ("gdi32.dll", EntryPoint="DeleteDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3200                 internal static extern bool Win32DeleteDC (IntPtr hdc);
3201
3202                 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleBitmap", CallingConvention=CallingConvention.StdCall)]
3203                 internal static extern IntPtr Win32CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight);
3204                 #endregion
3205         }
3206 }