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