* CreateParams.cs: Add a couple of helper methods and do a less string
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / XplatUIWin32.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25 //
26
27 // NOT COMPLETE
28
29 using System;
30 using System.Drawing;
31 using System.Drawing.Imaging;
32 using System.ComponentModel;
33 using System.Collections;
34 using System.Diagnostics;
35 using System.IO;
36 using System.Runtime.InteropServices;
37 using System.Text;
38
39
40 /// Win32 Version
41 namespace System.Windows.Forms {
42         internal class XplatUIWin32 : XplatUIDriver {
43                 #region Local Variables
44                 private static XplatUIWin32     instance;
45                 private static int              ref_count;
46                 private static IntPtr           FosterParent;
47
48                 internal static MouseButtons    mouse_state;
49                 internal static Point           mouse_position;
50                 internal static bool            grab_confined;
51                 internal static IntPtr          grab_hwnd;
52                 internal static Rectangle       grab_area;
53                 internal static WndProc         wnd_proc;
54                 internal static IntPtr          prev_mouse_hwnd;
55                 internal static IntPtr          override_cursor;
56                 internal static bool            caret_visible;
57
58                 internal static bool            themes_enabled;
59                 private Hashtable               timer_list;
60                 private static Queue            message_queue;
61                 private static IntPtr           clip_magic = new IntPtr(27051977);
62                 private static int              scroll_width;
63                 private static int              scroll_height;
64                 private static Hashtable        wm_nc_registered;
65                 private static RECT             clipped_cursor_rect;
66                 private Hashtable               registered_classes;
67                 private Hwnd HwndCreating; // the Hwnd we are currently creating (see CreateWindow)
68
69                 #endregion      // Local Variables
70
71                 #region Private Structs
72                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
73                 private struct WNDCLASS {
74                         internal int            style;
75                         internal WndProc        lpfnWndProc;
76                         internal int            cbClsExtra;
77                         internal int            cbWndExtra;
78                         internal IntPtr         hInstance;
79                         internal IntPtr         hIcon;
80                         internal IntPtr         hCursor;
81                         internal IntPtr         hbrBackground;
82                         [MarshalAs(UnmanagedType.LPWStr)]
83                         internal string         lpszMenuName;
84                         [MarshalAs(UnmanagedType.LPWStr)]
85                         internal string         lpszClassName;
86                 }
87
88                 [StructLayout(LayoutKind.Sequential)]
89                 internal struct RECT {
90                         internal int            left;
91                         internal int            top;
92                         internal int            right;
93                         internal int            bottom;
94
95                         public RECT (int left, int top, int right, int bottom)
96                         {
97                                 this.left = left;
98                                 this.top = top;
99                                 this.right = right;
100                                 this.bottom = bottom;
101                         }
102
103                         #region Instance Properties
104                         public int Height { get { return bottom - top; } }
105                         public int Width { get { return right - left; } }
106                         public Size Size { get { return new Size (Width, Height); } }
107                         public Point Location { get { return new Point (left, top); } }
108                         #endregion
109
110                         #region Instance Methods
111                         public Rectangle ToRectangle ()
112                         {
113                                 return Rectangle.FromLTRB (left, top, right, bottom);
114                         }
115
116                         public static RECT FromRectangle (Rectangle rectangle)
117                         {
118                                 return new RECT (rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
119                         }
120
121                         public override int GetHashCode ()
122                         {
123                                 return left ^ ((top << 13) | (top >> 0x13))
124                                   ^ ((Width << 0x1a) | (Width >> 6))
125                                   ^ ((Height << 7) | (Height >> 0x19));
126                         }
127                         
128                         public override string ToString ()
129                         {
130                                 return String.Format("RECT left={0}, top={1}, right={2}, bottom={3}, width={4}, height={5}", left, top, right, bottom, right-left, bottom-top);
131                         }
132                         #endregion
133
134                         #region Operator overloads
135                         public static implicit operator Rectangle (RECT rect)
136                         {
137                                 return Rectangle.FromLTRB (rect.left, rect.top, rect.right, rect.bottom);
138                         }
139
140                         public static implicit operator RECT (Rectangle rect)
141                         {
142                                 return new RECT (rect.Left, rect.Top, rect.Right, rect.Bottom);
143                         }
144                         #endregion
145                 }
146
147                 internal enum SPIAction {
148                         SPI_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                         string class_name = RegisterWindowClass (cp.ClassStyle);
1274                         HwndCreating = hwnd;
1275
1276                         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);
1277
1278                         HwndCreating = null;
1279
1280                         if (WindowHandle==IntPtr.Zero) {
1281                                 uint error = Win32GetLastError();
1282
1283                                 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1284                         }
1285
1286                         hwnd.ClientWindow = WindowHandle;
1287
1288                         Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1289
1290                         return WindowHandle;
1291                 }
1292
1293                 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1294                         CreateParams create_params = new CreateParams();
1295
1296                         create_params.Caption = "";
1297                         create_params.X = X;
1298                         create_params.Y = Y;
1299                         create_params.Width = Width;
1300                         create_params.Height = Height;
1301
1302                         create_params.ClassName=XplatUI.DefaultClassName;
1303                         create_params.ClassStyle = 0;
1304                         create_params.ExStyle=0;
1305                         create_params.Parent=IntPtr.Zero;
1306                         create_params.Param=0;
1307
1308                         return CreateWindow(create_params);
1309                 }
1310
1311                 internal override void DestroyWindow(IntPtr handle) {
1312                         Hwnd    hwnd;
1313
1314                         hwnd = Hwnd.ObjectFromHandle(handle);
1315                         Win32DestroyWindow(handle);
1316                         hwnd.Dispose();
1317                         return;
1318                 }
1319
1320                 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1321                         // We do nothing, Form has to handle WM_GETMINMAXINFO
1322                 }
1323
1324
1325                 internal override FormWindowState GetWindowState(IntPtr handle) {
1326                         uint style;
1327
1328                         style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1329                         if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1330                                 return FormWindowState.Maximized;
1331                         } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1332                                 return FormWindowState.Minimized;
1333                         }
1334                         return FormWindowState.Normal;
1335                 }
1336
1337                 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1338                         switch(state) {
1339                                 case FormWindowState.Normal: {
1340                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1341                                         return;
1342                                 }
1343
1344                                 case FormWindowState.Minimized: {
1345                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1346                                         return;
1347                                 }
1348
1349                                 case FormWindowState.Maximized: {
1350                                         Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1351                                         return;
1352                                 }
1353                         }
1354                 }
1355
1356                 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1357
1358                         Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1359                         Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1360
1361                         if ((cp.ExStyle & (int) WindowExStyles.WS_EX_TOOLWINDOW) > 0 || 
1362                                 ((cp.WindowStyle & (WindowStyles.WS_CAPTION)) != WindowStyles.WS_CAPTION && cp.control is Form))
1363                                 XplatUI.RequestNCRecalc (handle);
1364                 }
1365
1366                 internal override double GetWindowTransparency(IntPtr handle)
1367                 {
1368                         LayeredWindowAttributes lwa;
1369                         COLORREF clrRef;
1370                         byte alpha;
1371
1372                         if (0 == Win32GetLayeredWindowAttributes (handle, out clrRef, out alpha, out lwa))
1373                                 return 1.0;
1374
1375                         return ((double)alpha) / 255.0;
1376                 }
1377
1378                 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1379                         LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1380                         byte opacity = (byte)(transparency*255);
1381                         COLORREF clrRef = new COLORREF();
1382                         if (key != Color.Empty) {
1383                                 clrRef.R = key.R;
1384                                 clrRef.G = key.G;
1385                                 clrRef.B = key.B;
1386                                 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1387                         }
1388                         RECT rc;
1389                         rc.right = 1000;
1390                         rc.bottom = 1000;
1391                         Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1392                 }
1393
1394                 TransparencySupport support;
1395                 bool queried_transparency_support;
1396                 internal override TransparencySupport SupportsTransparency() {
1397                         if (queried_transparency_support)
1398                                 return support;
1399
1400                         bool flag;
1401                         support = TransparencySupport.None;
1402
1403                         flag = true;
1404                         try {
1405                                 Win32SetLayeredWindowAttributes (IntPtr.Zero, new COLORREF (), 255, LayeredWindowAttributes.LWA_ALPHA);
1406                         }
1407                         catch (EntryPointNotFoundException) { flag = false; }
1408                         catch { /* swallow everything else */ }
1409
1410                         if (flag) support |= TransparencySupport.Set;
1411
1412                         flag = true;
1413                         try {
1414                                 LayeredWindowAttributes lwa;
1415                                 COLORREF clrRef;
1416                                 byte alpha;
1417
1418                                 Win32GetLayeredWindowAttributes (IntPtr.Zero, out clrRef, out alpha, out lwa);
1419                         }
1420                         catch (EntryPointNotFoundException) { flag = false; }
1421                         catch { /* swallow everything else */ }
1422
1423                         if (flag) support |= TransparencySupport.Get;
1424
1425                         queried_transparency_support = true;
1426                         return support;
1427                 }
1428
1429                 internal override void UpdateWindow(IntPtr handle) {
1430                         Win32UpdateWindow(handle);
1431                 }
1432
1433                 internal override PaintEventArgs PaintEventStart(IntPtr handle, bool client) {
1434                         IntPtr          hdc;
1435                         PAINTSTRUCT     ps;
1436                         PaintEventArgs  paint_event;
1437                         RECT            rect;
1438                         Rectangle       clip_rect;
1439                         Hwnd            hwnd;
1440
1441                         clip_rect = new Rectangle();
1442                         rect = new RECT();
1443                         ps = new PAINTSTRUCT();
1444
1445                         hwnd = Hwnd.ObjectFromHandle(handle);
1446
1447                         if (client) {
1448                                 if (Win32GetUpdateRect(handle, ref rect, false)) {
1449                                         hdc = Win32BeginPaint(handle, ref ps);
1450
1451                                         hwnd.drawing_stack.Push (ps);
1452
1453                                         clip_rect = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top);
1454                                 } else {
1455                                         hdc = Win32GetDC(handle);
1456                                         // FIXME: Add the DC to internal list
1457
1458                                         hwnd.drawing_stack.Push (null);
1459
1460                                         clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
1461                                 }
1462                         } else {
1463                                 hdc = Win32GetWindowDC (handle);
1464
1465                                 hwnd.drawing_stack.Push (hdc);
1466
1467                                 // HACK this in for now
1468                                 Win32GetWindowRect (handle, out rect);
1469                                 clip_rect = new Rectangle(0, 0, rect.right-rect.left, rect.bottom-rect.top);
1470                         }
1471
1472                         Graphics dc = Graphics.FromHdc(hdc);
1473                         hwnd.drawing_stack.Push (dc);
1474
1475                         paint_event = new PaintEventArgs(dc, clip_rect);
1476
1477                         return paint_event;
1478                 }
1479
1480                 internal override void PaintEventEnd(IntPtr handle, bool client) {
1481                         Hwnd            hwnd;
1482
1483                         hwnd = Hwnd.ObjectFromHandle(handle);
1484
1485                         Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1486                         dc.Dispose ();
1487
1488                         if (client) {
1489                                 object o = hwnd.drawing_stack.Pop();
1490                                 if (o != null) {
1491                                         PAINTSTRUCT ps = (PAINTSTRUCT)o;
1492                                         Win32EndPaint(handle, ref ps);
1493                                 }
1494                         } else {
1495                                 IntPtr hdc = (IntPtr)hwnd.drawing_stack.Pop();
1496                                 Win32ReleaseDC(handle, hdc);
1497                         }
1498                 }
1499
1500
1501                 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1502                         Win32MoveWindow(handle, x, y, width, height, true);
1503                         return;
1504                 }
1505
1506                 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) {
1507                         IntPtr  parent;
1508                         RECT    rect;
1509                         POINT   pt;
1510
1511                         Win32GetWindowRect(handle, out rect);
1512                         width = rect.right - rect.left;
1513                         height = rect.bottom - rect.top;
1514
1515                         pt.x=rect.left;
1516                         pt.y=rect.top;
1517
1518                         parent = Win32GetParent(handle);
1519                         Win32ScreenToClient(parent, ref pt);
1520
1521                         x = pt.x;
1522                         y = pt.y;
1523
1524                         Win32GetClientRect(handle, out rect);
1525                         client_width = rect.right - rect.left;
1526                         client_height = rect.bottom - rect.top;
1527                         return;
1528                 }
1529
1530                 internal override void Activate(IntPtr handle) {
1531                         Win32SetActiveWindow(handle);
1532                 }
1533
1534                 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1535                         RECT rect;
1536
1537                         rect.left=rc.Left;
1538                         rect.top=rc.Top;
1539                         rect.right=rc.Right;
1540                         rect.bottom=rc.Bottom;
1541                         Win32InvalidateRect(handle, ref rect, clear);
1542                 }
1543
1544
1545                 internal override void InvalidateNC (IntPtr handle)
1546                 {
1547                         // found this gem at
1548                         // http://www.dotnet247.com/247reference/msgs/58/292037.aspx
1549                         Win32SetWindowPos(handle, IntPtr.Zero,
1550                                           0, 0, 0, 0,
1551                                           SetWindowPosFlags.SWP_NOMOVE |
1552                                           SetWindowPosFlags.SWP_NOSIZE |
1553                                           SetWindowPosFlags.SWP_NOZORDER |
1554                                           SetWindowPosFlags.SWP_NOACTIVATE |
1555                                           SetWindowPosFlags.SWP_DRAWFRAME);
1556                 }
1557
1558                 private IntPtr InternalWndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
1559                 {
1560                         if (HwndCreating != null && HwndCreating.ClientWindow == IntPtr.Zero)
1561                                 HwndCreating.ClientWindow = hWnd;
1562                         return NativeWindow.WndProc (hWnd, msg, wParam, lParam);
1563                 }
1564
1565                 internal override IntPtr DefWndProc(ref Message msg) {
1566                         msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1567                         return msg.Result;
1568                 }
1569
1570                 internal override void HandleException(Exception e) {
1571                         StackTrace st = new StackTrace(e);
1572                         Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1573                         Console.WriteLine("{0}{1}", e.Message, st.ToString());
1574                 }
1575
1576                 internal override void DoEvents() {
1577                         MSG msg = new MSG();
1578
1579                         if (override_cursor != IntPtr.Zero) {
1580                                 Cursor.Current = null;
1581                         }
1582
1583                         while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1584                                 XplatUI.TranslateMessage(ref msg);
1585                                 XplatUI.DispatchMessage(ref msg);
1586                         }
1587                 }
1588
1589                 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1590                         return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1591                 }
1592
1593                 internal override void PostQuitMessage(int exitCode) {
1594                         Win32PostQuitMessage(exitCode);
1595                 }
1596
1597                 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
1598                 {
1599                         if (wm_nc_registered == null)
1600                                 wm_nc_registered = new Hashtable ();
1601                                 
1602                         TMEFlags flags = TMEFlags.TME_NONCLIENT;
1603                         if (hover)
1604                                 flags |= TMEFlags.TME_HOVER;
1605                         if (leave)
1606                                 flags |= TMEFlags.TME_LEAVE;
1607
1608                         if (flags == TMEFlags.TME_NONCLIENT) {
1609                                 if (wm_nc_registered.Contains (hwnd)) {
1610                                         wm_nc_registered.Remove (hwnd);
1611                                 }
1612                         } else {
1613                                 if (!wm_nc_registered.Contains (hwnd)) {
1614                                         wm_nc_registered.Add (hwnd, flags);
1615                                 } else {
1616                                         wm_nc_registered [hwnd] = flags;
1617                                 }
1618                         }
1619                 }
1620
1621                 internal override void RequestNCRecalc(IntPtr handle) {
1622                         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);
1623                 }
1624
1625                 internal override void ResetMouseHover(IntPtr handle) {
1626                         TRACKMOUSEEVENT tme;
1627
1628                         tme = new TRACKMOUSEEVENT();
1629                         tme.size = Marshal.SizeOf(tme);
1630                         tme.hWnd = handle;
1631                         tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1632                         Win32TrackMouseEvent(ref tme);
1633                 }
1634
1635
1636                 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1637                         return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
1638                 }
1639
1640                 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
1641                         bool            result;
1642
1643                         msg.refobject = 0;
1644                         if (RetrieveMessage(ref msg)) {
1645                                 return true;
1646                         }
1647
1648                         if (blocking) {
1649                                 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
1650                         } else {
1651                                 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
1652                                 if (!result) {
1653                                         return false;
1654                                 }
1655                         }
1656
1657                         // We need to fake WM_MOUSE_ENTER
1658                         switch (msg.message) {
1659                                 case Msg.WM_LBUTTONDOWN: {
1660                                         mouse_state |= MouseButtons.Left;
1661                                         break;
1662                                 }
1663
1664                                 case Msg.WM_MBUTTONDOWN: {
1665                                         mouse_state |= MouseButtons.Middle;
1666                                         break;
1667                                 }
1668
1669                                 case Msg.WM_RBUTTONDOWN: {
1670                                         mouse_state |= MouseButtons.Right;
1671                                         break;
1672                                 }
1673
1674                                 case Msg.WM_LBUTTONUP: {
1675                                         mouse_state &= ~MouseButtons.Left;
1676                                         break;
1677                                 }
1678
1679                                 case Msg.WM_MBUTTONUP: {
1680                                         mouse_state &= ~MouseButtons.Middle;
1681                                         break;
1682                                 }
1683
1684                                 case Msg.WM_RBUTTONUP: {
1685                                         mouse_state &= ~MouseButtons.Right;
1686                                         break;
1687                                 }
1688
1689                                 case Msg.WM_ASYNC_MESSAGE: {
1690                                         XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
1691                                         break;
1692                                 }
1693
1694                                 case Msg.WM_MOUSEMOVE: {
1695                                         if (msg.hwnd != prev_mouse_hwnd) {
1696                                                 TRACKMOUSEEVENT tme;
1697
1698                                                 // The current message will be sent out next time around
1699                                                 StoreMessage(ref msg);
1700
1701                                                 // This is the message we want to send at this point
1702                                                 msg.message = Msg.WM_MOUSE_ENTER;
1703
1704                                                 prev_mouse_hwnd = msg.hwnd;
1705
1706                                                 tme = new TRACKMOUSEEVENT();
1707                                                 tme.size = Marshal.SizeOf(tme);
1708                                                 tme.hWnd = msg.hwnd;
1709                                                 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1710                                                 Win32TrackMouseEvent(ref tme);
1711                                                 return result;
1712                                         }
1713                                         break;
1714                                 }
1715
1716                                 case Msg.WM_NCMOUSEMOVE: {
1717                                         if (wm_nc_registered == null || !wm_nc_registered.Contains (msg.hwnd))
1718                                                 break;
1719                                                 
1720                                         TRACKMOUSEEVENT tme;
1721
1722                                         tme = new TRACKMOUSEEVENT ();
1723                                         tme.size = Marshal.SizeOf(tme);
1724                                         tme.hWnd = msg.hwnd;
1725                                         tme.dwFlags = (TMEFlags)wm_nc_registered[msg.hwnd];
1726                                         Win32TrackMouseEvent (ref tme);
1727                                         return result;
1728                                 }
1729
1730                                 case Msg.WM_DROPFILES: {
1731                                         return Win32DnD.HandleWMDropFiles(ref msg);
1732                                 }
1733
1734                                 case Msg.WM_MOUSELEAVE: {
1735                                         prev_mouse_hwnd = IntPtr.Zero;
1736                                         break;
1737                                 }
1738
1739                                 case Msg.WM_TIMER: {
1740                                         Timer timer=(Timer)timer_list[(int)msg.wParam];
1741
1742                                         if (timer != null) {
1743                                                 timer.FireTick();
1744                                         }
1745                                         break;
1746                                 }
1747                         }
1748
1749                         return result;
1750                 }
1751
1752                 internal override bool TranslateMessage(ref MSG msg) {
1753                         return Win32TranslateMessage(ref msg);
1754                 }
1755
1756                 internal override IntPtr DispatchMessage(ref MSG msg) {
1757                         return Win32DispatchMessage(ref msg);
1758                 }
1759
1760                 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
1761                         if (Top) {
1762                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1763                                 return true;
1764                         } else if (!Bottom) {
1765                                 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1766                         } else {
1767                                 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1768                                 return true;
1769                         }
1770                         return false;
1771                 }
1772
1773                 internal override bool SetTopmost(IntPtr hWnd, bool Enabled) {
1774                         if (Enabled) {
1775                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1776                                 return true;
1777                         } else {
1778                                 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1779                                 return true;
1780                         }
1781                 }
1782                 
1783                 internal override bool SetOwner(IntPtr hWnd, IntPtr hWndOwner) {
1784                         Win32SetWindowLong(hWnd, WindowLong.GWL_HWNDPARENT, (uint) hWndOwner);
1785                         return true;
1786                 }
1787
1788                 internal override bool Text(IntPtr handle, string text) {
1789                         Win32SetWindowText(handle, text);
1790                         return true;
1791                 }
1792
1793                 internal override bool GetText(IntPtr handle, out string text) {
1794                         StringBuilder sb;
1795
1796                         sb = new StringBuilder(256);
1797                         Win32GetWindowText(handle, sb, sb.Capacity);
1798                         text = sb.ToString();
1799                         return true;
1800                 }
1801
1802                 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
1803                 {
1804                         if (visible) {
1805                                 Control c = Control.FromHandle (handle);
1806                                 if (c is Form) {
1807                                         Form f;
1808
1809                                         f = (Form)Control.FromHandle (handle);
1810                                         WindowPlacementFlags flags = WindowPlacementFlags.SW_SHOWNORMAL;
1811                                         switch (f.WindowState) {
1812                                                 case FormWindowState.Normal: flags = WindowPlacementFlags.SW_SHOWNORMAL; break;
1813                                                 case FormWindowState.Minimized: flags = WindowPlacementFlags.SW_MINIMIZE; break;
1814                                                 case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
1815                                         }
1816                                         
1817                                         if (!f.ActivateOnShow)
1818                                                 flags = WindowPlacementFlags.SW_SHOWNOACTIVATE;
1819                                                 
1820                                         Win32ShowWindow (handle, flags);
1821                                 }
1822                                 else {
1823                                         if (c.ActivateOnShow)
1824                                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
1825                                         else
1826                                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
1827                                 }
1828                         }
1829                         else {
1830                                 Win32ShowWindow (handle, WindowPlacementFlags.SW_HIDE);
1831                         }
1832                         return true;
1833                 }
1834
1835                 internal override bool IsEnabled(IntPtr handle) {
1836                         return IsWindowEnabled (handle);
1837                 }
1838                 
1839                 internal override bool IsVisible(IntPtr handle) {
1840                         return IsWindowVisible (handle);
1841                 }
1842
1843                 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
1844                         Control c = Control.FromHandle (handle);
1845                         if (parent == IntPtr.Zero) {
1846                                 if (!(c is Form)) {
1847                                         Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
1848                                 }
1849                         }
1850                         else
1851                                 SetVisible (handle, c.is_visible, true);
1852                                 
1853                         // The Win32SetParent is lame, it can very well move the window
1854                         // ref: http://groups.google.com/group/microsoft.public.vb.winapi/browse_thread/thread/1b82ccc54231ecee/afa82835bfc0422a%23afa82835bfc0422a
1855                         // Here we save the position before changing the parent, and if it has changed afterwards restore it.
1856                         // Another possibility would be to intercept WM_WINDOWPOSCHANGING and restore the coords there, but this would require plumbing in weird places
1857                         // (either inside Control or add handling to InternalWndProc)
1858                         // We also need to remove WS_CHILD if making the window parent-less, and add it if we're parenting it.
1859                         RECT rect, rect2;
1860                         IntPtr result;
1861                         WindowStyles style, new_style;
1862                         
1863                         Win32GetWindowRect (handle, out rect);
1864                         style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
1865                         
1866                         if (parent == IntPtr.Zero) {
1867                                 new_style = style & ~WindowStyles.WS_CHILD;
1868                                 result = Win32SetParent (handle, FosterParent);
1869                         } else {
1870                                 new_style = style | WindowStyles.WS_CHILD;
1871                                 result = Win32SetParent (handle, parent);
1872                         }
1873                         if (style != new_style && c is Form) {
1874                                 Win32SetWindowLong (handle, WindowLong.GWL_STYLE, (uint) new_style);
1875                         }
1876                         Win32GetWindowRect (handle, out rect2);
1877                         if (rect.top != rect2.top && rect.left != rect2.left && c is Form) {
1878                                 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);
1879                         }
1880                         return result;
1881                 }
1882
1883                 // If we ever start using this, we should probably replace FosterParent with IntPtr.Zero
1884                 internal override IntPtr GetParent(IntPtr handle) {
1885                         return Win32GetParent(handle);
1886                 }
1887
1888                 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
1889                         grab_hwnd = hWnd;
1890                         Win32SetCapture(hWnd);
1891                         
1892                         if (ConfineToHwnd != IntPtr.Zero) {
1893                                 RECT window_rect;
1894                                 Win32GetWindowRect (ConfineToHwnd, out window_rect);
1895                                 Win32GetClipCursor (out clipped_cursor_rect);
1896                                 Win32ClipCursor (ref window_rect);
1897                         }
1898                 }
1899
1900                 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
1901                         hWnd = grab_hwnd;
1902                         GrabConfined = grab_confined;
1903                         GrabArea = grab_area;
1904                 }
1905
1906                 internal override void UngrabWindow(IntPtr hWnd) {
1907                         if (!(clipped_cursor_rect.top == 0 && clipped_cursor_rect.bottom == 0 && clipped_cursor_rect.left == 0 && clipped_cursor_rect.right == 0)) {
1908                                 Win32ClipCursor (ref clipped_cursor_rect);
1909                                 clipped_cursor_rect = new RECT ();
1910                         }
1911                         
1912                         Win32ReleaseCapture();
1913                         grab_hwnd = IntPtr.Zero;
1914                 }
1915
1916                 internal override bool CalculateWindowRect(ref Rectangle ClientRect, CreateParams cp, Menu menu, out Rectangle WindowRect) {
1917                         RECT    rect;
1918
1919                         rect.left=ClientRect.Left;
1920                         rect.top=ClientRect.Top;
1921                         rect.right=ClientRect.Right;
1922                         rect.bottom=ClientRect.Bottom;
1923
1924                         if (!Win32AdjustWindowRectEx(ref rect, cp.Style, menu != null, cp.ExStyle)) {
1925                                 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
1926                                 return false;
1927                         }
1928
1929                         WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
1930                         return true;
1931                 }
1932
1933                 internal override void SetCursor(IntPtr window, IntPtr cursor) {
1934                         Win32SetCursor(cursor);
1935                         return;
1936                 }
1937
1938                 internal override void ShowCursor(bool show) {
1939                         Win32ShowCursor(show);
1940                 }
1941
1942                 internal override void OverrideCursor(IntPtr cursor) {
1943                         Win32SetCursor(cursor);
1944                 }
1945
1946                 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
1947                         IntPtr  cursor;
1948                         Bitmap  cursor_bitmap;
1949                         Bitmap  cursor_mask;
1950                         Byte[]  cursor_bits;
1951                         Byte[]  mask_bits;
1952                         Color   pixel;
1953                         int     width;
1954                         int     height;
1955
1956                         // Win32 only allows creation cursors of a certain size
1957                         if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
1958                                 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1959                                 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1960                         } else {
1961                                 cursor_bitmap = bitmap;
1962                                 cursor_mask = mask;
1963                         }
1964
1965                         width = cursor_bitmap.Width;
1966                         height = cursor_bitmap.Height;
1967
1968                         cursor_bits = new Byte[(width / 8) * height];
1969                         mask_bits = new Byte[(width / 8) * height];
1970
1971                         for (int y = 0; y < height; y++) {
1972                                 for (int x = 0; x < width; x++) {
1973                                         pixel = cursor_bitmap.GetPixel(x, y);
1974
1975                                         if (pixel == cursor_pixel) {
1976                                                 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1977                                         }
1978
1979                                         pixel = cursor_mask.GetPixel(x, y);
1980
1981                                         if (pixel == mask_pixel) {
1982                                                 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1983                                         }
1984                                 }
1985                         }
1986
1987                         cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
1988
1989                         return cursor;
1990                 }
1991
1992                 [MonoTODO("Define the missing cursors")]
1993                 internal override IntPtr DefineStdCursor(StdCursor id) {
1994                         switch(id) {
1995                                 case StdCursor.AppStarting:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
1996                                 case StdCursor.Arrow:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1997                                 case StdCursor.Cross:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
1998                                 case StdCursor.Default:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1999                                 case StdCursor.Hand:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
2000                                 case StdCursor.Help:            return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
2001                                 case StdCursor.HSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2002                                 case StdCursor.IBeam:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
2003                                 case StdCursor.No:              return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
2004                                 case StdCursor.NoMove2D:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2005                                 case StdCursor.NoMoveHoriz:     return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2006                                 case StdCursor.NoMoveVert:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2007                                 case StdCursor.PanEast:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2008                                 case StdCursor.PanNE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2009                                 case StdCursor.PanNorth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2010                                 case StdCursor.PanNW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2011                                 case StdCursor.PanSE:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2012                                 case StdCursor.PanSouth:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2013                                 case StdCursor.PanSW:           return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2014                                 case StdCursor.PanWest:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2015                                 case StdCursor.SizeAll:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
2016                                 case StdCursor.SizeNESW:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
2017                                 case StdCursor.SizeNS:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
2018                                 case StdCursor.SizeNWSE:        return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
2019                                 case StdCursor.SizeWE:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
2020                                 case StdCursor.UpArrow:         return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
2021                                 case StdCursor.VSplit:          return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);          // FIXME
2022                                 case StdCursor.WaitCursor:      return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
2023                         }
2024                         throw new NotImplementedException ();
2025                 }
2026
2027                 internal override void DestroyCursor(IntPtr cursor) {
2028                         if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
2029                                 Win32DestroyCursor(cursor);
2030                         }
2031                 }
2032
2033                 [MonoTODO]
2034                 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
2035                         width = 20;
2036                         height = 20;
2037                         hotspot_x = 0;
2038                         hotspot_y = 0;
2039                 }
2040
2041                 internal override void SetCursorPos(IntPtr handle, int x, int y) {
2042                         Win32SetCursorPos(x, y);
2043                 }
2044
2045                 internal override Region GetClipRegion(IntPtr hwnd) {
2046                         Region region;
2047
2048                         region = new Region();
2049
2050                         Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
2051
2052                         return region;
2053                 }
2054
2055                 internal override void SetClipRegion(IntPtr hwnd, Region region) {
2056                         Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
2057                 }
2058
2059                 internal override void EnableWindow(IntPtr handle, bool Enable) {
2060                         Win32EnableWindow(handle, Enable);
2061                 }
2062
2063                 internal override void EndLoop(System.Threading.Thread thread) {
2064                         // Nothing to do
2065                 }
2066
2067                 internal override object StartLoop(System.Threading.Thread thread) {
2068                         return null;
2069                 }
2070
2071                 internal override void SetModal(IntPtr handle, bool Modal) {
2072                         // we do nothing on Win32
2073                 }
2074
2075                 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
2076                         POINT   pt;
2077
2078                         Win32GetCursorPos(out pt);
2079
2080                         if (handle!=IntPtr.Zero) {
2081                                 Win32ScreenToClient(handle, ref pt);
2082                         }
2083
2084                         x=pt.x;
2085                         y=pt.y;
2086                 }
2087
2088                 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
2089                 {
2090                         POINT pnt = new POINT();                        
2091
2092                         pnt.x = x;
2093                         pnt.y = y;
2094                         Win32ScreenToClient (handle, ref pnt);
2095
2096                         x = pnt.x;
2097                         y = pnt.y;
2098                 }
2099
2100                 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {                    
2101                         POINT   pnt = new POINT();                      
2102
2103                         pnt.x = x;
2104                         pnt.y = y;
2105
2106                         Win32ClientToScreen(handle, ref pnt);
2107
2108                         x = pnt.x;
2109                         y = pnt.y;
2110                 }
2111
2112                 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
2113                         RECT    rect;
2114  
2115                         Win32GetWindowRect(handle, out rect);
2116                         x -= rect.left + SystemInformation.FrameBorderSize.Width;
2117                         y -= rect.top + SystemInformation.FrameBorderSize.Height;
2118
2119                         WindowStyles style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
2120                         if (CreateParams.IsSet (style, WindowStyles.WS_CAPTION)) {
2121                                 y -= ThemeEngine.Current.CaptionHeight;
2122                         }
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                 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
2166                         Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
2167                         caret_visible = false;
2168                 }
2169
2170                 internal override void DestroyCaret(IntPtr hwnd) {
2171                         Win32DestroyCaret();
2172                 }
2173
2174                 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
2175                         Win32SetCaretPos(x, y);
2176                 }
2177
2178                 internal override void CaretVisible(IntPtr hwnd, bool visible) {
2179                         if (visible) {
2180                                 if (!caret_visible) {
2181                                         Win32ShowCaret(hwnd);
2182                                         caret_visible = true;
2183                                 }
2184                         } else {
2185                                 if (caret_visible) {
2186                                         Win32HideCaret(hwnd);
2187                                         caret_visible = false;
2188                                 }
2189                         }
2190                 }
2191
2192                 internal override IntPtr GetFocus() {
2193                         return Win32GetFocus();
2194                 }
2195
2196                 internal override void SetFocus(IntPtr hwnd) {
2197                         Win32SetFocus(hwnd);
2198                 }
2199
2200                 internal override IntPtr GetActive() {
2201                         return Win32GetActiveWindow();
2202                 }
2203
2204                 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
2205                         IntPtr          dc;
2206                         TEXTMETRIC      tm;
2207
2208                         tm = new TEXTMETRIC();
2209
2210                         dc = Win32GetDC(IntPtr.Zero);
2211                         Win32SelectObject(dc, font.ToHfont());
2212                         if (Win32GetTextMetrics(dc, ref tm) == false) {
2213                                 Win32ReleaseDC(IntPtr.Zero, dc);
2214                                 ascent = 0;
2215                                 descent = 0;
2216                                 return false;
2217                         }
2218                         Win32ReleaseDC(IntPtr.Zero, dc);
2219
2220                         ascent = tm.tmAscent;
2221                         descent = tm.tmDescent;
2222
2223                         return true;
2224                 }
2225
2226                 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
2227                         RECT    rect;
2228
2229                         rect = new RECT();
2230                         rect.left = rectangle.X;
2231                         rect.top = rectangle.Y;
2232                         rect.right = rectangle.Right;
2233                         rect.bottom = rectangle.Bottom;
2234
2235                         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));
2236                         Win32UpdateWindow(hwnd);
2237                 }
2238
2239                 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
2240                         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));
2241                 }
2242
2243                 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
2244                         NOTIFYICONDATA  nid;
2245
2246                         nid = new NOTIFYICONDATA();
2247
2248                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2249                         nid.hWnd = hwnd;
2250                         nid.uID = 1;
2251                         nid.uCallbackMessage = (uint)Msg.WM_USER;
2252                         nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2253
2254                         if (tip != null) {       
2255                                 nid.szTip = tip;
2256                                 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2257                         }
2258
2259                         if (icon != null) {
2260                                 nid.hIcon = icon.Handle;
2261                                 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2262                         }
2263
2264                         tt = null;
2265
2266                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
2267                 }
2268
2269                 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
2270                         NOTIFYICONDATA  nid;
2271
2272                         nid = new NOTIFYICONDATA();
2273
2274                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2275                         nid.hIcon = icon.Handle;
2276                         nid.hWnd = hwnd;
2277                         nid.uID = 1;
2278                         nid.uCallbackMessage = (uint)Msg.WM_USER;
2279                         nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2280
2281                         if (tip != null) {
2282                                 nid.szTip = tip;
2283                                 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2284                         }
2285
2286                         if (icon != null) {
2287                                 nid.hIcon = icon.Handle;
2288                                 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2289                         }
2290
2291                         return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2292                 }
2293
2294                 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
2295                         NOTIFYICONDATA  nid;
2296
2297                         nid = new NOTIFYICONDATA();
2298
2299                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2300                         nid.hWnd = hwnd;
2301                         nid.uID = 1;
2302                         nid.uFlags = 0;
2303
2304                         Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
2305                 }
2306
2307 #if NET_2_0
2308                 internal override void SystrayBalloon(IntPtr hwnd, int timeout, string title, string text, ToolTipIcon icon)
2309                 {
2310                         NOTIFYICONDATA  nid;
2311
2312                         nid = new NOTIFYICONDATA();
2313
2314                         nid.cbSize = (uint)Marshal.SizeOf(nid);
2315                         nid.hWnd = hwnd;
2316                         nid.uID = 1;
2317                         nid.uFlags = NotifyIconFlags.NIF_INFO;
2318                         nid.uTimeoutOrVersion = timeout;
2319                         nid.szInfoTitle = title;
2320                         nid.szInfo = text;
2321                         nid.dwInfoFlags = icon;
2322                         
2323                         Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2324                 }
2325 #endif
2326
2327                 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
2328                         // Nothing to do on Win32
2329                 }
2330
2331                 internal override void SetMenu(IntPtr handle, Menu menu) {
2332                         // Trigger WM_NCCALC
2333                         Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2334                 }
2335
2336                 internal override Point GetMenuOrigin(IntPtr handle) {
2337                         Form form = Control.FromHandle (handle) as Form;
2338                         if (form != null) {
2339                                 Hwnd.Borders borders = Hwnd.GetBorders (form.GetCreateParams (), null);
2340                                 return new Point(borders.left, borders.top);
2341                         }
2342                         return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2343                 }
2344
2345                 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2346                         Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : icon.Handle);    // 1 = large icon (0 would be small)
2347                 }
2348
2349                 internal override void ClipboardClose(IntPtr handle) {
2350                         if (handle != clip_magic) {
2351                                 throw new ArgumentException("handle is not a valid clipboard handle");
2352                         }
2353                         Win32CloseClipboard();
2354                 }
2355
2356                 internal override int ClipboardGetID(IntPtr handle, string format) {
2357                         if (handle != clip_magic) {
2358                                 throw new ArgumentException("handle is not a valid clipboard handle");
2359                         }
2360                         if (format == "Text" ) return 1;
2361                         else if (format == "Bitmap" ) return 2;
2362                         else if (format == "MetaFilePict" ) return 3;
2363                         else if (format == "SymbolicLink" ) return 4;
2364                         else if (format == "DataInterchangeFormat" ) return 5;
2365                         else if (format == "Tiff" ) return 6;
2366                         else if (format == "OEMText" ) return 7;
2367                         else if (format == "DeviceIndependentBitmap" ) return 8;
2368                         else if (format == "Palette" ) return 9;
2369                         else if (format == "PenData" ) return 10;
2370                         else if (format == "RiffAudio" ) return 11;
2371                         else if (format == "WaveAudio" ) return 12;
2372                         else if (format == "UnicodeText" ) return 13;
2373                         else if (format == "EnhancedMetafile" ) return 14;
2374                         else if (format == "FileDrop" ) return 15;
2375                         else if (format == "Locale" ) return 16;
2376
2377                         return (int)Win32RegisterClipboardFormat(format);
2378                 }
2379
2380                 internal override IntPtr ClipboardOpen(bool primary_selection) {
2381                         // Win32 does not have primary selection
2382                         Win32OpenClipboard(FosterParent);
2383                         return clip_magic;
2384                 }
2385
2386                 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2387                         uint    format;
2388                         int[]   result;
2389                         int     count;
2390
2391                         if (handle != clip_magic) {
2392                                 return null;
2393                         }
2394
2395                         // Count first
2396                         count = 0;
2397                         format = 0;
2398                         do {
2399                                 format = Win32EnumClipboardFormats(format);
2400                                 if (format != 0) {
2401                                         count++;
2402                                 }
2403                         } while (format != 0);
2404
2405                         // Now assign
2406                         result = new int[count];
2407                         count = 0;
2408                         format = 0;
2409                         do {
2410                                 format = Win32EnumClipboardFormats(format);
2411                                 if (format != 0) {
2412                                         result[count++] = (int)format;
2413                                 }
2414                         } while (format != 0);
2415
2416                         return result;
2417                 }
2418
2419
2420                 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2421                         IntPtr  hmem;
2422                         IntPtr  data;
2423                         object  obj;
2424
2425                         if (handle != clip_magic) {
2426                                 throw new ArgumentException("handle is not a valid clipboard handle");
2427                         }
2428
2429                         hmem = Win32GetClipboardData((uint)type);
2430                         if (hmem == IntPtr.Zero) {
2431                                 return null;
2432                         }
2433
2434                         data = Win32GlobalLock(hmem);
2435                         if (data == IntPtr.Zero) {
2436                                 uint error = Win32GetLastError();
2437                                 Console.WriteLine("Error: {0}", error);
2438                                 return null;
2439                         }
2440
2441                         obj = null;
2442
2443                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2444                                 obj = AnsiToString(data);
2445                         } else switch ((ClipboardFormats)type) {
2446                                 case ClipboardFormats.CF_TEXT: {
2447                                         obj = AnsiToString(data);
2448                                         break;
2449                                 }
2450
2451                                 case ClipboardFormats.CF_DIB: {
2452                                         obj = DIBtoImage(data);
2453                                         break;
2454                                 }
2455
2456                                 case ClipboardFormats.CF_UNICODETEXT: {
2457                                         obj = UnicodeToString(data);
2458                                         break;
2459                                 }
2460
2461                                 default: {
2462                                         if (converter != null && !converter(type, data, out obj)) {
2463                                                 obj = null;
2464                                         }
2465                                         break;
2466                                 }
2467                         }
2468                         Win32GlobalUnlock(hmem);
2469
2470                         return obj;
2471
2472                 }
2473
2474                 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
2475                         byte[]  data;
2476                         IntPtr  hmem;
2477                         IntPtr  hmem_ptr;
2478
2479                         if (handle != clip_magic) {
2480                                 throw new ArgumentException("handle is not a valid clipboard handle");
2481                         }
2482
2483                         if (obj == null) {
2484                                 // Just clear it
2485                                 Win32EmptyClipboard();
2486                                 return;
2487                         }
2488
2489                         if (type == -1) {
2490                                 if (obj is string) {
2491                                         type = (int)ClipboardFormats.CF_UNICODETEXT;
2492                                 } else if (obj is Image) {
2493                                         type = (int)ClipboardFormats.CF_DIB;
2494                                 }
2495                         }
2496
2497                         if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2498                                 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2499                                 Win32SetClipboardData((uint)type, hmem);
2500                                 return;
2501                         } else switch((ClipboardFormats)type) {
2502                                 case ClipboardFormats.CF_UNICODETEXT: {
2503                                         hmem = Marshal.StringToHGlobalUni((string)obj);
2504                                         Win32SetClipboardData((uint)type, hmem);
2505                                         return;
2506                                 }
2507
2508                                 case ClipboardFormats.CF_TEXT: {
2509                                         hmem = Marshal.StringToHGlobalAnsi((string)obj);
2510                                         Win32SetClipboardData((uint)type, hmem);
2511                                         return;
2512                                 }
2513
2514                                 case ClipboardFormats.CF_BITMAP:
2515                                 case ClipboardFormats.CF_DIB: {
2516                                         data = ImageToDIB((Image)obj);
2517
2518                                         hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2519                                         hmem_ptr = Win32GlobalLock(hmem);
2520                                         Marshal.Copy(data, 0, hmem_ptr, data.Length);
2521                                         Win32GlobalUnlock(hmem);
2522                                         Win32SetClipboardData((uint)ClipboardFormats.CF_DIB, hmem);
2523                                         return;
2524                                 }
2525
2526                                 default: {
2527                                         if (converter != null && converter(ref type, obj, out data)) {
2528                                                 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2529                                                 hmem_ptr = Win32GlobalLock(hmem);
2530                                                 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2531                                                 Win32GlobalUnlock(hmem);
2532                                                 Win32SetClipboardData((uint)type, hmem);
2533                                         }
2534                                         return;
2535                                 }
2536                         }
2537                 }
2538
2539                 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
2540                         if (allowed) {
2541                                 Win32DnD.RegisterDropTarget(hwnd);
2542                         } else {
2543                                 Win32DnD.UnregisterDropTarget(hwnd);
2544                         }
2545                 }
2546
2547                 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
2548                         return Win32DnD.StartDrag(hwnd, data, allowedEffects);
2549                 }
2550
2551                 // XXX this doesn't work at all for FrameStyle.Dashed - it draws like Thick, and in the Thick case
2552                 // the corners are drawn incorrectly.
2553                 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
2554                         IntPtr          hdc;
2555                         IntPtr          pen;
2556                         IntPtr          oldpen;
2557                         COLORREF        clrRef = new COLORREF();
2558
2559                         // If we want the standard hatch pattern we would
2560                         // need to create a brush
2561
2562                         clrRef.R = backColor.R;
2563                         clrRef.G = backColor.G;
2564                         clrRef.B = backColor.B;
2565
2566                         // Grab a pen
2567                         pen = Win32CreatePen (style == FrameStyle.Thick ? PenStyle.PS_SOLID : PenStyle.PS_DASH,
2568                                               style == FrameStyle.Thick ? 4 : 2, ref clrRef);
2569
2570                         hdc = Win32GetDC(IntPtr.Zero);
2571                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2572                         oldpen = Win32SelectObject(hdc, pen);
2573
2574                         Win32MoveToEx(hdc, rectangle.Left, rectangle.Top, IntPtr.Zero);
2575                         if ((rectangle.Width > 0) && (rectangle.Height > 0)) {
2576                                 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2577                                 Win32LineTo(hdc, rectangle.Right, rectangle.Bottom);
2578                                 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2579                                 Win32LineTo(hdc, rectangle.Left, rectangle.Top);
2580                         } else {
2581                                 if (rectangle.Width > 0) {
2582                                         Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2583                                 } else {
2584                                         Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2585                                 }
2586                         }
2587
2588                         Win32SelectObject(hdc, oldpen);
2589                         Win32DeleteObject(pen);
2590
2591                         Win32ReleaseDC(IntPtr.Zero, hdc);
2592                 }
2593
2594                 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
2595                         IntPtr          hdc;
2596                         IntPtr          pen;
2597                         IntPtr          oldpen;
2598                         POINT           pt;
2599                         COLORREF        clrRef = new COLORREF();
2600
2601                         pt = new POINT();
2602                         pt.x = 0;
2603                         pt.y = 0;
2604                         Win32ClientToScreen(IntPtr.Zero, ref pt);
2605
2606                         // If we want the standard hatch pattern we would
2607                         // need to create a brush
2608
2609                         clrRef.R = backColor.R;
2610                         clrRef.G = backColor.G;
2611                         clrRef.B = backColor.B;
2612
2613                         // Grab a pen
2614                         pen = Win32CreatePen(PenStyle.PS_SOLID, 1, ref clrRef);
2615
2616                         hdc = Win32GetDC(IntPtr.Zero);
2617                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2618                         oldpen = Win32SelectObject(hdc, pen);
2619
2620                         Win32MoveToEx(hdc, pt.x + start.X, pt.y + start.Y, IntPtr.Zero);
2621                         Win32LineTo(hdc, pt.x + end.X, pt.y + end.Y);
2622
2623                         Win32SelectObject(hdc, oldpen);
2624                         Win32DeleteObject(pen);
2625
2626                         Win32ReleaseDC(IntPtr.Zero, hdc);
2627                 }
2628
2629                 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
2630                 {
2631                         RECT    rect;
2632
2633                         rect = new RECT();
2634                         rect.left = rectangle.Left;
2635                         rect.top = rectangle.Top;
2636                         rect.right = rectangle.Right;
2637                         rect.bottom = rectangle.Bottom;
2638
2639                         IntPtr          hdc;
2640                         IntPtr          brush;
2641                         IntPtr          oldbrush;
2642                         COLORREF        clrRef = new COLORREF();
2643
2644                         clrRef.R = backColor.R;
2645                         clrRef.G = backColor.G;
2646                         clrRef.B = backColor.B;
2647
2648                         // Grab a brush
2649                         brush = Win32CreateSolidBrush (clrRef);
2650
2651                         hdc = Win32GetDC(IntPtr.Zero);
2652                         oldbrush = Win32SelectObject(hdc, brush);
2653
2654                         Win32PatBlt (hdc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, PatBltRop.DSTINVERT);
2655
2656                         Win32SelectObject(hdc, oldbrush);
2657                         Win32DeleteObject(brush);
2658
2659                         Win32ReleaseDC(IntPtr.Zero, hdc);
2660                 }
2661
2662                 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
2663                         IntPtr          hdc;
2664                         IntPtr          pen;
2665                         IntPtr          oldpen;
2666                         POINT           pt;
2667
2668                         pt = new POINT();
2669                         pt.x = 0;
2670                         pt.y = 0;
2671                         Win32ClientToScreen(handle, ref pt);
2672
2673                         // If we want the standard hatch pattern we would
2674                         // need to create a brush
2675
2676                         // Grab a pen
2677                         pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
2678
2679                         hdc = Win32GetDC(IntPtr.Zero);
2680                         Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2681                         oldpen = Win32SelectObject(hdc, pen);
2682
2683                         Control c = Control.FromHandle (handle);
2684                         if (c != null) {
2685                                 RECT window_rect;
2686                                 Win32GetWindowRect (c.Handle, out window_rect);
2687                                 Region r = new Region (new Rectangle(window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top));
2688                                 Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
2689                         }
2690
2691                         Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
2692                         if ((rect.Width > 0) && (rect.Height > 0)) {
2693                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2694                                 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
2695                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2696                                 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
2697                         } else {
2698                                 if (rect.Width > 0) {
2699                                         Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2700                                 } else {
2701                                         Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2702                                 }
2703                         }
2704
2705                         Win32SelectObject(hdc, oldpen);
2706                         Win32DeleteObject(pen);
2707                         if (c != null)
2708                                 Win32ExtSelectClipRgn(hdc, IntPtr.Zero, (int) ClipCombineMode.RGN_COPY);
2709
2710                         Win32ReleaseDC(IntPtr.Zero, hdc);
2711                 }
2712
2713                 internal override SizeF GetAutoScaleSize(Font font) {
2714                         Graphics        g;
2715                         float           width;
2716                         string          magic_string = "The quick brown fox jumped over the lazy dog.";
2717                         double          magic_number = 44.549996948242189;
2718
2719                         g = Graphics.FromHwnd(FosterParent);
2720
2721                         width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
2722                         return new SizeF(width, font.Height);
2723                 }
2724
2725                 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2726                         return Win32SendMessage(hwnd, message, wParam, lParam);
2727                 }
2728
2729                 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2730                         return Win32PostMessage(hwnd, message, wParam, lParam);
2731                 }
2732
2733                 internal override int SendInput (IntPtr hwnd, Queue keys) {
2734                         INPUT[] inputs = new INPUT[keys.Count];
2735                         const Int32 INPUT_KEYBOARD = 1;
2736                         uint returns = 0;
2737                         int i = 0;
2738                         while (keys.Count > 0) {
2739                                 MSG msg = (MSG)keys.Dequeue();
2740
2741                                 
2742                                 inputs[i].ki.wScan = 0;
2743                                 inputs[i].ki.time = 0;
2744                                 inputs[i].ki.dwFlags = (Int32)(msg.message == Msg.WM_KEYUP ? InputFlags.KEYEVENTF_KEYUP : 0);
2745                                 inputs[i].ki.wVk = (short)msg.wParam.ToInt32();
2746                                 inputs[i].type = INPUT_KEYBOARD;
2747                                 i++;
2748                         }
2749                         returns = Win32SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
2750
2751                         return (int) returns;
2752                 }
2753
2754                 internal override int KeyboardSpeed {
2755                         get {
2756                                 int speed = 0;
2757                                 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDSPEED, 0, ref speed, 0);
2758                                 //
2759                                 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
2760                                 //
2761                                 return speed;
2762                         }
2763                 }
2764
2765                 internal override int KeyboardDelay {
2766                         get {
2767                                 int delay = 1;
2768                                 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDDELAY, 0, ref delay, 0);
2769                                 //
2770                                 // Return values must range from 0 to 4, 0 meaning 250ms,
2771                                 // and 4 meaning 1000 ms.
2772                                 //
2773                                 return delay;
2774                         }
2775                 }
2776
2777                 private class WinBuffer
2778                 {
2779                         public IntPtr hdc;
2780                         public IntPtr bitmap;
2781
2782                         public WinBuffer (IntPtr hdc, IntPtr bitmap)
2783                         {
2784                                 this.hdc = hdc;
2785                                 this.bitmap = bitmap;
2786                         }
2787                 }
2788
2789                 internal override void CreateOffscreenDrawable (IntPtr handle, int width, int height, out object offscreen_drawable)
2790                 {
2791                         Graphics destG = Graphics.FromHwnd (handle);
2792                         IntPtr destHdc = destG.GetHdc ();
2793
2794                         IntPtr srcHdc = Win32CreateCompatibleDC (destHdc);
2795                         IntPtr srcBmp = Win32CreateCompatibleBitmap (destHdc, width, height);
2796                         Win32SelectObject (srcHdc, srcBmp);
2797
2798                         offscreen_drawable = new WinBuffer (srcHdc, srcBmp);
2799
2800                         destG.ReleaseHdc (destHdc);
2801                 }
2802
2803                 internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
2804                 {
2805                         return Graphics.FromHdc (((WinBuffer)offscreen_drawable).hdc);
2806                 }
2807
2808                 internal override void BlitFromOffscreen (IntPtr dest_handle, Graphics dest_dc, object offscreen_drawable, Graphics offscreen_dc, Rectangle r)
2809                 {
2810                         WinBuffer wb = (WinBuffer)offscreen_drawable;
2811
2812                         IntPtr destHdc = dest_dc.GetHdc ();
2813                         Win32BitBlt (destHdc, r.Left, r.Top, r.Width, r.Height, wb.hdc, r.Left, r.Top, TernaryRasterOperations.SRCCOPY);
2814                         dest_dc.ReleaseHdc (destHdc);
2815                 }
2816
2817                 internal override void DestroyOffscreenDrawable (object offscreen_drawable)
2818                 {
2819                         WinBuffer wb = (WinBuffer)offscreen_drawable;
2820
2821                         Win32DeleteObject (wb.bitmap);
2822                         Win32DeleteDC (wb.hdc);
2823                 }
2824
2825                 internal override event EventHandler Idle;
2826                 #endregion      // Public Static Methods
2827
2828                 #region Win32 Imports
2829                 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
2830                 private extern static uint Win32GetLastError();
2831
2832                 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2833                 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);
2834
2835                 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
2836                 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
2837
2838                 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2839                 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
2840
2841                 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2842                 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
2843
2844                 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
2845                 internal extern static bool Win32TranslateMessage(ref MSG msg);
2846
2847                 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2848                 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
2849
2850                 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
2851                 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
2852
2853                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2854                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2855
2856                 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2857                 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2858
2859                 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2860                 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
2861
2862                 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2863                 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
2864
2865                 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
2866                 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
2867
2868                 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2869                 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
2870
2871                 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2872                 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
2873
2874                 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
2875                 private extern static IntPtr Win32ShowCursor(bool bShow);
2876
2877                 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
2878                 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
2879
2880                 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
2881                 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
2882
2883                 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
2884                 private extern static bool Win32DestroyCursor(IntPtr hCursor);
2885
2886                 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2887                 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2888
2889                 //[DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2890                 //private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2891
2892                 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
2893                 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
2894
2895                 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
2896                 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
2897
2898                 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
2899                 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
2900
2901                 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
2902                 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2903
2904                 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
2905                 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2906
2907                 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
2908                 private extern static IntPtr Win32GetDC(IntPtr hWnd);
2909
2910                 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
2911                 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
2912
2913                 //[DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
2914                 //private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
2915
2916                 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
2917                 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
2918
2919                 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2920                 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
2921
2922                 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2923                 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
2924
2925                 //[DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2926                 //private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
2927
2928                 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
2929                 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
2930
2931                 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
2932                 private extern static IntPtr Win32ReleaseCapture();
2933
2934                 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
2935                 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
2936
2937                 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
2938                 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
2939
2940                 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
2941                 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
2942
2943                 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
2944                 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
2945
2946                 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
2947                 private extern static IntPtr Win32GetParent(IntPtr hWnd);
2948
2949                 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
2950                 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
2951
2952                 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
2953                 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
2954
2955                 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
2956                 private extern static bool Win32GetCursorPos(out POINT lpPoint);
2957
2958                 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
2959                 private extern static bool Win32SetCursorPos(int x, int y);
2960
2961                 //[DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
2962                 //private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
2963
2964                 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
2965                 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
2966
2967                 //[DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
2968                 //private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
2969
2970                 [DllImport ("gdi32.dll", EntryPoint="CreateSolidBrush", CallingConvention=CallingConvention.StdCall)]
2971                 private extern static IntPtr Win32CreateSolidBrush(COLORREF clrRef);
2972
2973                 [DllImport ("gdi32.dll", EntryPoint="PatBlt", CallingConvention=CallingConvention.StdCall)]
2974                 private extern static int Win32PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, PatBltRop dwRop);
2975
2976                 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
2977                 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
2978
2979                 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
2980                 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
2981
2982                 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2983                 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
2984
2985                 [DllImport ("user32.dll", EntryPoint="GetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2986                 private extern static uint Win32GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out LayeredWindowAttributes pwdFlags);
2987
2988                 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
2989                 private extern static bool Win32DeleteObject(IntPtr o);
2990
2991                 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
2992                 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
2993
2994                 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
2995                 private extern static IntPtr Win32GetDesktopWindow();
2996
2997                 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
2998                 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
2999
3000                 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
3001                 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
3002
3003                 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
3004                 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
3005
3006                 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
3007                 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
3008
3009                 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
3010                 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
3011
3012                 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
3013                 internal extern static IntPtr Win32GetFocus();
3014
3015                 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
3016                 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
3017
3018                 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
3019                 private  extern static bool Win32DestroyCaret();
3020
3021                 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
3022                 private  extern static bool Win32ShowCaret(IntPtr hwnd);
3023
3024                 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
3025                 private  extern static bool Win32HideCaret(IntPtr hwnd);
3026
3027                 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
3028                 private  extern static bool Win32SetCaretPos(int X, int Y);
3029
3030                 //[DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
3031                 //private  extern static uint Win32GetCaretBlinkTime();
3032
3033                 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3034                 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
3035
3036                 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
3037                 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
3038
3039                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3040                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3041
3042                 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3043                 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
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, IntPtr 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, IntPtr 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, IntPtr 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, ref RECT 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, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3059
3060                 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3061                 private extern static IntPtr Win32GetActiveWindow();
3062
3063                 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
3064                 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
3065
3066                 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3067                 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
3068
3069                 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
3070                 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
3071
3072                 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
3073                 private extern static bool IsWindowEnabled(IntPtr hwnd);
3074
3075                 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
3076                 private extern static bool IsWindowVisible(IntPtr hwnd);
3077
3078                 //[DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
3079                 //private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
3080
3081                 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3082                 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3083
3084                 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3085                 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3086
3087                 [DllImport ("user32.dll", EntryPoint="SendInput", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3088                 private extern static UInt32 Win32SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray)] INPUT[] inputs, Int32 cbSize);
3089
3090                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3091                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
3092                 
3093                 //[DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3094                 //private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
3095
3096                 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3097                 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
3098
3099                 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
3100                 private extern static bool Win32OpenClipboard(IntPtr hwnd);
3101
3102                 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
3103                 private extern static bool Win32EmptyClipboard();
3104
3105                 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3106                 private extern static uint Win32RegisterClipboardFormat(string format);
3107
3108                 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
3109                 private extern static bool Win32CloseClipboard();
3110
3111                 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
3112                 private extern static uint Win32EnumClipboardFormats(uint format);
3113
3114                 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
3115                 private extern static IntPtr Win32GetClipboardData(uint format);
3116
3117                 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
3118                 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
3119
3120                 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
3121                 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
3122
3123                 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
3124                 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
3125
3126                 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
3127                 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
3128
3129                 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
3130                 internal extern static uint Win32GlobalSize(IntPtr hMem);
3131
3132                 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
3133                 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
3134
3135                 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
3136                 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
3137
3138                 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
3139                 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
3140
3141                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3142                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
3143
3144                 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3145                 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
3146
3147                 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
3148                 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
3149
3150                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3151                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
3152
3153                 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3154                 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
3155
3156                 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
3157                 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
3158
3159                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3160                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
3161
3162                 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3163                 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
3164
3165                 [DllImport("gdi32.dll", EntryPoint = "ExcludeClipRect", CallingConvention = CallingConvention.StdCall)]
3166                 internal extern static int Win32ExcludeClipRect (IntPtr hdc, int left, int top,  int right, int bottom);
3167
3168                 [DllImport ("gdi32.dll", EntryPoint="ExtSelectClipRgn", CallingConvention=CallingConvention.StdCall)]
3169                 internal extern static int Win32ExtSelectClipRgn(IntPtr hdc, IntPtr hrgn, int mode);
3170
3171                 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3172                 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
3173
3174                 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3175                 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
3176
3177                 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3178                 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
3179
3180                 [DllImport ("user32.dll", EntryPoint="ClipCursor", CallingConvention=CallingConvention.StdCall)]
3181                 internal extern static bool Win32ClipCursor (ref RECT lpRect);
3182
3183                 [DllImport ("user32.dll", EntryPoint="GetClipCursor", CallingConvention=CallingConvention.StdCall)]
3184                 internal extern static bool Win32GetClipCursor (out RECT lpRect);
3185
3186                 [DllImport ("gdi32.dll", EntryPoint="BitBlt", CallingConvention=CallingConvention.StdCall)]
3187                 internal static extern bool Win32BitBlt (IntPtr hObject, int nXDest, int nYDest, int nWidth,
3188                    int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
3189
3190                 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3191                 internal static extern IntPtr Win32CreateCompatibleDC (IntPtr hdc);
3192
3193                 [DllImport ("gdi32.dll", EntryPoint="DeleteDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3194                 internal static extern bool Win32DeleteDC (IntPtr hdc);
3195
3196                 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleBitmap", CallingConvention=CallingConvention.StdCall)]
3197                 internal static extern IntPtr Win32CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight);
3198                 #endregion
3199         }
3200 }