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 + 1; } }
105 public int Width { get { return right - left + 1; } }
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,
156 internal enum WindowPlacementFlags {
160 SW_SHOWMINIMIZED = 2,
161 SW_SHOWMAXIMIZED = 3,
163 SW_SHOWNOACTIVATE = 4,
166 SW_SHOWMINNOACTIVE = 7,
170 SW_FORCEMINIMIZE = 11,
174 [StructLayout(LayoutKind.Sequential)]
175 private struct WINDOWPLACEMENT {
176 internal uint length;
178 internal WindowPlacementFlags showCmd;
179 internal POINT ptMinPosition;
180 internal POINT ptMaxPosition;
181 internal RECT rcNormalPosition;
184 [StructLayout(LayoutKind.Sequential)]
185 internal struct NCCALCSIZE_PARAMS {
189 internal IntPtr lppos;
193 private enum TMEFlags {
194 TME_HOVER = 0x00000001,
195 TME_LEAVE = 0x00000002,
196 TME_NONCLIENT = 0x00000010,
197 TME_QUERY = unchecked((int)0x40000000),
198 TME_CANCEL = unchecked((int)0x80000000)
201 [StructLayout(LayoutKind.Sequential)]
202 private struct TRACKMOUSEEVENT {
204 internal TMEFlags dwFlags;
205 internal IntPtr hWnd;
206 internal int dwHoverTime;
209 [StructLayout(LayoutKind.Sequential)]
210 private struct PAINTSTRUCT {
213 internal RECT rcPaint;
214 internal int fRestore;
215 internal int fIncUpdate;
216 internal int Reserved1;
217 internal int Reserved2;
218 internal int Reserved3;
219 internal int Reserved4;
220 internal int Reserved5;
221 internal int Reserved6;
222 internal int Reserved7;
223 internal int Reserved8;
226 [StructLayout(LayoutKind.Sequential)]
227 internal struct KEYBDINPUT {
229 internal short wScan;
230 internal Int32 dwFlags;
232 internal UIntPtr dwExtraInfo;
235 [StructLayout(LayoutKind.Sequential)]
236 internal struct MOUSEINPUT {
239 internal Int32 mouseData;
240 internal Int32 dwFlags;
242 internal UIntPtr dwExtraInfo;
245 [StructLayout(LayoutKind.Sequential)]
246 internal struct HARDWAREINPUT {
248 internal short wParamL;
249 internal short wParamH;
252 [StructLayout(LayoutKind.Explicit)]
253 internal struct INPUT {
258 internal MOUSEINPUT mi;
261 internal KEYBDINPUT ki;
264 internal HARDWAREINPUT hi;
268 internal enum InputFlags {
269 KEYEVENTF_EXTENDEDKEY = 0x0001,
270 KEYEVENTF_KEYUP = 0x0002,
271 KEYEVENTF_SCANCODE = 0x0003,
272 KEYEVENTF_UNICODE = 0x0004,
275 internal enum ClassStyle {
276 CS_VREDRAW = 0x00000001,
277 CS_HREDRAW = 0x00000002,
278 CS_KEYCVTWINDOW = 0x00000004,
279 CS_DBLCLKS = 0x00000008,
280 CS_OWNDC = 0x00000020,
281 CS_CLASSDC = 0x00000040,
282 CS_PARENTDC = 0x00000080,
283 CS_NOKEYCVT = 0x00000100,
284 CS_NOCLOSE = 0x00000200,
285 CS_SAVEBITS = 0x00000800,
286 CS_BYTEALIGNCLIENT = 0x00001000,
287 CS_BYTEALIGNWINDOW = 0x00002000,
288 CS_GLOBALCLASS = 0x00004000,
291 CS_DROPSHADOW = 0x00020000
294 internal enum SetWindowPosZOrder {
302 internal enum SetWindowPosFlags {
303 SWP_ASYNCWINDOWPOS = 0x4000,
304 SWP_DEFERERASE = 0x2000,
305 SWP_DRAWFRAME = 0x0020,
306 SWP_FRAMECHANGED = 0x0020,
307 SWP_HIDEWINDOW = 0x0080,
308 SWP_NOACTIVATE = 0x0010,
309 SWP_NOCOPYBITS = 0x0100,
311 SWP_NOOWNERZORDER = 0x0200,
312 SWP_NOREDRAW = 0x0008,
313 SWP_NOREPOSITION = 0x0200,
314 SWP_NOENDSCHANGING = 0x0400,
316 SWP_NOZORDER = 0x0004,
317 SWP_SHOWWINDOW = 0x0040
320 internal enum GetSysColorIndex {
322 COLOR_BACKGROUND = 1,
323 COLOR_ACTIVECAPTION = 2,
324 COLOR_INACTIVECAPTION = 3,
327 COLOR_WINDOWFRAME = 6,
329 COLOR_WINDOWTEXT = 8,
330 COLOR_CAPTIONTEXT = 9,
331 COLOR_ACTIVEBORDER = 10,
332 COLOR_INACTIVEBORDER = 11,
333 COLOR_APPWORKSPACE = 12,
334 COLOR_HIGHLIGHT = 13,
335 COLOR_HIGHLIGHTTEXT = 14,
337 COLOR_BTNSHADOW = 16,
340 COLOR_INACTIVECAPTIONTEXT = 19,
341 COLOR_BTNHIGHLIGHT = 20,
342 COLOR_3DDKSHADOW = 21,
348 COLOR_GRADIENTACTIVECAPTION = 27,
349 COLOR_GRADIENTINACTIVECAPTION = 28,
350 COLOR_MENUHIGHLIGHT = 29,
356 COLOR_3DHIGHLIGHT = 20,
357 COLOR_3DHILIGHT = 20,
358 COLOR_BTNHILIGHT = 20,
359 COLOR_MAXVALUE = 24,/* Maximum value */
362 private enum LoadCursorType {
371 IDC_SIZENWSE = 32642,
372 IDC_SIZENESW = 32643,
378 IDC_APPSTARTING = 32650,
384 private enum WindowLong {
395 private enum LogBrushStyle {
404 BS_DIBPATTERN8X8 = 8,
409 private enum LogBrushHatch {
410 HS_HORIZONTAL = 0, /* ----- */
411 HS_VERTICAL = 1, /* ||||| */
412 HS_FDIAGONAL = 2, /* \\\\\ */
413 HS_BDIAGONAL = 3, /* ///// */
414 HS_CROSS = 4, /* +++++ */
415 HS_DIAGCROSS = 5, /* xxxxx */
418 internal struct COLORREF {
425 [StructLayout(LayoutKind.Sequential)]
426 private struct LOGBRUSH {
427 internal LogBrushStyle lbStyle;
428 internal COLORREF lbColor;
429 internal LogBrushHatch lbHatch;
432 [StructLayout(LayoutKind.Sequential)]
433 internal struct TEXTMETRIC {
434 internal int tmHeight;
435 internal int tmAscent;
436 internal int tmDescent;
437 internal int tmInternalLeading;
438 internal int tmExternalLeading;
439 internal int tmAveCharWidth;
440 internal int tmMaxCharWidth;
441 internal int tmWeight;
442 internal int tmOverhang;
443 internal int tmDigitizedAspectX;
444 internal int tmDigitizedAspectY;
445 internal short tmFirstChar;
446 internal short tmLastChar;
447 internal short tmDefaultChar;
448 internal short tmBreakChar;
449 internal byte tmItalic;
450 internal byte tmUnderlined;
451 internal byte tmStruckOut;
452 internal byte tmPitchAndFamily;
453 internal byte tmCharSet;
456 public enum TernaryRasterOperations : uint
458 SRCCOPY = 0x00CC0020,
459 SRCPAINT = 0x00EE0086,
461 SRCINVERT = 0x00660046,
462 SRCERASE = 0x00440328,
463 NOTSRCCOPY = 0x00330008,
464 NOTSRCERASE = 0x001100A6,
465 MERGECOPY = 0x00C000CA,
466 MERGEPAINT = 0x00BB0226,
467 PATCOPY = 0x00F00021,
468 PATPAINT = 0x00FB0A09,
469 PATINVERT = 0x005A0049,
470 DSTINVERT = 0x00550009,
471 BLACKNESS = 0x00000042,
472 WHITENESS = 0x00FF0062
476 private enum ScrollWindowExFlags {
478 SW_SCROLLCHILDREN = 0x0001,
479 SW_INVALIDATE = 0x0002,
481 SW_SMOOTHSCROLL = 0x0010
484 internal enum SystemMetrics {
501 SM_CXFULLSCREEN = 16,
502 SM_CYFULLSCREEN = 17,
503 SM_CYKANJIWINDOW = 18,
504 SM_MOUSEPRESENT = 19,
523 SM_CXICONSPACING = 38,
524 SM_CYICONSPACING = 39,
525 SM_MENUDROPALIGNMENT = 40,
528 SM_CMOUSEBUTTONS = 43,
529 SM_CXFIXEDFRAME = SM_CXDLGFRAME,
530 SM_CYFIXEDFRAME = SM_CYDLGFRAME,
531 SM_CXSIZEFRAME = SM_CXFRAME,
532 SM_CYSIZEFRAME = SM_CYFRAME,
536 SM_CXMINSPACING = 47,
537 SM_CYMINSPACING = 48,
560 SM_MIDEASTENABLED = 74,
561 SM_MOUSEWHEELPRESENT = 75,
562 SM_XVIRTUALSCREEN = 76,
563 SM_YVIRTUALSCREEN = 77,
564 SM_CXVIRTUALSCREEN = 78,
565 SM_CYVIRTUALSCREEN = 79,
567 SM_SAMEDISPLAYFORMAT = 81,
569 SM_CXFOCUSBORDER = 83,
570 SM_CYFOCUSBORDER = 84,
576 // We'll only support _WIN32_IE < 0x0500 for now
577 internal enum NotifyIconMessage {
578 NIM_ADD = 0x00000000,
579 NIM_MODIFY = 0x00000001,
580 NIM_DELETE = 0x00000002,
584 internal enum NotifyIconFlags {
585 NIF_MESSAGE = 0x00000001,
586 NIF_ICON = 0x00000002,
587 NIF_TIP = 0x00000004,
590 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
591 internal struct NOTIFYICONDATA {
592 internal uint cbSize;
593 internal IntPtr hWnd;
595 internal NotifyIconFlags uFlags;
596 internal uint uCallbackMessage;
597 internal IntPtr hIcon;
598 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
599 internal string szTip;
603 internal enum DCExFlags {
604 DCX_WINDOW = 0x00000001,
605 DCX_CACHE = 0x00000002,
606 DCX_NORESETATTRS = 0x00000004,
607 DCX_CLIPCHILDREN = 0x00000008,
608 DCX_CLIPSIBLINGS = 0x00000010,
609 DCX_PARENTCLIP = 0x00000020,
610 DCX_EXCLUDERGN = 0x00000040,
611 DCX_INTERSECTRGN = 0x00000080,
612 DCX_EXCLUDEUPDATE = 0x00000100,
613 DCX_INTERSECTUPDATE = 0x00000200,
614 DCX_LOCKWINDOWUPDATE = 0x00000400,
615 DCX_USESTYLE = 0x00010000,
616 DCX_VALIDATE = 0x00200000
619 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
620 internal struct CLIENTCREATESTRUCT {
621 internal IntPtr hWindowMenu;
622 internal uint idFirstChild;
625 private enum ClassLong : int {
627 GCL_HBRBACKGROUND = -10,
631 GCL_CBWNDEXTRA = -18,
632 GCL_CBCLSEXTRA = -20,
640 internal enum GAllocFlags : uint {
642 GMEM_MOVEABLE = 0x0002,
643 GMEM_NOCOMPACT = 0x0010,
644 GMEM_NODISCARD = 0x0020,
645 GMEM_ZEROINIT = 0x0040,
646 GMEM_MODIFY = 0x0080,
647 GMEM_DISCARDABLE = 0x0100,
648 GMEM_NOT_BANKED = 0x1000,
650 GMEM_DDESHARE = 0x2000,
651 GMEM_NOTIFY = 0x4000,
652 GMEM_LOWER = GMEM_NOT_BANKED,
653 GMEM_VALID_FLAGS = 0x7F72,
654 GMEM_INVALID_HANDLE = 0x8000,
655 GHND = (GMEM_MOVEABLE | GMEM_ZEROINIT),
656 GPTR = (GMEM_FIXED | GMEM_ZEROINIT)
659 internal enum ROP2DrawMode : int {
679 internal enum PenStyle : int {
691 internal enum PatBltRop : int {
693 PATINVERT = 0x5a0049,
694 DSTINVERT = 0x550009,
695 BLACKNESS = 0x000042,
696 WHITENESS = 0xff0062,
699 internal enum StockObject : int {
706 HOLLOW_BRUSH = NULL_BRUSH,
711 ANSI_FIXED_FONT = 11,
714 DEVICE_DEFAULT_FONT = 14,
715 DEFAULT_PALETTE = 15,
716 SYSTEM_FIXED_FONT = 16
719 internal enum HatchStyle : int {
729 internal enum SndFlags : int {
732 SND_NODEFAULT = 0x0002,
736 SND_NOWAIT = 0x00002000,
737 SND_ALIAS = 0x00010000,
738 SND_ALIAS_ID = 0x00110000,
739 SND_FILENAME = 0x00020000,
740 SND_RESOURCE = 0x00040004,
742 SND_APPLICATION = 0x0080,
746 internal enum LayeredWindowAttributes : int {
754 #region Constructor & Destructor
755 private XplatUIWin32() {
756 // Handle singleton stuff first
759 mouse_state = MouseButtons.None;
760 mouse_position = Point.Empty;
762 message_queue = new Queue();
764 themes_enabled = false;
766 wnd_proc = new WndProc(NativeWindow.WndProc);
768 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);
770 if (FosterParent==IntPtr.Zero) {
771 Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
774 scroll_height = Win32GetSystemMetrics(SystemMetrics.SM_CYHSCROLL);
775 scroll_width = Win32GetSystemMetrics(SystemMetrics.SM_CXVSCROLL);
777 timer_list = new Hashtable ();
778 registered_classes = new Hashtable ();
780 #endregion // Constructor & Destructor
782 #region Private Support Methods
784 private string RegisterWindowClass (int classStyle)
788 lock (registered_classes) {
789 class_name = (string)registered_classes[classStyle];
791 if (class_name != null)
794 class_name = string.Format ("Mono.WinForms.{0}.{1}", System.Threading.Thread.GetDomainID ().ToString (), classStyle);
798 wndClass.style = classStyle;
799 wndClass.lpfnWndProc = wnd_proc;
800 wndClass.cbClsExtra = 0;
801 wndClass.cbWndExtra = 0;
802 wndClass.hbrBackground = (IntPtr)(GetSysColorIndex.COLOR_WINDOW + 1);
803 wndClass.hCursor = Win32LoadCursor (IntPtr.Zero, LoadCursorType.IDC_ARROW);
804 wndClass.hIcon = IntPtr.Zero;
805 wndClass.hInstance = IntPtr.Zero;
806 wndClass.lpszClassName = class_name;
807 wndClass.lpszMenuName = "";
809 bool result = Win32RegisterClass (ref wndClass);
812 Win32MessageBox (IntPtr.Zero, "Could not register the window class, win32 error " + Win32GetLastError ().ToString (), "Oops", 0);
814 registered_classes[classStyle] = class_name;
820 private static bool RetrieveMessage(ref MSG msg) {
823 if (message_queue.Count == 0) {
827 message = (MSG)message_queue.Dequeue();
833 private static bool StoreMessage(ref MSG msg) {
834 MSG message = new MSG();
837 message_queue.Enqueue(message);
842 internal static String AnsiToString(IntPtr ansi_data) {
843 return (string)Marshal.PtrToStringAnsi(ansi_data);
846 internal static String UnicodeToString(IntPtr unicode_data) {
847 return (string)Marshal.PtrToStringUni(unicode_data);
850 internal static Image DIBtoImage(IntPtr dib_data) {
851 BITMAPINFOHEADER bmi;
862 bmi = (BITMAPINFOHEADER)Marshal.PtrToStructure(dib_data, typeof(BITMAPINFOHEADER));
864 ncolors = (int)bmi.biClrUsed;
866 if (bmi.biBitCount < 24) {
867 ncolors = (int)(1 << bmi.biBitCount);
870 //palettesize = ncolors * 4;
872 imagesize = (int)bmi.biSizeImage;
873 if (imagesize == 0) {
874 imagesize = (int)(((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);
877 switch(bmi.biBitCount) {
878 case 1: { // Monochrome
879 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format1bppIndexed);
880 palette = new int[2];
885 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format4bppIndexed);
886 palette = new int[16];
891 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format8bppIndexed);
892 palette = new int[256];
898 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format32bppArgb);
899 palette = new int[0];
904 throw new Exception("Unexpected number of bits:" + bmi.biBitCount.ToString());
908 if (bmi.biBitCount < 24) {
909 pal = bmp.Palette; // Managed palette
910 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER))), palette, 0, palette.Length);
912 for (int i = 0; i < ncolors; i++) {
913 pal.Entries[i] = Color.FromArgb(palette[i] | unchecked((int)0xff000000));
918 bytesPerLine = (int)((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3);
919 bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
921 imagebits = new byte[bytesPerLine];
923 for (int y = 0; y < bmi.biHeight; y++) {
924 // Copy from source to managed
925 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER)) + palette.Length * 4 + bytesPerLine * y), imagebits, 0, bytesPerLine);
927 // Copy from managed to dest
928 Marshal.Copy(imagebits, 0, (IntPtr)((int)bits.Scan0 + bits.Stride * (bmi.biHeight - 1 - y)), imagebits.Length);
931 bmp.UnlockBits(bits);
936 internal static byte[] ImageToDIB(Image image) {
941 ms = new MemoryStream();
942 image.Save(ms, ImageFormat.Bmp);
943 buffer = ms.GetBuffer();
945 // Filter out the file header
946 retbuf = new byte[buffer.Length];
947 Array.Copy(buffer, 14, retbuf, 0, buffer.Length - 14);
951 internal static IntPtr DupGlobalMem(IntPtr mem) {
957 len = Win32GlobalSize(mem);
958 mem_ptr = Win32GlobalLock(mem);
960 dup = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE, (int)len);
961 dup_ptr = Win32GlobalLock(dup);
963 Win32CopyMemory(dup_ptr, mem_ptr, (int)len);
965 Win32GlobalUnlock(mem);
966 Win32GlobalUnlock(dup);
970 #endregion // Private Support Methods
972 #region Static Properties
973 internal override Keys ModifierKeys {
978 key_state = Keys.None;
980 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
981 if ((state & 0x8000) != 0) {
982 key_state |= Keys.Shift;
984 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
985 if ((state & 0x8000) != 0) {
986 key_state |= Keys.Control;
989 state = Win32GetKeyState(VirtualKeys.VK_MENU);
990 if ((state & 0x8000) != 0) {
991 key_state |= Keys.Alt;
998 internal override MouseButtons MouseButtons {
1004 internal override Point MousePosition {
1006 return mouse_position;
1010 internal override Size MouseHoverSize {
1015 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref width, 0);
1016 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref height, 0);
1017 return new Size(width, height);
1021 internal override int MouseHoverTime {
1025 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERTIME, 0, ref time, 0);
1030 internal override int HorizontalScrollBarHeight {
1032 return scroll_height;
1036 internal override bool UserClipWontExposeParent {
1043 internal override int VerticalScrollBarWidth {
1045 return scroll_width;
1049 internal override int MenuHeight {
1051 return Win32GetSystemMetrics(SystemMetrics.SM_CYMENU);
1055 internal override bool DropTarget {
1062 //throw new NotImplementedException("Need to figure out D'n'D for Win32");
1067 internal override int Caption {
1069 return Win32GetSystemMetrics(SystemMetrics.SM_CYCAPTION);
1073 internal override Size CursorSize {
1075 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CYCURSOR));
1079 internal override bool DragFullWindows {
1085 internal override Size DragSize {
1087 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXDRAG), Win32GetSystemMetrics(SystemMetrics.SM_CYDRAG));
1091 internal override Size FrameBorderSize {
1093 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXFRAME), Win32GetSystemMetrics(SystemMetrics.SM_CYFRAME));
1097 internal override Size IconSize {
1099 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXICON), Win32GetSystemMetrics(SystemMetrics.SM_CYICON));
1103 internal override Size MaxWindowTrackSize {
1105 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMAXTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMAXTRACK));
1109 internal override Size MinimizedWindowSize {
1111 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINIMIZED), Win32GetSystemMetrics(SystemMetrics.SM_CYMINIMIZED));
1115 internal override Size MinimizedWindowSpacingSize {
1117 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINSPACING), Win32GetSystemMetrics(SystemMetrics.SM_CYMINSPACING));
1121 internal override Size MinimumWindowSize {
1123 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMIN), Win32GetSystemMetrics(SystemMetrics.SM_CYMIN));
1127 internal override Size MinWindowTrackSize {
1129 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMINTRACK));
1133 internal override Size SmallIconSize {
1135 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXSMICON), Win32GetSystemMetrics(SystemMetrics.SM_CYSMICON));
1139 internal override int MouseButtonCount {
1141 return Win32GetSystemMetrics(SystemMetrics.SM_CMOUSEBUTTONS);
1145 internal override bool MouseButtonsSwapped {
1147 return Win32GetSystemMetrics(SystemMetrics.SM_SWAPBUTTON) != 0;
1151 internal override bool MouseWheelPresent {
1153 return Win32GetSystemMetrics(SystemMetrics.SM_MOUSEWHEELPRESENT) != 0;
1157 internal override Rectangle VirtualScreen {
1159 return new Rectangle( Win32GetSystemMetrics(SystemMetrics.SM_XVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_YVIRTUALSCREEN),
1160 Win32GetSystemMetrics(SystemMetrics.SM_CXVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYVIRTUALSCREEN));
1164 internal override Rectangle WorkingArea {
1169 Win32SystemParametersInfo(SPIAction.SPI_GETWORKAREA, 0, ref rect, 0);
1170 return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
1171 //return new Rectangle(0, 0, Win32GetSystemMetrics(SystemMetrics.SM.SM_CXSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYSCREEN));
1175 internal override bool ThemesEnabled {
1177 return XplatUIWin32.themes_enabled;
1182 #endregion // Static Properties
1184 #region Singleton Specific Code
1185 public static XplatUIWin32 GetInstance() {
1186 if (instance==null) {
1187 instance=new XplatUIWin32();
1193 public int Reference {
1200 #region Public Static Methods
1201 internal override IntPtr InitializeDriver() {
1205 internal override void ShutdownDriver(IntPtr token) {
1206 Console.WriteLine("XplatUIWin32 ShutdownDriver called");
1210 internal void Version() {
1211 Console.WriteLine("Xplat version $revision: $");
1214 internal override void AudibleAlert() {
1215 Win32PlaySound("Default", IntPtr.Zero, SndFlags.SND_ALIAS | SndFlags.SND_ASYNC | SndFlags.SND_NOSTOP | SndFlags.SND_NOWAIT);
1218 internal override void GetDisplaySize(out Size size) {
1221 Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
1223 size = new Size(rect.right - rect.left, rect.bottom - rect.top);
1226 internal override void EnableThemes() {
1227 themes_enabled=true;
1230 internal override IntPtr CreateWindow(CreateParams cp) {
1231 IntPtr WindowHandle;
1232 IntPtr ParentHandle;
1237 ParentHandle=cp.Parent;
1239 if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
1240 // We need to use our foster parent window until this poor child gets it's parent assigned
1241 ParentHandle = FosterParent;
1244 if ( ((cp.Style & (int)(WindowStyles.WS_CHILD | WindowStyles.WS_POPUP))==0) && ((cp.ExStyle & (int)WindowExStyles.WS_EX_APPWINDOW) == 0)) {
1245 // If we want to be hidden from the taskbar we need to be 'owned' by
1246 // something not on the taskbar. FosterParent is just that
1247 ParentHandle = FosterParent;
1252 string class_name = RegisterWindowClass (cp.ClassStyle);
1253 HwndCreating = hwnd;
1255 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);
1257 HwndCreating = null;
1259 if (WindowHandle==IntPtr.Zero) {
1260 uint error = Win32GetLastError();
1262 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1265 hwnd.ClientWindow = WindowHandle;
1267 Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1269 return WindowHandle;
1272 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1273 CreateParams create_params = new CreateParams();
1275 create_params.Caption = "";
1276 create_params.X = X;
1277 create_params.Y = Y;
1278 create_params.Width = Width;
1279 create_params.Height = Height;
1281 create_params.ClassName=XplatUI.DefaultClassName;
1282 create_params.ClassStyle = 0;
1283 create_params.ExStyle=0;
1284 create_params.Parent=IntPtr.Zero;
1285 create_params.Param=0;
1287 return CreateWindow(create_params);
1290 internal override void DestroyWindow(IntPtr handle) {
1293 hwnd = Hwnd.ObjectFromHandle(handle);
1294 Win32DestroyWindow(handle);
1299 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1300 // We do nothing, Form has to handle WM_GETMINMAXINFO
1304 internal override FormWindowState GetWindowState(IntPtr handle) {
1307 style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1308 if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1309 return FormWindowState.Maximized;
1310 } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1311 return FormWindowState.Minimized;
1313 return FormWindowState.Normal;
1316 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1318 case FormWindowState.Normal: {
1319 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1323 case FormWindowState.Minimized: {
1324 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1328 case FormWindowState.Maximized: {
1329 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1335 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1339 Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1340 Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1342 if ((cp.ExStyle & (int) WindowExStyles.WS_EX_TOOLWINDOW) > 0)
1343 XplatUI.RequestNCRecalc (handle);
1346 internal override double GetWindowTransparency(IntPtr handle)
1348 LayeredWindowAttributes lwa;
1352 if (0 == Win32GetLayeredWindowAttributes (handle, out clrRef, out alpha, out lwa))
1355 return ((double)alpha) / 255.0;
1358 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1359 LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1360 byte opacity = (byte)(transparency*255);
1361 COLORREF clrRef = new COLORREF();
1362 if (key != Color.Empty) {
1366 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1371 Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1374 TransparencySupport support;
1375 bool queried_transparency_support;
1376 internal override TransparencySupport SupportsTransparency() {
1377 if (queried_transparency_support)
1381 support = TransparencySupport.None;
1385 Win32SetLayeredWindowAttributes (IntPtr.Zero, new COLORREF (), 255, LayeredWindowAttributes.LWA_ALPHA);
1387 catch (EntryPointNotFoundException) { flag = false; }
1388 catch { /* swallow everything else */ }
1390 if (flag) support |= TransparencySupport.Set;
1394 LayeredWindowAttributes lwa;
1398 Win32GetLayeredWindowAttributes (IntPtr.Zero, out clrRef, out alpha, out lwa);
1400 catch (EntryPointNotFoundException) { flag = false; }
1401 catch { /* swallow everything else */ }
1403 if (flag) support |= TransparencySupport.Get;
1405 queried_transparency_support = true;
1409 internal override void UpdateWindow(IntPtr handle) {
1410 Win32UpdateWindow(handle);
1413 internal override PaintEventArgs PaintEventStart(IntPtr handle, bool client) {
1416 PaintEventArgs paint_event;
1418 Rectangle clip_rect;
1421 clip_rect = new Rectangle();
1423 ps = new PAINTSTRUCT();
1425 hwnd = Hwnd.ObjectFromHandle(handle);
1428 if (Win32GetUpdateRect(handle, ref rect, false)) {
1429 hdc = Win32BeginPaint(handle, ref ps);
1431 hwnd.drawing_stack.Push (ps);
1433 clip_rect = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top);
1435 hdc = Win32GetDC(handle);
1436 // FIXME: Add the DC to internal list
1438 hwnd.drawing_stack.Push (null);
1440 clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
1443 hdc = Win32GetWindowDC (handle);
1445 hwnd.drawing_stack.Push (hdc);
1447 // HACK this in for now
1448 Win32GetWindowRect (handle, out rect);
1449 clip_rect = new Rectangle(0, 0, rect.right-rect.left, rect.bottom-rect.top);
1452 Graphics dc = Graphics.FromHdc(hdc);
1453 hwnd.drawing_stack.Push (dc);
1455 paint_event = new PaintEventArgs(dc, clip_rect);
1460 internal override void PaintEventEnd(IntPtr handle, bool client) {
1463 hwnd = Hwnd.ObjectFromHandle(handle);
1465 Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1469 object o = hwnd.drawing_stack.Pop();
1471 PAINTSTRUCT ps = (PAINTSTRUCT)o;
1472 Win32EndPaint(handle, ref ps);
1475 IntPtr hdc = (IntPtr)hwnd.drawing_stack.Pop();
1476 Win32ReleaseDC(handle, hdc);
1481 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1482 Win32MoveWindow(handle, x, y, width, height, true);
1486 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) {
1491 Win32GetWindowRect(handle, out rect);
1492 width = rect.right - rect.left;
1493 height = rect.bottom - rect.top;
1498 parent = Win32GetParent(handle);
1499 Win32ScreenToClient(parent, ref pt);
1504 Win32GetClientRect(handle, out rect);
1505 client_width = rect.right - rect.left;
1506 client_height = rect.bottom - rect.top;
1510 internal override void Activate(IntPtr handle) {
1511 Win32SetActiveWindow(handle);
1514 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1519 rect.right=rc.Right;
1520 rect.bottom=rc.Bottom;
1521 Win32InvalidateRect(handle, ref rect, clear);
1525 internal override void InvalidateNC (IntPtr handle)
1527 // found this gem at
1528 // http://www.dotnet247.com/247reference/msgs/58/292037.aspx
1529 Win32SetWindowPos(handle, IntPtr.Zero,
1531 SetWindowPosFlags.SWP_NOMOVE |
1532 SetWindowPosFlags.SWP_NOSIZE |
1533 SetWindowPosFlags.SWP_NOZORDER |
1534 SetWindowPosFlags.SWP_NOACTIVATE |
1535 SetWindowPosFlags.SWP_DRAWFRAME);
1538 internal override IntPtr DefWndProc(ref Message msg) {
1539 if (HwndCreating != null && HwndCreating.ClientWindow != IntPtr.Zero)
1540 HwndCreating.ClientWindow = msg.HWnd;
1541 msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1545 internal override void HandleException(Exception e) {
1546 StackTrace st = new StackTrace(e);
1547 Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1548 Console.WriteLine("{0}{1}", e.Message, st.ToString());
1551 internal override void DoEvents() {
1552 MSG msg = new MSG();
1554 if (override_cursor != IntPtr.Zero) {
1555 Cursor.Current = null;
1558 while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1559 XplatUI.TranslateMessage(ref msg);
1560 XplatUI.DispatchMessage(ref msg);
1564 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1565 return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1568 internal override void PostQuitMessage(int exitCode) {
1569 Win32PostQuitMessage(exitCode);
1572 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
1574 if (wm_nc_registered == null)
1575 wm_nc_registered = new Hashtable ();
1577 TMEFlags flags = TMEFlags.TME_NONCLIENT;
1579 flags |= TMEFlags.TME_HOVER;
1581 flags |= TMEFlags.TME_LEAVE;
1583 if (flags == TMEFlags.TME_NONCLIENT) {
1584 if (wm_nc_registered.Contains (hwnd)) {
1585 wm_nc_registered.Remove (hwnd);
1588 if (!wm_nc_registered.Contains (hwnd)) {
1589 wm_nc_registered.Add (hwnd, flags);
1591 wm_nc_registered [hwnd] = flags;
1596 internal override void RequestNCRecalc(IntPtr handle) {
1597 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);
1600 internal override void ResetMouseHover(IntPtr handle) {
1601 TRACKMOUSEEVENT tme;
1603 tme = new TRACKMOUSEEVENT();
1604 tme.size = Marshal.SizeOf(tme);
1606 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1607 Win32TrackMouseEvent(ref tme);
1611 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1612 return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
1615 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
1619 if (RetrieveMessage(ref msg)) {
1624 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
1626 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
1632 // We need to fake WM_MOUSE_ENTER/WM_MOUSE_LEAVE
1633 switch (msg.message) {
1634 case Msg.WM_LBUTTONDOWN: {
1635 mouse_state |= MouseButtons.Left;
1639 case Msg.WM_MBUTTONDOWN: {
1640 mouse_state |= MouseButtons.Middle;
1644 case Msg.WM_RBUTTONDOWN: {
1645 mouse_state |= MouseButtons.Right;
1649 case Msg.WM_LBUTTONUP: {
1650 mouse_state &= ~MouseButtons.Left;
1654 case Msg.WM_MBUTTONUP: {
1655 mouse_state &= ~MouseButtons.Middle;
1659 case Msg.WM_RBUTTONUP: {
1660 mouse_state &= ~MouseButtons.Right;
1664 case Msg.WM_ASYNC_MESSAGE: {
1665 XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
1669 case Msg.WM_MOUSEMOVE: {
1670 if (msg.hwnd != prev_mouse_hwnd) {
1671 TRACKMOUSEEVENT tme;
1673 // The current message will be sent out next time around
1674 StoreMessage(ref msg);
1676 // This is the message we want to send at this point
1677 msg.message = Msg.WM_MOUSE_ENTER;
1679 prev_mouse_hwnd = msg.hwnd;
1681 tme = new TRACKMOUSEEVENT();
1682 tme.size = Marshal.SizeOf(tme);
1683 tme.hWnd = msg.hwnd;
1684 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1685 Win32TrackMouseEvent(ref tme);
1691 case Msg.WM_NCMOUSEMOVE: {
1692 if (wm_nc_registered == null || !wm_nc_registered.Contains (msg.hwnd))
1695 TRACKMOUSEEVENT tme;
1697 tme = new TRACKMOUSEEVENT ();
1698 tme.size = Marshal.SizeOf(tme);
1699 tme.hWnd = msg.hwnd;
1700 tme.dwFlags = (TMEFlags)wm_nc_registered[msg.hwnd];
1701 Win32TrackMouseEvent (ref tme);
1705 case Msg.WM_DROPFILES: {
1706 return Win32DnD.HandleWMDropFiles(ref msg);
1709 case Msg.WM_MOUSELEAVE: {
1710 prev_mouse_hwnd = IntPtr.Zero;
1711 msg.message=Msg.WM_MOUSE_LEAVE;
1715 case Msg.WM_TIMER: {
1716 Timer timer=(Timer)timer_list[(int)msg.wParam];
1718 if (timer != null) {
1728 internal override bool TranslateMessage(ref MSG msg) {
1729 return Win32TranslateMessage(ref msg);
1732 internal override IntPtr DispatchMessage(ref MSG msg) {
1733 return Win32DispatchMessage(ref msg);
1736 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
1738 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1740 } else if (!Bottom) {
1741 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1743 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1749 internal override bool SetTopmost(IntPtr hWnd, IntPtr hWndOwner, bool Enabled) {
1751 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1754 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1759 internal override bool Text(IntPtr handle, string text) {
1760 Win32SetWindowText(handle, text);
1764 internal override bool GetText(IntPtr handle, out string text) {
1767 sb = new StringBuilder(256);
1768 Win32GetWindowText(handle, sb, sb.Capacity);
1769 text = sb.ToString();
1773 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
1776 if (Control.FromHandle (handle) is Form) {
1779 f = (Form)Control.FromHandle (handle);
1780 WindowPlacementFlags flags = WindowPlacementFlags.SW_SHOWNORMAL;
1781 switch (f.WindowState) {
1782 case FormWindowState.Normal: flags = WindowPlacementFlags.SW_SHOWNORMAL; break;
1783 case FormWindowState.Minimized: flags = WindowPlacementFlags.SW_MINIMIZE; break;
1784 case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
1787 if (Hwnd.ObjectFromHandle (handle).no_activate)
1788 flags = WindowPlacementFlags.SW_SHOWNOACTIVATE;
1790 Win32ShowWindow (handle, flags);
1793 if (Hwnd.ObjectFromHandle (handle).no_activate)
1794 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
1796 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
1800 Win32ShowWindow (handle, WindowPlacementFlags.SW_HIDE);
1805 internal override bool IsEnabled(IntPtr handle) {
1806 return IsWindowEnabled (handle);
1809 internal override bool IsVisible(IntPtr handle) {
1810 return IsWindowVisible (handle);
1813 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
1814 Control c = Control.FromHandle (handle);
1815 if (parent == IntPtr.Zero) {
1817 Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
1821 SetVisible (handle, c.is_visible, true);
1823 if (parent == IntPtr.Zero)
1824 return Win32SetParent (handle, FosterParent);
1826 return Win32SetParent(handle, parent);
1829 // If we ever start using this, we should probably replace FosterParent with IntPtr.Zero
1830 internal override IntPtr GetParent(IntPtr handle) {
1831 return Win32GetParent(handle);
1834 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
1836 Win32SetCapture(hWnd);
1838 if (ConfineToHwnd != IntPtr.Zero) {
1840 Win32GetWindowRect (ConfineToHwnd, out window_rect);
1841 Win32GetClipCursor (out clipped_cursor_rect);
1842 Win32ClipCursor (ref window_rect);
1846 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
1848 GrabConfined = grab_confined;
1849 GrabArea = grab_area;
1852 internal override void UngrabWindow(IntPtr hWnd) {
1853 if (!(clipped_cursor_rect.top == 0 && clipped_cursor_rect.bottom == 0 && clipped_cursor_rect.left == 0 && clipped_cursor_rect.right == 0)) {
1854 Win32ClipCursor (ref clipped_cursor_rect);
1855 clipped_cursor_rect = new RECT ();
1858 Win32ReleaseCapture();
1859 grab_hwnd = IntPtr.Zero;
1862 internal override bool CalculateWindowRect(ref Rectangle ClientRect, int Style, int ExStyle, Menu menu, out Rectangle WindowRect) {
1865 rect.left=ClientRect.Left;
1866 rect.top=ClientRect.Top;
1867 rect.right=ClientRect.Right;
1868 rect.bottom=ClientRect.Bottom;
1870 if (!Win32AdjustWindowRectEx(ref rect, Style, menu != null, ExStyle)) {
1871 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
1875 WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
1879 internal override void SetCursor(IntPtr window, IntPtr cursor) {
1880 Win32SetCursor(cursor);
1884 internal override void ShowCursor(bool show) {
1885 Win32ShowCursor(show);
1888 internal override void OverrideCursor(IntPtr cursor) {
1889 Win32SetCursor(cursor);
1892 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
1894 Bitmap cursor_bitmap;
1902 // Win32 only allows creation cursors of a certain size
1903 if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
1904 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1905 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1907 cursor_bitmap = bitmap;
1911 width = cursor_bitmap.Width;
1912 height = cursor_bitmap.Height;
1914 cursor_bits = new Byte[(width / 8) * height];
1915 mask_bits = new Byte[(width / 8) * height];
1917 for (int y = 0; y < height; y++) {
1918 for (int x = 0; x < width; x++) {
1919 pixel = cursor_bitmap.GetPixel(x, y);
1921 if (pixel == cursor_pixel) {
1922 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1925 pixel = cursor_mask.GetPixel(x, y);
1927 if (pixel == mask_pixel) {
1928 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1933 cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
1938 [MonoTODO("Define the missing cursors")]
1939 internal override IntPtr DefineStdCursor(StdCursor id) {
1941 case StdCursor.AppStarting: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
1942 case StdCursor.Arrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1943 case StdCursor.Cross: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
1944 case StdCursor.Default: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1945 case StdCursor.Hand: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
1946 case StdCursor.Help: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
1947 case StdCursor.HSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1948 case StdCursor.IBeam: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
1949 case StdCursor.No: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
1950 case StdCursor.NoMove2D: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1951 case StdCursor.NoMoveHoriz: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1952 case StdCursor.NoMoveVert: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1953 case StdCursor.PanEast: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1954 case StdCursor.PanNE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1955 case StdCursor.PanNorth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1956 case StdCursor.PanNW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1957 case StdCursor.PanSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1958 case StdCursor.PanSouth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1959 case StdCursor.PanSW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1960 case StdCursor.PanWest: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1961 case StdCursor.SizeAll: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
1962 case StdCursor.SizeNESW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
1963 case StdCursor.SizeNS: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
1964 case StdCursor.SizeNWSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
1965 case StdCursor.SizeWE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
1966 case StdCursor.UpArrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
1967 case StdCursor.VSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1968 case StdCursor.WaitCursor: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
1970 throw new NotImplementedException ();
1973 internal override void DestroyCursor(IntPtr cursor) {
1974 if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
1975 Win32DestroyCursor(cursor);
1980 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
1987 internal override void SetCursorPos(IntPtr handle, int x, int y) {
1988 Win32SetCursorPos(x, y);
1991 internal override Region GetClipRegion(IntPtr hwnd) {
1994 region = new Region();
1996 Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
2001 internal override void SetClipRegion(IntPtr hwnd, Region region) {
2002 Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
2005 internal override void EnableWindow(IntPtr handle, bool Enable) {
2006 Win32EnableWindow(handle, Enable);
2009 internal override void EndLoop(System.Threading.Thread thread) {
2013 internal override object StartLoop(System.Threading.Thread thread) {
2017 internal override void SetModal(IntPtr handle, bool Modal) {
2018 // we do nothing on Win32
2021 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
2024 Win32GetCursorPos(out pt);
2026 if (handle!=IntPtr.Zero) {
2027 Win32ScreenToClient(handle, ref pt);
2034 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
2036 POINT pnt = new POINT();
2040 Win32ScreenToClient (handle, ref pnt);
2046 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {
2047 POINT pnt = new POINT();
2052 Win32ClientToScreen(handle, ref pnt);
2058 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
2061 Win32GetWindowRect(handle, out rect);
2062 x -= rect.left + SystemInformation.FrameBorderSize.Width;
2063 y -= rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
2067 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {
2070 Win32GetWindowRect(handle, out rect);
2071 x += rect.left + SystemInformation.FrameBorderSize.Width;
2072 y += rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
2076 internal override void SendAsyncMethod (AsyncMethodData method)
2078 Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
2081 internal override void SetTimer (Timer timer)
2085 index = timer.GetHashCode();
2088 timer_list[index]=timer;
2091 Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero);
2094 internal override void KillTimer (Timer timer)
2098 index = timer.GetHashCode();
2100 Win32KillTimer(FosterParent, index);
2103 timer_list.Remove(index);
2107 private void FakeStyles (CreateParams cp)
2109 if (cp.HasWindowManager) {
2110 // Remove all styles but WS_VISIBLE.
2111 cp.WindowStyle &= WindowStyles.WS_VISIBLE;
2112 // Set styles that enables us to use the window manager.
2113 cp.WindowStyle |= WindowStyles.WS_CHILD | WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS;
2118 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
2119 Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
2120 caret_visible = false;
2123 internal override void DestroyCaret(IntPtr hwnd) {
2124 Win32DestroyCaret();
2127 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
2128 Win32SetCaretPos(x, y);
2131 internal override void CaretVisible(IntPtr hwnd, bool visible) {
2133 if (!caret_visible) {
2134 Win32ShowCaret(hwnd);
2135 caret_visible = true;
2138 if (caret_visible) {
2139 Win32HideCaret(hwnd);
2140 caret_visible = false;
2145 internal override IntPtr GetFocus() {
2146 return Win32GetFocus();
2149 internal override void SetFocus(IntPtr hwnd) {
2150 Win32SetFocus(hwnd);
2153 internal override IntPtr GetActive() {
2154 return Win32GetActiveWindow();
2157 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
2161 tm = new TEXTMETRIC();
2163 dc = Win32GetDC(IntPtr.Zero);
2164 Win32SelectObject(dc, font.ToHfont());
2165 if (Win32GetTextMetrics(dc, ref tm) == false) {
2166 Win32ReleaseDC(IntPtr.Zero, dc);
2171 Win32ReleaseDC(IntPtr.Zero, dc);
2173 ascent = tm.tmAscent;
2174 descent = tm.tmDescent;
2179 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
2183 rect.left = rectangle.X;
2184 rect.top = rectangle.Y;
2185 rect.right = rectangle.Right;
2186 rect.bottom = rectangle.Bottom;
2188 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));
2189 Win32UpdateWindow(hwnd);
2192 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
2193 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));
2196 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
2199 nid = new NOTIFYICONDATA();
2201 nid.cbSize = (uint)Marshal.SizeOf(nid);
2204 nid.uCallbackMessage = (uint)Msg.WM_USER;
2205 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2209 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2213 nid.hIcon = icon.Handle;
2214 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2219 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
2222 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
2225 nid = new NOTIFYICONDATA();
2227 nid.cbSize = (uint)Marshal.SizeOf(nid);
2228 nid.hIcon = icon.Handle;
2231 nid.uCallbackMessage = (uint)Msg.WM_USER;
2232 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2236 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2240 nid.hIcon = icon.Handle;
2241 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2244 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2247 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
2250 nid = new NOTIFYICONDATA();
2252 nid.cbSize = (uint)Marshal.SizeOf(nid);
2257 Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
2261 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
2262 // Nothing to do on Win32
2265 internal override void SetMenu(IntPtr handle, Menu menu) {
2266 // Trigger WM_NCCALC
2267 Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2270 internal override Point GetMenuOrigin(IntPtr handle) {
2271 return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2274 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2275 Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : icon.Handle); // 1 = large icon (0 would be small)
2278 internal override void ClipboardClose(IntPtr handle) {
2279 if (handle != clip_magic) {
2280 throw new ArgumentException("handle is not a valid clipboard handle");
2282 Win32CloseClipboard();
2285 internal override int ClipboardGetID(IntPtr handle, string format) {
2286 if (handle != clip_magic) {
2287 throw new ArgumentException("handle is not a valid clipboard handle");
2289 if (format == "Text" ) return 1;
2290 else if (format == "Bitmap" ) return 2;
2291 else if (format == "MetaFilePict" ) return 3;
2292 else if (format == "SymbolicLink" ) return 4;
2293 else if (format == "DataInterchangeFormat" ) return 5;
2294 else if (format == "Tiff" ) return 6;
2295 else if (format == "OEMText" ) return 7;
2296 else if (format == "DeviceIndependentBitmap" ) return 8;
2297 else if (format == "Palette" ) return 9;
2298 else if (format == "PenData" ) return 10;
2299 else if (format == "RiffAudio" ) return 11;
2300 else if (format == "WaveAudio" ) return 12;
2301 else if (format == "UnicodeText" ) return 13;
2302 else if (format == "EnhancedMetafile" ) return 14;
2303 else if (format == "FileDrop" ) return 15;
2304 else if (format == "Locale" ) return 16;
2306 return (int)Win32RegisterClipboardFormat(format);
2309 internal override IntPtr ClipboardOpen(bool primary_selection) {
2310 // Win32 does not have primary selection
2311 Win32OpenClipboard(FosterParent);
2315 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2320 if (handle != clip_magic) {
2328 format = Win32EnumClipboardFormats(format);
2332 } while (format != 0);
2335 result = new int[count];
2339 format = Win32EnumClipboardFormats(format);
2341 result[count++] = (int)format;
2343 } while (format != 0);
2349 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2354 if (handle != clip_magic) {
2355 throw new ArgumentException("handle is not a valid clipboard handle");
2358 hmem = Win32GetClipboardData((uint)type);
2359 if (hmem == IntPtr.Zero) {
2363 data = Win32GlobalLock(hmem);
2364 if (data == IntPtr.Zero) {
2365 uint error = Win32GetLastError();
2366 Console.WriteLine("Error: {0}", error);
2372 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2373 obj = AnsiToString(data);
2374 } else switch ((ClipboardFormats)type) {
2375 case ClipboardFormats.CF_TEXT: {
2376 obj = AnsiToString(data);
2380 case ClipboardFormats.CF_DIB: {
2381 obj = DIBtoImage(data);
2385 case ClipboardFormats.CF_UNICODETEXT: {
2386 obj = UnicodeToString(data);
2391 if (converter != null && !converter(type, data, out obj)) {
2397 Win32GlobalUnlock(hmem);
2403 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
2408 if (handle != clip_magic) {
2409 throw new ArgumentException("handle is not a valid clipboard handle");
2414 Win32EmptyClipboard();
2419 if (obj is string) {
2420 type = (int)ClipboardFormats.CF_UNICODETEXT;
2421 } else if (obj is Image) {
2422 type = (int)ClipboardFormats.CF_DIB;
2426 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2427 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2428 Win32SetClipboardData((uint)type, hmem);
2430 } else switch((ClipboardFormats)type) {
2431 case ClipboardFormats.CF_UNICODETEXT: {
2432 hmem = Marshal.StringToHGlobalUni((string)obj);
2433 Win32SetClipboardData((uint)type, hmem);
2437 case ClipboardFormats.CF_TEXT: {
2438 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2439 Win32SetClipboardData((uint)type, hmem);
2443 case ClipboardFormats.CF_BITMAP:
2444 case ClipboardFormats.CF_DIB: {
2445 data = ImageToDIB((Image)obj);
2447 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2448 hmem_ptr = Win32GlobalLock(hmem);
2449 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2450 Win32GlobalUnlock(hmem);
2451 Win32SetClipboardData((uint)ClipboardFormats.CF_DIB, hmem);
2456 if (converter != null && converter(ref type, obj, out data)) {
2457 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2458 hmem_ptr = Win32GlobalLock(hmem);
2459 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2460 Win32GlobalUnlock(hmem);
2461 Win32SetClipboardData((uint)type, hmem);
2468 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
2470 Win32DnD.RegisterDropTarget(hwnd);
2472 Win32DnD.UnregisterDropTarget(hwnd);
2476 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
2477 return Win32DnD.StartDrag(hwnd, data, allowedEffects);
2480 // XXX this doesn't work at all for FrameStyle.Dashed - it draws like Thick, and in the Thick case
2481 // the corners are drawn incorrectly.
2482 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
2486 COLORREF clrRef = new COLORREF();
2488 // If we want the standard hatch pattern we would
2489 // need to create a brush
2491 clrRef.R = backColor.R;
2492 clrRef.G = backColor.G;
2493 clrRef.B = backColor.B;
2496 pen = Win32CreatePen (style == FrameStyle.Thick ? PenStyle.PS_SOLID : PenStyle.PS_DASH,
2497 style == FrameStyle.Thick ? 4 : 2, ref clrRef);
2499 hdc = Win32GetDC(IntPtr.Zero);
2500 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2501 oldpen = Win32SelectObject(hdc, pen);
2503 Win32MoveToEx(hdc, rectangle.Left, rectangle.Top, IntPtr.Zero);
2504 if ((rectangle.Width > 0) && (rectangle.Height > 0)) {
2505 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2506 Win32LineTo(hdc, rectangle.Right, rectangle.Bottom);
2507 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2508 Win32LineTo(hdc, rectangle.Left, rectangle.Top);
2510 if (rectangle.Width > 0) {
2511 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2513 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2517 Win32SelectObject(hdc, oldpen);
2518 Win32DeleteObject(pen);
2520 Win32ReleaseDC(IntPtr.Zero, hdc);
2523 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
2528 COLORREF clrRef = new COLORREF();
2533 Win32ClientToScreen(IntPtr.Zero, ref pt);
2535 // If we want the standard hatch pattern we would
2536 // need to create a brush
2538 clrRef.R = backColor.R;
2539 clrRef.G = backColor.G;
2540 clrRef.B = backColor.B;
2543 pen = Win32CreatePen(PenStyle.PS_SOLID, 1, ref clrRef);
2545 hdc = Win32GetDC(IntPtr.Zero);
2546 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2547 oldpen = Win32SelectObject(hdc, pen);
2549 Win32MoveToEx(hdc, pt.x + start.X, pt.y + start.Y, IntPtr.Zero);
2550 Win32LineTo(hdc, pt.x + end.X, pt.y + end.Y);
2552 Win32SelectObject(hdc, oldpen);
2553 Win32DeleteObject(pen);
2555 Win32ReleaseDC(IntPtr.Zero, hdc);
2558 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
2563 rect.left = rectangle.Left;
2564 rect.top = rectangle.Top;
2565 rect.right = rectangle.Right;
2566 rect.bottom = rectangle.Bottom;
2571 COLORREF clrRef = new COLORREF();
2573 clrRef.R = backColor.R;
2574 clrRef.G = backColor.G;
2575 clrRef.B = backColor.B;
2578 brush = Win32CreateSolidBrush (clrRef);
2580 hdc = Win32GetDC(IntPtr.Zero);
2581 oldbrush = Win32SelectObject(hdc, brush);
2583 Win32PatBlt (hdc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, PatBltRop.DSTINVERT);
2585 Win32SelectObject(hdc, oldbrush);
2586 Win32DeleteObject(brush);
2588 Win32ReleaseDC(IntPtr.Zero, hdc);
2591 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
2600 Win32ClientToScreen(handle, ref pt);
2602 // If we want the standard hatch pattern we would
2603 // need to create a brush
2606 pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
2608 hdc = Win32GetDC(IntPtr.Zero);
2609 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2610 oldpen = Win32SelectObject(hdc, pen);
2612 Control c = Control.FromHandle (handle);
2615 Win32GetWindowRect (c.Handle, out window_rect);
2616 Region r = new Region (new Rectangle(window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top));
2617 Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
2620 Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
2621 if ((rect.Width > 0) && (rect.Height > 0)) {
2622 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2623 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
2624 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2625 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
2627 if (rect.Width > 0) {
2628 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2630 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2634 Win32SelectObject(hdc, oldpen);
2635 Win32DeleteObject(pen);
2637 Win32ExtSelectClipRgn(hdc, IntPtr.Zero, (int) ClipCombineMode.RGN_COPY);
2639 Win32ReleaseDC(IntPtr.Zero, hdc);
2642 internal override SizeF GetAutoScaleSize(Font font) {
2645 string magic_string = "The quick brown fox jumped over the lazy dog.";
2646 double magic_number = 44.549996948242189;
2648 g = Graphics.FromHwnd(FosterParent);
2650 width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
2651 return new SizeF(width, font.Height);
2654 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2655 return Win32SendMessage(hwnd, message, wParam, lParam);
2658 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2659 return Win32PostMessage(hwnd, message, wParam, lParam);
2662 internal override int SendInput (IntPtr hwnd, Queue keys) {
2663 INPUT[] inputs = new INPUT[keys.Count];
2664 const Int32 INPUT_KEYBOARD = 1;
2667 while (keys.Count > 0) {
2668 MSG msg = (MSG)keys.Dequeue();
2671 inputs[i].ki.wScan = 0;
2672 inputs[i].ki.time = 0;
2673 inputs[i].ki.dwFlags = (Int32)(msg.message == Msg.WM_KEYUP ? InputFlags.KEYEVENTF_KEYUP : 0);
2674 inputs[i].ki.wVk = (short)msg.wParam.ToInt32();
2675 inputs[i].type = INPUT_KEYBOARD;
2678 returns = Win32SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
2680 return (int) returns;
2683 internal override int KeyboardSpeed {
2686 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDSPEED, 0, ref speed, 0);
2688 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
2694 internal override int KeyboardDelay {
2697 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDDELAY, 0, ref delay, 0);
2699 // Return values must range from 0 to 4, 0 meaning 250ms,
2700 // and 4 meaning 1000 ms.
2706 private class WinBuffer
2709 public IntPtr bitmap;
2711 public WinBuffer (IntPtr hdc, IntPtr bitmap)
2714 this.bitmap = bitmap;
2718 internal override void CreateOffscreenDrawable (IntPtr handle, int width, int height, out object offscreen_drawable)
2720 Graphics destG = Graphics.FromHwnd (handle);
2721 IntPtr destHdc = destG.GetHdc ();
2723 IntPtr srcHdc = Win32CreateCompatibleDC (destHdc);
2724 IntPtr srcBmp = Win32CreateCompatibleBitmap (destHdc, width, height);
2725 Win32SelectObject (srcHdc, srcBmp);
2727 offscreen_drawable = new WinBuffer (srcHdc, srcBmp);
2729 destG.ReleaseHdc (destHdc);
2732 internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
2734 return Graphics.FromHdc (((WinBuffer)offscreen_drawable).hdc);
2737 internal override void BlitFromOffscreen (IntPtr dest_handle, Graphics dest_dc, object offscreen_drawable, Graphics offscreen_dc, Rectangle r)
2739 WinBuffer wb = (WinBuffer)offscreen_drawable;
2741 IntPtr destHdc = dest_dc.GetHdc ();
2742 Win32BitBlt (destHdc, r.Left, r.Top, r.Width, r.Height, wb.hdc, r.Left, r.Top, TernaryRasterOperations.SRCCOPY);
2743 dest_dc.ReleaseHdc (destHdc);
2746 internal override void DestroyOffscreenDrawable (object offscreen_drawable)
2748 WinBuffer wb = (WinBuffer)offscreen_drawable;
2750 Win32DeleteObject (wb.bitmap);
2751 Win32DeleteDC (wb.hdc);
2754 internal override event EventHandler Idle;
2755 #endregion // Public Static Methods
2757 #region Win32 Imports
2758 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
2759 private extern static uint Win32GetLastError();
2761 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2762 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);
2764 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
2765 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
2767 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2768 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
2770 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2771 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
2773 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
2774 internal extern static bool Win32TranslateMessage(ref MSG msg);
2776 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2777 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
2779 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
2780 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
2782 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2783 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2785 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2786 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2788 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2789 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
2791 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2792 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
2794 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
2795 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
2797 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2798 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
2800 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2801 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
2803 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
2804 private extern static IntPtr Win32ShowCursor(bool bShow);
2806 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
2807 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
2809 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
2810 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
2812 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
2813 private extern static bool Win32DestroyCursor(IntPtr hCursor);
2815 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2816 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2818 //[DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2819 //private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2821 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
2822 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
2824 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
2825 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
2827 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
2828 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
2830 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
2831 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2833 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
2834 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2836 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
2837 private extern static IntPtr Win32GetDC(IntPtr hWnd);
2839 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
2840 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
2842 //[DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
2843 //private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
2845 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
2846 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
2848 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2849 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
2851 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2852 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
2854 //[DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2855 //private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
2857 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
2858 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
2860 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
2861 private extern static IntPtr Win32ReleaseCapture();
2863 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
2864 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
2866 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
2867 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
2869 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
2870 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
2872 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
2873 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
2875 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
2876 private extern static IntPtr Win32GetParent(IntPtr hWnd);
2878 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
2879 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
2881 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
2882 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
2884 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
2885 private extern static bool Win32GetCursorPos(out POINT lpPoint);
2887 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
2888 private extern static bool Win32SetCursorPos(int x, int y);
2890 //[DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
2891 //private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
2893 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
2894 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
2896 //[DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
2897 //private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
2899 [DllImport ("gdi32.dll", EntryPoint="CreateSolidBrush", CallingConvention=CallingConvention.StdCall)]
2900 private extern static IntPtr Win32CreateSolidBrush(COLORREF clrRef);
2902 [DllImport ("gdi32.dll", EntryPoint="PatBlt", CallingConvention=CallingConvention.StdCall)]
2903 private extern static int Win32PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, PatBltRop dwRop);
2905 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
2906 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
2908 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
2909 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
2911 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2912 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
2914 [DllImport ("user32.dll", EntryPoint="GetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2915 private extern static uint Win32GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out LayeredWindowAttributes pwdFlags);
2917 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
2918 private extern static bool Win32DeleteObject(IntPtr o);
2920 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
2921 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
2923 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
2924 private extern static IntPtr Win32GetDesktopWindow();
2926 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
2927 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
2929 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
2930 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
2932 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
2933 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
2935 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
2936 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
2938 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
2939 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
2941 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
2942 internal extern static IntPtr Win32GetFocus();
2944 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
2945 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
2947 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
2948 private extern static bool Win32DestroyCaret();
2950 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
2951 private extern static bool Win32ShowCaret(IntPtr hwnd);
2953 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
2954 private extern static bool Win32HideCaret(IntPtr hwnd);
2956 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
2957 private extern static bool Win32SetCaretPos(int X, int Y);
2959 //[DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
2960 //private extern static uint Win32GetCaretBlinkTime();
2962 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2963 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
2965 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
2966 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
2968 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2969 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
2971 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2972 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
2974 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2975 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
2977 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2978 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2980 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2981 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2983 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2984 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2986 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2987 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2989 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
2990 private extern static IntPtr Win32GetActiveWindow();
2992 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
2993 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
2995 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2996 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
2998 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
2999 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
3001 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
3002 private extern static bool IsWindowEnabled(IntPtr hwnd);
3004 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
3005 private extern static bool IsWindowVisible(IntPtr hwnd);
3007 //[DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
3008 //private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
3010 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3011 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3013 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3014 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3016 [DllImport ("user32.dll", EntryPoint="SendInput", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3017 private extern static UInt32 Win32SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray)] INPUT[] inputs, Int32 cbSize);
3019 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3020 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
3022 //[DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3023 //private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
3025 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3026 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
3028 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
3029 private extern static bool Win32OpenClipboard(IntPtr hwnd);
3031 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
3032 private extern static bool Win32EmptyClipboard();
3034 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3035 private extern static uint Win32RegisterClipboardFormat(string format);
3037 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
3038 private extern static bool Win32CloseClipboard();
3040 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
3041 private extern static uint Win32EnumClipboardFormats(uint format);
3043 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
3044 private extern static IntPtr Win32GetClipboardData(uint format);
3046 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
3047 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
3049 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
3050 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
3052 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
3053 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
3055 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
3056 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
3058 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
3059 internal extern static uint Win32GlobalSize(IntPtr hMem);
3061 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
3062 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
3064 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
3065 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
3067 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
3068 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
3070 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3071 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
3073 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3074 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
3076 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
3077 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
3079 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3080 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
3082 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3083 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
3085 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
3086 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
3088 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3089 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
3091 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3092 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
3094 [DllImport("gdi32.dll", EntryPoint = "ExcludeClipRect", CallingConvention = CallingConvention.StdCall)]
3095 internal extern static int Win32ExcludeClipRect (IntPtr hdc, int left, int top, int right, int bottom);
3097 [DllImport ("gdi32.dll", EntryPoint="ExtSelectClipRgn", CallingConvention=CallingConvention.StdCall)]
3098 internal extern static int Win32ExtSelectClipRgn(IntPtr hdc, IntPtr hrgn, int mode);
3100 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3101 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
3103 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3104 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
3106 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3107 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
3109 [DllImport ("user32.dll", EntryPoint="ClipCursor", CallingConvention=CallingConvention.StdCall)]
3110 internal extern static bool Win32ClipCursor (ref RECT lpRect);
3112 [DllImport ("user32.dll", EntryPoint="GetClipCursor", CallingConvention=CallingConvention.StdCall)]
3113 internal extern static bool Win32GetClipCursor (out RECT lpRect);
3115 [DllImport ("gdi32.dll", EntryPoint="BitBlt", CallingConvention=CallingConvention.StdCall)]
3116 internal static extern bool Win32BitBlt (IntPtr hObject, int nXDest, int nYDest, int nWidth,
3117 int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
3119 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3120 internal static extern IntPtr Win32CreateCompatibleDC (IntPtr hdc);
3122 [DllImport ("gdi32.dll", EntryPoint="DeleteDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3123 internal static extern bool Win32DeleteDC (IntPtr hdc);
3125 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleBitmap", CallingConvention=CallingConvention.StdCall)]
3126 internal static extern IntPtr Win32CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight);