f91f76407b8f7f08ea6cedcb04dad06f57079bda
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / win32functions.cs
1 /*
2  * Copyright (C) 5/11/2002 Carlos Harvey Perez 
3  * Modifcations and additons, Copyright Ximian 2002/3
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject
11  * to the following conditions:
12  * 
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.
20  * IN NO EVENT SHALL CARLOS HARVEY PEREZ BE LIABLE FOR ANY CLAIM,
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  * 
25  * Except as contained in this notice, the name of Carlos Harvey Perez
26  * shall not be used in advertising or otherwise to promote the sale,
27  * use or other dealings in this Software without prior written
28  * authorization from Carlos Harvey Perez.
29  */
30
31 using System;
32 using System.Drawing;
33 using System.Runtime.InteropServices;
34 using System.Text;
35 using System.Diagnostics;
36
37 //using UtilityLibrary.WinControls;
38
39 //namespace UtilityLibrary.Win32
40 namespace System.Windows.Forms{
41         /// <summary>
42         /// Windows API Functions
43         /// </summary>
44         public class Win32 
45         {
46                 #region Constructors
47                 // No need to construct this object
48                 #endregion
49                 
50                 #region Constans values
51                 internal const string TOOLBARCLASSNAME = "ToolbarWindow32";
52                 internal const string REBARCLASSNAME = "ReBarWindow32";
53                 internal const string PROGRESSBARCLASSNAME = "msctls_progress32";
54                 internal const string SCROLLBAR = "SCROLLBAR";
55                 internal const string TOOLTIPS_CLASS = "tooltips_class32";
56                 internal const string MDICLIENTCLASSNAME = "MDICLIENT";
57                 internal const string TABCONTROL = "SysTabControl32";
58                 internal const string DEFAULT_WINDOW_CLASS = "mono_scrollable_control";
59                 
60                 #endregion
61
62                 #region CallBacks
63                 internal delegate IntPtr FnHookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam);
64                 internal delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
65                 internal delegate int CompareFunc(IntPtr param1, IntPtr param2, IntPtr sortParam);
66                 internal delegate int WinProc(IntPtr hWnd, int message, int wParam, int lParam);
67                 internal delegate int WinProcMsg(IntPtr hWnd, Msg message, int wParam, int lParam);
68                 #endregion
69
70                 #region Kernel32.dll functions
71                 [DllImport("kernel32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
72                 internal static extern int GetCurrentThreadId();
73                 [DllImport("kernel32.dll")]
74                 internal static extern int GetDriveType(string rootPathName);
75                 [DllImport("kernel32.dll")]
76                 internal static extern int GetVolumeInformation(string drivePath,
77                         StringBuilder volumeNameBuffer,
78                         int driveNameBufferSize,
79                         out int serialNumber,
80                         out int maxFileNameLength,
81                         out int fileSystemFlags,
82                         StringBuilder systemNameBuffer,
83                         int systemNameBufferSize);
84
85                 [DllImport("kernel32.dll")]
86                 internal static extern IntPtr LoadLibraryA(string filename);
87                 
88                 [DllImport("kernel32.dll")]
89                 internal static extern bool FreeLibrary(IntPtr handle);
90                 
91                 [DllImport("kernel32.dll", EntryPoint="OutputDebugStringW")]
92                 internal static extern void OutputDebugString(string message);
93
94                 [DllImport ("kernel32.dll", CallingConvention = CallingConvention.StdCall,
95                          CharSet = CharSet.Auto)]
96                 internal extern static uint GetLastError ();
97                 
98                 [DllImport ("kernel32.dll", CallingConvention = CallingConvention.StdCall,
99                          CharSet = CharSet.Auto, EntryPoint = "FormatMessageW")]
100                 internal extern static uint  FormatMessage (
101                         uint flags, IntPtr lpSource,uint messageId, uint languageId,
102                         StringBuilder lpBuffer, int nSize, IntPtr Arguments);
103                                 
104                 internal static string FormatMessage(uint error) {
105                         StringBuilder sb = new StringBuilder(2048);
106                         Win32.FormatMessage( (uint)(FM_.FORMAT_MESSAGE_FROM_SYSTEM | FM_.FORMAT_MESSAGE_IGNORE_INSERTS),
107                                 IntPtr.Zero, error, 0, sb, sb.Capacity, IntPtr.Zero);
108                         return sb.ToString();
109                 }
110         #endregion
111         
112                 #region Gdi32.dll functions
113                 [DllImport("gdi32.dll")]
114                 static internal extern bool StretchBlt(IntPtr hDCDest, int XOriginDest, int YOriginDest, int WidthDest, int HeightDest,
115                         IntPtr hDCSrc,  int XOriginScr, int YOriginSrc, int WidthScr, int HeightScr, PatBltTypes Rop);
116                 [DllImport("gdi32.dll")]
117                 static internal extern IntPtr CreateCompatibleDC(IntPtr hDC);
118                 [DllImport("gdi32.dll")]
119                 static internal extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int Width, int Heigth);
120                 [DllImport("gdi32.dll")]
121                 static internal extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
122                 [DllImport("gdi32.dll")]
123                 static internal extern bool BitBlt(IntPtr hDCDest, int XOriginDest, int YOriginDest, int WidthDest, int HeightDest,
124                         IntPtr hDCSrc,  int XOriginScr, int YOriginSrc, PatBltTypes flags);
125                 [DllImport("gdi32.dll")]
126                 static internal extern IntPtr DeleteDC(IntPtr hDC);
127                 [DllImport("gdi32.dll")]
128                 static internal extern bool PatBlt(IntPtr hDC, int XLeft, int YLeft, int Width, int Height, int Rop);
129                 [DllImport("gdi32.dll")]
130                 static internal extern bool DeleteObject(IntPtr hObject);
131                 [DllImport("gdi32.dll")]
132                 static internal extern int GetPixel(IntPtr hDC, int XPos, int YPos);
133                 [DllImport("gdi32.dll")]
134                 static internal extern int SetMapMode(IntPtr hDC, int fnMapMode);
135                 [DllImport("gdi32.dll")]
136                 static internal extern int GetObjectType(IntPtr handle);
137                 [DllImport("gdi32.dll")]
138                 internal static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO_FLAT bmi, 
139                         int iUsage, ref int ppvBits, IntPtr hSection, int dwOffset);
140                 [DllImport("gdi32.dll")]
141                 internal static extern int GetDIBits(IntPtr hDC, IntPtr hbm, int StartScan, int ScanLines, int lpBits, BITMAPINFOHEADER bmi, int usage);
142                 [DllImport("gdi32.dll")]
143                 internal static extern int GetDIBits(IntPtr hdc, IntPtr hbm, int StartScan, int ScanLines, int lpBits, ref BITMAPINFO_FLAT bmi, int usage);
144                 [DllImport("gdi32.dll")]
145                 internal static extern IntPtr GetPaletteEntries(IntPtr hpal, int iStartIndex, int nEntries, byte[] lppe);
146                 [DllImport("gdi32.dll")]
147                 internal static extern IntPtr GetSystemPaletteEntries(IntPtr hdc, int iStartIndex, int nEntries, byte[] lppe);
148                 [DllImport("gdi32.dll")]
149                 internal static extern int SetDCBrushColor(IntPtr hdc,  int crColor);
150                 [DllImport("gdi32.dll")]
151                 internal static extern IntPtr CreateSolidBrush(int crColor);
152                 [DllImport("gdi32.dll")]
153                 internal static extern BackgroundMode SetBkMode(IntPtr hDC, BackgroundMode mode);
154                 [DllImport("gdi32.dll")]
155                 internal static extern int SetViewportOrgEx(IntPtr hdc,  int x, int y,  int param);
156                 [DllImport("gdi32.dll")]
157                 internal static extern int SetTextColor(IntPtr hDC, int colorRef);
158                 [DllImport("gdi32.dll")]
159                 internal static extern int SetStretchBltMode(IntPtr hDC, StrechModeFlags StrechMode);
160                 [DllImport("gdi32.dll")]
161                 internal static extern int SetPixel(IntPtr hDC, int x, int y, int color);
162                 [DllImport("gdi32.dll")]
163                 internal static extern IntPtr CreatePen(PenStyle penStyle, int width, int color);
164                 [DllImport("gdi32.dll")]
165                 internal static extern int GetClipRgn(IntPtr hDC, ref IntPtr region);
166                 [DllImport("gdi32.dll")]
167                 internal static extern IntPtr CreateRectRgn(int nLeftRect,  int TopRect, int nRightRect, int nBottomRect);
168                 [DllImport("gdi32.dll")]
169                 internal static extern int GetRgnBox(IntPtr hRegion, ref RECT rc);
170                 [DllImport("gdi32.dll")]
171                 internal static extern IntPtr GetStockObject(GSO_ objectType);
172                 [DllImport("gdi32.dll",CharSet = CharSet.Ansi,EntryPoint="ExtTextOutA")]
173                 internal static extern int ExtTextOut(IntPtr hdc, int x, int y,
174                         ExtTextOutFlags options, ref RECT rc, int str, int strLen, IntPtr distances);
175                 [DllImport("gdi32.dll",CharSet = CharSet.Ansi,EntryPoint="ExtTextOutA")]
176                 internal static extern int ExtTextOut(IntPtr hdc, int x, int y,
177                         ExtTextOutFlags options, ref RECT rc, string str, int strLen, IntPtr distances);
178                 [DllImport("gdi32.dll",CharSet = CharSet.Ansi,EntryPoint="GetTextExtentPoint32A")]
179                 internal static extern bool GetTextExtentPoint32(IntPtr hDC, string lpString, int cbString, ref SIZE lpSize);
180
181
182                 [DllImport ("gdi32.dll", 
183                          CallingConvention = CallingConvention.StdCall, 
184                          CharSet = CharSet.Auto)]
185                 internal static extern uint GetBkColor (IntPtr hdc);
186
187                 [DllImport ("gdi32.dll", 
188                          CallingConvention = CallingConvention.StdCall, 
189                          CharSet = CharSet.Auto)]
190                 internal static extern uint SetBkColor (IntPtr hdc, uint crColor);
191
192                 internal static int RGB(Color color) 
193                 {
194                         return color.R | (color.G << 8) | (color.B << 16);
195                 }
196                 
197                 #endregion
198
199                 #region Uxtheme.dll functions
200                 [DllImport("uxtheme.dll")]
201                 static public extern int SetWindowTheme(IntPtr hWnd, StringBuilder AppID, StringBuilder ClassID);
202                 static public void DisableWindowsXPTheme(IntPtr hWnd) 
203                 {
204                         // Disable using the Window XP Theme for the Window handle
205                         // passed as a parameter
206                         StringBuilder applicationName = new StringBuilder(" ", 1); 
207                         StringBuilder classIDs = new StringBuilder(" " , 1); 
208                         Win32.SetWindowTheme(hWnd, applicationName, classIDs);
209                 }
210                 #endregion
211         
212                 #region user32.dll functions
213                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
214                 static internal extern IntPtr GetDesktopWindow();
215                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
216                 static internal extern bool ShowWindow(IntPtr hWnd, ShowWindowStyles State);
217                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
218                 static internal extern bool SetForegroundWindow(IntPtr hWnd);
219                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
220                 static internal extern bool OpenClipboard(IntPtr hWndNewOwner);
221                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
222                 static internal extern bool CloseClipboard();
223                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
224                 static internal extern bool EmptyClipboard();
225                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
226                 static internal extern IntPtr SetClipboardData( int Format, IntPtr hData);
227                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
228                 static internal extern bool GetMenuItemRect(IntPtr hWnd, IntPtr hMenu, int Item, ref RECT rc);
229                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
230                 internal static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
231                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
232                 internal static extern int SendMessage(IntPtr hWnd, Msg msg, int wParam, int lParam);
233                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
234                 internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);
235                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
236                 internal static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref RECT lParam);
237                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
238                 internal static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref POINT lParam);
239                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
240                 internal static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);
241                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
242                 internal static extern int SendMessage(IntPtr hWnd, Msg msg, int wParam, string lParam);
243                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
244                 internal static extern void SendMessage(IntPtr hWnd, ToolBarMessages msg, int wParam, ref TBBUTTON lParam);
245                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
246                 internal static extern void SendMessage(IntPtr hWnd, ToolBarMessages msg, int wParam, ref TBBUTTONINFO lParam);
247                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
248                 internal static extern int SendMessage(IntPtr hWnd, RebarMessages msg, int wParam, ref REBARBANDINFO lParam);
249                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
250                 internal static extern void SendMessage(IntPtr hWnd, TreeViewMessages msg, int wParam, ref TVITEM lParam);
251                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
252                 internal static extern void SendMessage(IntPtr hWnd, TreeViewMessages msg, int wParam, ref TVINSERTSTRUCT lParam);
253                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
254                 internal static extern void SendMessage(IntPtr hWnd, TreeViewMessages msg, int wParam, ref TVSORTCB lParam);
255                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
256                 internal static extern int SendMessage(IntPtr hWnd, TreeViewMessages msg, int wParam, ref TVHITTESTINFO hti);
257                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
258                 internal static extern void SendMessage(IntPtr hWnd, ListViewMessages msg, int wParam, ref LVITEM lParam);
259                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
260                 internal static extern void SendMessage(IntPtr hWnd, HeaderControlMessages msg, int wParam, ref HDITEM lParam);
261                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
262                 internal static extern void SendMessage(IntPtr hWnd, HeaderControlMessages msg, int wParam, ref HD_HITTESTINFO hti);
263                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
264                 internal static extern int SendMessage(IntPtr hWnd, HeaderControlMessages msg, int wParam, int lParam);
265                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="PostMessageA")]
266                 internal static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);
267                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="PostMessageA")]
268                 internal static extern IntPtr PostMessage(IntPtr hWnd, Msg msg, int wParam, int lParam);
269                 [DllImport("user32.dll", CharSet=CharSet.Auto, EntryPoint="SetWindowsHookExW")]
270                 internal static extern IntPtr SetWindowsHookEx(WindowsHookCodes hookid, HookProc pfnhook, IntPtr hinst, int threadid);
271                 [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
272                 internal static extern bool UnhookWindowsHookEx(IntPtr hhook);
273                 [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
274                 internal static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam);
275                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="DrawTextA")]
276                 internal extern static int DrawText(IntPtr hdc, string lpString, int nCount, ref RECT lpRect, DrawTextFormatFlags flags);
277                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
278                 internal extern static IntPtr GetDlgItem(IntPtr hDlg, int nControlID);
279                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
280                 internal extern static int InvalidateRect(IntPtr hWnd,  ref RECT rc, int bErase);
281                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
282                 internal extern static int InvalidateRect(IntPtr hWnd,  IntPtr rc, int bErase);
283                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
284                 internal static extern bool WaitMessage();
285                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
286                 internal static extern int SendMessage2ref(IntPtr hWnd, int msg, ref int wParam, ref int lParam);
287
288                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="PeekMessageA")]
289                 internal static extern bool PeekMessage(ref MESSAGE msg, int hWnd, int wFilterMin, int wFilterMax, PeekMessageFlags flags);
290
291                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="GetMessageA")]
292                 internal static extern bool GetMessage(ref MESSAGE msg, int hWnd, int wFilterMin, int wFilterMax);
293
294                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
295                 internal static extern bool TranslateMessage(ref MESSAGE msg);
296
297                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="DispatchMessageA")]
298                 internal static extern bool DispatchMessage(ref MESSAGE msg);
299
300                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="LoadCursorA")]
301                 internal static extern IntPtr LoadCursor(IntPtr hInstance, CursorType cursor);
302
303                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
304                 internal static extern IntPtr SetCursor(IntPtr hCursor);
305
306                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
307                 internal static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
308
309                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
310                 internal static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
311
312                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
313                 internal static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, 
314                         IntPtr hdcSrc, ref POINT pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, UpdateLayeredWindowFlags dwFlags);
315
316                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
317                 internal static extern bool ClientToScreen(IntPtr hWnd, ref POINT pt);
318                 
319                 internal static bool ClientToScreen(IntPtr hWnd, ref RECT rect) {
320                         POINT pt1 = new POINT();
321                         pt1.x = rect.left;
322                         pt1.y = rect.top;
323                         POINT pt2 = new POINT();
324                         pt2.x = rect.right;
325                         pt2.y = rect.bottom;
326                         bool result = Win32.ClientToScreen(hWnd, ref pt1);
327                         result &= Win32.ClientToScreen(hWnd, ref pt2);
328                         rect.left = pt1.x;
329                         rect.top = pt1.y;
330                         rect.right = pt2.x;
331                         rect.bottom = pt2.y;
332                         return result;
333                 }
334
335
336                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
337                 internal static extern bool ScreenToClient(IntPtr hWnd, ref POINT pt);
338
339                 internal static bool ScreenToClient(IntPtr hWnd, ref RECT rect) {
340                         POINT pt1 = new POINT();
341                         pt1.x = rect.left;
342                         pt1.y = rect.top;
343                         POINT pt2 = new POINT();
344                         pt2.x = rect.right;
345                         pt2.y = rect.bottom;
346                         bool result = Win32.ScreenToClient(hWnd, ref pt1);
347                         result &= Win32.ScreenToClient(hWnd, ref pt2);
348                         rect.left = pt1.x;
349                         rect.top = pt1.y;
350                         rect.right = pt2.x;
351                         rect.bottom = pt2.y;
352                         return result;
353                 }
354
355                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
356                 internal static extern bool TrackMouseEvent(ref TRACKMOUSEEVENT tme);
357
358                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
359                 internal static extern bool SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
360
361                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
362                 internal static extern short GetKeyState(int virtKey);
363
364                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
365                 internal static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
366
367                 [DllImport("user32.dll", CharSet=CharSet.Auto, EntryPoint="GetClassNameW")]
368                 internal static extern int GetClassName(IntPtr hWnd,  StringBuilder ClassName, int nMaxCount);
369
370                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="SetWindowLongA")]
371                 internal static extern int SetWindowLong(IntPtr hWnd, GetWindowLongFlag flag, int dwNewLong);
372
373                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="SetWindowLongA")]
374                 internal static extern IntPtr SetWindowLong(IntPtr hWnd, GetWindowLongFlag flag, WinProc winProc);
375
376                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="SetWindowLongA")]
377                 internal static extern IntPtr SetWindowLong(IntPtr hWnd, GetWindowLongFlag flag, WndProc winProc);
378
379                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
380                 internal static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hRegion, int flags);
381
382                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
383                 internal static extern IntPtr GetWindowDC(IntPtr hWnd);
384
385                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
386                 internal static extern int FillRect(IntPtr hDC, ref RECT rect, IntPtr hBrush);
387
388                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SetWindowTextA")]
389                 internal static extern int SetWindowText(IntPtr hWnd, string text);
390
391                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="GetWindowTextA")]
392                 internal static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);
393
394                 [DllImport("user32.dll", CharSet=CharSet.Ansi,EntryPoint="SendMessageA")]
395                 static internal extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
396
397                 [DllImport("user32.dll", CharSet=CharSet.Auto)] 
398                 static internal extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
399
400                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
401                 static internal extern int ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
402
403                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
404                 static internal extern int GetSystemMetrics(SystemMetricsCodes code);
405
406                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
407                 static internal extern int SetScrollInfo(IntPtr hwnd,  int bar, ref SCROLLINFO si, int fRedraw);
408
409                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
410                 static internal extern int ShowScrollBar(IntPtr hWnd, int bar,  int show);
411
412                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
413                 static internal extern int EnableScrollBar(IntPtr hWnd, int flags, int arrows);
414
415                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
416                 static internal extern int BringWindowToTop(IntPtr hWnd);
417
418                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
419                 static internal extern int GetScrollInfo(IntPtr hwnd, int bar, ref SCROLLINFO si);
420
421                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
422                 static internal extern int ScrollWindowEx(IntPtr hWnd, int dx, int dy, 
423                         ref RECT rcScroll, ref RECT rcClip, IntPtr UpdateRegion, ref RECT rcInvalidated, int flags);
424
425                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
426                 static internal extern bool IsWindow(IntPtr hWnd);
427
428                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
429                 static internal extern int LockWindowUpdate(IntPtr hWnd);
430
431                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
432                 static internal extern bool ValidateRect(IntPtr hWnd, ref RECT rcInvalidated);
433
434                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
435                 static internal extern bool ValidateRect(IntPtr hWnd, IntPtr rc);
436
437                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
438                 static internal extern int GetScrollBarInfo(IntPtr hWnd, SystemObject id, ref SCROLLBARINFO sbi);
439
440                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="GetWindowLongA")]
441                 static internal extern IntPtr GetWindowLong(IntPtr hWnd, GetWindowLongFlag flag);
442
443                 [DllImport("user32.dll", CharSet=CharSet.Auto, EntryPoint="SetPropW")]
444                 static internal extern int SetProp(IntPtr hWnd, IntPtr atom, IntPtr hData);
445
446                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="CallWindowProcA")]
447                 static internal extern int CallWindowProc(IntPtr hOldProc, IntPtr hWnd, int message, int wParam, int lParam);
448
449                 [DllImport("user32.dll", CharSet=CharSet.Auto)]
450                 static internal extern int EndMenu();
451
452                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="DefWindowProcA")]
453                 static internal extern int DefWindowProc(IntPtr hWnd, int message, int wParam, int lParam);
454
455                 [DllImport("user32.dll", CharSet=CharSet.Unicode,EntryPoint="DefMDIChildProcA")]
456                 static internal extern IntPtr DefMDIChildProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
457
458                 [DllImport("user32.dll", CharSet=CharSet.Unicode,EntryPoint="DefFrameProcA")]
459                 static internal extern IntPtr DefFrameProc(IntPtr hWnd, IntPtr hWndMDIClient, Msg Msg, IntPtr wParam, IntPtr lParam);
460                 
461                 [DllImport("user32.dll", CharSet=CharSet.Auto,EntryPoint="LoadCursorA")]
462                 static internal extern IntPtr LoadCursor(IntPtr hInstance, LC_ standardCursor);
463
464                 [DllImport("user32.dll", CharSet=CharSet.Auto, EntryPoint="RegisterWindowMessageA")]
465                 static internal extern int RegisterWindowMessage( string message_name);
466
467                 [DllImport ("user32.dll", CallingConvention = CallingConvention.StdCall, 
468                          CharSet = CharSet.Auto)]
469                 internal static extern IntPtr GetMenu (IntPtr hWnd);
470                 
471                 [DllImport ("user32.dll", CallingConvention = CallingConvention.StdCall, 
472                          CharSet = CharSet.Auto)]
473                 internal static extern int SetMenu (IntPtr hWnd, IntPtr hMenu);
474
475                 [DllImport ("user32.dll", CallingConvention = CallingConvention.StdCall, 
476                          CharSet = CharSet.Ansi)]
477                 internal static extern int InsertMenuA(IntPtr hMenu, uint pos, uint uflags, IntPtr NewItem, string item);
478                 
479                 [DllImport ("user32.dll", CallingConvention = CallingConvention.StdCall, 
480                          CharSet = CharSet.Ansi)]
481                 internal static extern int RemoveMenu(IntPtr hMenu, uint pos, uint uflags);
482                 
483                 [DllImport ("user32.dll", CallingConvention = CallingConvention.StdCall)]
484                 internal static extern int DrawMenuBar (IntPtr hWnd);
485                 
486                 [DllImport ("user32.dll", CallingConvention = CallingConvention.StdCall)]
487                 internal static extern int SetMenuDefaultItem(IntPtr hMenu, int uItem, int fByPos );
488
489                 [DllImport ("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
490                 internal extern static int AdjustWindowRect( ref RECT rc, int dwStyle, int bMenu);
491
492                 [DllImport ("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
493                 internal extern static int AdjustWindowRectEx( ref RECT rc, int dwStyle, int bMenu, int dwStyleEx);
494
495                 [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
496                 internal static extern int DrawEdge(IntPtr hdc, ref RECT rc, Border3DStyle edge, Border3DSide flags);
497
498                 [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
499                 internal static extern int DrawFrameControl(IntPtr hdc, ref RECT rc, uint uType, uint uState);
500                 [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
501                 internal static extern int DrawFocusRect( IntPtr hdc, ref RECT rc);
502                 [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
503                 internal static extern IntPtr WindowFromPoint( POINT pt);
504                 [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
505                 internal static extern int GetSysColor( GetSysColorIndex color);
506
507                 internal delegate void TimerProc(IntPtr hWnd, uint uMsg, uint idEvent, int dwTime);
508                 [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
509                 internal static extern uint SetTimer (IntPtr hWnd, uint nIDEvent, uint uElapse, TimerProc lpTimerFunc);
510                 [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
511                 internal static extern bool KillTimer (IntPtr hWnd, uint nIDEvent);
512                 #endregion
513
514                 #region Shell32.dll functions
515
516                 [DllImport("shell32.dll", CharSet=CharSet.Auto)]
517                 internal static extern IntPtr SHGetFileInfo(string drivePath, int fileAttributes,
518                         out SHFILEINFO fileInfo, int countBytesFileInfo, ShellFileInfoFlags flags);
519
520                 [DllImport("shell32.dll", CharSet=CharSet.Auto)]
521                 internal static extern IntPtr SHGetFileInfo(IntPtr idl, int fileAttributes,
522                         out SHFILEINFO fileInfo, int countBytesFileInfo, ShellFileInfoFlags flags);
523
524                 [DllImport("shell32.dll", CharSet=CharSet.Auto)]
525                 internal static extern int SHGetSpecialFolderLocation(IntPtr hwndOwner, ShellSpecialFolder folder, out IntPtr idl);
526
527                 [DllImport("shell32.dll", CharSet=CharSet.Auto)]
528                 internal static extern int SHGetMalloc(out IMalloc alloc);
529
530                 [DllImport("shell32.dll", CharSet=CharSet.Auto)]
531                 internal static extern int SHGetDesktopFolder(out IShellFolder folder);
532
533                 [DllImport("shell32.dll", CharSet=CharSet.Auto)]
534                 internal static extern int SHGetPathFromIDList(IntPtr idl, StringBuilder path);
535
536                 internal static void SHFreeMalloc(IntPtr handle) 
537                 {
538                         IMalloc alloc = null;
539                         try 
540                         {
541                                 Win32.SHGetMalloc(out alloc);
542                                 Debug.Assert(alloc != null);
543                                 alloc.Free(handle);
544                                 // Free allocator itself
545                                 IUnknown iUnknown = (IUnknown)alloc;
546                                 iUnknown.Release();
547                         }
548                         catch (Exception e) 
549                         {
550                                 // In case the Garbage collector is trying to free
551                                 // this memory from its own thread
552                                 Debug.WriteLine(e.Message);
553                         }
554                 }
555
556                 #endregion
557
558                 #region Common Controls functions
559
560                 [DllImport("comctl32.dll")]
561                 internal static extern bool InitCommonControlsEx(INITCOMMONCONTROLSEX icc);
562
563                 [DllImport("comctl32.dll")]
564                 internal static extern bool InitCommonControls();
565
566                 [DllImport("comctl32.dll", EntryPoint="DllGetVersion")]
567                 internal extern static int GetCommonControlDLLVersion(ref DLLVERSIONINFO dvi);
568
569                 [DllImport("comctl32.dll")]
570                 internal static extern IntPtr ImageList_Create(int width, int height, int flags, int count, int grow);
571
572                 [DllImport("comctl32.dll")]
573                 internal static extern bool ImageList_Destroy(IntPtr handle);
574
575                 [DllImport("comctl32.dll")]
576                 internal static extern int ImageList_Add(IntPtr imageHandle, IntPtr hBitmap, IntPtr hMask);
577
578                 [DllImport("comctl32.dll")]
579                 internal static extern bool ImageList_Remove(IntPtr imageHandle, int index);
580
581                 [DllImport("comctl32.dll")]
582                 internal static extern bool ImageList_BeginDrag(IntPtr imageHandle, int imageIndex, int xHotSpot, int yHotSpot);
583
584                 [DllImport("comctl32.dll")]
585                 internal static extern bool ImageList_DragEnter(IntPtr hWndLock, int x, int y);
586
587                 [DllImport("comctl32.dll")]
588                 internal static extern bool ImageList_DragMove(int x, int y);
589
590                 [DllImport("comctl32.dll")]
591                 internal static extern bool ImageList_DragLeave(IntPtr hWndLock);
592
593                 [DllImport("comctl32.dll")]
594                 internal static extern void ImageList_EndDrag();
595
596                 [DllImport("comctl32.dll")]
597                 internal static extern bool ImageList_Draw(IntPtr hImageList, int imageIndex, 
598                         IntPtr hDCDest, int x, int y, ImageListDrawFlags flags);
599
600                 [DllImport("comctl32.dll")]
601                 internal static extern int ImageList_DrawEx(
602                         IntPtr hImageList, int imageIndex, IntPtr hDCDest, int x, int y, int dx, int dy, 
603                         uint backColor, uint foregColor, ImageListDrawFlags flags);
604
605                 [DllImport("comctl32.dll")]
606                 internal static extern int ImageList_DragShowNolock(int show);
607                 
608                 [DllImport("comctl32.dll")]
609                 internal static extern int ImageList_AddMasked(IntPtr hImageList, IntPtr hBitmap, int crMask);
610
611                 [DllImport("comctl32.dll")]
612                 internal static extern int ImageList_SetDragCursorImage(IntPtr himlDrag, int iDrag, int dxHotspot, int dyHotspot);
613
614                 internal static int ImageList_DrawEx(IntPtr hImageList, int imageIndex, IntPtr hDCDest, int x, int y, int dx, int dy,   
615                         ImageListDrawColor backColor, ImageListDrawColor foreColor, ImageListDrawFlags flags) 
616                 {
617                         uint bColor = (uint)ImageListDrawColors.CLR_NONE;
618                         if ( backColor == ImageListDrawColor.Default )
619                                 bColor =  (uint)ImageListDrawColors.CLR_DEFAULT;
620
621                         uint fColor = (uint)ImageListDrawColors.CLR_NONE;
622                         if ( foreColor == ImageListDrawColor.Default )
623                                 fColor =  (uint)ImageListDrawColors.CLR_DEFAULT;
624                          
625                         // Call actual function
626                         return ImageList_DrawEx(hImageList, imageIndex, hDCDest, x, y, dx, dy, bColor, fColor, flags);
627                 }
628
629                 
630                 static internal bool IsCommonCtrl6() 
631                 {
632                         DLLVERSIONINFO dllVersion = new DLLVERSIONINFO();
633                         // We are assummng here that anything greater or equal than 6
634                         // will have the new XP theme drawing enable
635                         dllVersion.cbSize = Marshal.SizeOf(typeof(DLLVERSIONINFO));
636                         Win32.GetCommonControlDLLVersion(ref dllVersion);
637                         return (dllVersion.dwMajorVersion >= 6);
638                 }
639
640                 #endregion
641
642                 #region Win32 Macro-Like helpers
643                 internal static int X_LPARAM(int lParam) 
644                 {
645                         return (lParam & 0xffff);
646                 }
647          
648                 internal static int Y_LPARAM(int lParam) 
649                 {
650                         return (lParam >> 16);
651                 }
652
653                 internal static Point GetPointFromLPARAM(int lParam) 
654                 {
655                         return new Point(X_LPARAM(lParam), Y_LPARAM(lParam));
656                 }
657
658                 internal static int LOW_ORDER(int param) 
659                 {
660                         return (ushort)param /*(param & 0xffff)*/;
661                 }
662
663                 internal static int HIGH_ORDER(int param) 
664                 {
665                         return (param >> 16);
666                 }
667
668                 internal static int INDEXTOOVERLAYMASK(int index) 
669                 {
670                         return (int)((uint)index << 8); 
671                 }
672
673                 internal static int OVERLAYMASKTOINDEX(int index) 
674                 {
675                         return (int)((uint)index >> 8);
676                 }
677
678                 internal static int INDEXTOSTATEIMAGEMASK(int i) 
679                 {
680                         return (int)((uint)i << 12);
681                 }
682
683                 internal static int STATEIMAGEMASKTOINDEX(int i) 
684                 {
685                         return (int)((uint)i >> 12);
686                 }
687
688                 internal static short HRESULT_CODE(int hr) 
689                 {
690                         return (short)(hr & 0xFFFF);
691                 }
692
693                 internal static bool SUCCEEDED(int status) 
694                 {
695                         return (status >= 0);
696                 }
697
698                 internal static bool FAILED(int status) 
699                 {
700                         return (status < 0);
701                 }
702
703                 internal static int  MAKEINTRESOURCE(int res) 
704                 {
705                         return 0x0000FFFF & res;
706                 }
707                 
708                 internal static int MAKELONG(int lo, int hi)
709                 {
710                         return (hi << 16) | (lo & 0x0000ffff);
711                 }
712                 #endregion
713
714                 #region Mono win32 Fuinctions
715
716                 internal delegate IntPtr WndProc (IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
717
718                 
719                 [DllImport ("user32.dll", 
720                          CallingConvention = CallingConvention.StdCall,
721                          CharSet = CharSet.Ansi, EntryPoint = "RegisterClassA")]
722                 internal static extern uint RegisterClass(ref WNDCLASS wndClass);
723                 
724                 #region Added by Dennis hayes 10-20-2002
725                 //correct?
726                 [DllImport ("user32.dll", 
727                          CallingConvention = CallingConvention.StdCall,
728                          CharSet = CharSet.Auto,EntryPoint="SendMessageA")]
729                 internal static extern uint SendMessage(
730                         IntPtr hWnd, uint Msg,
731                         IntPtr wParam, IntPtr lParam);
732
733                 [DllImport ("user32.dll", 
734                          CallingConvention = CallingConvention.StdCall,
735                          CharSet = CharSet.Auto)]
736                 internal static extern bool GetWindowPlacement(
737                         IntPtr hWnd,
738                         ref  WINDOWPLACEMENT  lpwndpl  // position data
739                         );
740                 #endregion
741
742                 [DllImport ("user32.dll", 
743                          CallingConvention = CallingConvention.StdCall,
744                          CharSet = CharSet.Ansi, EntryPoint = "CreateWindowExA")]
745                 internal static extern IntPtr CreateWindowEx (
746                         uint dwExStyle, string lpClassName, 
747                         string lpWindowName, uint dwStyle, 
748                         int x, int y, int nWidth, int nHeight,
749                         IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance,
750                         [ MarshalAs( UnmanagedType.AsAny )]
751                         object lpParam);
752
753                 [DllImport ("user32.dll", 
754                          CallingConvention = CallingConvention.StdCall, 
755                          CharSet = CharSet.Auto)]
756                 internal static extern IntPtr CreateMenu ();
757
758                 [DllImport ("user32.dll", 
759                          CallingConvention = CallingConvention.StdCall, 
760                          CharSet = CharSet.Ansi)]
761                 internal static extern bool AppendMenuA(IntPtr hMenu, uint uflags, IntPtr NewItem, string item);
762
763                 [DllImport ("user32.dll", 
764                          CallingConvention = CallingConvention.StdCall)]
765                 internal static extern bool DestroyMenu (IntPtr hMenu);
766                 
767                 [DllImport ("user32.dll", CallingConvention = 
768                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
769                 internal extern static IntPtr DefWindowProcA (
770                         IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
771
772                 [DllImport ("user32.dll", CallingConvention = 
773                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
774                 internal extern static int DestroyWindow (IntPtr hWnd);
775
776                 [DllImport ("user32.dll", CallingConvention = 
777                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
778                 internal static extern int ShowWindow (IntPtr hWnd, 
779                         uint nCmdShow);
780
781                 [DllImport ("user32.dll", CallingConvention = 
782                          CallingConvention.StdCall,CharSet = CharSet.Auto)]
783                 internal static extern int GetMessageA (ref MSG msg, int hwnd, 
784                         int msgFrom,  int msgTo);
785
786                 [DllImport ("user32.dll", CallingConvention = 
787                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
788                 internal static extern int  TranslateMessage (ref MSG msg);
789
790                 [DllImport ("user32.dll", CallingConvention =
791                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
792                 internal static extern int DispatchMessageA (ref MSG msg);
793
794                 [DllImport ("user32.dll", CallingConvention = 
795                          CallingConvention.StdCall, CharSet = CharSet.Auto)]
796                 internal static extern int PeekMessageA (
797                         ref MSG msg, IntPtr hWnd, uint wMsgFilterMin, 
798                         uint wMsgFilterMax, uint wRemoveMsg);
799
800                 [DllImport ("user32.dll", CallingConvention = 
801                          CallingConvention.StdCall,
802                          CharSet = CharSet.Auto)]
803                 internal extern static void PostQuitMessage (int nExitCode);
804
805                 [DllImport ("user32.dll", CallingConvention = 
806                          CallingConvention.StdCall,
807                          CharSet = CharSet.Auto)]
808                 internal extern static IntPtr SetActiveWindow (IntPtr hWnd);
809
810                 [DllImport ("user32.dll", CallingConvention =
811                          CallingConvention.StdCall,
812                          CharSet = CharSet.Auto)]
813                 internal extern static int CloseWindow (IntPtr hWnd);
814
815                 [DllImport ("user32.dll", CallingConvention =
816                          CallingConvention.StdCall,
817                          CharSet = CharSet.Auto)]
818                 internal extern static int SetWindowPos (
819                         IntPtr hWnd, SetWindowPosZOrder pos,
820                         int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
821
822                 [DllImport ("user32.dll", 
823                          CallingConvention = CallingConvention.StdCall, 
824                          CharSet = CharSet.Ansi)]
825                 internal static extern int MessageBoxA (
826                         IntPtr hWnd, string pText, string pCaption, uint uType);
827
828                 [DllImport ("user32.dll", 
829                          CallingConvention = CallingConvention.StdCall, 
830                          CharSet = CharSet.Auto)]
831                 internal static extern IntPtr SetParent (
832                         IntPtr hWndChild, IntPtr hWndNewParent);
833
834                 [DllImport ("user32.dll", 
835                          CallingConvention = CallingConvention.StdCall, 
836                          CharSet = CharSet.Auto)]
837                 internal static extern IntPtr GetParent (IntPtr hWnd);
838
839                 [DllImport ("user32.dll", 
840                          CallingConvention = CallingConvention.StdCall, 
841                          CharSet = CharSet.Auto)]
842                 internal static extern IntPtr GetWindow (IntPtr hWnd, uint uCmd);
843
844                 [DllImport ("user32.dll", 
845                          CallingConvention = CallingConvention.StdCall,
846                          CharSet = CharSet.Ansi)]
847                 internal static extern bool SetWindowTextA (
848                         IntPtr hWnd, string lpString);
849
850                 [DllImport ("user32.dll", 
851                          CallingConvention = CallingConvention.StdCall, 
852                          CharSet = CharSet.Auto)]
853                 internal static extern bool UpdateWindow (IntPtr hWnd);
854
855                 [DllImport ("user32.dll", 
856                          CallingConvention = CallingConvention.StdCall, 
857                          CharSet = CharSet.Auto)]
858                 internal static extern IntPtr GetDC (IntPtr hWnd);
859
860                 [DllImport ("user32.dll", 
861                          CallingConvention = CallingConvention.StdCall, 
862                          CharSet = CharSet.Auto)]
863                 internal static extern int ReleaseDC (IntPtr hWnd, IntPtr hDC);
864
865                 [DllImport ("user32.dll", 
866                          CallingConvention = CallingConvention.StdCall, 
867                          CharSet = CharSet.Auto)]
868                 internal static extern IntPtr GetFocus();
869
870                 [DllImport ("user32.dll", 
871                          CallingConvention = CallingConvention.StdCall, 
872                          CharSet = CharSet.Auto)]
873                 internal static extern IntPtr SetFocus (IntPtr hWnd);
874
875                 [DllImport ("user32.dll", 
876                          CallingConvention = CallingConvention.StdCall, 
877                          CharSet = CharSet.Auto)]
878                 internal static extern bool IsWindowEnabled (IntPtr hWnd);
879
880                 [DllImport ("user32.dll", 
881                          CallingConvention = CallingConvention.StdCall, 
882                          CharSet = CharSet.Auto)]
883                 internal static extern bool IsMenu (IntPtr hWnd);
884
885
886                 [DllImport ("user32.dll", 
887                          CallingConvention = CallingConvention.StdCall, 
888                          CharSet = CharSet.Auto)]
889                 internal static extern bool EnableWindow (
890                         IntPtr hWnd, bool bEnable);
891
892                 [DllImport ("user32.dll", 
893                          CallingConvention = CallingConvention.StdCall, 
894                          CharSet = CharSet.Auto)]
895                 internal static extern bool IsDialogMessage (
896                         IntPtr hWnd, ref MSG mes);
897
898                 [DllImport ("user32.dll", 
899                          CallingConvention = CallingConvention.StdCall, 
900                          CharSet = CharSet.Auto)]
901                 internal static extern bool GetWindowRect (
902                         IntPtr hWnd, ref RECT lpRect);
903
904                 [DllImport ("user32.dll", 
905                          CallingConvention = CallingConvention.StdCall, 
906                          CharSet = CharSet.Auto)]
907                 internal static extern bool GetClientRect (
908                         IntPtr hWnd, ref RECT lpRect);
909
910                 [DllImport ("user32.dll", 
911                          CallingConvention = CallingConvention.StdCall, 
912                          CharSet = CharSet.Auto)]
913                 internal static extern bool InvalidateRect (
914                         IntPtr hWnd, ref RECT lpRect, bool bErase); 
915
916                 [DllImport ("user32.dll", 
917                          CallingConvention = CallingConvention.StdCall, 
918                          CharSet = CharSet.Auto)]
919                 internal static extern IntPtr GetCapture ();
920
921                 [DllImport ("user32.dll", 
922                          CallingConvention = CallingConvention.StdCall, 
923                          CharSet = CharSet.Auto)]
924                 internal static extern IntPtr SetCapture (IntPtr hWnd);
925
926                 [DllImport ("user32.dll", 
927                          CallingConvention = CallingConvention.StdCall, 
928                          CharSet = CharSet.Auto)]
929                 internal static extern bool ReleaseCapture ();
930
931                 [DllImport ("user32.dll", 
932                          CallingConvention = CallingConvention.StdCall, 
933                          CharSet = CharSet.Auto)]
934                 internal static extern int GetWindowTextA (
935                         IntPtr hWnd, StringBuilder lpString, int nMaxCount);
936
937                 [DllImport ("user32.dll", 
938                          CallingConvention = CallingConvention.StdCall, 
939                          CharSet = CharSet.Auto)]
940                 internal static extern int GetWindowTextLengthA (IntPtr hWnd);
941
942                 [DllImport ("user32.dll", 
943                          CallingConvention = CallingConvention.StdCall, 
944                          CharSet = CharSet.Auto)]
945                 internal static extern bool GetCursorPos (ref POINT lpPoint);
946
947                 [DllImport ("Comdlg32.dll",
948                          CallingConvention = CallingConvention.StdCall, 
949                          CharSet = CharSet.Ansi)]
950                 internal static extern bool GetOpenFileName ( ref OPENFILENAME lpofn );
951
952                 [DllImport ("Comdlg32.dll",
953                          CallingConvention = CallingConvention.StdCall, 
954                          CharSet = CharSet.Ansi)]
955                 internal static extern uint CommDlgExtendedError ( );
956
957                 #endregion
958
959                 internal static void UpdateWindowStyle( IntPtr hwnd, int RemoveStyle, int AddStyle) {
960                         if( Win32.IsWindow(hwnd)) {
961                                 int style = Win32.GetWindowLong(hwnd, GetWindowLongFlag.GWL_STYLE).ToInt32();
962                                 style &= ~RemoveStyle;
963                                 style |= AddStyle;
964                                 Win32.SetWindowLong(hwnd, GetWindowLongFlag.GWL_STYLE, style);
965                                 Win32.SetWindowPos(hwnd, 0, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE |
966                                         SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOSIZE |
967                                         SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_FRAMECHANGED);
968                         }
969                 }
970                 
971                 internal static ButtonStyles ContentAlignment2SystemButtonStyle( ContentAlignment contentAlign) {
972                         ButtonStyles sysButtonStyle = 0;
973                         
974                         if( contentAlign == ContentAlignment.BottomCenter ||
975                                 contentAlign == ContentAlignment.BottomLeft ||
976                                 contentAlign == ContentAlignment.BottomRight) {
977                                 sysButtonStyle |= ButtonStyles.BS_BOTTOM;
978                         }
979                         else if(contentAlign == ContentAlignment.TopCenter ||
980                                 contentAlign == ContentAlignment.TopLeft ||
981                                 contentAlign == ContentAlignment.TopRight) {
982                                 sysButtonStyle |= ButtonStyles.BS_TOP;
983                         }
984                         else {
985                                 sysButtonStyle |= ButtonStyles.BS_VCENTER;
986                         }
987
988                         if( contentAlign == ContentAlignment.BottomLeft ||
989                                 contentAlign == ContentAlignment.MiddleLeft ||
990                                 contentAlign == ContentAlignment.TopLeft) {
991                                 sysButtonStyle |= ButtonStyles.BS_LEFT;
992                         }
993                         else if(contentAlign == ContentAlignment.BottomRight ||
994                                 contentAlign == ContentAlignment.MiddleRight ||
995                                 contentAlign == ContentAlignment.TopRight) {
996                                 sysButtonStyle |= ButtonStyles.BS_RIGHT;
997                         }
998                         else {
999                                 sysButtonStyle |= ButtonStyles.BS_CENTER;
1000                         }
1001                         return sysButtonStyle;
1002                 }
1003
1004                 internal static StringFormat ContentAlignment2StringFormat( ContentAlignment contentAlign) {
1005                         StringAlignment alignment = 0;
1006                         StringAlignment lineAlignment = 0;
1007                         
1008                         if( contentAlign == ContentAlignment.BottomCenter ||
1009                                 contentAlign == ContentAlignment.BottomLeft ||
1010                                 contentAlign == ContentAlignment.BottomRight) {
1011                                 lineAlignment = StringAlignment.Far;
1012                         }
1013                         else if(contentAlign == ContentAlignment.TopCenter ||
1014                                 contentAlign == ContentAlignment.TopLeft ||
1015                                 contentAlign == ContentAlignment.TopRight) {
1016                                 lineAlignment = StringAlignment.Near;
1017                         }
1018                         else {
1019                                 lineAlignment = StringAlignment.Center;
1020                         }
1021
1022                         if( contentAlign == ContentAlignment.BottomLeft ||
1023                                 contentAlign == ContentAlignment.MiddleLeft ||
1024                                 contentAlign == ContentAlignment.TopLeft) {
1025                                 alignment = StringAlignment.Near;
1026                         }
1027                         else if(contentAlign == ContentAlignment.BottomRight ||
1028                                 contentAlign == ContentAlignment.MiddleRight ||
1029                                 contentAlign == ContentAlignment.TopRight) {
1030                                 alignment = StringAlignment.Far;
1031                         }
1032                         else {
1033                                 alignment = StringAlignment.Center;
1034                         }
1035                         StringFormat result = new StringFormat();
1036                         result.Alignment = alignment;
1037                         result.LineAlignment = lineAlignment;
1038                         return result;
1039                 }
1040                 
1041                 internal static DrawTextFormatFlags ContentAlignment2DrawTextFormat( ContentAlignment contentAlign) {
1042                         DrawTextFormatFlags format = 0;
1043                         
1044                         if( contentAlign == ContentAlignment.BottomCenter ||
1045                                 contentAlign == ContentAlignment.BottomLeft ||
1046                                 contentAlign == ContentAlignment.BottomRight) {
1047                                 format |= DrawTextFormatFlags.DT_BOTTOM;
1048                         }
1049                         else if(contentAlign == ContentAlignment.TopCenter ||
1050                                 contentAlign == ContentAlignment.TopLeft ||
1051                                 contentAlign == ContentAlignment.TopRight) {
1052                                 format |= DrawTextFormatFlags.DT_TOP;
1053                         }
1054                         else {
1055                                 format |= DrawTextFormatFlags.DT_VCENTER;
1056                         }
1057
1058                         if( contentAlign == ContentAlignment.BottomLeft ||
1059                                 contentAlign == ContentAlignment.MiddleLeft ||
1060                                 contentAlign == ContentAlignment.TopLeft) {
1061                                 format |= DrawTextFormatFlags.DT_LEFT;
1062                         }
1063                         else if(contentAlign == ContentAlignment.BottomRight ||
1064                                 contentAlign == ContentAlignment.MiddleRight ||
1065                                 contentAlign == ContentAlignment.TopRight) {
1066                                 format |= DrawTextFormatFlags.DT_RIGHT;
1067                         }
1068                         else {
1069                                 format |= DrawTextFormatFlags.DT_CENTER;
1070                         }
1071                         return format;
1072                 }
1073                 
1074                 internal static void DrawText(Graphics paintOn, string text, Font font, Color color, Rectangle rect, ContentAlignment alignment) {
1075
1076                         RECT rc = new RECT();
1077                         rc.left = rect.Left;
1078                         rc.top = rect.Top;
1079                         rc.right = rect.Right;
1080                         rc.bottom = rect.Bottom;
1081                         
1082                         IntPtr hdc = paintOn.GetHdc();
1083                         int prevColor = Win32.SetTextColor(hdc, RGB(color));
1084                         IntPtr prevFont = Win32.SelectObject(hdc, font.ToHfont());
1085                         BackgroundMode prevBkMode = Win32.SetBkMode(hdc, BackgroundMode.TRANSPARENT);
1086                         Win32.DrawText(hdc, text, text.Length, ref rc, 
1087                                DrawTextFormatFlags.DT_SINGLELINE | Win32.ContentAlignment2DrawTextFormat(alignment));
1088                         Win32.SetBkMode(hdc, prevBkMode);
1089                         Win32.SelectObject(hdc, prevFont);
1090                         Win32.SetTextColor(hdc, prevColor);
1091                         paintOn.ReleaseHdc(hdc);
1092                 }
1093
1094                 internal static SIZE GetTextExtent ( IntPtr hWnd, string text ) {
1095                         IntPtr hOldFont = new IntPtr ( 0 );
1096                         IntPtr hFont = new IntPtr ( Win32.SendMessage ( hWnd, (int)Msg.WM_GETFONT, 0, 0 ) );
1097                         IntPtr hDC   = Win32.GetWindowDC ( hWnd );
1098                         if ( hFont != IntPtr.Zero )
1099                                 hOldFont = Win32.SelectObject ( hDC, hFont );
1100                         SIZE size = new SIZE();
1101                         Win32.GetTextExtentPoint32 ( hDC, text, text.Length, ref size);
1102                         if ( hOldFont != IntPtr.Zero )
1103                                 Win32.SelectObject ( hDC, hOldFont );
1104                         Win32.ReleaseDC ( hWnd, hDC );
1105                         return size;
1106                 }
1107         }
1108 }