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