56c7b0112c8664e21d6982f14476523c1397257f
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / win32functions.cs
1 /*
2  * Copyright (C) 5/11/2002 Carlos Harvey Perez 
3  * 
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject
10  * to the following conditions:
11  * 
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  * 
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT.
19  * IN NO EVENT SHALL CARLOS HARVEY PEREZ BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  * 
24  * Except as contained in this notice, the name of Carlos Harvey Perez
25  * shall not be used in advertising or otherwise to promote the sale,
26  * use or other dealings in this Software without prior written
27  * authorization from Carlos Harvey Perez.
28  */
29
30 using System;
31 using System.Drawing;
32 using System.Runtime.InteropServices;
33 using System.Text;
34 using System.Diagnostics;
35
36 //using UtilityLibrary.WinControls;
37
38 //namespace UtilityLibrary.Win32
39 namespace System.Windows.Forms{
40         /// <summary>
41         /// Windows API Functions
42         /// </summary>
43         public class Win32
44         {
45                 #region Constructors
46                 // No need to construct this object
47                 #endregion
48                 
49                 #region Constans values
50                 internal const string TOOLBARCLASSNAME = "ToolbarWindow32";
51                 internal const string REBARCLASSNAME = "ReBarWindow32";
52                 internal const string PROGRESSBARCLASSNAME = "msctls_progress32";
53                 internal const string SCROLLBAR = "SCROLLBAR";
54                 #endregion
55
56                 #region CallBacks
57                 internal delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
58                 internal delegate int CompareFunc(IntPtr param1, IntPtr param2, IntPtr sortParam);
59                 internal delegate int WinProc(IntPtr hWnd, int message, int wParam, int lParam);
60                 #endregion
61
62                 #region Kernel32.dll functions
63                 [DllImport("kernel32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
64                 internal static extern int GetCurrentThreadId();
65                 [DllImport("kernel32.dll")]
66                 internal static extern int GetDriveType(string rootPathName);
67                 [DllImport("kernel32.dll")]
68                 internal static extern int GetVolumeInformation(string drivePath,
69                         StringBuilder volumeNameBuffer,
70                         int driveNameBufferSize,
71                         out int serialNumber,
72                         out int maxFileNameLength,
73                         out int fileSystemFlags,
74                         StringBuilder systemNameBuffer,
75                         int systemNameBufferSize);
76                 [DllImport("kernel32.dll")]
77                 internal static extern void OutputDebugString(string message);
78                 #endregion
79         
80                 #region Gdi32.dll functions
81                 [DllImport("gdi32.dll")]
82                 static internal extern bool StretchBlt(IntPtr hDCDest, int XOriginDest, int YOriginDest, int WidthDest, int HeightDest,
83                         IntPtr hDCSrc,  int XOriginScr, int YOriginSrc, int WidthScr, int HeightScr, PatBltTypes Rop);
84                 [DllImport("gdi32.dll")]
85                 static internal extern IntPtr CreateCompatibleDC(IntPtr hDC);
86                 [DllImport("gdi32.dll")]
87                 static internal extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int Width, int Heigth);
88                 [DllImport("gdi32.dll")]
89                 static internal extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
90                 [DllImport("gdi32.dll")]
91                 static internal extern bool BitBlt(IntPtr hDCDest, int XOriginDest, int YOriginDest, int WidthDest, int HeightDest,
92                         IntPtr hDCSrc,  int XOriginScr, int YOriginSrc, PatBltTypes flags);
93                 [DllImport("gdi32.dll")]
94                 static internal extern IntPtr DeleteDC(IntPtr hDC);
95                 [DllImport("gdi32.dll")]
96                 static internal extern bool PatBlt(IntPtr hDC, int XLeft, int YLeft, int Width, int Height, int Rop);
97                 [DllImport("gdi32.dll")]
98                 static internal extern bool DeleteObject(IntPtr hObject);
99                 [DllImport("gdi32.dll")]
100                 static internal extern int GetPixel(IntPtr hDC, int XPos, int YPos);
101                 [DllImport("gdi32.dll")]
102                 static internal extern int SetMapMode(IntPtr hDC, int fnMapMode);
103                 [DllImport("gdi32.dll")]
104                 static internal extern int GetObjectType(IntPtr handle);
105                 [DllImport("gdi32")]
106                 internal static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO_FLAT bmi, 
107                         int iUsage, ref int ppvBits, IntPtr hSection, int dwOffset);
108                 [DllImport("gdi32")]
109                 internal static extern int GetDIBits(IntPtr hDC, IntPtr hbm, int StartScan, int ScanLines, int lpBits, BITMAPINFOHEADER bmi, int usage);
110                 [DllImport("gdi32")]
111                 internal static extern int GetDIBits(IntPtr hdc, IntPtr hbm, int StartScan, int ScanLines, int lpBits, ref BITMAPINFO_FLAT bmi, int usage);
112                 [DllImport("gdi32")]
113                 internal static extern IntPtr GetPaletteEntries(IntPtr hpal, int iStartIndex, int nEntries, byte[] lppe);
114                 [DllImport("gdi32")]
115                 internal static extern IntPtr GetSystemPaletteEntries(IntPtr hdc, int iStartIndex, int nEntries, byte[] lppe);
116                 [DllImport("gdi32")]
117                 internal static extern int SetDCBrushColor(IntPtr hdc,  int crColor);
118                 [DllImport("gdi32")]
119                 internal static extern IntPtr CreateSolidBrush(int crColor);
120                 [DllImport("gdi32")]
121                 internal static extern int SetBkMode(IntPtr hDC, BackgroundMode mode);
122                 [DllImport("gdi32")]
123                 internal static extern int SetViewportOrgEx(IntPtr hdc,  int x, int y,  int param);
124                 [DllImport("gdi32")]
125         internal static extern int SetTextColor(IntPtr hDC, int colorRef);
126                 [DllImport("gdi32")]
127                 internal static extern int SetStretchBltMode(IntPtr hDC, StrechModeFlags StrechMode);
128                 [DllImport("gdi32")]
129                 internal static extern int SetPixel(IntPtr hDC, int x, int y, int color);
130                 [DllImport("gdi32")]
131                 internal static extern IntPtr CreatePen(PenStyle penStyle, int width, int color);
132                 [DllImport("gdi32")]
133                 internal static extern int GetClipRgn(IntPtr hDC, ref IntPtr region);
134                 [DllImport("gdi32")]
135                 internal static extern IntPtr CreateRectRgn(int nLeftRect,  int TopRect, int nRightRect, int nBottomRect);
136                 [DllImport("gdi32")]
137                 internal static extern int GetRgnBox(IntPtr hRegion, ref RECT rc);
138                 #endregion
139
140                 #region Uxtheme.dll functions
141                 [DllImport("uxtheme.dll")]
142                 static public extern int SetWindowTheme(IntPtr hWnd, StringBuilder AppID, StringBuilder ClassID);
143                 static public void DisableWindowsXPTheme(IntPtr hWnd)
144                 {
145                         // Disable using the Window XP Theme for the Window handle
146                         // passed as a parameter
147                         StringBuilder applicationName = new StringBuilder(" ", 1); 
148                         StringBuilder classIDs = new StringBuilder(" " , 1); 
149                         Win32.SetWindowTheme(hWnd, applicationName, classIDs);
150                 }
151                 #endregion
152         
153                 #region User32.dll functions
154                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
155                 static internal extern IntPtr GetDesktopWindow();
156                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
157                 static internal extern bool ShowWindow(IntPtr hWnd, ShowWindowStyles State);
158                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
159                 static internal extern bool SetForegroundWindow(IntPtr hWnd);
160                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
161                 static internal extern bool OpenClipboard(IntPtr hWndNewOwner);
162                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
163                 static internal extern bool CloseClipboard();
164                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
165                 static internal extern bool EmptyClipboard();
166                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
167                 static internal extern IntPtr SetClipboardData( int Format, IntPtr hData);
168                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
169                 static internal extern bool GetMenuItemRect(IntPtr hWnd, IntPtr hMenu, int Item, ref RECT rc);
170                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
171                 internal static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
172                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
173                 internal static extern int SendMessage(IntPtr hWnd, Msg msg, int wParam, int lParam);
174                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
175                 internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);
176                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
177                 internal static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref RECT lParam);
178                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
179                 internal static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref POINT lParam);
180                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
181                 internal static extern void SendMessage(IntPtr hWnd, ToolBarMessages msg, int wParam, ref TBBUTTON lParam);
182                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
183                 internal static extern void SendMessage(IntPtr hWnd, ToolBarMessages msg, int wParam, ref TBBUTTONINFO lParam);
184                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
185                 internal static extern int SendMessage(IntPtr hWnd, RebarMessages msg, int wParam, ref REBARBANDINFO lParam);
186                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
187                 internal static extern void SendMessage(IntPtr hWnd, TreeViewMessages msg, int wParam, ref TVITEM lParam);
188                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
189                 internal static extern void SendMessage(IntPtr hWnd, TreeViewMessages msg, int wParam, ref TVINSERTSTRUCT lParam);
190                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
191                 internal static extern void SendMessage(IntPtr hWnd, TreeViewMessages msg, int wParam, ref TVSORTCB lParam);
192                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
193                 internal static extern int SendMessage(IntPtr hWnd, TreeViewMessages msg, int wParam, ref TVHITTESTINFO hti);
194                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
195                 internal static extern void SendMessage(IntPtr hWnd, ListViewMessages msg, int wParam, ref LVITEM lParam);
196                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
197                 internal static extern void SendMessage(IntPtr hWnd, HeaderControlMessages msg, int wParam, ref HDITEM lParam);
198                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
199                 internal static extern void SendMessage(IntPtr hWnd, HeaderControlMessages msg, int wParam, ref HD_HITTESTINFO hti);
200                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
201                 internal static extern int SendMessage(IntPtr hWnd, HeaderControlMessages msg, int wParam, int lParam);
202                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
203                 internal static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);
204                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
205                 internal static extern IntPtr PostMessage(IntPtr hWnd, Msg msg, int wParam, int lParam);
206                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
207                 internal static extern IntPtr SetWindowsHookEx(WindowsHookCodes hookid, HookProc pfnhook, IntPtr hinst, int threadid);
208                 [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
209                 internal static extern bool UnhookWindowsHookEx(IntPtr hhook);
210                 [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
211                 internal static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam);
212                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
213                 internal extern static int DrawText(IntPtr hdc, string lpString, int nCount, ref RECT lpRect, DrawTextFormatFlags flags);
214                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
215                 internal extern static IntPtr GetDlgItem(IntPtr hDlg, int nControlID);
216                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
217         internal extern static int InvalidateRect(IntPtr hWnd,  ref RECT rc, int bErase);
218                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
219                 internal extern static int InvalidateRect(IntPtr hWnd,  IntPtr rc, int bErase);
220                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
221                 internal static extern bool WaitMessage();
222
223                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
224                 internal static extern bool PeekMessage(ref MESSAGE msg, int hWnd, int wFilterMin, int wFilterMax, PeekMessageFlags flags);
225
226                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
227                 internal static extern bool GetMessage(ref MESSAGE msg, int hWnd, int wFilterMin, int wFilterMax);
228
229                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
230                 internal static extern bool TranslateMessage(ref MESSAGE msg);
231
232                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
233                 internal static extern bool DispatchMessage(ref MESSAGE msg);
234
235                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
236                 internal static extern IntPtr LoadCursor(IntPtr hInstance, CursorType cursor);
237
238                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
239                 internal static extern IntPtr SetCursor(IntPtr hCursor);
240
241                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
242                 internal static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
243
244                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
245                 internal static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
246
247                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
248                 internal static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, 
249                         IntPtr hdcSrc, ref POINT pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, UpdateLayeredWindowFlags dwFlags);
250
251                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
252                 internal static extern bool ClientToScreen(IntPtr hWnd, ref POINT pt);
253
254                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
255                 internal static extern bool TrackMouseEvent(ref TRACKMOUSEEVENTS tme);
256
257                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
258                 internal static extern bool SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
259
260                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
261                 internal static extern short GetKeyState(int virtKey);
262
263                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
264                 internal static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
265
266                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
267                 internal static extern int GetClassName(IntPtr hWnd,  StringBuilder ClassName, int nMaxCount);
268
269                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
270                 internal static extern int SetWindowLong(IntPtr hWnd, GetWindowLongFlag flag, int dwNewLong);
271
272                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
273                 internal static extern int SetWindowLong(IntPtr hWnd, GetWindowLongFlag flag, WinProc winProc);
274
275                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
276                 internal static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hRegion, int flags);
277
278                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
279                 internal static extern IntPtr GetWindowDC(IntPtr hWnd);
280
281                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
282                 internal static extern int FillRect(IntPtr hDC, ref RECT rect, IntPtr hBrush);
283
284                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
285                 internal static extern int SetWindowText(IntPtr hWnd, string text);
286
287                 [DllImport("User32.dll", CharSet=CharSet.Auto)]
288                 internal static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);
289
290                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
291                 static internal extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
292
293                 [DllImport("user32.dll", CharSet=CharSet.Auto)] 
294                 static internal extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
295
296                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
297                 static internal extern int ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
298
299                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
300                 static internal extern int GetSystemMetrics(SystemMetricsCodes code);
301
302                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
303                 static internal extern int SetScrollInfo(IntPtr hwnd,  int bar, ref SCROLLINFO si, int fRedraw);
304
305                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
306                 static internal extern int ShowScrollBar(IntPtr hWnd, int bar,  int show);
307
308                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
309                 static internal extern int EnableScrollBar(IntPtr hWnd, int flags, int arrows);
310
311                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
312                 static internal extern int BringWindowToTop(IntPtr hWnd);
313
314                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
315                 static internal extern int GetScrollInfo(IntPtr hwnd, int bar, ref SCROLLINFO si);
316
317                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
318                 static internal extern int ScrollWindowEx(IntPtr hWnd, int dx, int dy, 
319                         ref RECT rcScroll, ref RECT rcClip, IntPtr UpdateRegion, ref RECT rcInvalidated, int flags);
320
321                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
322                 static internal extern bool IsWindow(IntPtr hWnd);
323
324                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
325                 static internal extern int LockWindowUpdate(IntPtr hWnd);
326
327                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
328                 static internal extern bool ValidateRect(IntPtr hWnd, ref RECT rcInvalidated);
329
330                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
331                 static internal extern bool ValidateRect(IntPtr hWnd, IntPtr rc);
332
333                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
334                 static internal extern int GetScrollBarInfo(IntPtr hWnd, SystemObject id, ref SCROLLBARINFO sbi);
335
336                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
337                 static internal extern IntPtr GetWindowLong(IntPtr hWnd, GetWindowLongFlag flag);
338
339                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
340                 static internal extern int SetProp(IntPtr hWnd, IntPtr atom, IntPtr hData);
341
342                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
343                 static internal extern int CallWindowProc(IntPtr hOldProc, IntPtr hWnd, int message, int wParam, int lParam);
344
345                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
346                 static internal extern int EndMenu();
347
348                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
349         static internal extern int DefWindowProc(IntPtr hWnd, int message, int wParam, int lParam);
350
351                 #endregion
352
353                 #region Shell32.dll functions
354
355                 [DllImport("Shell32.dll", CharSet=CharSet.Auto)]
356                 internal static extern IntPtr SHGetFileInfo(string drivePath, int fileAttributes, 
357                         out SHFILEINFO fileInfo, int countBytesFileInfo, ShellFileInfoFlags flags);
358
359                 [DllImport("Shell32.dll", CharSet=CharSet.Auto)]
360                 internal static extern IntPtr SHGetFileInfo(IntPtr idl, int fileAttributes, 
361                         out SHFILEINFO fileInfo, int countBytesFileInfo, ShellFileInfoFlags flags);
362
363                 [DllImport("Shell32.dll", CharSet=CharSet.Auto)]
364                 internal static extern int SHGetSpecialFolderLocation(IntPtr hwndOwner, ShellSpecialFolder folder, out IntPtr idl);
365
366                 [DllImport("Shell32.dll", CharSet=CharSet.Auto)]
367                 internal static extern int SHGetMalloc(out IMalloc alloc);
368
369                 [DllImport("Shell32.dll", CharSet=CharSet.Auto)]
370                 internal static extern int SHGetDesktopFolder(out IShellFolder folder);
371                 
372                 [DllImport("Shell32.dll", CharSet=CharSet.Auto)]
373                 internal static extern int SHGetPathFromIDList(IntPtr idl, StringBuilder path);
374
375                 internal static void SHFreeMalloc(IntPtr handle)
376                 {
377                         IMalloc alloc = null;
378                         try 
379                         {
380                                 Win32.SHGetMalloc(out alloc);
381                                 Debug.Assert(alloc != null);
382                                 alloc.Free(handle);
383                                 // Free allocator itself
384                                 IUnknown iUnknown = (IUnknown)alloc;
385                                 iUnknown.Release();
386                         }
387                         catch (Exception e)
388                         {
389                                 // In case the Garbage collector is trying to free
390                                 // this memory from its own thread
391                                 Debug.WriteLine(e.Message);
392                         }
393                 }
394                 
395                 #endregion
396
397                 #region Common Controls functions
398
399                 [DllImport("comctl32.dll")]
400                 internal static extern bool InitCommonControlsEx(INITCOMMONCONTROLSEX icc);
401
402                 [DllImport("comctl32.dll")]
403                 internal static extern bool InitCommonControls();
404
405                 [DllImport("comctl32.dll", EntryPoint="DllGetVersion")]
406                 internal extern static int GetCommonControlDLLVersion(ref DLLVERSIONINFO dvi);
407
408                 [DllImport("comctl32.dll")]
409                 internal static extern IntPtr ImageList_Create(int width, int height, int flags, int count, int grow);
410
411                 [DllImport("comctl32.dll")]
412                 internal static extern bool ImageList_Destroy(IntPtr handle);
413
414                 [DllImport("comctl32.dll")]
415                 internal static extern int ImageList_Add(IntPtr imageHandle, IntPtr hBitmap, IntPtr hMask);
416
417                 [DllImport("comctl32.dll")]
418                 internal static extern bool ImageList_Remove(IntPtr imageHandle, int index);
419
420                 [DllImport("comctl32.dll")]
421                 internal static extern bool ImageList_BeginDrag(IntPtr imageHandle, int imageIndex, int xHotSpot, int yHotSpot);
422
423                 [DllImport("comctl32.dll")]
424                 internal static extern bool ImageList_DragEnter(IntPtr hWndLock, int x, int y);
425
426                 [DllImport("comctl32.dll")]
427                 internal static extern bool ImageList_DragMove(int x, int y);
428
429                 [DllImport("comctl32.dll")]
430                 internal static extern bool ImageList_DragLeave(IntPtr hWndLock);
431
432                 [DllImport("comctl32.dll")]
433                 internal static extern void ImageList_EndDrag();
434
435                 [DllImport("comctl32.dll")]
436                 internal static extern bool ImageList_Draw(IntPtr hImageList, int imageIndex, 
437                         IntPtr hDCDest, int x, int y, ImageListDrawFlags flags);
438
439                 [DllImport("comctl32.dll")]
440                 internal static extern int ImageList_DrawEx(
441                         IntPtr hImageList, int imageIndex, IntPtr hDCDest, int x, int y, int dx, int dy, 
442                         uint backColor, uint foregColor, ImageListDrawFlags flags);
443
444                 [DllImport("comctl32.dll")]
445                 internal static extern int ImageList_DragShowNolock(int show);
446                 
447                 [DllImport("comctl32.dll")]
448                 internal static extern int ImageList_AddMasked(IntPtr hImageList, IntPtr hBitmap, int crMask);
449
450                 [DllImport("comctl32.dll")]
451                 internal static extern int ImageList_SetDragCursorImage(IntPtr himlDrag, int iDrag, int dxHotspot, int dyHotspot);
452                                                                 
453                 internal static int ImageList_DrawEx(IntPtr hImageList, int imageIndex, IntPtr hDCDest, int x, int y, int dx, int dy,   
454                         ImageListDrawColor backColor, ImageListDrawColor foreColor, ImageListDrawFlags flags)
455                 {
456             uint bColor = (uint)ImageListDrawColors.CLR_NONE;
457                         if ( backColor == ImageListDrawColor.Default )
458                                 bColor =  (uint)ImageListDrawColors.CLR_DEFAULT;
459
460                         uint fColor = (uint)ImageListDrawColors.CLR_NONE;
461                         if ( foreColor == ImageListDrawColor.Default )
462                                 fColor =  (uint)ImageListDrawColors.CLR_DEFAULT;
463                          
464                         // Call actual function
465                         return ImageList_DrawEx(hImageList, imageIndex, hDCDest, x, y, dx, dy, bColor, fColor, flags);
466                 }
467
468                 
469                 static internal bool IsCommonCtrl6()
470                 {
471                         DLLVERSIONINFO dllVersion = new DLLVERSIONINFO();
472                         // We are assummng here that anything greater or equal than 6
473                         // will have the new XP theme drawing enable
474                         dllVersion.cbSize = Marshal.SizeOf(typeof(DLLVERSIONINFO));
475                         Win32.GetCommonControlDLLVersion(ref dllVersion);
476                         return (dllVersion.dwMajorVersion >= 6);
477                 }
478
479                 #endregion
480
481                 #region Win32 Macro-Like helpers
482                 internal static int X_LPARAM(int lParam)
483                 {
484                         return (lParam & 0xffff);
485                 }
486          
487                 internal static int Y_LPARAM(int lParam)
488                 {
489                         return (lParam >> 16);
490                 }
491
492                 internal static Point GetPointFromLPARAM(int lParam)
493                 {
494                         return new Point(X_LPARAM(lParam), Y_LPARAM(lParam));
495                 }
496
497                 internal static int LOW_ORDER(int param)
498                 {
499                         return (param & 0xffff);
500                 }
501
502                 internal static int HIGH_ORDER(int param)
503                 {
504                         return (param >> 16);
505                 }
506
507                 internal static int INDEXTOOVERLAYMASK(int index)
508                 {
509                         return (int)((uint)index << 8); 
510                 }
511
512                 internal static int OVERLAYMASKTOINDEX(int index)
513                 {
514                         return (int)((uint)index >> 8); 
515                 }
516
517                 internal static int INDEXTOSTATEIMAGEMASK(int i)
518                 {
519                         return (int)((uint)i << 12);
520                 }
521
522                 internal static int STATEIMAGEMASKTOINDEX(int i)
523                 {
524                          return (int)((uint)i >> 12);
525                 }
526
527                 internal static short HRESULT_CODE(int hr)
528                 {
529              return (short)(hr & 0xFFFF);
530                 }
531
532                 internal static bool SUCCEEDED(int status)
533                 {
534                         return (status >= 0);
535                 }
536
537                 internal static bool FAILED(int status)
538                 {
539                         return (status < 0);
540                 }
541
542                 internal static int  MAKEINTRESOURCE(int res)
543                 {
544                         return 0x0000FFFF & res;
545                 }
546                 #endregion
547
548
549                 #region Mono win32 Fuinctions
550
551                 internal delegate IntPtr WndProc (IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
552                 
553                 [DllImport ("user32.dll", 
554                          CallingConvention = CallingConvention.StdCall,
555                          CharSet = CharSet.Ansi, EntryPoint = "RegisterClassA")]
556                 internal static extern uint RegisterClass(ref WNDCLASS wndClass);
557                 
558                 #region Added by Dennis hayes 10-20-2002
559                 //correct?
560                 [DllImport ("user32.dll", 
561                          CallingConvention = CallingConvention.StdCall,
562                          CharSet = CharSet.Auto)]
563                 internal static extern uint SendMessage(
564                         IntPtr hWnd, uint Msg,
565                         IntPtr wParam, IntPtr lParam);
566
567                 [DllImport ("user32.dll", 
568                          CallingConvention = CallingConvention.StdCall,
569                          CharSet = CharSet.Auto)]
570                 internal static extern bool GetWindowPlacement(
571                         IntPtr hWnd,
572                         ref  WINDOWPLACEMENT  lpwndpl  // position data
573                         );
574                 #endregion
575
576                 [DllImport ("user32.dll", 
577                          CallingConvention = CallingConvention.StdCall,
578                          CharSet = CharSet.Ansi, EntryPoint = "CreateWindowExA")]
579                 internal static extern IntPtr CreateWindowEx (
580                         uint dwExStyle, string lpClassName, 
581                         string lpWindowName, uint dwStyle, 
582                         int x, int y, int nWidth, int nHeight,
583                         IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance,
584                         ref object lpParam);
585
586                 [DllImport ("user32.dll", 
587                          CallingConvention = CallingConvention.StdCall, 
588                          CharSet = CharSet.Auto)]
589                 internal static extern IntPtr CreateMenu ();
590
591                 [DllImport ("user32.dll", 
592                          CallingConvention = CallingConvention.StdCall, 
593                          CharSet = CharSet.Ansi)]
594                 internal static extern bool AppendMenuA(IntPtr hMenu, uint uflags, IntPtr NewItem, string item);
595
596                 [DllImport ("user32.dll", 
597                          CallingConvention = CallingConvention.StdCall)]
598                 internal static extern bool DestroyMenu (IntPtr hMenu);
599                 
600                 [DllImport ("user32.dll", CallingConvention = 
601                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
602                 internal extern static IntPtr DefWindowProcA (
603                         IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
604
605                 [DllImport ("user32.dll", CallingConvention = 
606                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
607                 internal extern static int DestroyWindow (IntPtr hWnd);
608
609                 [DllImport ("user32.dll", CallingConvention = 
610                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
611                 internal static extern int ShowWindow (IntPtr hWnd, 
612                         uint nCmdShow);
613
614                 [DllImport ("user32.dll", CallingConvention = 
615                          CallingConvention.StdCall,CharSet = CharSet.Auto)]
616                 internal static extern int GetMessageA (ref MSG msg, int hwnd, 
617                         int msgFrom,  int msgTo);
618
619                 [DllImport ("user32.dll", CallingConvention = 
620                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
621                 internal static extern int  TranslateMessage (ref MSG msg);
622
623                 [DllImport ("user32.dll", CallingConvention = 
624                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
625                 internal static extern int DispatchMessageA (ref MSG msg);
626
627                 [DllImport ("user32.dll", CallingConvention = 
628                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
629                 internal static extern int PeekMessageA (
630                         ref MSG msg, IntPtr hWnd, uint wMsgFilterMin, 
631                         uint wMsgFilterMax, uint wRemoveMsg);
632
633                 [DllImport ("user32.dll", CallingConvention = 
634                          CallingConvention.StdCall,
635                          CharSet = CharSet.Auto)]
636                 internal extern static void PostQuitMessage (int nExitCode);
637
638                 [DllImport ("user32.dll", CallingConvention = 
639                          CallingConvention.StdCall,
640                          CharSet = CharSet.Auto)]
641                 internal extern static IntPtr SetActiveWindow (IntPtr hWnd);
642
643                 [DllImport ("user32.dll", CallingConvention = 
644                          CallingConvention.StdCall,
645                          CharSet = CharSet.Auto)]
646                 internal extern static int CloseWindow (IntPtr hWnd);
647
648                 [DllImport ("user32.dll", CallingConvention = 
649                          CallingConvention.StdCall,
650                          CharSet = CharSet.Auto)]
651                 internal extern static int SetWindowPos (
652                         IntPtr hWnd, SetWindowPosZOrder pos,
653                         int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
654
655                 [DllImport ("user32.dll", 
656                          CallingConvention = CallingConvention.StdCall, 
657                          CharSet = CharSet.Auto)]
658                 internal static extern int MessageBoxA (
659                         IntPtr hWnd, string pText, string pCaption, uint uType);
660
661                 [DllImport ("user32.dll", 
662                          CallingConvention = CallingConvention.StdCall, 
663                          CharSet = CharSet.Auto)]
664                 internal static extern IntPtr SetParent (
665                         IntPtr hWndChild, IntPtr hWndNewParent);
666
667                 [DllImport ("user32.dll", 
668                          CallingConvention = CallingConvention.StdCall, 
669                          CharSet = CharSet.Auto)]
670                 internal static extern IntPtr GetParent (IntPtr hWnd);
671
672                 [DllImport ("user32.dll", 
673                          CallingConvention = CallingConvention.StdCall, 
674                          CharSet = CharSet.Auto)]
675                 internal static extern bool SetWindowTextA (
676                         IntPtr hWnd, string lpString);
677
678                 [DllImport ("user32.dll", 
679                          CallingConvention = CallingConvention.StdCall, 
680                          CharSet = CharSet.Auto)]
681                 internal static extern bool UpdateWindow (IntPtr hWnd);
682
683                 [DllImport ("user32.dll", 
684                          CallingConvention = CallingConvention.StdCall, 
685                          CharSet = CharSet.Auto)]
686                 internal static extern uint GetBkColor (IntPtr hdc);
687
688                 [DllImport ("user32.dll", 
689                          CallingConvention = CallingConvention.StdCall, 
690                          CharSet = CharSet.Auto)]
691                 internal static extern uint SetBkColor (IntPtr hdc, uint crColor);
692
693                 [DllImport ("user32.dll", 
694                          CallingConvention = CallingConvention.StdCall, 
695                          CharSet = CharSet.Auto)]
696                 internal static extern IntPtr GetDC (IntPtr hWnd);
697
698                 [DllImport ("user32.dll", 
699                          CallingConvention = CallingConvention.StdCall, 
700                          CharSet = CharSet.Auto)]
701                 internal static extern int ReleaseDC (IntPtr hWnd, IntPtr hDC);
702
703                 [DllImport ("user32.dll", 
704                          CallingConvention = CallingConvention.StdCall, 
705                          CharSet = CharSet.Auto)]
706                 internal static extern IntPtr GetFocus();
707
708                 [DllImport ("user32.dll", 
709                          CallingConvention = CallingConvention.StdCall, 
710                          CharSet = CharSet.Auto)]
711                 internal static extern IntPtr SetFocus (IntPtr hWnd);
712
713                 [DllImport ("user32.dll", 
714                          CallingConvention = CallingConvention.StdCall, 
715                          CharSet = CharSet.Auto)]
716                 internal static extern bool IsWindowEnabled (IntPtr hWnd);
717
718                 [DllImport ("user32.dll", 
719                          CallingConvention = CallingConvention.StdCall, 
720                          CharSet = CharSet.Auto)]
721                 internal static extern bool IsMenu (IntPtr hWnd);
722
723                 
724                 [DllImport ("user32.dll", 
725                          CallingConvention = CallingConvention.StdCall, 
726                          CharSet = CharSet.Auto)]
727                 internal static extern bool EnableWindow (
728                         IntPtr hWnd, bool bEnable);
729
730                 [DllImport ("user32.dll", 
731                          CallingConvention = CallingConvention.StdCall, 
732                          CharSet = CharSet.Auto)]
733                 internal static extern bool GetWindowRect (
734                         IntPtr hWnd, ref RECT lpRect);
735
736                 [DllImport ("user32.dll", 
737                          CallingConvention = CallingConvention.StdCall, 
738                          CharSet = CharSet.Auto)]
739                 internal static extern bool GetClientRect (
740                         IntPtr hWnd, ref RECT lpRect);
741
742                 [DllImport ("user32.dll", 
743                          CallingConvention = CallingConvention.StdCall, 
744                          CharSet = CharSet.Auto)]
745                 internal static extern bool InvalidateRect (
746                         IntPtr hWnd, ref RECT lpRect, bool bErase); 
747
748                 [DllImport ("user32.dll", 
749                          CallingConvention = CallingConvention.StdCall, 
750                          CharSet = CharSet.Auto)]
751                 internal static extern IntPtr GetCapture ();
752
753                 [DllImport ("user32.dll", 
754                          CallingConvention = CallingConvention.StdCall, 
755                          CharSet = CharSet.Auto)]
756                 internal static extern IntPtr SetCapture (IntPtr hWnd);
757
758                 [DllImport ("user32.dll", 
759                          CallingConvention = CallingConvention.StdCall, 
760                          CharSet = CharSet.Auto)]
761                 internal static extern bool ReleaseCapture ();
762
763                 [DllImport ("user32.dll", 
764                          CallingConvention = CallingConvention.StdCall, 
765                          CharSet = CharSet.Auto)]
766                 internal static extern int GetWindowTextA (
767                         IntPtr hWnd, ref String lpString, int nMaxCount);
768
769                 [DllImport ("user32.dll", 
770                          CallingConvention = CallingConvention.StdCall, 
771                          CharSet = CharSet.Auto)]
772                 internal static extern int GetWindowTextLengthA (IntPtr hWnd);
773
774                 [DllImport ("user32.dll", 
775                          CallingConvention = CallingConvention.StdCall, 
776                          CharSet = CharSet.Auto)]
777                 internal static extern bool GetCursorPos (ref POINT lpPoint);
778
779                 #endregion
780
781                 [DllImport ("user32.dll", 
782                          CallingConvention = CallingConvention.StdCall, 
783                          CharSet = CharSet.Auto)]
784                 internal static extern IntPtr GetMenu (IntPtr hWnd);
785                 
786                 [DllImport ("user32.dll", 
787                          CallingConvention = CallingConvention.StdCall, 
788                          CharSet = CharSet.Auto)]
789                 internal static extern int SetMenu (IntPtr hWnd, IntPtr hMenu);
790
791                 [DllImport ("user32.dll", 
792                          CallingConvention = CallingConvention.StdCall, 
793                          CharSet = CharSet.Ansi)]
794                 internal static extern int InsertMenuA(IntPtr hMenu, uint pos, uint uflags, IntPtr NewItem, string item);
795                 
796                 [DllImport ("user32.dll", 
797                          CallingConvention = CallingConvention.StdCall, 
798                          CharSet = CharSet.Ansi)]
799                 internal static extern int RemoveMenu(IntPtr hMenu, uint pos, uint uflags);
800                 
801                 [DllImport ("user32.dll", 
802                          CallingConvention = CallingConvention.StdCall)]
803                 internal static extern int DrawMenuBar (IntPtr hWnd);
804                 
805                 [DllImport ("user32.dll", 
806                          CallingConvention = CallingConvention.StdCall)]
807                 internal static extern int SetMenuDefaultItem(IntPtr hMenu, int uItem, int fByPos );
808
809
810                 [DllImport ("user32.dll", CallingConvention = 
811                          CallingConvention.StdCall,
812                          CharSet = CharSet.Auto)]
813                 internal extern static int AdjustWindowRect (
814                         ref RECT rc, int dwStyle, int bMenu);
815
816
817
818                 [DllImport ("kernel32.dll", CallingConvention = 
819                          CallingConvention.StdCall,
820                          CharSet = CharSet.Auto)]
821                 internal extern static uint GetLastError ();
822                 
823                 [DllImport ("kernel32.dll", CallingConvention = 
824                          CallingConvention.StdCall,
825                          CharSet = CharSet.Auto)]
826                 internal extern static uint  FormatMessage (
827                         uint flags, IntPtr lpSource,
828                         uint messageId, uint languageId,
829                         StringBuilder lpBuffer, int nSize,
830                         IntPtr Arguments);
831                                 
832                 internal static string FormatMessage(uint error) {
833                         StringBuilder sb = new StringBuilder(2048);
834                         Win32.FormatMessage( (uint)(FM_.FORMAT_MESSAGE_FROM_SYSTEM | FM_.FORMAT_MESSAGE_IGNORE_INSERTS),
835                                 IntPtr.Zero, error, 0, sb, sb.Capacity, IntPtr.Zero);
836                         return sb.ToString();
837                 }
838
839
840
841         }
842
843 }