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