1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2006 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
31 using System.Drawing.Imaging;
32 using System.ComponentModel;
33 using System.Collections;
34 using System.Diagnostics;
36 using System.Runtime.InteropServices;
41 namespace System.Windows.Forms {
42 internal class XplatUIWin32 : XplatUIDriver {
43 #region Local Variables
44 private static XplatUIWin32 instance;
45 private static int ref_count;
46 private static IntPtr FosterParent;
48 internal static MouseButtons mouse_state;
49 internal static Point mouse_position;
50 internal static bool grab_confined;
51 internal static IntPtr grab_hwnd;
52 internal static Rectangle grab_area;
53 internal static WndProc wnd_proc;
54 internal static IntPtr prev_mouse_hwnd;
55 internal static IntPtr override_cursor;
56 internal static bool caret_visible;
58 internal static bool themes_enabled;
59 private Hashtable timer_list;
60 private static Queue message_queue;
61 private static IntPtr clip_magic = new IntPtr(27051977);
62 private static int scroll_width;
63 private static int scroll_height;
64 private static Hashtable wm_nc_registered;
65 private static RECT clipped_cursor_rect;
66 private Hashtable registered_classes;
67 private Hwnd HwndCreating; // the Hwnd we are currently creating (see CreateWindow)
69 #endregion // Local Variables
71 #region Private Structs
72 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
73 private struct WNDCLASS {
75 internal WndProc lpfnWndProc;
76 internal int cbClsExtra;
77 internal int cbWndExtra;
78 internal IntPtr hInstance;
79 internal IntPtr hIcon;
80 internal IntPtr hCursor;
81 internal IntPtr hbrBackground;
82 [MarshalAs(UnmanagedType.LPWStr)]
83 internal string lpszMenuName;
84 [MarshalAs(UnmanagedType.LPWStr)]
85 internal string lpszClassName;
88 [StructLayout(LayoutKind.Sequential)]
89 internal struct RECT {
95 public RECT (int left, int top, int right, int bottom)
100 this.bottom = bottom;
103 #region Instance Properties
104 public int Height { get { return bottom - top; } }
105 public int Width { get { return right - left; } }
106 public Size Size { get { return new Size (Width, Height); } }
107 public Point Location { get { return new Point (left, top); } }
110 #region Instance Methods
111 public Rectangle ToRectangle ()
113 return Rectangle.FromLTRB (left, top, right, bottom);
116 public static RECT FromRectangle (Rectangle rectangle)
118 return new RECT (rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
121 public override int GetHashCode ()
123 return left ^ ((top << 13) | (top >> 0x13))
124 ^ ((Width << 0x1a) | (Width >> 6))
125 ^ ((Height << 7) | (Height >> 0x19));
128 public override string ToString ()
130 return String.Format("RECT left={0}, top={1}, right={2}, bottom={3}, width={4}, height={5}", left, top, right, bottom, right-left, bottom-top);
134 #region Operator overloads
135 public static implicit operator Rectangle (RECT rect)
137 return Rectangle.FromLTRB (rect.left, rect.top, rect.right, rect.bottom);
140 public static implicit operator RECT (Rectangle rect)
142 return new RECT (rect.Left, rect.Top, rect.Right, rect.Bottom);
147 internal enum SPIAction {
148 SPI_GETDRAGFULLWINDOWS = 0x0026,
149 SPI_GETKEYBOARDSPEED = 0x000A,
150 SPI_GETKEYBOARDDELAY = 0x0016,
151 SPI_GETKEYBOARDCUES = 0x100A,
152 SPI_GETWORKAREA = 0x0030,
153 SPI_GETMOUSEHOVERWIDTH = 0x0062,
154 SPI_GETMOUSEHOVERHEIGHT = 0x0064,
155 SPI_GETMOUSEHOVERTIME = 0x0066,
156 SPI_GETWHEELSCROLLLINES = 0x0068
159 internal enum WindowPlacementFlags {
163 SW_SHOWMINIMIZED = 2,
164 SW_SHOWMAXIMIZED = 3,
166 SW_SHOWNOACTIVATE = 4,
169 SW_SHOWMINNOACTIVE = 7,
173 SW_FORCEMINIMIZE = 11,
177 [StructLayout(LayoutKind.Sequential)]
178 private struct WINDOWPLACEMENT {
179 internal uint length;
181 internal WindowPlacementFlags showCmd;
182 internal POINT ptMinPosition;
183 internal POINT ptMaxPosition;
184 internal RECT rcNormalPosition;
187 [StructLayout(LayoutKind.Sequential)]
188 internal struct NCCALCSIZE_PARAMS {
192 internal IntPtr lppos;
196 private enum TMEFlags {
197 TME_HOVER = 0x00000001,
198 TME_LEAVE = 0x00000002,
199 TME_NONCLIENT = 0x00000010,
200 TME_QUERY = unchecked((int)0x40000000),
201 TME_CANCEL = unchecked((int)0x80000000)
204 [StructLayout(LayoutKind.Sequential)]
205 private struct TRACKMOUSEEVENT {
207 internal TMEFlags dwFlags;
208 internal IntPtr hWnd;
209 internal int dwHoverTime;
212 [StructLayout(LayoutKind.Sequential)]
213 private struct PAINTSTRUCT {
216 internal RECT rcPaint;
217 internal int fRestore;
218 internal int fIncUpdate;
219 internal int Reserved1;
220 internal int Reserved2;
221 internal int Reserved3;
222 internal int Reserved4;
223 internal int Reserved5;
224 internal int Reserved6;
225 internal int Reserved7;
226 internal int Reserved8;
229 [StructLayout(LayoutKind.Sequential)]
230 internal struct KEYBDINPUT {
232 internal short wScan;
233 internal Int32 dwFlags;
235 internal UIntPtr dwExtraInfo;
238 [StructLayout(LayoutKind.Sequential)]
239 internal struct MOUSEINPUT {
242 internal Int32 mouseData;
243 internal Int32 dwFlags;
245 internal UIntPtr dwExtraInfo;
248 [StructLayout(LayoutKind.Sequential)]
249 internal struct HARDWAREINPUT {
251 internal short wParamL;
252 internal short wParamH;
255 [StructLayout(LayoutKind.Explicit)]
256 internal struct INPUT {
261 internal MOUSEINPUT mi;
264 internal KEYBDINPUT ki;
267 internal HARDWAREINPUT hi;
271 internal enum InputFlags {
272 KEYEVENTF_EXTENDEDKEY = 0x0001,
273 KEYEVENTF_KEYUP = 0x0002,
274 KEYEVENTF_SCANCODE = 0x0003,
275 KEYEVENTF_UNICODE = 0x0004,
278 internal enum ClassStyle {
279 CS_VREDRAW = 0x00000001,
280 CS_HREDRAW = 0x00000002,
281 CS_KEYCVTWINDOW = 0x00000004,
282 CS_DBLCLKS = 0x00000008,
283 CS_OWNDC = 0x00000020,
284 CS_CLASSDC = 0x00000040,
285 CS_PARENTDC = 0x00000080,
286 CS_NOKEYCVT = 0x00000100,
287 CS_NOCLOSE = 0x00000200,
288 CS_SAVEBITS = 0x00000800,
289 CS_BYTEALIGNCLIENT = 0x00001000,
290 CS_BYTEALIGNWINDOW = 0x00002000,
291 CS_GLOBALCLASS = 0x00004000,
294 CS_DROPSHADOW = 0x00020000
297 internal enum SetWindowPosZOrder {
305 internal enum SetWindowPosFlags {
306 SWP_ASYNCWINDOWPOS = 0x4000,
307 SWP_DEFERERASE = 0x2000,
308 SWP_DRAWFRAME = 0x0020,
309 SWP_FRAMECHANGED = 0x0020,
310 SWP_HIDEWINDOW = 0x0080,
311 SWP_NOACTIVATE = 0x0010,
312 SWP_NOCOPYBITS = 0x0100,
314 SWP_NOOWNERZORDER = 0x0200,
315 SWP_NOREDRAW = 0x0008,
316 SWP_NOREPOSITION = 0x0200,
317 SWP_NOENDSCHANGING = 0x0400,
319 SWP_NOZORDER = 0x0004,
320 SWP_SHOWWINDOW = 0x0040
323 internal enum GetSysColorIndex {
325 COLOR_BACKGROUND = 1,
326 COLOR_ACTIVECAPTION = 2,
327 COLOR_INACTIVECAPTION = 3,
330 COLOR_WINDOWFRAME = 6,
332 COLOR_WINDOWTEXT = 8,
333 COLOR_CAPTIONTEXT = 9,
334 COLOR_ACTIVEBORDER = 10,
335 COLOR_INACTIVEBORDER = 11,
336 COLOR_APPWORKSPACE = 12,
337 COLOR_HIGHLIGHT = 13,
338 COLOR_HIGHLIGHTTEXT = 14,
340 COLOR_BTNSHADOW = 16,
343 COLOR_INACTIVECAPTIONTEXT = 19,
344 COLOR_BTNHIGHLIGHT = 20,
345 COLOR_3DDKSHADOW = 21,
351 COLOR_GRADIENTACTIVECAPTION = 27,
352 COLOR_GRADIENTINACTIVECAPTION = 28,
353 COLOR_MENUHIGHLIGHT = 29,
359 COLOR_3DHIGHLIGHT = 20,
360 COLOR_3DHILIGHT = 20,
361 COLOR_BTNHILIGHT = 20,
362 COLOR_MAXVALUE = 24,/* Maximum value */
365 private enum LoadCursorType {
374 IDC_SIZENWSE = 32642,
375 IDC_SIZENESW = 32643,
381 IDC_APPSTARTING = 32650,
387 private enum WindowLong {
398 private enum LogBrushStyle {
407 BS_DIBPATTERN8X8 = 8,
412 private enum LogBrushHatch {
413 HS_HORIZONTAL = 0, /* ----- */
414 HS_VERTICAL = 1, /* ||||| */
415 HS_FDIAGONAL = 2, /* \\\\\ */
416 HS_BDIAGONAL = 3, /* ///// */
417 HS_CROSS = 4, /* +++++ */
418 HS_DIAGCROSS = 5, /* xxxxx */
421 internal struct COLORREF {
428 [StructLayout(LayoutKind.Sequential)]
429 private struct LOGBRUSH {
430 internal LogBrushStyle lbStyle;
431 internal COLORREF lbColor;
432 internal LogBrushHatch lbHatch;
435 [StructLayout(LayoutKind.Sequential)]
436 internal struct TEXTMETRIC {
437 internal int tmHeight;
438 internal int tmAscent;
439 internal int tmDescent;
440 internal int tmInternalLeading;
441 internal int tmExternalLeading;
442 internal int tmAveCharWidth;
443 internal int tmMaxCharWidth;
444 internal int tmWeight;
445 internal int tmOverhang;
446 internal int tmDigitizedAspectX;
447 internal int tmDigitizedAspectY;
448 internal short tmFirstChar;
449 internal short tmLastChar;
450 internal short tmDefaultChar;
451 internal short tmBreakChar;
452 internal byte tmItalic;
453 internal byte tmUnderlined;
454 internal byte tmStruckOut;
455 internal byte tmPitchAndFamily;
456 internal byte tmCharSet;
459 public enum TernaryRasterOperations : uint
461 SRCCOPY = 0x00CC0020,
462 SRCPAINT = 0x00EE0086,
464 SRCINVERT = 0x00660046,
465 SRCERASE = 0x00440328,
466 NOTSRCCOPY = 0x00330008,
467 NOTSRCERASE = 0x001100A6,
468 MERGECOPY = 0x00C000CA,
469 MERGEPAINT = 0x00BB0226,
470 PATCOPY = 0x00F00021,
471 PATPAINT = 0x00FB0A09,
472 PATINVERT = 0x005A0049,
473 DSTINVERT = 0x00550009,
474 BLACKNESS = 0x00000042,
475 WHITENESS = 0x00FF0062
479 private enum ScrollWindowExFlags {
481 SW_SCROLLCHILDREN = 0x0001,
482 SW_INVALIDATE = 0x0002,
484 SW_SMOOTHSCROLL = 0x0010
487 internal enum SystemMetrics {
504 SM_CXFULLSCREEN = 16,
505 SM_CYFULLSCREEN = 17,
506 SM_CYKANJIWINDOW = 18,
507 SM_MOUSEPRESENT = 19,
526 SM_CXICONSPACING = 38,
527 SM_CYICONSPACING = 39,
528 SM_MENUDROPALIGNMENT = 40,
531 SM_CMOUSEBUTTONS = 43,
532 SM_CXFIXEDFRAME = SM_CXDLGFRAME,
533 SM_CYFIXEDFRAME = SM_CYDLGFRAME,
534 SM_CXSIZEFRAME = SM_CXFRAME,
535 SM_CYSIZEFRAME = SM_CYFRAME,
539 SM_CXMINSPACING = 47,
540 SM_CYMINSPACING = 48,
563 SM_MIDEASTENABLED = 74,
564 SM_MOUSEWHEELPRESENT = 75,
565 SM_XVIRTUALSCREEN = 76,
566 SM_YVIRTUALSCREEN = 77,
567 SM_CXVIRTUALSCREEN = 78,
568 SM_CYVIRTUALSCREEN = 79,
570 SM_SAMEDISPLAYFORMAT = 81,
572 SM_CXFOCUSBORDER = 83,
573 SM_CYFOCUSBORDER = 84,
579 // We'll only support _WIN32_IE < 0x0500 for now
580 internal enum NotifyIconMessage {
581 NIM_ADD = 0x00000000,
582 NIM_MODIFY = 0x00000001,
583 NIM_DELETE = 0x00000002,
587 internal enum NotifyIconFlags {
588 NIF_MESSAGE = 0x00000001,
589 NIF_ICON = 0x00000002,
590 NIF_TIP = 0x00000004,
591 NIF_STATE = 0x00000008,
592 NIF_INFO = 0x00000010
595 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
596 internal struct NOTIFYICONDATA {
597 internal uint cbSize;
598 internal IntPtr hWnd;
600 internal NotifyIconFlags uFlags;
601 internal uint uCallbackMessage;
602 internal IntPtr hIcon;
603 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
604 internal string szTip;
605 internal int dwState;
606 internal int dwStateMask;
607 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
608 internal string szInfo;
609 internal int uTimeoutOrVersion;
610 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
611 internal string szInfoTitle;
613 internal ToolTipIcon dwInfoFlags;
615 internal int dwInfoFlags;
620 internal enum DCExFlags {
621 DCX_WINDOW = 0x00000001,
622 DCX_CACHE = 0x00000002,
623 DCX_NORESETATTRS = 0x00000004,
624 DCX_CLIPCHILDREN = 0x00000008,
625 DCX_CLIPSIBLINGS = 0x00000010,
626 DCX_PARENTCLIP = 0x00000020,
627 DCX_EXCLUDERGN = 0x00000040,
628 DCX_INTERSECTRGN = 0x00000080,
629 DCX_EXCLUDEUPDATE = 0x00000100,
630 DCX_INTERSECTUPDATE = 0x00000200,
631 DCX_LOCKWINDOWUPDATE = 0x00000400,
632 DCX_USESTYLE = 0x00010000,
633 DCX_VALIDATE = 0x00200000
636 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
637 internal struct CLIENTCREATESTRUCT {
638 internal IntPtr hWindowMenu;
639 internal uint idFirstChild;
642 private enum ClassLong : int {
644 GCL_HBRBACKGROUND = -10,
648 GCL_CBWNDEXTRA = -18,
649 GCL_CBCLSEXTRA = -20,
657 internal enum GAllocFlags : uint {
659 GMEM_MOVEABLE = 0x0002,
660 GMEM_NOCOMPACT = 0x0010,
661 GMEM_NODISCARD = 0x0020,
662 GMEM_ZEROINIT = 0x0040,
663 GMEM_MODIFY = 0x0080,
664 GMEM_DISCARDABLE = 0x0100,
665 GMEM_NOT_BANKED = 0x1000,
667 GMEM_DDESHARE = 0x2000,
668 GMEM_NOTIFY = 0x4000,
669 GMEM_LOWER = GMEM_NOT_BANKED,
670 GMEM_VALID_FLAGS = 0x7F72,
671 GMEM_INVALID_HANDLE = 0x8000,
672 GHND = (GMEM_MOVEABLE | GMEM_ZEROINIT),
673 GPTR = (GMEM_FIXED | GMEM_ZEROINIT)
676 internal enum ROP2DrawMode : int {
696 internal enum PenStyle : int {
708 internal enum PatBltRop : int {
710 PATINVERT = 0x5a0049,
711 DSTINVERT = 0x550009,
712 BLACKNESS = 0x000042,
713 WHITENESS = 0xff0062,
716 internal enum StockObject : int {
723 HOLLOW_BRUSH = NULL_BRUSH,
728 ANSI_FIXED_FONT = 11,
731 DEVICE_DEFAULT_FONT = 14,
732 DEFAULT_PALETTE = 15,
733 SYSTEM_FIXED_FONT = 16
736 internal enum HatchStyle : int {
746 internal enum SndFlags : int {
749 SND_NODEFAULT = 0x0002,
753 SND_NOWAIT = 0x00002000,
754 SND_ALIAS = 0x00010000,
755 SND_ALIAS_ID = 0x00110000,
756 SND_FILENAME = 0x00020000,
757 SND_RESOURCE = 0x00040004,
759 SND_APPLICATION = 0x0080,
763 internal enum LayeredWindowAttributes : int {
771 #region Constructor & Destructor
772 private XplatUIWin32() {
773 // Handle singleton stuff first
776 mouse_state = MouseButtons.None;
777 mouse_position = Point.Empty;
779 message_queue = new Queue();
781 themes_enabled = false;
783 wnd_proc = new WndProc(InternalWndProc);
785 FosterParent=Win32CreateWindow(WindowExStyles.WS_EX_TOOLWINDOW, "static", "Foster Parent Window", WindowStyles.WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
787 if (FosterParent==IntPtr.Zero) {
788 Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
791 scroll_height = Win32GetSystemMetrics(SystemMetrics.SM_CYHSCROLL);
792 scroll_width = Win32GetSystemMetrics(SystemMetrics.SM_CXVSCROLL);
794 timer_list = new Hashtable ();
795 registered_classes = new Hashtable ();
797 #endregion // Constructor & Destructor
799 #region Private Support Methods
801 private string RegisterWindowClass (int classStyle)
805 lock (registered_classes) {
806 class_name = (string)registered_classes[classStyle];
808 if (class_name != null)
811 class_name = string.Format ("Mono.WinForms.{0}.{1}", System.Threading.Thread.GetDomainID ().ToString (), classStyle);
815 wndClass.style = classStyle;
816 wndClass.lpfnWndProc = wnd_proc;
817 wndClass.cbClsExtra = 0;
818 wndClass.cbWndExtra = 0;
819 wndClass.hbrBackground = (IntPtr)(GetSysColorIndex.COLOR_WINDOW + 1);
820 wndClass.hCursor = Win32LoadCursor (IntPtr.Zero, LoadCursorType.IDC_ARROW);
821 wndClass.hIcon = IntPtr.Zero;
822 wndClass.hInstance = IntPtr.Zero;
823 wndClass.lpszClassName = class_name;
824 wndClass.lpszMenuName = "";
826 bool result = Win32RegisterClass (ref wndClass);
829 Win32MessageBox (IntPtr.Zero, "Could not register the window class, win32 error " + Win32GetLastError ().ToString (), "Oops", 0);
831 registered_classes[classStyle] = class_name;
837 private static bool RetrieveMessage(ref MSG msg) {
840 if (message_queue.Count == 0) {
844 message = (MSG)message_queue.Dequeue();
850 private static bool StoreMessage(ref MSG msg) {
851 MSG message = new MSG();
854 message_queue.Enqueue(message);
859 internal static String AnsiToString(IntPtr ansi_data) {
860 return (string)Marshal.PtrToStringAnsi(ansi_data);
863 internal static String UnicodeToString(IntPtr unicode_data) {
864 return (string)Marshal.PtrToStringUni(unicode_data);
867 internal static Image DIBtoImage(IntPtr dib_data) {
868 BITMAPINFOHEADER bmi;
879 bmi = (BITMAPINFOHEADER)Marshal.PtrToStructure(dib_data, typeof(BITMAPINFOHEADER));
881 ncolors = (int)bmi.biClrUsed;
883 if (bmi.biBitCount < 24) {
884 ncolors = (int)(1 << bmi.biBitCount);
887 //palettesize = ncolors * 4;
889 imagesize = (int)bmi.biSizeImage;
890 if (imagesize == 0) {
891 imagesize = (int)(((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);
894 switch(bmi.biBitCount) {
895 case 1: { // Monochrome
896 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format1bppIndexed);
897 palette = new int[2];
902 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format4bppIndexed);
903 palette = new int[16];
908 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format8bppIndexed);
909 palette = new int[256];
915 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format32bppArgb);
916 palette = new int[0];
921 throw new Exception("Unexpected number of bits:" + bmi.biBitCount.ToString());
925 if (bmi.biBitCount < 24) {
926 pal = bmp.Palette; // Managed palette
927 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER))), palette, 0, palette.Length);
929 for (int i = 0; i < ncolors; i++) {
930 pal.Entries[i] = Color.FromArgb(palette[i] | unchecked((int)0xff000000));
935 bytesPerLine = (int)((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3);
936 bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
938 imagebits = new byte[bytesPerLine];
940 for (int y = 0; y < bmi.biHeight; y++) {
941 // Copy from source to managed
942 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER)) + palette.Length * 4 + bytesPerLine * y), imagebits, 0, bytesPerLine);
944 // Copy from managed to dest
945 Marshal.Copy(imagebits, 0, (IntPtr)((int)bits.Scan0 + bits.Stride * (bmi.biHeight - 1 - y)), imagebits.Length);
948 bmp.UnlockBits(bits);
953 internal static byte[] ImageToDIB(Image image) {
958 ms = new MemoryStream();
959 image.Save(ms, ImageFormat.Bmp);
960 buffer = ms.GetBuffer();
962 // Filter out the file header
963 retbuf = new byte[buffer.Length];
964 Array.Copy(buffer, 14, retbuf, 0, buffer.Length - 14);
968 internal static IntPtr DupGlobalMem(IntPtr mem) {
974 len = Win32GlobalSize(mem);
975 mem_ptr = Win32GlobalLock(mem);
977 dup = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE, (int)len);
978 dup_ptr = Win32GlobalLock(dup);
980 Win32CopyMemory(dup_ptr, mem_ptr, (int)len);
982 Win32GlobalUnlock(mem);
983 Win32GlobalUnlock(dup);
987 #endregion // Private Support Methods
989 #region Static Properties
990 internal override void RaiseIdle (EventArgs e)
996 internal override Keys ModifierKeys {
1001 key_state = Keys.None;
1003 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
1004 if ((state & 0x8000) != 0) {
1005 key_state |= Keys.Shift;
1007 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
1008 if ((state & 0x8000) != 0) {
1009 key_state |= Keys.Control;
1012 state = Win32GetKeyState(VirtualKeys.VK_MENU);
1013 if ((state & 0x8000) != 0) {
1014 key_state |= Keys.Alt;
1021 internal override MouseButtons MouseButtons {
1027 internal override Point MousePosition {
1029 return mouse_position;
1033 internal override Size MouseHoverSize {
1038 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref width, 0);
1039 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref height, 0);
1040 return new Size(width, height);
1044 internal override int MouseHoverTime {
1048 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERTIME, 0, ref time, 0);
1053 internal override int MouseWheelScrollDelta {
1056 Win32SystemParametersInfo(SPIAction.SPI_GETWHEELSCROLLLINES, 0, ref delta, 0);
1061 internal override int HorizontalScrollBarHeight {
1063 return scroll_height;
1067 internal override bool UserClipWontExposeParent {
1074 internal override int VerticalScrollBarWidth {
1076 return scroll_width;
1080 internal override int MenuHeight {
1082 return Win32GetSystemMetrics(SystemMetrics.SM_CYMENU);
1086 internal override Size Border3DSize {
1088 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXEDGE),
1089 Win32GetSystemMetrics (SystemMetrics.SM_CYEDGE));
1093 internal override Size BorderSize {
1095 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXBORDER),
1096 Win32GetSystemMetrics (SystemMetrics.SM_CYBORDER));
1100 internal override bool DropTarget {
1107 //throw new NotImplementedException("Need to figure out D'n'D for Win32");
1112 internal override Size CaptionButtonSize {
1114 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXSIZE),
1115 Win32GetSystemMetrics (SystemMetrics.SM_CYSIZE));
1119 internal override int CaptionHeight {
1121 return Win32GetSystemMetrics(SystemMetrics.SM_CYCAPTION);
1125 internal override Size CursorSize {
1127 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CYCURSOR));
1131 internal override bool DragFullWindows {
1134 Win32SystemParametersInfo (SPIAction.SPI_GETDRAGFULLWINDOWS, 0, ref full, 0);
1139 internal override Size DragSize {
1141 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXDRAG), Win32GetSystemMetrics(SystemMetrics.SM_CYDRAG));
1145 internal override Size DoubleClickSize {
1147 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXDOUBLECLK),
1148 Win32GetSystemMetrics (SystemMetrics.SM_CYDOUBLECLK));
1152 internal override int DoubleClickTime {
1154 return Win32GetDoubleClickTime ();
1158 internal override Size FixedFrameBorderSize {
1160 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXFIXEDFRAME),
1161 Win32GetSystemMetrics (SystemMetrics.SM_CYFIXEDFRAME));
1165 internal override Size FrameBorderSize {
1167 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXFRAME), Win32GetSystemMetrics(SystemMetrics.SM_CYFRAME));
1171 internal override Size IconSize {
1173 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXICON), Win32GetSystemMetrics(SystemMetrics.SM_CYICON));
1177 internal override Size MaxWindowTrackSize {
1179 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMAXTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMAXTRACK));
1183 internal override bool MenuAccessKeysUnderlined {
1186 Win32SystemParametersInfo (SPIAction.SPI_GETKEYBOARDCUES, 0, ref underlined, 0);
1187 return (underlined != 0);
1191 internal override Size MinimizedWindowSize {
1193 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINIMIZED), Win32GetSystemMetrics(SystemMetrics.SM_CYMINIMIZED));
1197 internal override Size MinimizedWindowSpacingSize {
1199 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINSPACING), Win32GetSystemMetrics(SystemMetrics.SM_CYMINSPACING));
1203 internal override Size MinimumWindowSize {
1205 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMIN), Win32GetSystemMetrics(SystemMetrics.SM_CYMIN));
1209 internal override Size MinWindowTrackSize {
1211 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMINTRACK));
1215 internal override Size SmallIconSize {
1217 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXSMICON), Win32GetSystemMetrics(SystemMetrics.SM_CYSMICON));
1221 internal override int MouseButtonCount {
1223 return Win32GetSystemMetrics(SystemMetrics.SM_CMOUSEBUTTONS);
1227 internal override bool MouseButtonsSwapped {
1229 return Win32GetSystemMetrics(SystemMetrics.SM_SWAPBUTTON) != 0;
1233 internal override bool MouseWheelPresent {
1235 return Win32GetSystemMetrics(SystemMetrics.SM_MOUSEWHEELPRESENT) != 0;
1239 internal override Rectangle VirtualScreen {
1241 return new Rectangle( Win32GetSystemMetrics(SystemMetrics.SM_XVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_YVIRTUALSCREEN),
1242 Win32GetSystemMetrics(SystemMetrics.SM_CXVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYVIRTUALSCREEN));
1246 internal override Rectangle WorkingArea {
1251 Win32SystemParametersInfo(SPIAction.SPI_GETWORKAREA, 0, ref rect, 0);
1252 return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
1253 //return new Rectangle(0, 0, Win32GetSystemMetrics(SystemMetrics.SM.SM_CXSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYSCREEN));
1257 internal override bool ThemesEnabled {
1259 return XplatUIWin32.themes_enabled;
1264 #endregion // Static Properties
1266 #region Singleton Specific Code
1267 public static XplatUIWin32 GetInstance() {
1268 if (instance==null) {
1269 instance=new XplatUIWin32();
1275 public int Reference {
1282 #region Public Static Methods
1283 internal override IntPtr InitializeDriver() {
1287 internal override void ShutdownDriver(IntPtr token) {
1288 Console.WriteLine("XplatUIWin32 ShutdownDriver called");
1292 internal void Version() {
1293 Console.WriteLine("Xplat version $revision: $");
1296 internal override void AudibleAlert() {
1297 Win32PlaySound("Default", IntPtr.Zero, SndFlags.SND_ALIAS | SndFlags.SND_ASYNC | SndFlags.SND_NOSTOP | SndFlags.SND_NOWAIT);
1300 internal override void GetDisplaySize(out Size size) {
1303 Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
1305 size = new Size(rect.right - rect.left, rect.bottom - rect.top);
1308 internal override void EnableThemes() {
1309 themes_enabled=true;
1312 internal override IntPtr CreateWindow(CreateParams cp) {
1313 IntPtr WindowHandle;
1314 IntPtr ParentHandle;
1319 ParentHandle=cp.Parent;
1321 if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
1322 // We need to use our foster parent window until this poor child gets it's parent assigned
1323 ParentHandle = FosterParent;
1326 if ( ((cp.Style & (int)(WindowStyles.WS_CHILD | WindowStyles.WS_POPUP))==0) && ((cp.ExStyle & (int)WindowExStyles.WS_EX_APPWINDOW) == 0)) {
1327 // If we want to be hidden from the taskbar we need to be 'owned' by
1328 // something not on the taskbar. FosterParent is just that
1329 ParentHandle = FosterParent;
1332 string class_name = RegisterWindowClass (cp.ClassStyle);
1333 HwndCreating = hwnd;
1335 WindowHandle = Win32CreateWindow (cp.WindowExStyle, class_name, cp.Caption, cp.WindowStyle, cp.X, cp.Y, cp.Width, cp.Height, ParentHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
1337 HwndCreating = null;
1339 if (WindowHandle==IntPtr.Zero) {
1340 uint error = Win32GetLastError();
1342 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1345 hwnd.ClientWindow = WindowHandle;
1347 Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1349 return WindowHandle;
1352 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1353 CreateParams create_params = new CreateParams();
1355 create_params.Caption = "";
1356 create_params.X = X;
1357 create_params.Y = Y;
1358 create_params.Width = Width;
1359 create_params.Height = Height;
1361 create_params.ClassName=XplatUI.DefaultClassName;
1362 create_params.ClassStyle = 0;
1363 create_params.ExStyle=0;
1364 create_params.Parent=IntPtr.Zero;
1365 create_params.Param=0;
1367 return CreateWindow(create_params);
1370 internal override void DestroyWindow(IntPtr handle) {
1373 hwnd = Hwnd.ObjectFromHandle(handle);
1374 Win32DestroyWindow(handle);
1379 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1380 // We do nothing, Form has to handle WM_GETMINMAXINFO
1384 internal override FormWindowState GetWindowState(IntPtr handle) {
1387 style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1388 if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1389 return FormWindowState.Maximized;
1390 } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1391 return FormWindowState.Minimized;
1393 return FormWindowState.Normal;
1396 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1398 case FormWindowState.Normal: {
1399 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1403 case FormWindowState.Minimized: {
1404 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1408 case FormWindowState.Maximized: {
1409 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1415 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1417 Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1418 Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1420 if ((cp.ExStyle & (int) WindowExStyles.WS_EX_TOOLWINDOW) > 0 ||
1421 ((cp.WindowStyle & (WindowStyles.WS_CAPTION)) != WindowStyles.WS_CAPTION && cp.control is Form))
1422 XplatUI.RequestNCRecalc (handle);
1425 internal override double GetWindowTransparency(IntPtr handle)
1427 LayeredWindowAttributes lwa;
1431 if (0 == Win32GetLayeredWindowAttributes (handle, out clrRef, out alpha, out lwa))
1434 return ((double)alpha) / 255.0;
1437 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1438 LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1439 byte opacity = (byte)(transparency*255);
1440 COLORREF clrRef = new COLORREF();
1441 if (key != Color.Empty) {
1445 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1450 Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1453 TransparencySupport support;
1454 bool queried_transparency_support;
1455 internal override TransparencySupport SupportsTransparency() {
1456 if (queried_transparency_support)
1460 support = TransparencySupport.None;
1464 Win32SetLayeredWindowAttributes (IntPtr.Zero, new COLORREF (), 255, LayeredWindowAttributes.LWA_ALPHA);
1466 catch (EntryPointNotFoundException) { flag = false; }
1467 catch { /* swallow everything else */ }
1469 if (flag) support |= TransparencySupport.Set;
1473 LayeredWindowAttributes lwa;
1477 Win32GetLayeredWindowAttributes (IntPtr.Zero, out clrRef, out alpha, out lwa);
1479 catch (EntryPointNotFoundException) { flag = false; }
1480 catch { /* swallow everything else */ }
1482 if (flag) support |= TransparencySupport.Get;
1484 queried_transparency_support = true;
1488 internal override void UpdateWindow(IntPtr handle) {
1489 Win32UpdateWindow(handle);
1492 internal override PaintEventArgs PaintEventStart(IntPtr handle, bool client) {
1495 PaintEventArgs paint_event;
1497 Rectangle clip_rect;
1500 clip_rect = new Rectangle();
1502 ps = new PAINTSTRUCT();
1504 hwnd = Hwnd.ObjectFromHandle(handle);
1507 if (Win32GetUpdateRect(handle, ref rect, false)) {
1508 hdc = Win32BeginPaint(handle, ref ps);
1510 hwnd.drawing_stack.Push (ps);
1512 clip_rect = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top);
1514 hdc = Win32GetDC(handle);
1515 // FIXME: Add the DC to internal list
1517 hwnd.drawing_stack.Push (null);
1519 clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
1522 hdc = Win32GetWindowDC (handle);
1524 hwnd.drawing_stack.Push (hdc);
1526 // HACK this in for now
1527 Win32GetWindowRect (handle, out rect);
1528 clip_rect = new Rectangle(0, 0, rect.right-rect.left, rect.bottom-rect.top);
1531 Graphics dc = Graphics.FromHdc(hdc);
1532 hwnd.drawing_stack.Push (dc);
1534 paint_event = new PaintEventArgs(dc, clip_rect);
1539 internal override void PaintEventEnd(IntPtr handle, bool client) {
1542 hwnd = Hwnd.ObjectFromHandle(handle);
1544 Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1548 object o = hwnd.drawing_stack.Pop();
1550 PAINTSTRUCT ps = (PAINTSTRUCT)o;
1551 Win32EndPaint(handle, ref ps);
1554 IntPtr hdc = (IntPtr)hwnd.drawing_stack.Pop();
1555 Win32ReleaseDC(handle, hdc);
1560 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1561 Win32MoveWindow(handle, x, y, width, height, true);
1565 internal override void GetWindowPos(IntPtr handle, bool is_toplevel, out int x, out int y, out int width, out int height, out int client_width, out int client_height) {
1570 Win32GetWindowRect(handle, out rect);
1571 width = rect.right - rect.left;
1572 height = rect.bottom - rect.top;
1577 parent = Win32GetParent(handle);
1578 Win32ScreenToClient(parent, ref pt);
1583 Win32GetClientRect(handle, out rect);
1584 client_width = rect.right - rect.left;
1585 client_height = rect.bottom - rect.top;
1589 internal override void Activate(IntPtr handle) {
1590 Win32SetActiveWindow(handle);
1593 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1598 rect.right=rc.Right;
1599 rect.bottom=rc.Bottom;
1600 Win32InvalidateRect(handle, ref rect, clear);
1604 internal override void InvalidateNC (IntPtr handle)
1606 // found this gem at
1607 // http://www.dotnet247.com/247reference/msgs/58/292037.aspx
1608 Win32SetWindowPos(handle, IntPtr.Zero,
1610 SetWindowPosFlags.SWP_NOMOVE |
1611 SetWindowPosFlags.SWP_NOSIZE |
1612 SetWindowPosFlags.SWP_NOZORDER |
1613 SetWindowPosFlags.SWP_NOACTIVATE |
1614 SetWindowPosFlags.SWP_DRAWFRAME);
1617 private IntPtr InternalWndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
1619 if (HwndCreating != null && HwndCreating.ClientWindow == IntPtr.Zero)
1620 HwndCreating.ClientWindow = hWnd;
1621 return NativeWindow.WndProc (hWnd, msg, wParam, lParam);
1624 internal override IntPtr DefWndProc(ref Message msg) {
1625 msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1629 internal override void HandleException(Exception e) {
1630 StackTrace st = new StackTrace(e);
1631 Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1632 Console.WriteLine("{0}{1}", e.Message, st.ToString());
1635 internal override void DoEvents() {
1636 MSG msg = new MSG();
1638 if (override_cursor != IntPtr.Zero) {
1639 Cursor.Current = null;
1642 while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1643 XplatUI.TranslateMessage(ref msg);
1644 XplatUI.DispatchMessage(ref msg);
1648 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1649 return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1652 internal override void PostQuitMessage(int exitCode) {
1653 Win32PostQuitMessage(exitCode);
1656 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
1658 if (wm_nc_registered == null)
1659 wm_nc_registered = new Hashtable ();
1661 TMEFlags flags = TMEFlags.TME_NONCLIENT;
1663 flags |= TMEFlags.TME_HOVER;
1665 flags |= TMEFlags.TME_LEAVE;
1667 if (flags == TMEFlags.TME_NONCLIENT) {
1668 if (wm_nc_registered.Contains (hwnd)) {
1669 wm_nc_registered.Remove (hwnd);
1672 if (!wm_nc_registered.Contains (hwnd)) {
1673 wm_nc_registered.Add (hwnd, flags);
1675 wm_nc_registered [hwnd] = flags;
1680 internal override void RequestNCRecalc(IntPtr handle) {
1681 Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOACTIVATE);
1684 internal override void ResetMouseHover(IntPtr handle) {
1685 TRACKMOUSEEVENT tme;
1687 tme = new TRACKMOUSEEVENT();
1688 tme.size = Marshal.SizeOf(tme);
1690 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1691 Win32TrackMouseEvent(ref tme);
1695 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1696 return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
1699 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
1703 if (RetrieveMessage(ref msg)) {
1708 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
1710 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
1716 // We need to fake WM_MOUSE_ENTER
1717 switch (msg.message) {
1718 case Msg.WM_LBUTTONDOWN: {
1719 mouse_state |= MouseButtons.Left;
1723 case Msg.WM_MBUTTONDOWN: {
1724 mouse_state |= MouseButtons.Middle;
1728 case Msg.WM_RBUTTONDOWN: {
1729 mouse_state |= MouseButtons.Right;
1733 case Msg.WM_LBUTTONUP: {
1734 mouse_state &= ~MouseButtons.Left;
1738 case Msg.WM_MBUTTONUP: {
1739 mouse_state &= ~MouseButtons.Middle;
1743 case Msg.WM_RBUTTONUP: {
1744 mouse_state &= ~MouseButtons.Right;
1748 case Msg.WM_ASYNC_MESSAGE: {
1749 XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
1753 case Msg.WM_MOUSEMOVE: {
1754 if (msg.hwnd != prev_mouse_hwnd) {
1755 TRACKMOUSEEVENT tme;
1757 // The current message will be sent out next time around
1758 StoreMessage(ref msg);
1760 // This is the message we want to send at this point
1761 msg.message = Msg.WM_MOUSE_ENTER;
1763 prev_mouse_hwnd = msg.hwnd;
1765 tme = new TRACKMOUSEEVENT();
1766 tme.size = Marshal.SizeOf(tme);
1767 tme.hWnd = msg.hwnd;
1768 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1769 Win32TrackMouseEvent(ref tme);
1775 case Msg.WM_NCMOUSEMOVE: {
1776 if (wm_nc_registered == null || !wm_nc_registered.Contains (msg.hwnd))
1779 TRACKMOUSEEVENT tme;
1781 tme = new TRACKMOUSEEVENT ();
1782 tme.size = Marshal.SizeOf(tme);
1783 tme.hWnd = msg.hwnd;
1784 tme.dwFlags = (TMEFlags)wm_nc_registered[msg.hwnd];
1785 Win32TrackMouseEvent (ref tme);
1789 case Msg.WM_DROPFILES: {
1790 return Win32DnD.HandleWMDropFiles(ref msg);
1793 case Msg.WM_MOUSELEAVE: {
1794 prev_mouse_hwnd = IntPtr.Zero;
1798 case Msg.WM_TIMER: {
1799 Timer timer=(Timer)timer_list[(int)msg.wParam];
1801 if (timer != null) {
1811 internal override bool TranslateMessage(ref MSG msg) {
1812 return Win32TranslateMessage(ref msg);
1815 internal override IntPtr DispatchMessage(ref MSG msg) {
1816 return Win32DispatchMessage(ref msg);
1819 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
1821 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1823 } else if (!Bottom) {
1824 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1826 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1832 internal override bool SetTopmost(IntPtr hWnd, bool Enabled) {
1834 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1837 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1842 internal override bool SetOwner(IntPtr hWnd, IntPtr hWndOwner) {
1843 Win32SetWindowLong(hWnd, WindowLong.GWL_HWNDPARENT, (uint) hWndOwner);
1847 internal override bool Text(IntPtr handle, string text) {
1848 Win32SetWindowText(handle, text);
1852 internal override bool GetText(IntPtr handle, out string text) {
1855 sb = new StringBuilder(256);
1856 Win32GetWindowText(handle, sb, sb.Capacity);
1857 text = sb.ToString();
1861 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
1864 Control c = Control.FromHandle (handle);
1868 f = (Form)Control.FromHandle (handle);
1869 WindowPlacementFlags flags = WindowPlacementFlags.SW_SHOWNORMAL;
1870 switch (f.WindowState) {
1871 case FormWindowState.Normal: flags = WindowPlacementFlags.SW_SHOWNORMAL; break;
1872 case FormWindowState.Minimized: flags = WindowPlacementFlags.SW_MINIMIZE; break;
1873 case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
1876 if (!f.ActivateOnShow)
1877 flags = WindowPlacementFlags.SW_SHOWNOACTIVATE;
1879 Win32ShowWindow (handle, flags);
1882 if (c.ActivateOnShow)
1883 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
1885 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
1889 Win32ShowWindow (handle, WindowPlacementFlags.SW_HIDE);
1894 internal override bool IsEnabled(IntPtr handle) {
1895 return IsWindowEnabled (handle);
1898 internal override bool IsVisible(IntPtr handle) {
1899 return IsWindowVisible (handle);
1902 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
1903 Control c = Control.FromHandle (handle);
1904 if (parent == IntPtr.Zero) {
1906 Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
1910 SetVisible (handle, c.is_visible, true);
1912 // The Win32SetParent is lame, it can very well move the window
1913 // ref: http://groups.google.com/group/microsoft.public.vb.winapi/browse_thread/thread/1b82ccc54231ecee/afa82835bfc0422a%23afa82835bfc0422a
1914 // Here we save the position before changing the parent, and if it has changed afterwards restore it.
1915 // Another possibility would be to intercept WM_WINDOWPOSCHANGING and restore the coords there, but this would require plumbing in weird places
1916 // (either inside Control or add handling to InternalWndProc)
1917 // We also need to remove WS_CHILD if making the window parent-less, and add it if we're parenting it.
1920 WindowStyles style, new_style;
1922 Win32GetWindowRect (handle, out rect);
1923 style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
1925 if (parent == IntPtr.Zero) {
1926 new_style = style & ~WindowStyles.WS_CHILD;
1927 result = Win32SetParent (handle, FosterParent);
1929 new_style = style | WindowStyles.WS_CHILD;
1930 result = Win32SetParent (handle, parent);
1932 if (style != new_style && c is Form) {
1933 Win32SetWindowLong (handle, WindowLong.GWL_STYLE, (uint) new_style);
1935 Win32GetWindowRect (handle, out rect2);
1936 if (rect.top != rect2.top && rect.left != rect2.left && c is Form) {
1937 Win32SetWindowPos (handle, IntPtr.Zero, rect.top, rect.left, rect.Width, rect.Height, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOREDRAW | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOENDSCHANGING | SetWindowPosFlags.SWP_NOACTIVATE);
1942 // If we ever start using this, we should probably replace FosterParent with IntPtr.Zero
1943 internal override IntPtr GetParent(IntPtr handle) {
1944 return Win32GetParent(handle);
1947 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
1949 Win32SetCapture(hWnd);
1951 if (ConfineToHwnd != IntPtr.Zero) {
1953 Win32GetWindowRect (ConfineToHwnd, out window_rect);
1954 Win32GetClipCursor (out clipped_cursor_rect);
1955 Win32ClipCursor (ref window_rect);
1959 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
1961 GrabConfined = grab_confined;
1962 GrabArea = grab_area;
1965 internal override void UngrabWindow(IntPtr hWnd) {
1966 if (!(clipped_cursor_rect.top == 0 && clipped_cursor_rect.bottom == 0 && clipped_cursor_rect.left == 0 && clipped_cursor_rect.right == 0)) {
1967 Win32ClipCursor (ref clipped_cursor_rect);
1968 clipped_cursor_rect = new RECT ();
1971 Win32ReleaseCapture();
1972 grab_hwnd = IntPtr.Zero;
1975 internal override bool CalculateWindowRect(ref Rectangle ClientRect, CreateParams cp, Menu menu, out Rectangle WindowRect) {
1978 rect.left=ClientRect.Left;
1979 rect.top=ClientRect.Top;
1980 rect.right=ClientRect.Right;
1981 rect.bottom=ClientRect.Bottom;
1983 if (!Win32AdjustWindowRectEx(ref rect, cp.Style, menu != null, cp.ExStyle)) {
1984 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
1988 WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
1992 internal override void SetCursor(IntPtr window, IntPtr cursor) {
1993 Win32SetCursor(cursor);
1997 internal override void ShowCursor(bool show) {
1998 Win32ShowCursor(show);
2001 internal override void OverrideCursor(IntPtr cursor) {
2002 Win32SetCursor(cursor);
2005 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
2007 Bitmap cursor_bitmap;
2015 // Win32 only allows creation cursors of a certain size
2016 if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
2017 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
2018 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
2020 cursor_bitmap = bitmap;
2024 width = cursor_bitmap.Width;
2025 height = cursor_bitmap.Height;
2027 cursor_bits = new Byte[(width / 8) * height];
2028 mask_bits = new Byte[(width / 8) * height];
2030 for (int y = 0; y < height; y++) {
2031 for (int x = 0; x < width; x++) {
2032 pixel = cursor_bitmap.GetPixel(x, y);
2034 if (pixel == cursor_pixel) {
2035 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
2038 pixel = cursor_mask.GetPixel(x, y);
2040 if (pixel == mask_pixel) {
2041 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
2046 cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
2051 internal override Bitmap DefineStdCursorBitmap (StdCursor id)
2053 // We load the cursor, create a bitmap, draw the cursor onto the bitmap and return the bitmap.
2054 IntPtr cursor = DefineStdCursor (id);
2055 // Windows only have one possible cursor size!
2056 int width = Win32GetSystemMetrics (SystemMetrics.SM_CXCURSOR);
2057 int height = Win32GetSystemMetrics (SystemMetrics.SM_CYCURSOR);
2058 Bitmap bmp = new Bitmap (width, height);
2059 Graphics gc = Graphics.FromImage (bmp);
2060 IntPtr hdc = gc.GetHdc ();
2061 Win32DrawIcon (hdc, 0, 0, cursor);
2062 gc.ReleaseHdc (hdc);
2067 [MonoTODO("Define the missing cursors")]
2068 internal override IntPtr DefineStdCursor(StdCursor id) {
2070 case StdCursor.AppStarting: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
2071 case StdCursor.Arrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2072 case StdCursor.Cross: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
2073 case StdCursor.Default: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2074 case StdCursor.Hand: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
2075 case StdCursor.Help: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
2076 case StdCursor.HSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2077 case StdCursor.IBeam: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
2078 case StdCursor.No: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
2079 case StdCursor.NoMove2D: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2080 case StdCursor.NoMoveHoriz: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2081 case StdCursor.NoMoveVert: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2082 case StdCursor.PanEast: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2083 case StdCursor.PanNE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2084 case StdCursor.PanNorth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2085 case StdCursor.PanNW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2086 case StdCursor.PanSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2087 case StdCursor.PanSouth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2088 case StdCursor.PanSW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2089 case StdCursor.PanWest: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2090 case StdCursor.SizeAll: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
2091 case StdCursor.SizeNESW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
2092 case StdCursor.SizeNS: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
2093 case StdCursor.SizeNWSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
2094 case StdCursor.SizeWE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
2095 case StdCursor.UpArrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
2096 case StdCursor.VSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2097 case StdCursor.WaitCursor: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
2099 throw new NotImplementedException ();
2102 internal override void DestroyCursor(IntPtr cursor) {
2103 if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
2104 Win32DestroyCursor(cursor);
2109 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
2116 internal override void SetCursorPos(IntPtr handle, int x, int y) {
2117 Win32SetCursorPos(x, y);
2120 internal override Region GetClipRegion(IntPtr hwnd) {
2123 region = new Region();
2125 Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
2130 internal override void SetClipRegion(IntPtr hwnd, Region region) {
2131 Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
2134 internal override void EnableWindow(IntPtr handle, bool Enable) {
2135 Win32EnableWindow(handle, Enable);
2138 internal override void EndLoop(System.Threading.Thread thread) {
2142 internal override object StartLoop(System.Threading.Thread thread) {
2146 internal override void SetModal(IntPtr handle, bool Modal) {
2147 // we do nothing on Win32
2150 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
2153 Win32GetCursorPos(out pt);
2155 if (handle!=IntPtr.Zero) {
2156 Win32ScreenToClient(handle, ref pt);
2163 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
2165 POINT pnt = new POINT();
2169 Win32ScreenToClient (handle, ref pnt);
2175 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {
2176 POINT pnt = new POINT();
2181 Win32ClientToScreen(handle, ref pnt);
2187 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
2190 Win32GetWindowRect(handle, out rect);
2191 x -= rect.left + SystemInformation.FrameBorderSize.Width;
2192 y -= rect.top + SystemInformation.FrameBorderSize.Height;
2194 WindowStyles style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
2195 if (CreateParams.IsSet (style, WindowStyles.WS_CAPTION)) {
2196 y -= ThemeEngine.Current.CaptionHeight;
2200 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {
2203 Win32GetWindowRect(handle, out rect);
2204 x += rect.left + SystemInformation.FrameBorderSize.Width;
2205 y += rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
2209 internal override void SendAsyncMethod (AsyncMethodData method)
2211 Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
2214 internal override void SetTimer (Timer timer)
2218 index = timer.GetHashCode();
2221 timer_list[index]=timer;
2224 Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero);
2227 internal override void KillTimer (Timer timer)
2231 index = timer.GetHashCode();
2233 Win32KillTimer(FosterParent, index);
2236 timer_list.Remove(index);
2240 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
2241 Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
2242 caret_visible = false;
2245 internal override void DestroyCaret(IntPtr hwnd) {
2246 Win32DestroyCaret();
2249 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
2250 Win32SetCaretPos(x, y);
2253 internal override void CaretVisible(IntPtr hwnd, bool visible) {
2255 if (!caret_visible) {
2256 Win32ShowCaret(hwnd);
2257 caret_visible = true;
2260 if (caret_visible) {
2261 Win32HideCaret(hwnd);
2262 caret_visible = false;
2267 internal override IntPtr GetFocus() {
2268 return Win32GetFocus();
2271 internal override void SetFocus(IntPtr hwnd) {
2272 Win32SetFocus(hwnd);
2275 internal override IntPtr GetActive() {
2276 return Win32GetActiveWindow();
2279 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
2283 tm = new TEXTMETRIC();
2285 dc = Win32GetDC(IntPtr.Zero);
2286 Win32SelectObject(dc, font.ToHfont());
2287 if (Win32GetTextMetrics(dc, ref tm) == false) {
2288 Win32ReleaseDC(IntPtr.Zero, dc);
2293 Win32ReleaseDC(IntPtr.Zero, dc);
2295 ascent = tm.tmAscent;
2296 descent = tm.tmDescent;
2301 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
2305 rect.left = rectangle.X;
2306 rect.top = rectangle.Y;
2307 rect.right = rectangle.Right;
2308 rect.bottom = rectangle.Bottom;
2310 Win32ScrollWindowEx(hwnd, XAmount, YAmount, IntPtr.Zero, ref rect, IntPtr.Zero, IntPtr.Zero, ScrollWindowExFlags.SW_INVALIDATE | ScrollWindowExFlags.SW_ERASE | (with_children ? ScrollWindowExFlags.SW_SCROLLCHILDREN : ScrollWindowExFlags.SW_NONE));
2311 Win32UpdateWindow(hwnd);
2314 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
2315 Win32ScrollWindowEx(hwnd, XAmount, YAmount, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ScrollWindowExFlags.SW_INVALIDATE | ScrollWindowExFlags.SW_ERASE | (with_children ? ScrollWindowExFlags.SW_SCROLLCHILDREN : ScrollWindowExFlags.SW_NONE));
2318 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
2321 nid = new NOTIFYICONDATA();
2323 nid.cbSize = (uint)Marshal.SizeOf(nid);
2326 nid.uCallbackMessage = (uint)Msg.WM_USER;
2327 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2331 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2335 nid.hIcon = icon.Handle;
2336 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2341 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
2344 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
2347 nid = new NOTIFYICONDATA();
2349 nid.cbSize = (uint)Marshal.SizeOf(nid);
2350 nid.hIcon = icon.Handle;
2353 nid.uCallbackMessage = (uint)Msg.WM_USER;
2354 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2358 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2362 nid.hIcon = icon.Handle;
2363 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2366 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2369 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
2372 nid = new NOTIFYICONDATA();
2374 nid.cbSize = (uint)Marshal.SizeOf(nid);
2379 Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
2383 internal override void SystrayBalloon(IntPtr hwnd, int timeout, string title, string text, ToolTipIcon icon)
2387 nid = new NOTIFYICONDATA();
2389 nid.cbSize = (uint)Marshal.SizeOf(nid);
2392 nid.uFlags = NotifyIconFlags.NIF_INFO;
2393 nid.uTimeoutOrVersion = timeout;
2394 nid.szInfoTitle = title;
2396 nid.dwInfoFlags = icon;
2398 Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2402 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
2403 // Nothing to do on Win32
2406 internal override void SetMenu(IntPtr handle, Menu menu) {
2407 // Trigger WM_NCCALC
2408 Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2411 internal override Point GetMenuOrigin(IntPtr handle) {
2412 Form form = Control.FromHandle (handle) as Form;
2414 Hwnd.Borders borders = Hwnd.GetBorders (form.GetCreateParams (), null);
2415 return new Point(borders.left, borders.top);
2417 return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2420 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2421 Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : icon.Handle); // 1 = large icon (0 would be small)
2424 internal override void ClipboardClose(IntPtr handle) {
2425 if (handle != clip_magic) {
2426 throw new ArgumentException("handle is not a valid clipboard handle");
2428 Win32CloseClipboard();
2431 internal override int ClipboardGetID(IntPtr handle, string format) {
2432 if (handle != clip_magic) {
2433 throw new ArgumentException("handle is not a valid clipboard handle");
2435 if (format == "Text" ) return 1;
2436 else if (format == "Bitmap" ) return 2;
2437 else if (format == "MetaFilePict" ) return 3;
2438 else if (format == "SymbolicLink" ) return 4;
2439 else if (format == "DataInterchangeFormat" ) return 5;
2440 else if (format == "Tiff" ) return 6;
2441 else if (format == "OEMText" ) return 7;
2442 else if (format == "DeviceIndependentBitmap" ) return 8;
2443 else if (format == "Palette" ) return 9;
2444 else if (format == "PenData" ) return 10;
2445 else if (format == "RiffAudio" ) return 11;
2446 else if (format == "WaveAudio" ) return 12;
2447 else if (format == "UnicodeText" ) return 13;
2448 else if (format == "EnhancedMetafile" ) return 14;
2449 else if (format == "FileDrop" ) return 15;
2450 else if (format == "Locale" ) return 16;
2452 return (int)Win32RegisterClipboardFormat(format);
2455 internal override IntPtr ClipboardOpen(bool primary_selection) {
2456 // Win32 does not have primary selection
2457 Win32OpenClipboard(FosterParent);
2461 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2466 if (handle != clip_magic) {
2474 format = Win32EnumClipboardFormats(format);
2478 } while (format != 0);
2481 result = new int[count];
2485 format = Win32EnumClipboardFormats(format);
2487 result[count++] = (int)format;
2489 } while (format != 0);
2495 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2500 if (handle != clip_magic) {
2501 throw new ArgumentException("handle is not a valid clipboard handle");
2504 hmem = Win32GetClipboardData((uint)type);
2505 if (hmem == IntPtr.Zero) {
2509 data = Win32GlobalLock(hmem);
2510 if (data == IntPtr.Zero) {
2511 uint error = Win32GetLastError();
2512 Console.WriteLine("Error: {0}", error);
2518 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2519 obj = AnsiToString(data);
2520 } else switch ((ClipboardFormats)type) {
2521 case ClipboardFormats.CF_TEXT: {
2522 obj = AnsiToString(data);
2526 case ClipboardFormats.CF_DIB: {
2527 obj = DIBtoImage(data);
2531 case ClipboardFormats.CF_UNICODETEXT: {
2532 obj = UnicodeToString(data);
2537 if (converter != null && !converter(type, data, out obj)) {
2543 Win32GlobalUnlock(hmem);
2549 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
2554 if (handle != clip_magic) {
2555 throw new ArgumentException("handle is not a valid clipboard handle");
2560 Win32EmptyClipboard();
2565 if (obj is string) {
2566 type = (int)ClipboardFormats.CF_UNICODETEXT;
2567 } else if (obj is Image) {
2568 type = (int)ClipboardFormats.CF_DIB;
2572 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2573 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2574 Win32SetClipboardData((uint)type, hmem);
2576 } else switch((ClipboardFormats)type) {
2577 case ClipboardFormats.CF_UNICODETEXT: {
2578 hmem = Marshal.StringToHGlobalUni((string)obj);
2579 Win32SetClipboardData((uint)type, hmem);
2583 case ClipboardFormats.CF_TEXT: {
2584 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2585 Win32SetClipboardData((uint)type, hmem);
2589 case ClipboardFormats.CF_BITMAP:
2590 case ClipboardFormats.CF_DIB: {
2591 data = ImageToDIB((Image)obj);
2593 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2594 hmem_ptr = Win32GlobalLock(hmem);
2595 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2596 Win32GlobalUnlock(hmem);
2597 Win32SetClipboardData((uint)ClipboardFormats.CF_DIB, hmem);
2602 if (converter != null && converter(ref type, obj, out data)) {
2603 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2604 hmem_ptr = Win32GlobalLock(hmem);
2605 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2606 Win32GlobalUnlock(hmem);
2607 Win32SetClipboardData((uint)type, hmem);
2614 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
2616 Win32DnD.RegisterDropTarget(hwnd);
2618 Win32DnD.UnregisterDropTarget(hwnd);
2622 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
2623 return Win32DnD.StartDrag(hwnd, data, allowedEffects);
2626 // XXX this doesn't work at all for FrameStyle.Dashed - it draws like Thick, and in the Thick case
2627 // the corners are drawn incorrectly.
2628 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
2632 COLORREF clrRef = new COLORREF();
2634 // If we want the standard hatch pattern we would
2635 // need to create a brush
2637 clrRef.R = backColor.R;
2638 clrRef.G = backColor.G;
2639 clrRef.B = backColor.B;
2642 pen = Win32CreatePen (style == FrameStyle.Thick ? PenStyle.PS_SOLID : PenStyle.PS_DASH,
2643 style == FrameStyle.Thick ? 4 : 2, ref clrRef);
2645 hdc = Win32GetDC(IntPtr.Zero);
2646 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2647 oldpen = Win32SelectObject(hdc, pen);
2649 Win32MoveToEx(hdc, rectangle.Left, rectangle.Top, IntPtr.Zero);
2650 if ((rectangle.Width > 0) && (rectangle.Height > 0)) {
2651 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2652 Win32LineTo(hdc, rectangle.Right, rectangle.Bottom);
2653 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2654 Win32LineTo(hdc, rectangle.Left, rectangle.Top);
2656 if (rectangle.Width > 0) {
2657 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2659 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2663 Win32SelectObject(hdc, oldpen);
2664 Win32DeleteObject(pen);
2666 Win32ReleaseDC(IntPtr.Zero, hdc);
2669 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
2674 COLORREF clrRef = new COLORREF();
2679 Win32ClientToScreen(IntPtr.Zero, ref pt);
2681 // If we want the standard hatch pattern we would
2682 // need to create a brush
2684 clrRef.R = backColor.R;
2685 clrRef.G = backColor.G;
2686 clrRef.B = backColor.B;
2689 pen = Win32CreatePen(PenStyle.PS_SOLID, 1, ref clrRef);
2691 hdc = Win32GetDC(IntPtr.Zero);
2692 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2693 oldpen = Win32SelectObject(hdc, pen);
2695 Win32MoveToEx(hdc, pt.x + start.X, pt.y + start.Y, IntPtr.Zero);
2696 Win32LineTo(hdc, pt.x + end.X, pt.y + end.Y);
2698 Win32SelectObject(hdc, oldpen);
2699 Win32DeleteObject(pen);
2701 Win32ReleaseDC(IntPtr.Zero, hdc);
2704 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
2709 rect.left = rectangle.Left;
2710 rect.top = rectangle.Top;
2711 rect.right = rectangle.Right;
2712 rect.bottom = rectangle.Bottom;
2717 COLORREF clrRef = new COLORREF();
2719 clrRef.R = backColor.R;
2720 clrRef.G = backColor.G;
2721 clrRef.B = backColor.B;
2724 brush = Win32CreateSolidBrush (clrRef);
2726 hdc = Win32GetDC(IntPtr.Zero);
2727 oldbrush = Win32SelectObject(hdc, brush);
2729 Win32PatBlt (hdc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, PatBltRop.DSTINVERT);
2731 Win32SelectObject(hdc, oldbrush);
2732 Win32DeleteObject(brush);
2734 Win32ReleaseDC(IntPtr.Zero, hdc);
2737 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
2746 Win32ClientToScreen(handle, ref pt);
2748 // If we want the standard hatch pattern we would
2749 // need to create a brush
2752 pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
2754 hdc = Win32GetDC(IntPtr.Zero);
2755 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2756 oldpen = Win32SelectObject(hdc, pen);
2758 Control c = Control.FromHandle (handle);
2761 Win32GetWindowRect (c.Handle, out window_rect);
2762 Region r = new Region (new Rectangle(window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top));
2763 Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
2766 Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
2767 if ((rect.Width > 0) && (rect.Height > 0)) {
2768 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2769 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
2770 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2771 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
2773 if (rect.Width > 0) {
2774 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2776 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2780 Win32SelectObject(hdc, oldpen);
2781 Win32DeleteObject(pen);
2783 Win32ExtSelectClipRgn(hdc, IntPtr.Zero, (int) ClipCombineMode.RGN_COPY);
2785 Win32ReleaseDC(IntPtr.Zero, hdc);
2788 internal override SizeF GetAutoScaleSize(Font font) {
2791 string magic_string = "The quick brown fox jumped over the lazy dog.";
2792 double magic_number = 44.549996948242189;
2794 g = Graphics.FromHwnd(FosterParent);
2796 width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
2797 return new SizeF(width, font.Height);
2800 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2801 return Win32SendMessage(hwnd, message, wParam, lParam);
2804 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2805 return Win32PostMessage(hwnd, message, wParam, lParam);
2808 internal override int SendInput (IntPtr hwnd, Queue keys) {
2809 INPUT[] inputs = new INPUT[keys.Count];
2810 const Int32 INPUT_KEYBOARD = 1;
2813 while (keys.Count > 0) {
2814 MSG msg = (MSG)keys.Dequeue();
2817 inputs[i].ki.wScan = 0;
2818 inputs[i].ki.time = 0;
2819 inputs[i].ki.dwFlags = (Int32)(msg.message == Msg.WM_KEYUP ? InputFlags.KEYEVENTF_KEYUP : 0);
2820 inputs[i].ki.wVk = (short)msg.wParam.ToInt32();
2821 inputs[i].type = INPUT_KEYBOARD;
2824 returns = Win32SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
2826 return (int) returns;
2829 internal override int KeyboardSpeed {
2832 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDSPEED, 0, ref speed, 0);
2834 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
2840 internal override int KeyboardDelay {
2843 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDDELAY, 0, ref delay, 0);
2845 // Return values must range from 0 to 4, 0 meaning 250ms,
2846 // and 4 meaning 1000 ms.
2852 private class WinBuffer
2855 public IntPtr bitmap;
2857 public WinBuffer (IntPtr hdc, IntPtr bitmap)
2860 this.bitmap = bitmap;
2864 internal override void CreateOffscreenDrawable (IntPtr handle, int width, int height, out object offscreen_drawable)
2866 Graphics destG = Graphics.FromHwnd (handle);
2867 IntPtr destHdc = destG.GetHdc ();
2869 IntPtr srcHdc = Win32CreateCompatibleDC (destHdc);
2870 IntPtr srcBmp = Win32CreateCompatibleBitmap (destHdc, width, height);
2871 Win32SelectObject (srcHdc, srcBmp);
2873 offscreen_drawable = new WinBuffer (srcHdc, srcBmp);
2875 destG.ReleaseHdc (destHdc);
2878 internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
2880 return Graphics.FromHdc (((WinBuffer)offscreen_drawable).hdc);
2883 internal override void BlitFromOffscreen (IntPtr dest_handle, Graphics dest_dc, object offscreen_drawable, Graphics offscreen_dc, Rectangle r)
2885 WinBuffer wb = (WinBuffer)offscreen_drawable;
2887 IntPtr destHdc = dest_dc.GetHdc ();
2888 Win32BitBlt (destHdc, r.Left, r.Top, r.Width, r.Height, wb.hdc, r.Left, r.Top, TernaryRasterOperations.SRCCOPY);
2889 dest_dc.ReleaseHdc (destHdc);
2892 internal override void DestroyOffscreenDrawable (object offscreen_drawable)
2894 WinBuffer wb = (WinBuffer)offscreen_drawable;
2896 Win32DeleteObject (wb.bitmap);
2897 Win32DeleteDC (wb.hdc);
2900 internal override event EventHandler Idle;
2901 #endregion // Public Static Methods
2903 #region Win32 Imports
2904 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
2905 private extern static uint Win32GetLastError();
2907 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2908 internal extern static IntPtr Win32CreateWindow(WindowExStyles dwExStyle, string lpClassName, string lpWindowName, WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lParam);
2910 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
2911 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
2913 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2914 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
2916 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2917 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
2919 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
2920 internal extern static bool Win32TranslateMessage(ref MSG msg);
2922 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2923 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
2925 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
2926 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
2928 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2929 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2931 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2932 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2934 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2935 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
2937 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2938 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
2940 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
2941 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
2943 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2944 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
2946 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2947 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
2949 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
2950 private extern static IntPtr Win32ShowCursor(bool bShow);
2952 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
2953 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
2955 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
2956 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
2958 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
2959 private extern static bool Win32DestroyCursor(IntPtr hCursor);
2961 [DllImport ("user32.dll", EntryPoint = "DrawIcon", CallingConvention = CallingConvention.StdCall)]
2962 private extern static bool Win32DrawIcon (IntPtr hDC, int X, int Y, IntPtr hIcon);
2964 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2965 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2967 //[DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2968 //private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2970 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
2971 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
2973 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
2974 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
2976 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
2977 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
2979 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
2980 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2982 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
2983 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2985 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
2986 private extern static IntPtr Win32GetDC(IntPtr hWnd);
2988 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
2989 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
2991 //[DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
2992 //private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
2994 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
2995 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
2997 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2998 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
3000 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
3001 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
3003 //[DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
3004 //private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
3006 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
3007 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
3009 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
3010 private extern static IntPtr Win32ReleaseCapture();
3012 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
3013 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
3015 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
3016 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
3018 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
3019 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
3021 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
3022 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
3024 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
3025 private extern static IntPtr Win32GetParent(IntPtr hWnd);
3027 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3028 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
3030 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
3031 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
3033 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
3034 private extern static bool Win32GetCursorPos(out POINT lpPoint);
3036 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
3037 private extern static bool Win32SetCursorPos(int x, int y);
3039 //[DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
3040 //private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
3042 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
3043 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
3045 //[DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
3046 //private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
3048 [DllImport ("gdi32.dll", EntryPoint="CreateSolidBrush", CallingConvention=CallingConvention.StdCall)]
3049 private extern static IntPtr Win32CreateSolidBrush(COLORREF clrRef);
3051 [DllImport ("gdi32.dll", EntryPoint="PatBlt", CallingConvention=CallingConvention.StdCall)]
3052 private extern static int Win32PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, PatBltRop dwRop);
3054 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
3055 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
3057 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
3058 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
3060 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
3061 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
3063 [DllImport ("user32.dll", EntryPoint="GetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
3064 private extern static uint Win32GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out LayeredWindowAttributes pwdFlags);
3066 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
3067 private extern static bool Win32DeleteObject(IntPtr o);
3069 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
3070 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
3072 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
3073 private extern static IntPtr Win32GetDesktopWindow();
3075 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
3076 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
3078 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
3079 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
3081 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
3082 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
3084 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
3085 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
3087 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
3088 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
3090 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
3091 internal extern static IntPtr Win32GetFocus();
3093 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
3094 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
3096 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
3097 private extern static bool Win32DestroyCaret();
3099 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
3100 private extern static bool Win32ShowCaret(IntPtr hwnd);
3102 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
3103 private extern static bool Win32HideCaret(IntPtr hwnd);
3105 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
3106 private extern static bool Win32SetCaretPos(int X, int Y);
3108 //[DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
3109 //private extern static uint Win32GetCaretBlinkTime();
3111 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3112 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
3114 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
3115 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
3117 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3118 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3120 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3121 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3123 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3124 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3126 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3127 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3129 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3130 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3132 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3133 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3135 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3136 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3138 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3139 private extern static IntPtr Win32GetActiveWindow();
3141 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
3142 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
3144 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3145 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
3147 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
3148 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
3150 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
3151 private extern static bool IsWindowEnabled(IntPtr hwnd);
3153 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
3154 private extern static bool IsWindowVisible(IntPtr hwnd);
3156 //[DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
3157 //private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
3159 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3160 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3162 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3163 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3165 [DllImport ("user32.dll", EntryPoint="SendInput", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3166 private extern static UInt32 Win32SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray)] INPUT[] inputs, Int32 cbSize);
3168 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3169 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
3171 //[DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3172 //private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
3174 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3175 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
3177 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
3178 private extern static bool Win32OpenClipboard(IntPtr hwnd);
3180 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
3181 private extern static bool Win32EmptyClipboard();
3183 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3184 private extern static uint Win32RegisterClipboardFormat(string format);
3186 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
3187 private extern static bool Win32CloseClipboard();
3189 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
3190 private extern static uint Win32EnumClipboardFormats(uint format);
3192 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
3193 private extern static IntPtr Win32GetClipboardData(uint format);
3195 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
3196 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
3198 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
3199 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
3201 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
3202 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
3204 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
3205 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
3207 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
3208 internal extern static uint Win32GlobalSize(IntPtr hMem);
3210 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
3211 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
3213 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
3214 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
3216 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
3217 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
3219 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3220 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
3222 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3223 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
3225 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
3226 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
3228 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3229 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
3231 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3232 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
3234 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
3235 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
3237 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3238 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
3240 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3241 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
3243 [DllImport("gdi32.dll", EntryPoint = "ExcludeClipRect", CallingConvention = CallingConvention.StdCall)]
3244 internal extern static int Win32ExcludeClipRect (IntPtr hdc, int left, int top, int right, int bottom);
3246 [DllImport ("gdi32.dll", EntryPoint="ExtSelectClipRgn", CallingConvention=CallingConvention.StdCall)]
3247 internal extern static int Win32ExtSelectClipRgn(IntPtr hdc, IntPtr hrgn, int mode);
3249 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3250 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
3252 [DllImport ("user32.dll", EntryPoint="GetDoubleClickTime", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3253 private extern static int Win32GetDoubleClickTime ();
3255 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3256 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
3258 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3259 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
3261 [DllImport ("user32.dll", EntryPoint="ClipCursor", CallingConvention=CallingConvention.StdCall)]
3262 internal extern static bool Win32ClipCursor (ref RECT lpRect);
3264 [DllImport ("user32.dll", EntryPoint="GetClipCursor", CallingConvention=CallingConvention.StdCall)]
3265 internal extern static bool Win32GetClipCursor (out RECT lpRect);
3267 [DllImport ("gdi32.dll", EntryPoint="BitBlt", CallingConvention=CallingConvention.StdCall)]
3268 internal static extern bool Win32BitBlt (IntPtr hObject, int nXDest, int nYDest, int nWidth,
3269 int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
3271 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3272 internal static extern IntPtr Win32CreateCompatibleDC (IntPtr hdc);
3274 [DllImport ("gdi32.dll", EntryPoint="DeleteDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3275 internal static extern bool Win32DeleteDC (IntPtr hdc);
3277 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleBitmap", CallingConvention=CallingConvention.StdCall)]
3278 internal static extern IntPtr Win32CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight);