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