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_GETKEYBOARDSPEED = 0x000A,
149 SPI_GETKEYBOARDDELAY = 0x0016,
150 SPI_GETWORKAREA = 0x0030,
151 SPI_GETMOUSEHOVERWIDTH = 0x0062,
152 SPI_GETMOUSEHOVERHEIGHT = 0x0064,
153 SPI_GETMOUSEHOVERTIME = 0x0066,
154 SPI_GETWHEELSCROLLLINES = 0x0068
157 internal enum WindowPlacementFlags {
161 SW_SHOWMINIMIZED = 2,
162 SW_SHOWMAXIMIZED = 3,
164 SW_SHOWNOACTIVATE = 4,
167 SW_SHOWMINNOACTIVE = 7,
171 SW_FORCEMINIMIZE = 11,
175 [StructLayout(LayoutKind.Sequential)]
176 private struct WINDOWPLACEMENT {
177 internal uint length;
179 internal WindowPlacementFlags showCmd;
180 internal POINT ptMinPosition;
181 internal POINT ptMaxPosition;
182 internal RECT rcNormalPosition;
185 [StructLayout(LayoutKind.Sequential)]
186 internal struct NCCALCSIZE_PARAMS {
190 internal IntPtr lppos;
194 private enum TMEFlags {
195 TME_HOVER = 0x00000001,
196 TME_LEAVE = 0x00000002,
197 TME_NONCLIENT = 0x00000010,
198 TME_QUERY = unchecked((int)0x40000000),
199 TME_CANCEL = unchecked((int)0x80000000)
202 [StructLayout(LayoutKind.Sequential)]
203 private struct TRACKMOUSEEVENT {
205 internal TMEFlags dwFlags;
206 internal IntPtr hWnd;
207 internal int dwHoverTime;
210 [StructLayout(LayoutKind.Sequential)]
211 private struct PAINTSTRUCT {
214 internal RECT rcPaint;
215 internal int fRestore;
216 internal int fIncUpdate;
217 internal int Reserved1;
218 internal int Reserved2;
219 internal int Reserved3;
220 internal int Reserved4;
221 internal int Reserved5;
222 internal int Reserved6;
223 internal int Reserved7;
224 internal int Reserved8;
227 [StructLayout(LayoutKind.Sequential)]
228 internal struct KEYBDINPUT {
230 internal short wScan;
231 internal Int32 dwFlags;
233 internal UIntPtr dwExtraInfo;
236 [StructLayout(LayoutKind.Sequential)]
237 internal struct MOUSEINPUT {
240 internal Int32 mouseData;
241 internal Int32 dwFlags;
243 internal UIntPtr dwExtraInfo;
246 [StructLayout(LayoutKind.Sequential)]
247 internal struct HARDWAREINPUT {
249 internal short wParamL;
250 internal short wParamH;
253 [StructLayout(LayoutKind.Explicit)]
254 internal struct INPUT {
259 internal MOUSEINPUT mi;
262 internal KEYBDINPUT ki;
265 internal HARDWAREINPUT hi;
269 internal enum InputFlags {
270 KEYEVENTF_EXTENDEDKEY = 0x0001,
271 KEYEVENTF_KEYUP = 0x0002,
272 KEYEVENTF_SCANCODE = 0x0003,
273 KEYEVENTF_UNICODE = 0x0004,
276 internal enum ClassStyle {
277 CS_VREDRAW = 0x00000001,
278 CS_HREDRAW = 0x00000002,
279 CS_KEYCVTWINDOW = 0x00000004,
280 CS_DBLCLKS = 0x00000008,
281 CS_OWNDC = 0x00000020,
282 CS_CLASSDC = 0x00000040,
283 CS_PARENTDC = 0x00000080,
284 CS_NOKEYCVT = 0x00000100,
285 CS_NOCLOSE = 0x00000200,
286 CS_SAVEBITS = 0x00000800,
287 CS_BYTEALIGNCLIENT = 0x00001000,
288 CS_BYTEALIGNWINDOW = 0x00002000,
289 CS_GLOBALCLASS = 0x00004000,
292 CS_DROPSHADOW = 0x00020000
295 internal enum SetWindowPosZOrder {
303 internal enum SetWindowPosFlags {
304 SWP_ASYNCWINDOWPOS = 0x4000,
305 SWP_DEFERERASE = 0x2000,
306 SWP_DRAWFRAME = 0x0020,
307 SWP_FRAMECHANGED = 0x0020,
308 SWP_HIDEWINDOW = 0x0080,
309 SWP_NOACTIVATE = 0x0010,
310 SWP_NOCOPYBITS = 0x0100,
312 SWP_NOOWNERZORDER = 0x0200,
313 SWP_NOREDRAW = 0x0008,
314 SWP_NOREPOSITION = 0x0200,
315 SWP_NOENDSCHANGING = 0x0400,
317 SWP_NOZORDER = 0x0004,
318 SWP_SHOWWINDOW = 0x0040
321 internal enum GetSysColorIndex {
323 COLOR_BACKGROUND = 1,
324 COLOR_ACTIVECAPTION = 2,
325 COLOR_INACTIVECAPTION = 3,
328 COLOR_WINDOWFRAME = 6,
330 COLOR_WINDOWTEXT = 8,
331 COLOR_CAPTIONTEXT = 9,
332 COLOR_ACTIVEBORDER = 10,
333 COLOR_INACTIVEBORDER = 11,
334 COLOR_APPWORKSPACE = 12,
335 COLOR_HIGHLIGHT = 13,
336 COLOR_HIGHLIGHTTEXT = 14,
338 COLOR_BTNSHADOW = 16,
341 COLOR_INACTIVECAPTIONTEXT = 19,
342 COLOR_BTNHIGHLIGHT = 20,
343 COLOR_3DDKSHADOW = 21,
349 COLOR_GRADIENTACTIVECAPTION = 27,
350 COLOR_GRADIENTINACTIVECAPTION = 28,
351 COLOR_MENUHIGHLIGHT = 29,
357 COLOR_3DHIGHLIGHT = 20,
358 COLOR_3DHILIGHT = 20,
359 COLOR_BTNHILIGHT = 20,
360 COLOR_MAXVALUE = 24,/* Maximum value */
363 private enum LoadCursorType {
372 IDC_SIZENWSE = 32642,
373 IDC_SIZENESW = 32643,
379 IDC_APPSTARTING = 32650,
385 private enum WindowLong {
396 private enum LogBrushStyle {
405 BS_DIBPATTERN8X8 = 8,
410 private enum LogBrushHatch {
411 HS_HORIZONTAL = 0, /* ----- */
412 HS_VERTICAL = 1, /* ||||| */
413 HS_FDIAGONAL = 2, /* \\\\\ */
414 HS_BDIAGONAL = 3, /* ///// */
415 HS_CROSS = 4, /* +++++ */
416 HS_DIAGCROSS = 5, /* xxxxx */
419 internal struct COLORREF {
426 [StructLayout(LayoutKind.Sequential)]
427 private struct LOGBRUSH {
428 internal LogBrushStyle lbStyle;
429 internal COLORREF lbColor;
430 internal LogBrushHatch lbHatch;
433 [StructLayout(LayoutKind.Sequential)]
434 internal struct TEXTMETRIC {
435 internal int tmHeight;
436 internal int tmAscent;
437 internal int tmDescent;
438 internal int tmInternalLeading;
439 internal int tmExternalLeading;
440 internal int tmAveCharWidth;
441 internal int tmMaxCharWidth;
442 internal int tmWeight;
443 internal int tmOverhang;
444 internal int tmDigitizedAspectX;
445 internal int tmDigitizedAspectY;
446 internal short tmFirstChar;
447 internal short tmLastChar;
448 internal short tmDefaultChar;
449 internal short tmBreakChar;
450 internal byte tmItalic;
451 internal byte tmUnderlined;
452 internal byte tmStruckOut;
453 internal byte tmPitchAndFamily;
454 internal byte tmCharSet;
457 public enum TernaryRasterOperations : uint
459 SRCCOPY = 0x00CC0020,
460 SRCPAINT = 0x00EE0086,
462 SRCINVERT = 0x00660046,
463 SRCERASE = 0x00440328,
464 NOTSRCCOPY = 0x00330008,
465 NOTSRCERASE = 0x001100A6,
466 MERGECOPY = 0x00C000CA,
467 MERGEPAINT = 0x00BB0226,
468 PATCOPY = 0x00F00021,
469 PATPAINT = 0x00FB0A09,
470 PATINVERT = 0x005A0049,
471 DSTINVERT = 0x00550009,
472 BLACKNESS = 0x00000042,
473 WHITENESS = 0x00FF0062
477 private enum ScrollWindowExFlags {
479 SW_SCROLLCHILDREN = 0x0001,
480 SW_INVALIDATE = 0x0002,
482 SW_SMOOTHSCROLL = 0x0010
485 internal enum SystemMetrics {
502 SM_CXFULLSCREEN = 16,
503 SM_CYFULLSCREEN = 17,
504 SM_CYKANJIWINDOW = 18,
505 SM_MOUSEPRESENT = 19,
524 SM_CXICONSPACING = 38,
525 SM_CYICONSPACING = 39,
526 SM_MENUDROPALIGNMENT = 40,
529 SM_CMOUSEBUTTONS = 43,
530 SM_CXFIXEDFRAME = SM_CXDLGFRAME,
531 SM_CYFIXEDFRAME = SM_CYDLGFRAME,
532 SM_CXSIZEFRAME = SM_CXFRAME,
533 SM_CYSIZEFRAME = SM_CYFRAME,
537 SM_CXMINSPACING = 47,
538 SM_CYMINSPACING = 48,
561 SM_MIDEASTENABLED = 74,
562 SM_MOUSEWHEELPRESENT = 75,
563 SM_XVIRTUALSCREEN = 76,
564 SM_YVIRTUALSCREEN = 77,
565 SM_CXVIRTUALSCREEN = 78,
566 SM_CYVIRTUALSCREEN = 79,
568 SM_SAMEDISPLAYFORMAT = 81,
570 SM_CXFOCUSBORDER = 83,
571 SM_CYFOCUSBORDER = 84,
577 // We'll only support _WIN32_IE < 0x0500 for now
578 internal enum NotifyIconMessage {
579 NIM_ADD = 0x00000000,
580 NIM_MODIFY = 0x00000001,
581 NIM_DELETE = 0x00000002,
585 internal enum NotifyIconFlags {
586 NIF_MESSAGE = 0x00000001,
587 NIF_ICON = 0x00000002,
588 NIF_TIP = 0x00000004,
589 NIF_STATE = 0x00000008,
590 NIF_INFO = 0x00000010
593 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
594 internal struct NOTIFYICONDATA {
595 internal uint cbSize;
596 internal IntPtr hWnd;
598 internal NotifyIconFlags uFlags;
599 internal uint uCallbackMessage;
600 internal IntPtr hIcon;
601 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
602 internal string szTip;
603 internal int dwState;
604 internal int dwStateMask;
605 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
606 internal string szInfo;
607 internal int uTimeoutOrVersion;
608 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
609 internal string szInfoTitle;
611 internal ToolTipIcon dwInfoFlags;
613 internal int dwInfoFlags;
618 internal enum DCExFlags {
619 DCX_WINDOW = 0x00000001,
620 DCX_CACHE = 0x00000002,
621 DCX_NORESETATTRS = 0x00000004,
622 DCX_CLIPCHILDREN = 0x00000008,
623 DCX_CLIPSIBLINGS = 0x00000010,
624 DCX_PARENTCLIP = 0x00000020,
625 DCX_EXCLUDERGN = 0x00000040,
626 DCX_INTERSECTRGN = 0x00000080,
627 DCX_EXCLUDEUPDATE = 0x00000100,
628 DCX_INTERSECTUPDATE = 0x00000200,
629 DCX_LOCKWINDOWUPDATE = 0x00000400,
630 DCX_USESTYLE = 0x00010000,
631 DCX_VALIDATE = 0x00200000
634 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
635 internal struct CLIENTCREATESTRUCT {
636 internal IntPtr hWindowMenu;
637 internal uint idFirstChild;
640 private enum ClassLong : int {
642 GCL_HBRBACKGROUND = -10,
646 GCL_CBWNDEXTRA = -18,
647 GCL_CBCLSEXTRA = -20,
655 internal enum GAllocFlags : uint {
657 GMEM_MOVEABLE = 0x0002,
658 GMEM_NOCOMPACT = 0x0010,
659 GMEM_NODISCARD = 0x0020,
660 GMEM_ZEROINIT = 0x0040,
661 GMEM_MODIFY = 0x0080,
662 GMEM_DISCARDABLE = 0x0100,
663 GMEM_NOT_BANKED = 0x1000,
665 GMEM_DDESHARE = 0x2000,
666 GMEM_NOTIFY = 0x4000,
667 GMEM_LOWER = GMEM_NOT_BANKED,
668 GMEM_VALID_FLAGS = 0x7F72,
669 GMEM_INVALID_HANDLE = 0x8000,
670 GHND = (GMEM_MOVEABLE | GMEM_ZEROINIT),
671 GPTR = (GMEM_FIXED | GMEM_ZEROINIT)
674 internal enum ROP2DrawMode : int {
694 internal enum PenStyle : int {
706 internal enum PatBltRop : int {
708 PATINVERT = 0x5a0049,
709 DSTINVERT = 0x550009,
710 BLACKNESS = 0x000042,
711 WHITENESS = 0xff0062,
714 internal enum StockObject : int {
721 HOLLOW_BRUSH = NULL_BRUSH,
726 ANSI_FIXED_FONT = 11,
729 DEVICE_DEFAULT_FONT = 14,
730 DEFAULT_PALETTE = 15,
731 SYSTEM_FIXED_FONT = 16
734 internal enum HatchStyle : int {
744 internal enum SndFlags : int {
747 SND_NODEFAULT = 0x0002,
751 SND_NOWAIT = 0x00002000,
752 SND_ALIAS = 0x00010000,
753 SND_ALIAS_ID = 0x00110000,
754 SND_FILENAME = 0x00020000,
755 SND_RESOURCE = 0x00040004,
757 SND_APPLICATION = 0x0080,
761 internal enum LayeredWindowAttributes : int {
769 #region Constructor & Destructor
770 private XplatUIWin32() {
771 // Handle singleton stuff first
774 mouse_state = MouseButtons.None;
775 mouse_position = Point.Empty;
777 message_queue = new Queue();
779 themes_enabled = false;
781 wnd_proc = new WndProc(InternalWndProc);
783 FosterParent=Win32CreateWindow((int)WindowExStyles.WS_EX_TOOLWINDOW, "static", "Foster Parent Window", (int)WindowStyles.WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
785 if (FosterParent==IntPtr.Zero) {
786 Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
789 scroll_height = Win32GetSystemMetrics(SystemMetrics.SM_CYHSCROLL);
790 scroll_width = Win32GetSystemMetrics(SystemMetrics.SM_CXVSCROLL);
792 timer_list = new Hashtable ();
793 registered_classes = new Hashtable ();
795 #endregion // Constructor & Destructor
797 #region Private Support Methods
799 private string RegisterWindowClass (int classStyle)
803 lock (registered_classes) {
804 class_name = (string)registered_classes[classStyle];
806 if (class_name != null)
809 class_name = string.Format ("Mono.WinForms.{0}.{1}", System.Threading.Thread.GetDomainID ().ToString (), classStyle);
813 wndClass.style = classStyle;
814 wndClass.lpfnWndProc = wnd_proc;
815 wndClass.cbClsExtra = 0;
816 wndClass.cbWndExtra = 0;
817 wndClass.hbrBackground = (IntPtr)(GetSysColorIndex.COLOR_WINDOW + 1);
818 wndClass.hCursor = Win32LoadCursor (IntPtr.Zero, LoadCursorType.IDC_ARROW);
819 wndClass.hIcon = IntPtr.Zero;
820 wndClass.hInstance = IntPtr.Zero;
821 wndClass.lpszClassName = class_name;
822 wndClass.lpszMenuName = "";
824 bool result = Win32RegisterClass (ref wndClass);
827 Win32MessageBox (IntPtr.Zero, "Could not register the window class, win32 error " + Win32GetLastError ().ToString (), "Oops", 0);
829 registered_classes[classStyle] = class_name;
835 private static bool RetrieveMessage(ref MSG msg) {
838 if (message_queue.Count == 0) {
842 message = (MSG)message_queue.Dequeue();
848 private static bool StoreMessage(ref MSG msg) {
849 MSG message = new MSG();
852 message_queue.Enqueue(message);
857 internal static String AnsiToString(IntPtr ansi_data) {
858 return (string)Marshal.PtrToStringAnsi(ansi_data);
861 internal static String UnicodeToString(IntPtr unicode_data) {
862 return (string)Marshal.PtrToStringUni(unicode_data);
865 internal static Image DIBtoImage(IntPtr dib_data) {
866 BITMAPINFOHEADER bmi;
877 bmi = (BITMAPINFOHEADER)Marshal.PtrToStructure(dib_data, typeof(BITMAPINFOHEADER));
879 ncolors = (int)bmi.biClrUsed;
881 if (bmi.biBitCount < 24) {
882 ncolors = (int)(1 << bmi.biBitCount);
885 //palettesize = ncolors * 4;
887 imagesize = (int)bmi.biSizeImage;
888 if (imagesize == 0) {
889 imagesize = (int)(((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);
892 switch(bmi.biBitCount) {
893 case 1: { // Monochrome
894 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format1bppIndexed);
895 palette = new int[2];
900 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format4bppIndexed);
901 palette = new int[16];
906 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format8bppIndexed);
907 palette = new int[256];
913 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format32bppArgb);
914 palette = new int[0];
919 throw new Exception("Unexpected number of bits:" + bmi.biBitCount.ToString());
923 if (bmi.biBitCount < 24) {
924 pal = bmp.Palette; // Managed palette
925 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER))), palette, 0, palette.Length);
927 for (int i = 0; i < ncolors; i++) {
928 pal.Entries[i] = Color.FromArgb(palette[i] | unchecked((int)0xff000000));
933 bytesPerLine = (int)((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3);
934 bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
936 imagebits = new byte[bytesPerLine];
938 for (int y = 0; y < bmi.biHeight; y++) {
939 // Copy from source to managed
940 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER)) + palette.Length * 4 + bytesPerLine * y), imagebits, 0, bytesPerLine);
942 // Copy from managed to dest
943 Marshal.Copy(imagebits, 0, (IntPtr)((int)bits.Scan0 + bits.Stride * (bmi.biHeight - 1 - y)), imagebits.Length);
946 bmp.UnlockBits(bits);
951 internal static byte[] ImageToDIB(Image image) {
956 ms = new MemoryStream();
957 image.Save(ms, ImageFormat.Bmp);
958 buffer = ms.GetBuffer();
960 // Filter out the file header
961 retbuf = new byte[buffer.Length];
962 Array.Copy(buffer, 14, retbuf, 0, buffer.Length - 14);
966 internal static IntPtr DupGlobalMem(IntPtr mem) {
972 len = Win32GlobalSize(mem);
973 mem_ptr = Win32GlobalLock(mem);
975 dup = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE, (int)len);
976 dup_ptr = Win32GlobalLock(dup);
978 Win32CopyMemory(dup_ptr, mem_ptr, (int)len);
980 Win32GlobalUnlock(mem);
981 Win32GlobalUnlock(dup);
985 #endregion // Private Support Methods
987 #region Static Properties
988 internal override Keys ModifierKeys {
993 key_state = Keys.None;
995 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
996 if ((state & 0x8000) != 0) {
997 key_state |= Keys.Shift;
999 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
1000 if ((state & 0x8000) != 0) {
1001 key_state |= Keys.Control;
1004 state = Win32GetKeyState(VirtualKeys.VK_MENU);
1005 if ((state & 0x8000) != 0) {
1006 key_state |= Keys.Alt;
1013 internal override MouseButtons MouseButtons {
1019 internal override Point MousePosition {
1021 return mouse_position;
1025 internal override Size MouseHoverSize {
1030 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref width, 0);
1031 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref height, 0);
1032 return new Size(width, height);
1036 internal override int MouseHoverTime {
1040 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERTIME, 0, ref time, 0);
1045 internal override int MouseWheelScrollDelta {
1048 Win32SystemParametersInfo(SPIAction.SPI_GETWHEELSCROLLLINES, 0, ref delta, 0);
1053 internal override int HorizontalScrollBarHeight {
1055 return scroll_height;
1059 internal override bool UserClipWontExposeParent {
1066 internal override int VerticalScrollBarWidth {
1068 return scroll_width;
1072 internal override int MenuHeight {
1074 return Win32GetSystemMetrics(SystemMetrics.SM_CYMENU);
1078 internal override bool DropTarget {
1085 //throw new NotImplementedException("Need to figure out D'n'D for Win32");
1090 internal override int Caption {
1092 return Win32GetSystemMetrics(SystemMetrics.SM_CYCAPTION);
1096 internal override Size CursorSize {
1098 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CYCURSOR));
1102 internal override bool DragFullWindows {
1108 internal override Size DragSize {
1110 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXDRAG), Win32GetSystemMetrics(SystemMetrics.SM_CYDRAG));
1114 internal override Size FrameBorderSize {
1116 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXFRAME), Win32GetSystemMetrics(SystemMetrics.SM_CYFRAME));
1120 internal override Size IconSize {
1122 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXICON), Win32GetSystemMetrics(SystemMetrics.SM_CYICON));
1126 internal override Size MaxWindowTrackSize {
1128 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMAXTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMAXTRACK));
1132 internal override Size MinimizedWindowSize {
1134 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINIMIZED), Win32GetSystemMetrics(SystemMetrics.SM_CYMINIMIZED));
1138 internal override Size MinimizedWindowSpacingSize {
1140 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINSPACING), Win32GetSystemMetrics(SystemMetrics.SM_CYMINSPACING));
1144 internal override Size MinimumWindowSize {
1146 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMIN), Win32GetSystemMetrics(SystemMetrics.SM_CYMIN));
1150 internal override Size MinWindowTrackSize {
1152 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMINTRACK));
1156 internal override Size SmallIconSize {
1158 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXSMICON), Win32GetSystemMetrics(SystemMetrics.SM_CYSMICON));
1162 internal override int MouseButtonCount {
1164 return Win32GetSystemMetrics(SystemMetrics.SM_CMOUSEBUTTONS);
1168 internal override bool MouseButtonsSwapped {
1170 return Win32GetSystemMetrics(SystemMetrics.SM_SWAPBUTTON) != 0;
1174 internal override bool MouseWheelPresent {
1176 return Win32GetSystemMetrics(SystemMetrics.SM_MOUSEWHEELPRESENT) != 0;
1180 internal override Rectangle VirtualScreen {
1182 return new Rectangle( Win32GetSystemMetrics(SystemMetrics.SM_XVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_YVIRTUALSCREEN),
1183 Win32GetSystemMetrics(SystemMetrics.SM_CXVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYVIRTUALSCREEN));
1187 internal override Rectangle WorkingArea {
1192 Win32SystemParametersInfo(SPIAction.SPI_GETWORKAREA, 0, ref rect, 0);
1193 return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
1194 //return new Rectangle(0, 0, Win32GetSystemMetrics(SystemMetrics.SM.SM_CXSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYSCREEN));
1198 internal override bool ThemesEnabled {
1200 return XplatUIWin32.themes_enabled;
1205 #endregion // Static Properties
1207 #region Singleton Specific Code
1208 public static XplatUIWin32 GetInstance() {
1209 if (instance==null) {
1210 instance=new XplatUIWin32();
1216 public int Reference {
1223 #region Public Static Methods
1224 internal override IntPtr InitializeDriver() {
1228 internal override void ShutdownDriver(IntPtr token) {
1229 Console.WriteLine("XplatUIWin32 ShutdownDriver called");
1233 internal void Version() {
1234 Console.WriteLine("Xplat version $revision: $");
1237 internal override void AudibleAlert() {
1238 Win32PlaySound("Default", IntPtr.Zero, SndFlags.SND_ALIAS | SndFlags.SND_ASYNC | SndFlags.SND_NOSTOP | SndFlags.SND_NOWAIT);
1241 internal override void GetDisplaySize(out Size size) {
1244 Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
1246 size = new Size(rect.right - rect.left, rect.bottom - rect.top);
1249 internal override void EnableThemes() {
1250 themes_enabled=true;
1253 internal override IntPtr CreateWindow(CreateParams cp) {
1254 IntPtr WindowHandle;
1255 IntPtr ParentHandle;
1260 ParentHandle=cp.Parent;
1262 if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
1263 // We need to use our foster parent window until this poor child gets it's parent assigned
1264 ParentHandle = FosterParent;
1267 if ( ((cp.Style & (int)(WindowStyles.WS_CHILD | WindowStyles.WS_POPUP))==0) && ((cp.ExStyle & (int)WindowExStyles.WS_EX_APPWINDOW) == 0)) {
1268 // If we want to be hidden from the taskbar we need to be 'owned' by
1269 // something not on the taskbar. FosterParent is just that
1270 ParentHandle = FosterParent;
1273 string class_name = RegisterWindowClass (cp.ClassStyle);
1274 HwndCreating = hwnd;
1276 WindowHandle = Win32CreateWindow ((uint)cp.ExStyle, class_name, cp.Caption, (uint)cp.Style, cp.X, cp.Y, cp.Width, cp.Height, ParentHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
1278 HwndCreating = null;
1280 if (WindowHandle==IntPtr.Zero) {
1281 uint error = Win32GetLastError();
1283 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1286 hwnd.ClientWindow = WindowHandle;
1288 Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1290 return WindowHandle;
1293 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1294 CreateParams create_params = new CreateParams();
1296 create_params.Caption = "";
1297 create_params.X = X;
1298 create_params.Y = Y;
1299 create_params.Width = Width;
1300 create_params.Height = Height;
1302 create_params.ClassName=XplatUI.DefaultClassName;
1303 create_params.ClassStyle = 0;
1304 create_params.ExStyle=0;
1305 create_params.Parent=IntPtr.Zero;
1306 create_params.Param=0;
1308 return CreateWindow(create_params);
1311 internal override void DestroyWindow(IntPtr handle) {
1314 hwnd = Hwnd.ObjectFromHandle(handle);
1315 Win32DestroyWindow(handle);
1320 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1321 // We do nothing, Form has to handle WM_GETMINMAXINFO
1325 internal override FormWindowState GetWindowState(IntPtr handle) {
1328 style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1329 if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1330 return FormWindowState.Maximized;
1331 } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1332 return FormWindowState.Minimized;
1334 return FormWindowState.Normal;
1337 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1339 case FormWindowState.Normal: {
1340 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1344 case FormWindowState.Minimized: {
1345 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1349 case FormWindowState.Maximized: {
1350 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1356 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1358 Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1359 Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1361 if ((cp.ExStyle & (int) WindowExStyles.WS_EX_TOOLWINDOW) > 0 ||
1362 ((cp.WindowStyle & (WindowStyles.WS_CAPTION)) != WindowStyles.WS_CAPTION && cp.control is Form))
1363 XplatUI.RequestNCRecalc (handle);
1366 internal override double GetWindowTransparency(IntPtr handle)
1368 LayeredWindowAttributes lwa;
1372 if (0 == Win32GetLayeredWindowAttributes (handle, out clrRef, out alpha, out lwa))
1375 return ((double)alpha) / 255.0;
1378 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1379 LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1380 byte opacity = (byte)(transparency*255);
1381 COLORREF clrRef = new COLORREF();
1382 if (key != Color.Empty) {
1386 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1391 Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1394 TransparencySupport support;
1395 bool queried_transparency_support;
1396 internal override TransparencySupport SupportsTransparency() {
1397 if (queried_transparency_support)
1401 support = TransparencySupport.None;
1405 Win32SetLayeredWindowAttributes (IntPtr.Zero, new COLORREF (), 255, LayeredWindowAttributes.LWA_ALPHA);
1407 catch (EntryPointNotFoundException) { flag = false; }
1408 catch { /* swallow everything else */ }
1410 if (flag) support |= TransparencySupport.Set;
1414 LayeredWindowAttributes lwa;
1418 Win32GetLayeredWindowAttributes (IntPtr.Zero, out clrRef, out alpha, out lwa);
1420 catch (EntryPointNotFoundException) { flag = false; }
1421 catch { /* swallow everything else */ }
1423 if (flag) support |= TransparencySupport.Get;
1425 queried_transparency_support = true;
1429 internal override void UpdateWindow(IntPtr handle) {
1430 Win32UpdateWindow(handle);
1433 internal override PaintEventArgs PaintEventStart(IntPtr handle, bool client) {
1436 PaintEventArgs paint_event;
1438 Rectangle clip_rect;
1441 clip_rect = new Rectangle();
1443 ps = new PAINTSTRUCT();
1445 hwnd = Hwnd.ObjectFromHandle(handle);
1448 if (Win32GetUpdateRect(handle, ref rect, false)) {
1449 hdc = Win32BeginPaint(handle, ref ps);
1451 hwnd.drawing_stack.Push (ps);
1453 clip_rect = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top);
1455 hdc = Win32GetDC(handle);
1456 // FIXME: Add the DC to internal list
1458 hwnd.drawing_stack.Push (null);
1460 clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
1463 hdc = Win32GetWindowDC (handle);
1465 hwnd.drawing_stack.Push (hdc);
1467 // HACK this in for now
1468 Win32GetWindowRect (handle, out rect);
1469 clip_rect = new Rectangle(0, 0, rect.right-rect.left, rect.bottom-rect.top);
1472 Graphics dc = Graphics.FromHdc(hdc);
1473 hwnd.drawing_stack.Push (dc);
1475 paint_event = new PaintEventArgs(dc, clip_rect);
1480 internal override void PaintEventEnd(IntPtr handle, bool client) {
1483 hwnd = Hwnd.ObjectFromHandle(handle);
1485 Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1489 object o = hwnd.drawing_stack.Pop();
1491 PAINTSTRUCT ps = (PAINTSTRUCT)o;
1492 Win32EndPaint(handle, ref ps);
1495 IntPtr hdc = (IntPtr)hwnd.drawing_stack.Pop();
1496 Win32ReleaseDC(handle, hdc);
1501 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1502 Win32MoveWindow(handle, x, y, width, height, true);
1506 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) {
1511 Win32GetWindowRect(handle, out rect);
1512 width = rect.right - rect.left;
1513 height = rect.bottom - rect.top;
1518 parent = Win32GetParent(handle);
1519 Win32ScreenToClient(parent, ref pt);
1524 Win32GetClientRect(handle, out rect);
1525 client_width = rect.right - rect.left;
1526 client_height = rect.bottom - rect.top;
1530 internal override void Activate(IntPtr handle) {
1531 Win32SetActiveWindow(handle);
1534 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1539 rect.right=rc.Right;
1540 rect.bottom=rc.Bottom;
1541 Win32InvalidateRect(handle, ref rect, clear);
1545 internal override void InvalidateNC (IntPtr handle)
1547 // found this gem at
1548 // http://www.dotnet247.com/247reference/msgs/58/292037.aspx
1549 Win32SetWindowPos(handle, IntPtr.Zero,
1551 SetWindowPosFlags.SWP_NOMOVE |
1552 SetWindowPosFlags.SWP_NOSIZE |
1553 SetWindowPosFlags.SWP_NOZORDER |
1554 SetWindowPosFlags.SWP_NOACTIVATE |
1555 SetWindowPosFlags.SWP_DRAWFRAME);
1558 private IntPtr InternalWndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
1560 if (HwndCreating != null && HwndCreating.ClientWindow == IntPtr.Zero)
1561 HwndCreating.ClientWindow = hWnd;
1562 return NativeWindow.WndProc (hWnd, msg, wParam, lParam);
1565 internal override IntPtr DefWndProc(ref Message msg) {
1566 msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1570 internal override void HandleException(Exception e) {
1571 StackTrace st = new StackTrace(e);
1572 Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1573 Console.WriteLine("{0}{1}", e.Message, st.ToString());
1576 internal override void DoEvents() {
1577 MSG msg = new MSG();
1579 if (override_cursor != IntPtr.Zero) {
1580 Cursor.Current = null;
1583 while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1584 XplatUI.TranslateMessage(ref msg);
1585 XplatUI.DispatchMessage(ref msg);
1589 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1590 return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1593 internal override void PostQuitMessage(int exitCode) {
1594 Win32PostQuitMessage(exitCode);
1597 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
1599 if (wm_nc_registered == null)
1600 wm_nc_registered = new Hashtable ();
1602 TMEFlags flags = TMEFlags.TME_NONCLIENT;
1604 flags |= TMEFlags.TME_HOVER;
1606 flags |= TMEFlags.TME_LEAVE;
1608 if (flags == TMEFlags.TME_NONCLIENT) {
1609 if (wm_nc_registered.Contains (hwnd)) {
1610 wm_nc_registered.Remove (hwnd);
1613 if (!wm_nc_registered.Contains (hwnd)) {
1614 wm_nc_registered.Add (hwnd, flags);
1616 wm_nc_registered [hwnd] = flags;
1621 internal override void RequestNCRecalc(IntPtr handle) {
1622 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);
1625 internal override void ResetMouseHover(IntPtr handle) {
1626 TRACKMOUSEEVENT tme;
1628 tme = new TRACKMOUSEEVENT();
1629 tme.size = Marshal.SizeOf(tme);
1631 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1632 Win32TrackMouseEvent(ref tme);
1636 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1637 return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
1640 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
1644 if (RetrieveMessage(ref msg)) {
1649 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
1651 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
1657 // We need to fake WM_MOUSE_ENTER
1658 switch (msg.message) {
1659 case Msg.WM_LBUTTONDOWN: {
1660 mouse_state |= MouseButtons.Left;
1664 case Msg.WM_MBUTTONDOWN: {
1665 mouse_state |= MouseButtons.Middle;
1669 case Msg.WM_RBUTTONDOWN: {
1670 mouse_state |= MouseButtons.Right;
1674 case Msg.WM_LBUTTONUP: {
1675 mouse_state &= ~MouseButtons.Left;
1679 case Msg.WM_MBUTTONUP: {
1680 mouse_state &= ~MouseButtons.Middle;
1684 case Msg.WM_RBUTTONUP: {
1685 mouse_state &= ~MouseButtons.Right;
1689 case Msg.WM_ASYNC_MESSAGE: {
1690 XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
1694 case Msg.WM_MOUSEMOVE: {
1695 if (msg.hwnd != prev_mouse_hwnd) {
1696 TRACKMOUSEEVENT tme;
1698 // The current message will be sent out next time around
1699 StoreMessage(ref msg);
1701 // This is the message we want to send at this point
1702 msg.message = Msg.WM_MOUSE_ENTER;
1704 prev_mouse_hwnd = msg.hwnd;
1706 tme = new TRACKMOUSEEVENT();
1707 tme.size = Marshal.SizeOf(tme);
1708 tme.hWnd = msg.hwnd;
1709 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1710 Win32TrackMouseEvent(ref tme);
1716 case Msg.WM_NCMOUSEMOVE: {
1717 if (wm_nc_registered == null || !wm_nc_registered.Contains (msg.hwnd))
1720 TRACKMOUSEEVENT tme;
1722 tme = new TRACKMOUSEEVENT ();
1723 tme.size = Marshal.SizeOf(tme);
1724 tme.hWnd = msg.hwnd;
1725 tme.dwFlags = (TMEFlags)wm_nc_registered[msg.hwnd];
1726 Win32TrackMouseEvent (ref tme);
1730 case Msg.WM_DROPFILES: {
1731 return Win32DnD.HandleWMDropFiles(ref msg);
1734 case Msg.WM_MOUSELEAVE: {
1735 prev_mouse_hwnd = IntPtr.Zero;
1739 case Msg.WM_TIMER: {
1740 Timer timer=(Timer)timer_list[(int)msg.wParam];
1742 if (timer != null) {
1752 internal override bool TranslateMessage(ref MSG msg) {
1753 return Win32TranslateMessage(ref msg);
1756 internal override IntPtr DispatchMessage(ref MSG msg) {
1757 return Win32DispatchMessage(ref msg);
1760 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
1762 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1764 } else if (!Bottom) {
1765 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1767 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1773 internal override bool SetTopmost(IntPtr hWnd, bool Enabled) {
1775 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1778 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1783 internal override bool SetOwner(IntPtr hWnd, IntPtr hWndOwner) {
1784 Win32SetWindowLong(hWnd, WindowLong.GWL_HWNDPARENT, (uint) hWndOwner);
1788 internal override bool Text(IntPtr handle, string text) {
1789 Win32SetWindowText(handle, text);
1793 internal override bool GetText(IntPtr handle, out string text) {
1796 sb = new StringBuilder(256);
1797 Win32GetWindowText(handle, sb, sb.Capacity);
1798 text = sb.ToString();
1802 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
1805 Control c = Control.FromHandle (handle);
1809 f = (Form)Control.FromHandle (handle);
1810 WindowPlacementFlags flags = WindowPlacementFlags.SW_SHOWNORMAL;
1811 switch (f.WindowState) {
1812 case FormWindowState.Normal: flags = WindowPlacementFlags.SW_SHOWNORMAL; break;
1813 case FormWindowState.Minimized: flags = WindowPlacementFlags.SW_MINIMIZE; break;
1814 case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
1817 if (!f.ActivateOnShow)
1818 flags = WindowPlacementFlags.SW_SHOWNOACTIVATE;
1820 Win32ShowWindow (handle, flags);
1823 if (c.ActivateOnShow)
1824 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
1826 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
1830 Win32ShowWindow (handle, WindowPlacementFlags.SW_HIDE);
1835 internal override bool IsEnabled(IntPtr handle) {
1836 return IsWindowEnabled (handle);
1839 internal override bool IsVisible(IntPtr handle) {
1840 return IsWindowVisible (handle);
1843 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
1844 Control c = Control.FromHandle (handle);
1845 if (parent == IntPtr.Zero) {
1847 Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
1851 SetVisible (handle, c.is_visible, true);
1853 // The Win32SetParent is lame, it can very well move the window
1854 // ref: http://groups.google.com/group/microsoft.public.vb.winapi/browse_thread/thread/1b82ccc54231ecee/afa82835bfc0422a%23afa82835bfc0422a
1855 // Here we save the position before changing the parent, and if it has changed afterwards restore it.
1856 // Another possibility would be to intercept WM_WINDOWPOSCHANGING and restore the coords there, but this would require plumbing in weird places
1857 // (either inside Control or add handling to InternalWndProc)
1858 // We also need to remove WS_CHILD if making the window parent-less, and add it if we're parenting it.
1861 WindowStyles style, new_style;
1863 Win32GetWindowRect (handle, out rect);
1864 style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
1866 if (parent == IntPtr.Zero) {
1867 new_style = style & ~WindowStyles.WS_CHILD;
1868 result = Win32SetParent (handle, FosterParent);
1870 new_style = style | WindowStyles.WS_CHILD;
1871 result = Win32SetParent (handle, parent);
1873 if (style != new_style && c is Form) {
1874 Win32SetWindowLong (handle, WindowLong.GWL_STYLE, (uint) new_style);
1876 Win32GetWindowRect (handle, out rect2);
1877 if (rect.top != rect2.top && rect.left != rect2.left && c is Form) {
1878 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);
1883 // If we ever start using this, we should probably replace FosterParent with IntPtr.Zero
1884 internal override IntPtr GetParent(IntPtr handle) {
1885 return Win32GetParent(handle);
1888 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
1890 Win32SetCapture(hWnd);
1892 if (ConfineToHwnd != IntPtr.Zero) {
1894 Win32GetWindowRect (ConfineToHwnd, out window_rect);
1895 Win32GetClipCursor (out clipped_cursor_rect);
1896 Win32ClipCursor (ref window_rect);
1900 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
1902 GrabConfined = grab_confined;
1903 GrabArea = grab_area;
1906 internal override void UngrabWindow(IntPtr hWnd) {
1907 if (!(clipped_cursor_rect.top == 0 && clipped_cursor_rect.bottom == 0 && clipped_cursor_rect.left == 0 && clipped_cursor_rect.right == 0)) {
1908 Win32ClipCursor (ref clipped_cursor_rect);
1909 clipped_cursor_rect = new RECT ();
1912 Win32ReleaseCapture();
1913 grab_hwnd = IntPtr.Zero;
1916 internal override bool CalculateWindowRect(ref Rectangle ClientRect, CreateParams cp, Menu menu, out Rectangle WindowRect) {
1919 rect.left=ClientRect.Left;
1920 rect.top=ClientRect.Top;
1921 rect.right=ClientRect.Right;
1922 rect.bottom=ClientRect.Bottom;
1924 if (!Win32AdjustWindowRectEx(ref rect, cp.Style, menu != null, cp.ExStyle)) {
1925 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
1929 WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
1933 internal override void SetCursor(IntPtr window, IntPtr cursor) {
1934 Win32SetCursor(cursor);
1938 internal override void ShowCursor(bool show) {
1939 Win32ShowCursor(show);
1942 internal override void OverrideCursor(IntPtr cursor) {
1943 Win32SetCursor(cursor);
1946 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
1948 Bitmap cursor_bitmap;
1956 // Win32 only allows creation cursors of a certain size
1957 if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
1958 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1959 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1961 cursor_bitmap = bitmap;
1965 width = cursor_bitmap.Width;
1966 height = cursor_bitmap.Height;
1968 cursor_bits = new Byte[(width / 8) * height];
1969 mask_bits = new Byte[(width / 8) * height];
1971 for (int y = 0; y < height; y++) {
1972 for (int x = 0; x < width; x++) {
1973 pixel = cursor_bitmap.GetPixel(x, y);
1975 if (pixel == cursor_pixel) {
1976 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1979 pixel = cursor_mask.GetPixel(x, y);
1981 if (pixel == mask_pixel) {
1982 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1987 cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
1992 [MonoTODO("Define the missing cursors")]
1993 internal override IntPtr DefineStdCursor(StdCursor id) {
1995 case StdCursor.AppStarting: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
1996 case StdCursor.Arrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1997 case StdCursor.Cross: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
1998 case StdCursor.Default: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1999 case StdCursor.Hand: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
2000 case StdCursor.Help: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
2001 case StdCursor.HSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2002 case StdCursor.IBeam: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
2003 case StdCursor.No: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
2004 case StdCursor.NoMove2D: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2005 case StdCursor.NoMoveHoriz: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2006 case StdCursor.NoMoveVert: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2007 case StdCursor.PanEast: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2008 case StdCursor.PanNE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2009 case StdCursor.PanNorth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2010 case StdCursor.PanNW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2011 case StdCursor.PanSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2012 case StdCursor.PanSouth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2013 case StdCursor.PanSW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2014 case StdCursor.PanWest: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2015 case StdCursor.SizeAll: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
2016 case StdCursor.SizeNESW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
2017 case StdCursor.SizeNS: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
2018 case StdCursor.SizeNWSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
2019 case StdCursor.SizeWE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
2020 case StdCursor.UpArrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
2021 case StdCursor.VSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2022 case StdCursor.WaitCursor: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
2024 throw new NotImplementedException ();
2027 internal override void DestroyCursor(IntPtr cursor) {
2028 if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
2029 Win32DestroyCursor(cursor);
2034 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
2041 internal override void SetCursorPos(IntPtr handle, int x, int y) {
2042 Win32SetCursorPos(x, y);
2045 internal override Region GetClipRegion(IntPtr hwnd) {
2048 region = new Region();
2050 Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
2055 internal override void SetClipRegion(IntPtr hwnd, Region region) {
2056 Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
2059 internal override void EnableWindow(IntPtr handle, bool Enable) {
2060 Win32EnableWindow(handle, Enable);
2063 internal override void EndLoop(System.Threading.Thread thread) {
2067 internal override object StartLoop(System.Threading.Thread thread) {
2071 internal override void SetModal(IntPtr handle, bool Modal) {
2072 // we do nothing on Win32
2075 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
2078 Win32GetCursorPos(out pt);
2080 if (handle!=IntPtr.Zero) {
2081 Win32ScreenToClient(handle, ref pt);
2088 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
2090 POINT pnt = new POINT();
2094 Win32ScreenToClient (handle, ref pnt);
2100 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {
2101 POINT pnt = new POINT();
2106 Win32ClientToScreen(handle, ref pnt);
2112 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
2115 Win32GetWindowRect(handle, out rect);
2116 x -= rect.left + SystemInformation.FrameBorderSize.Width;
2117 y -= rect.top + SystemInformation.FrameBorderSize.Height;
2119 WindowStyles style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
2120 if (CreateParams.IsSet (style, WindowStyles.WS_CAPTION)) {
2121 y -= ThemeEngine.Current.CaptionHeight;
2125 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {
2128 Win32GetWindowRect(handle, out rect);
2129 x += rect.left + SystemInformation.FrameBorderSize.Width;
2130 y += rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
2134 internal override void SendAsyncMethod (AsyncMethodData method)
2136 Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
2139 internal override void SetTimer (Timer timer)
2143 index = timer.GetHashCode();
2146 timer_list[index]=timer;
2149 Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero);
2152 internal override void KillTimer (Timer timer)
2156 index = timer.GetHashCode();
2158 Win32KillTimer(FosterParent, index);
2161 timer_list.Remove(index);
2165 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
2166 Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
2167 caret_visible = false;
2170 internal override void DestroyCaret(IntPtr hwnd) {
2171 Win32DestroyCaret();
2174 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
2175 Win32SetCaretPos(x, y);
2178 internal override void CaretVisible(IntPtr hwnd, bool visible) {
2180 if (!caret_visible) {
2181 Win32ShowCaret(hwnd);
2182 caret_visible = true;
2185 if (caret_visible) {
2186 Win32HideCaret(hwnd);
2187 caret_visible = false;
2192 internal override IntPtr GetFocus() {
2193 return Win32GetFocus();
2196 internal override void SetFocus(IntPtr hwnd) {
2197 Win32SetFocus(hwnd);
2200 internal override IntPtr GetActive() {
2201 return Win32GetActiveWindow();
2204 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
2208 tm = new TEXTMETRIC();
2210 dc = Win32GetDC(IntPtr.Zero);
2211 Win32SelectObject(dc, font.ToHfont());
2212 if (Win32GetTextMetrics(dc, ref tm) == false) {
2213 Win32ReleaseDC(IntPtr.Zero, dc);
2218 Win32ReleaseDC(IntPtr.Zero, dc);
2220 ascent = tm.tmAscent;
2221 descent = tm.tmDescent;
2226 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
2230 rect.left = rectangle.X;
2231 rect.top = rectangle.Y;
2232 rect.right = rectangle.Right;
2233 rect.bottom = rectangle.Bottom;
2235 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));
2236 Win32UpdateWindow(hwnd);
2239 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
2240 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));
2243 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
2246 nid = new NOTIFYICONDATA();
2248 nid.cbSize = (uint)Marshal.SizeOf(nid);
2251 nid.uCallbackMessage = (uint)Msg.WM_USER;
2252 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2256 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2260 nid.hIcon = icon.Handle;
2261 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2266 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
2269 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
2272 nid = new NOTIFYICONDATA();
2274 nid.cbSize = (uint)Marshal.SizeOf(nid);
2275 nid.hIcon = icon.Handle;
2278 nid.uCallbackMessage = (uint)Msg.WM_USER;
2279 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2283 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2287 nid.hIcon = icon.Handle;
2288 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2291 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2294 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
2297 nid = new NOTIFYICONDATA();
2299 nid.cbSize = (uint)Marshal.SizeOf(nid);
2304 Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
2308 internal override void SystrayBalloon(IntPtr hwnd, int timeout, string title, string text, ToolTipIcon icon)
2312 nid = new NOTIFYICONDATA();
2314 nid.cbSize = (uint)Marshal.SizeOf(nid);
2317 nid.uFlags = NotifyIconFlags.NIF_INFO;
2318 nid.uTimeoutOrVersion = timeout;
2319 nid.szInfoTitle = title;
2321 nid.dwInfoFlags = icon;
2323 Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2327 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
2328 // Nothing to do on Win32
2331 internal override void SetMenu(IntPtr handle, Menu menu) {
2332 // Trigger WM_NCCALC
2333 Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2336 internal override Point GetMenuOrigin(IntPtr handle) {
2337 Form form = Control.FromHandle (handle) as Form;
2339 Hwnd.Borders borders = Hwnd.GetBorders (form.GetCreateParams (), null);
2340 return new Point(borders.left, borders.top);
2342 return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2345 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2346 Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : icon.Handle); // 1 = large icon (0 would be small)
2349 internal override void ClipboardClose(IntPtr handle) {
2350 if (handle != clip_magic) {
2351 throw new ArgumentException("handle is not a valid clipboard handle");
2353 Win32CloseClipboard();
2356 internal override int ClipboardGetID(IntPtr handle, string format) {
2357 if (handle != clip_magic) {
2358 throw new ArgumentException("handle is not a valid clipboard handle");
2360 if (format == "Text" ) return 1;
2361 else if (format == "Bitmap" ) return 2;
2362 else if (format == "MetaFilePict" ) return 3;
2363 else if (format == "SymbolicLink" ) return 4;
2364 else if (format == "DataInterchangeFormat" ) return 5;
2365 else if (format == "Tiff" ) return 6;
2366 else if (format == "OEMText" ) return 7;
2367 else if (format == "DeviceIndependentBitmap" ) return 8;
2368 else if (format == "Palette" ) return 9;
2369 else if (format == "PenData" ) return 10;
2370 else if (format == "RiffAudio" ) return 11;
2371 else if (format == "WaveAudio" ) return 12;
2372 else if (format == "UnicodeText" ) return 13;
2373 else if (format == "EnhancedMetafile" ) return 14;
2374 else if (format == "FileDrop" ) return 15;
2375 else if (format == "Locale" ) return 16;
2377 return (int)Win32RegisterClipboardFormat(format);
2380 internal override IntPtr ClipboardOpen(bool primary_selection) {
2381 // Win32 does not have primary selection
2382 Win32OpenClipboard(FosterParent);
2386 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2391 if (handle != clip_magic) {
2399 format = Win32EnumClipboardFormats(format);
2403 } while (format != 0);
2406 result = new int[count];
2410 format = Win32EnumClipboardFormats(format);
2412 result[count++] = (int)format;
2414 } while (format != 0);
2420 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2425 if (handle != clip_magic) {
2426 throw new ArgumentException("handle is not a valid clipboard handle");
2429 hmem = Win32GetClipboardData((uint)type);
2430 if (hmem == IntPtr.Zero) {
2434 data = Win32GlobalLock(hmem);
2435 if (data == IntPtr.Zero) {
2436 uint error = Win32GetLastError();
2437 Console.WriteLine("Error: {0}", error);
2443 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2444 obj = AnsiToString(data);
2445 } else switch ((ClipboardFormats)type) {
2446 case ClipboardFormats.CF_TEXT: {
2447 obj = AnsiToString(data);
2451 case ClipboardFormats.CF_DIB: {
2452 obj = DIBtoImage(data);
2456 case ClipboardFormats.CF_UNICODETEXT: {
2457 obj = UnicodeToString(data);
2462 if (converter != null && !converter(type, data, out obj)) {
2468 Win32GlobalUnlock(hmem);
2474 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
2479 if (handle != clip_magic) {
2480 throw new ArgumentException("handle is not a valid clipboard handle");
2485 Win32EmptyClipboard();
2490 if (obj is string) {
2491 type = (int)ClipboardFormats.CF_UNICODETEXT;
2492 } else if (obj is Image) {
2493 type = (int)ClipboardFormats.CF_DIB;
2497 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2498 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2499 Win32SetClipboardData((uint)type, hmem);
2501 } else switch((ClipboardFormats)type) {
2502 case ClipboardFormats.CF_UNICODETEXT: {
2503 hmem = Marshal.StringToHGlobalUni((string)obj);
2504 Win32SetClipboardData((uint)type, hmem);
2508 case ClipboardFormats.CF_TEXT: {
2509 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2510 Win32SetClipboardData((uint)type, hmem);
2514 case ClipboardFormats.CF_BITMAP:
2515 case ClipboardFormats.CF_DIB: {
2516 data = ImageToDIB((Image)obj);
2518 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2519 hmem_ptr = Win32GlobalLock(hmem);
2520 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2521 Win32GlobalUnlock(hmem);
2522 Win32SetClipboardData((uint)ClipboardFormats.CF_DIB, hmem);
2527 if (converter != null && converter(ref type, obj, out data)) {
2528 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2529 hmem_ptr = Win32GlobalLock(hmem);
2530 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2531 Win32GlobalUnlock(hmem);
2532 Win32SetClipboardData((uint)type, hmem);
2539 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
2541 Win32DnD.RegisterDropTarget(hwnd);
2543 Win32DnD.UnregisterDropTarget(hwnd);
2547 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
2548 return Win32DnD.StartDrag(hwnd, data, allowedEffects);
2551 // XXX this doesn't work at all for FrameStyle.Dashed - it draws like Thick, and in the Thick case
2552 // the corners are drawn incorrectly.
2553 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
2557 COLORREF clrRef = new COLORREF();
2559 // If we want the standard hatch pattern we would
2560 // need to create a brush
2562 clrRef.R = backColor.R;
2563 clrRef.G = backColor.G;
2564 clrRef.B = backColor.B;
2567 pen = Win32CreatePen (style == FrameStyle.Thick ? PenStyle.PS_SOLID : PenStyle.PS_DASH,
2568 style == FrameStyle.Thick ? 4 : 2, ref clrRef);
2570 hdc = Win32GetDC(IntPtr.Zero);
2571 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2572 oldpen = Win32SelectObject(hdc, pen);
2574 Win32MoveToEx(hdc, rectangle.Left, rectangle.Top, IntPtr.Zero);
2575 if ((rectangle.Width > 0) && (rectangle.Height > 0)) {
2576 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2577 Win32LineTo(hdc, rectangle.Right, rectangle.Bottom);
2578 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2579 Win32LineTo(hdc, rectangle.Left, rectangle.Top);
2581 if (rectangle.Width > 0) {
2582 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2584 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2588 Win32SelectObject(hdc, oldpen);
2589 Win32DeleteObject(pen);
2591 Win32ReleaseDC(IntPtr.Zero, hdc);
2594 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
2599 COLORREF clrRef = new COLORREF();
2604 Win32ClientToScreen(IntPtr.Zero, ref pt);
2606 // If we want the standard hatch pattern we would
2607 // need to create a brush
2609 clrRef.R = backColor.R;
2610 clrRef.G = backColor.G;
2611 clrRef.B = backColor.B;
2614 pen = Win32CreatePen(PenStyle.PS_SOLID, 1, ref clrRef);
2616 hdc = Win32GetDC(IntPtr.Zero);
2617 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2618 oldpen = Win32SelectObject(hdc, pen);
2620 Win32MoveToEx(hdc, pt.x + start.X, pt.y + start.Y, IntPtr.Zero);
2621 Win32LineTo(hdc, pt.x + end.X, pt.y + end.Y);
2623 Win32SelectObject(hdc, oldpen);
2624 Win32DeleteObject(pen);
2626 Win32ReleaseDC(IntPtr.Zero, hdc);
2629 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
2634 rect.left = rectangle.Left;
2635 rect.top = rectangle.Top;
2636 rect.right = rectangle.Right;
2637 rect.bottom = rectangle.Bottom;
2642 COLORREF clrRef = new COLORREF();
2644 clrRef.R = backColor.R;
2645 clrRef.G = backColor.G;
2646 clrRef.B = backColor.B;
2649 brush = Win32CreateSolidBrush (clrRef);
2651 hdc = Win32GetDC(IntPtr.Zero);
2652 oldbrush = Win32SelectObject(hdc, brush);
2654 Win32PatBlt (hdc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, PatBltRop.DSTINVERT);
2656 Win32SelectObject(hdc, oldbrush);
2657 Win32DeleteObject(brush);
2659 Win32ReleaseDC(IntPtr.Zero, hdc);
2662 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
2671 Win32ClientToScreen(handle, ref pt);
2673 // If we want the standard hatch pattern we would
2674 // need to create a brush
2677 pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
2679 hdc = Win32GetDC(IntPtr.Zero);
2680 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2681 oldpen = Win32SelectObject(hdc, pen);
2683 Control c = Control.FromHandle (handle);
2686 Win32GetWindowRect (c.Handle, out window_rect);
2687 Region r = new Region (new Rectangle(window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top));
2688 Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
2691 Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
2692 if ((rect.Width > 0) && (rect.Height > 0)) {
2693 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2694 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
2695 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2696 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
2698 if (rect.Width > 0) {
2699 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2701 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2705 Win32SelectObject(hdc, oldpen);
2706 Win32DeleteObject(pen);
2708 Win32ExtSelectClipRgn(hdc, IntPtr.Zero, (int) ClipCombineMode.RGN_COPY);
2710 Win32ReleaseDC(IntPtr.Zero, hdc);
2713 internal override SizeF GetAutoScaleSize(Font font) {
2716 string magic_string = "The quick brown fox jumped over the lazy dog.";
2717 double magic_number = 44.549996948242189;
2719 g = Graphics.FromHwnd(FosterParent);
2721 width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
2722 return new SizeF(width, font.Height);
2725 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2726 return Win32SendMessage(hwnd, message, wParam, lParam);
2729 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2730 return Win32PostMessage(hwnd, message, wParam, lParam);
2733 internal override int SendInput (IntPtr hwnd, Queue keys) {
2734 INPUT[] inputs = new INPUT[keys.Count];
2735 const Int32 INPUT_KEYBOARD = 1;
2738 while (keys.Count > 0) {
2739 MSG msg = (MSG)keys.Dequeue();
2742 inputs[i].ki.wScan = 0;
2743 inputs[i].ki.time = 0;
2744 inputs[i].ki.dwFlags = (Int32)(msg.message == Msg.WM_KEYUP ? InputFlags.KEYEVENTF_KEYUP : 0);
2745 inputs[i].ki.wVk = (short)msg.wParam.ToInt32();
2746 inputs[i].type = INPUT_KEYBOARD;
2749 returns = Win32SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
2751 return (int) returns;
2754 internal override int KeyboardSpeed {
2757 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDSPEED, 0, ref speed, 0);
2759 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
2765 internal override int KeyboardDelay {
2768 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDDELAY, 0, ref delay, 0);
2770 // Return values must range from 0 to 4, 0 meaning 250ms,
2771 // and 4 meaning 1000 ms.
2777 private class WinBuffer
2780 public IntPtr bitmap;
2782 public WinBuffer (IntPtr hdc, IntPtr bitmap)
2785 this.bitmap = bitmap;
2789 internal override void CreateOffscreenDrawable (IntPtr handle, int width, int height, out object offscreen_drawable)
2791 Graphics destG = Graphics.FromHwnd (handle);
2792 IntPtr destHdc = destG.GetHdc ();
2794 IntPtr srcHdc = Win32CreateCompatibleDC (destHdc);
2795 IntPtr srcBmp = Win32CreateCompatibleBitmap (destHdc, width, height);
2796 Win32SelectObject (srcHdc, srcBmp);
2798 offscreen_drawable = new WinBuffer (srcHdc, srcBmp);
2800 destG.ReleaseHdc (destHdc);
2803 internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
2805 return Graphics.FromHdc (((WinBuffer)offscreen_drawable).hdc);
2808 internal override void BlitFromOffscreen (IntPtr dest_handle, Graphics dest_dc, object offscreen_drawable, Graphics offscreen_dc, Rectangle r)
2810 WinBuffer wb = (WinBuffer)offscreen_drawable;
2812 IntPtr destHdc = dest_dc.GetHdc ();
2813 Win32BitBlt (destHdc, r.Left, r.Top, r.Width, r.Height, wb.hdc, r.Left, r.Top, TernaryRasterOperations.SRCCOPY);
2814 dest_dc.ReleaseHdc (destHdc);
2817 internal override void DestroyOffscreenDrawable (object offscreen_drawable)
2819 WinBuffer wb = (WinBuffer)offscreen_drawable;
2821 Win32DeleteObject (wb.bitmap);
2822 Win32DeleteDC (wb.hdc);
2825 internal override event EventHandler Idle;
2826 #endregion // Public Static Methods
2828 #region Win32 Imports
2829 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
2830 private extern static uint Win32GetLastError();
2832 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2833 internal extern static IntPtr Win32CreateWindow(uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lParam);
2835 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
2836 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
2838 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2839 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
2841 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2842 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
2844 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
2845 internal extern static bool Win32TranslateMessage(ref MSG msg);
2847 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2848 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
2850 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
2851 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
2853 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2854 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2856 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2857 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2859 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2860 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
2862 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2863 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
2865 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
2866 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
2868 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2869 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
2871 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2872 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
2874 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
2875 private extern static IntPtr Win32ShowCursor(bool bShow);
2877 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
2878 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
2880 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
2881 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
2883 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
2884 private extern static bool Win32DestroyCursor(IntPtr hCursor);
2886 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2887 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2889 //[DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2890 //private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2892 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
2893 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
2895 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
2896 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
2898 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
2899 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
2901 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
2902 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2904 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
2905 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2907 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
2908 private extern static IntPtr Win32GetDC(IntPtr hWnd);
2910 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
2911 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
2913 //[DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
2914 //private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
2916 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
2917 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
2919 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2920 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
2922 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2923 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
2925 //[DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2926 //private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
2928 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
2929 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
2931 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
2932 private extern static IntPtr Win32ReleaseCapture();
2934 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
2935 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
2937 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
2938 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
2940 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
2941 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
2943 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
2944 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
2946 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
2947 private extern static IntPtr Win32GetParent(IntPtr hWnd);
2949 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
2950 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
2952 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
2953 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
2955 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
2956 private extern static bool Win32GetCursorPos(out POINT lpPoint);
2958 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
2959 private extern static bool Win32SetCursorPos(int x, int y);
2961 //[DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
2962 //private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
2964 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
2965 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
2967 //[DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
2968 //private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
2970 [DllImport ("gdi32.dll", EntryPoint="CreateSolidBrush", CallingConvention=CallingConvention.StdCall)]
2971 private extern static IntPtr Win32CreateSolidBrush(COLORREF clrRef);
2973 [DllImport ("gdi32.dll", EntryPoint="PatBlt", CallingConvention=CallingConvention.StdCall)]
2974 private extern static int Win32PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, PatBltRop dwRop);
2976 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
2977 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
2979 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
2980 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
2982 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2983 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
2985 [DllImport ("user32.dll", EntryPoint="GetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2986 private extern static uint Win32GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out LayeredWindowAttributes pwdFlags);
2988 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
2989 private extern static bool Win32DeleteObject(IntPtr o);
2991 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
2992 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
2994 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
2995 private extern static IntPtr Win32GetDesktopWindow();
2997 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
2998 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
3000 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
3001 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
3003 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
3004 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
3006 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
3007 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
3009 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
3010 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
3012 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
3013 internal extern static IntPtr Win32GetFocus();
3015 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
3016 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
3018 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
3019 private extern static bool Win32DestroyCaret();
3021 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
3022 private extern static bool Win32ShowCaret(IntPtr hwnd);
3024 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
3025 private extern static bool Win32HideCaret(IntPtr hwnd);
3027 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
3028 private extern static bool Win32SetCaretPos(int X, int Y);
3030 //[DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
3031 //private extern static uint Win32GetCaretBlinkTime();
3033 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3034 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
3036 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
3037 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
3039 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3040 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3042 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3043 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3045 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3046 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3048 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3049 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3051 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3052 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3054 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3055 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3057 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3058 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3060 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3061 private extern static IntPtr Win32GetActiveWindow();
3063 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
3064 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
3066 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3067 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
3069 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
3070 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
3072 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
3073 private extern static bool IsWindowEnabled(IntPtr hwnd);
3075 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
3076 private extern static bool IsWindowVisible(IntPtr hwnd);
3078 //[DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
3079 //private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
3081 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3082 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3084 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3085 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3087 [DllImport ("user32.dll", EntryPoint="SendInput", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3088 private extern static UInt32 Win32SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray)] INPUT[] inputs, Int32 cbSize);
3090 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3091 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
3093 //[DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3094 //private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
3096 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3097 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
3099 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
3100 private extern static bool Win32OpenClipboard(IntPtr hwnd);
3102 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
3103 private extern static bool Win32EmptyClipboard();
3105 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3106 private extern static uint Win32RegisterClipboardFormat(string format);
3108 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
3109 private extern static bool Win32CloseClipboard();
3111 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
3112 private extern static uint Win32EnumClipboardFormats(uint format);
3114 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
3115 private extern static IntPtr Win32GetClipboardData(uint format);
3117 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
3118 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
3120 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
3121 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
3123 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
3124 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
3126 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
3127 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
3129 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
3130 internal extern static uint Win32GlobalSize(IntPtr hMem);
3132 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
3133 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
3135 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
3136 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
3138 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
3139 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
3141 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3142 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
3144 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3145 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
3147 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
3148 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
3150 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3151 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
3153 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3154 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
3156 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
3157 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
3159 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3160 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
3162 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3163 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
3165 [DllImport("gdi32.dll", EntryPoint = "ExcludeClipRect", CallingConvention = CallingConvention.StdCall)]
3166 internal extern static int Win32ExcludeClipRect (IntPtr hdc, int left, int top, int right, int bottom);
3168 [DllImport ("gdi32.dll", EntryPoint="ExtSelectClipRgn", CallingConvention=CallingConvention.StdCall)]
3169 internal extern static int Win32ExtSelectClipRgn(IntPtr hdc, IntPtr hrgn, int mode);
3171 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3172 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
3174 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3175 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
3177 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3178 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
3180 [DllImport ("user32.dll", EntryPoint="ClipCursor", CallingConvention=CallingConvention.StdCall)]
3181 internal extern static bool Win32ClipCursor (ref RECT lpRect);
3183 [DllImport ("user32.dll", EntryPoint="GetClipCursor", CallingConvention=CallingConvention.StdCall)]
3184 internal extern static bool Win32GetClipCursor (out RECT lpRect);
3186 [DllImport ("gdi32.dll", EntryPoint="BitBlt", CallingConvention=CallingConvention.StdCall)]
3187 internal static extern bool Win32BitBlt (IntPtr hObject, int nXDest, int nYDest, int nWidth,
3188 int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
3190 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3191 internal static extern IntPtr Win32CreateCompatibleDC (IntPtr hdc);
3193 [DllImport ("gdi32.dll", EntryPoint="DeleteDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3194 internal static extern bool Win32DeleteDC (IntPtr hdc);
3196 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleBitmap", CallingConvention=CallingConvention.StdCall)]
3197 internal static extern IntPtr Win32CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight);