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