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;
67 #endregion // Local Variables
69 #region Private Structs
70 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
71 private struct WNDCLASS {
73 internal WndProc lpfnWndProc;
74 internal int cbClsExtra;
75 internal int cbWndExtra;
76 internal IntPtr hInstance;
77 internal IntPtr hIcon;
78 internal IntPtr hCursor;
79 internal IntPtr hbrBackground;
80 [MarshalAs(UnmanagedType.LPWStr)]
81 internal string lpszMenuName;
82 [MarshalAs(UnmanagedType.LPWStr)]
83 internal string lpszClassName;
86 [StructLayout(LayoutKind.Sequential)]
87 internal struct RECT {
92 public override string ToString() {
93 return String.Format("RECT left={0}, top={1}, right={2}, bottom={3}, width={4}, height={5}", left, top, right, bottom, right-left, bottom-top);
98 [StructLayout(LayoutKind.Sequential)]
99 internal struct POINT {
104 internal enum SPIAction {
105 SPI_GETWORKAREA = 0x0030,
106 SPI_GETMOUSEHOVERWIDTH = 0x0062,
107 SPI_GETMOUSEHOVERHEIGHT = 0x0064,
108 SPI_GETMOUSEHOVERTIME = 0x0066,
111 internal enum WindowPlacementFlags {
115 SW_SHOWMINIMIZED = 2,
116 SW_SHOWMAXIMIZED = 3,
118 SW_SHOWNOACTIVATE = 4,
121 SW_SHOWMINNOACTIVE = 7,
125 SW_FORCEMINIMIZE = 11,
129 [StructLayout(LayoutKind.Sequential)]
130 private struct WINDOWPLACEMENT {
131 internal uint length;
133 internal WindowPlacementFlags showCmd;
134 internal POINT ptMinPosition;
135 internal POINT ptMaxPosition;
136 internal RECT rcNormalPosition;
139 [StructLayout(LayoutKind.Sequential)]
140 internal struct NCCALCSIZE_PARAMS {
144 internal IntPtr lppos;
148 private enum TMEFlags {
149 TME_HOVER = 0x00000001,
150 TME_LEAVE = 0x00000002,
151 TME_NONCLIENT = 0x00000010,
152 TME_QUERY = unchecked((int)0x40000000),
153 TME_CANCEL = unchecked((int)0x80000000)
156 [StructLayout(LayoutKind.Sequential)]
157 private struct TRACKMOUSEEVENT {
159 internal TMEFlags dwFlags;
160 internal IntPtr hWnd;
161 internal int dwHoverTime;
164 [StructLayout(LayoutKind.Sequential)]
165 private struct PAINTSTRUCT {
168 internal RECT rcPaint;
169 internal int fRestore;
170 internal int fIncUpdate;
171 internal int Reserved1;
172 internal int Reserved2;
173 internal int Reserved3;
174 internal int Reserved4;
175 internal int Reserved5;
176 internal int Reserved6;
177 internal int Reserved7;
178 internal int Reserved8;
181 [StructLayout(LayoutKind.Sequential)]
182 internal struct KEYBDINPUT {
184 internal short wScan;
185 internal Int32 dwFlags;
187 internal UIntPtr dwExtraInfo;
190 [StructLayout(LayoutKind.Sequential)]
191 internal struct MOUSEINPUT {
194 internal Int32 mouseData;
195 internal Int32 dwFlags;
197 internal UIntPtr dwExtraInfo;
200 [StructLayout(LayoutKind.Sequential)]
201 internal struct HARDWAREINPUT {
203 internal short wParamL;
204 internal short wParamH;
207 [StructLayout(LayoutKind.Explicit)]
208 internal struct INPUT {
213 internal MOUSEINPUT mi;
216 internal KEYBDINPUT ki;
219 internal HARDWAREINPUT hi;
223 internal enum InputFlags {
224 KEYEVENTF_EXTENDEDKEY = 0x0001,
225 KEYEVENTF_KEYUP = 0x0002,
226 KEYEVENTF_SCANCODE = 0x0003,
227 KEYEVENTF_UNICODE = 0x0004,
230 internal enum ClassStyle {
231 CS_VREDRAW = 0x00000001,
232 CS_HREDRAW = 0x00000002,
233 CS_KEYCVTWINDOW = 0x00000004,
234 CS_DBLCLKS = 0x00000008,
235 CS_OWNDC = 0x00000020,
236 CS_CLASSDC = 0x00000040,
237 CS_PARENTDC = 0x00000080,
238 CS_NOKEYCVT = 0x00000100,
239 CS_NOCLOSE = 0x00000200,
240 CS_SAVEBITS = 0x00000800,
241 CS_BYTEALIGNCLIENT = 0x00001000,
242 CS_BYTEALIGNWINDOW = 0x00002000,
243 CS_GLOBALCLASS = 0x00004000,
247 internal enum SetWindowPosZOrder {
255 internal enum SetWindowPosFlags {
256 SWP_ASYNCWINDOWPOS = 0x4000,
257 SWP_DEFERERASE = 0x2000,
258 SWP_DRAWFRAME = 0x0020,
259 SWP_FRAMECHANGED = 0x0020,
260 SWP_HIDEWINDOW = 0x0080,
261 SWP_NOACTIVATE = 0x0010,
262 SWP_NOCOPYBITS = 0x0100,
264 SWP_NOOWNERZORDER = 0x0200,
265 SWP_NOREDRAW = 0x0008,
266 SWP_NOREPOSITION = 0x0200,
267 SWP_NOENDSCHANGING = 0x0400,
269 SWP_NOZORDER = 0x0004,
270 SWP_SHOWWINDOW = 0x0040
273 internal enum GetSysColorIndex {
275 COLOR_BACKGROUND = 1,
276 COLOR_ACTIVECAPTION = 2,
277 COLOR_INACTIVECAPTION = 3,
280 COLOR_WINDOWFRAME = 6,
282 COLOR_WINDOWTEXT = 8,
283 COLOR_CAPTIONTEXT = 9,
284 COLOR_ACTIVEBORDER = 10,
285 COLOR_INACTIVEBORDER = 11,
286 COLOR_APPWORKSPACE = 12,
287 COLOR_HIGHLIGHT = 13,
288 COLOR_HIGHLIGHTTEXT = 14,
290 COLOR_BTNSHADOW = 16,
293 COLOR_INACTIVECAPTIONTEXT = 19,
294 COLOR_BTNHIGHLIGHT = 20,
295 COLOR_3DDKSHADOW = 21,
301 COLOR_GRADIENTACTIVECAPTION = 27,
302 COLOR_GRADIENTINACTIVECAPTION = 28,
303 COLOR_MENUHIGHLIGHT = 29,
309 COLOR_3DHIGHLIGHT = 20,
310 COLOR_3DHILIGHT = 20,
311 COLOR_BTNHILIGHT = 20,
312 COLOR_MAXVALUE = 24,/* Maximum value */
315 private enum LoadCursorType {
324 IDC_SIZENWSE = 32642,
325 IDC_SIZENESW = 32643,
331 IDC_APPSTARTING = 32650,
337 private enum WindowLong {
348 private enum LogBrushStyle {
357 BS_DIBPATTERN8X8 = 8,
362 private enum LogBrushHatch {
363 HS_HORIZONTAL = 0, /* ----- */
364 HS_VERTICAL = 1, /* ||||| */
365 HS_FDIAGONAL = 2, /* \\\\\ */
366 HS_BDIAGONAL = 3, /* ///// */
367 HS_CROSS = 4, /* +++++ */
368 HS_DIAGCROSS = 5, /* xxxxx */
371 internal struct COLORREF {
378 [StructLayout(LayoutKind.Sequential)]
379 private struct LOGBRUSH {
380 internal LogBrushStyle lbStyle;
381 internal COLORREF lbColor;
382 internal LogBrushHatch lbHatch;
385 [StructLayout(LayoutKind.Sequential)]
386 internal struct TEXTMETRIC {
387 internal int tmHeight;
388 internal int tmAscent;
389 internal int tmDescent;
390 internal int tmInternalLeading;
391 internal int tmExternalLeading;
392 internal int tmAveCharWidth;
393 internal int tmMaxCharWidth;
394 internal int tmWeight;
395 internal int tmOverhang;
396 internal int tmDigitizedAspectX;
397 internal int tmDigitizedAspectY;
398 internal short tmFirstChar;
399 internal short tmLastChar;
400 internal short tmDefaultChar;
401 internal short tmBreakChar;
402 internal byte tmItalic;
403 internal byte tmUnderlined;
404 internal byte tmStruckOut;
405 internal byte tmPitchAndFamily;
406 internal byte tmCharSet;
410 private enum ScrollWindowExFlags {
412 SW_SCROLLCHILDREN = 0x0001,
413 SW_INVALIDATE = 0x0002,
415 SW_SMOOTHSCROLL = 0x0010
418 internal enum SystemMetrics {
435 SM_CXFULLSCREEN = 16,
436 SM_CYFULLSCREEN = 17,
437 SM_CYKANJIWINDOW = 18,
438 SM_MOUSEPRESENT = 19,
457 SM_CXICONSPACING = 38,
458 SM_CYICONSPACING = 39,
459 SM_MENUDROPALIGNMENT = 40,
462 SM_CMOUSEBUTTONS = 43,
463 SM_CXFIXEDFRAME = SM_CXDLGFRAME,
464 SM_CYFIXEDFRAME = SM_CYDLGFRAME,
465 SM_CXSIZEFRAME = SM_CXFRAME,
466 SM_CYSIZEFRAME = SM_CYFRAME,
470 SM_CXMINSPACING = 47,
471 SM_CYMINSPACING = 48,
494 SM_MIDEASTENABLED = 74,
495 SM_MOUSEWHEELPRESENT = 75,
496 SM_XVIRTUALSCREEN = 76,
497 SM_YVIRTUALSCREEN = 77,
498 SM_CXVIRTUALSCREEN = 78,
499 SM_CYVIRTUALSCREEN = 79,
501 SM_SAMEDISPLAYFORMAT = 81,
503 SM_CXFOCUSBORDER = 83,
504 SM_CYFOCUSBORDER = 84,
510 // We'll only support _WIN32_IE < 0x0500 for now
511 internal enum NotifyIconMessage {
512 NIM_ADD = 0x00000000,
513 NIM_MODIFY = 0x00000001,
514 NIM_DELETE = 0x00000002,
518 internal enum NotifyIconFlags {
519 NIF_MESSAGE = 0x00000001,
520 NIF_ICON = 0x00000002,
521 NIF_TIP = 0x00000004,
524 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
525 internal struct NOTIFYICONDATA {
526 internal uint cbSize;
527 internal IntPtr hWnd;
529 internal NotifyIconFlags uFlags;
530 internal uint uCallbackMessage;
531 internal IntPtr hIcon;
532 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
533 internal string szTip;
537 internal enum DCExFlags {
538 DCX_WINDOW = 0x00000001,
539 DCX_CACHE = 0x00000002,
540 DCX_NORESETATTRS = 0x00000004,
541 DCX_CLIPCHILDREN = 0x00000008,
542 DCX_CLIPSIBLINGS = 0x00000010,
543 DCX_PARENTCLIP = 0x00000020,
544 DCX_EXCLUDERGN = 0x00000040,
545 DCX_INTERSECTRGN = 0x00000080,
546 DCX_EXCLUDEUPDATE = 0x00000100,
547 DCX_INTERSECTUPDATE = 0x00000200,
548 DCX_LOCKWINDOWUPDATE = 0x00000400,
549 DCX_USESTYLE = 0x00010000,
550 DCX_VALIDATE = 0x00200000
553 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
554 internal struct CLIENTCREATESTRUCT {
555 internal IntPtr hWindowMenu;
556 internal uint idFirstChild;
559 private enum ClassLong : int {
561 GCL_HBRBACKGROUND = -10,
565 GCL_CBWNDEXTRA = -18,
566 GCL_CBCLSEXTRA = -20,
574 internal enum GAllocFlags : uint {
576 GMEM_MOVEABLE = 0x0002,
577 GMEM_NOCOMPACT = 0x0010,
578 GMEM_NODISCARD = 0x0020,
579 GMEM_ZEROINIT = 0x0040,
580 GMEM_MODIFY = 0x0080,
581 GMEM_DISCARDABLE = 0x0100,
582 GMEM_NOT_BANKED = 0x1000,
584 GMEM_DDESHARE = 0x2000,
585 GMEM_NOTIFY = 0x4000,
586 GMEM_LOWER = GMEM_NOT_BANKED,
587 GMEM_VALID_FLAGS = 0x7F72,
588 GMEM_INVALID_HANDLE = 0x8000,
589 GHND = (GMEM_MOVEABLE | GMEM_ZEROINIT),
590 GPTR = (GMEM_FIXED | GMEM_ZEROINIT)
593 internal enum ROP2DrawMode : int {
613 internal enum PenStyle : int {
625 internal enum PatBltRop : int {
627 PATINVERT = 0x5a0049,
628 DSTINVERT = 0x550009,
629 BLACKNESS = 0x000042,
630 WHITENESS = 0xff0062,
633 internal enum StockObject : int {
640 HOLLOW_BRUSH = NULL_BRUSH,
645 ANSI_FIXED_FONT = 11,
648 DEVICE_DEFAULT_FONT = 14,
649 DEFAULT_PALETTE = 15,
650 SYSTEM_FIXED_FONT = 16
653 internal enum HatchStyle : int {
663 internal enum SndFlags : int {
666 SND_NODEFAULT = 0x0002,
670 SND_NOWAIT = 0x00002000,
671 SND_ALIAS = 0x00010000,
672 SND_ALIAS_ID = 0x00110000,
673 SND_FILENAME = 0x00020000,
674 SND_RESOURCE = 0x00040004,
676 SND_APPLICATION = 0x0080,
680 internal enum LayeredWindowAttributes : int {
688 #region Constructor & Destructor
689 private XplatUIWin32() {
693 // Handle singleton stuff first
696 mouse_state = MouseButtons.None;
697 mouse_position = Point.Empty;
699 message_queue = new Queue();
701 themes_enabled = false;
703 // Prepare 'our' window class
704 wnd_proc = new WndProc(NativeWindow.WndProc);
705 wndClass.style = (int)(ClassStyle.CS_OWNDC | ClassStyle.CS_DBLCLKS);
706 wndClass.lpfnWndProc = wnd_proc;
707 wndClass.cbClsExtra = 0;
708 wndClass.cbWndExtra = 0;
709 wndClass.hbrBackground = IntPtr.Zero;
710 wndClass.hCursor = Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
711 wndClass.hIcon = IntPtr.Zero;
712 wndClass.hInstance = IntPtr.Zero;
713 wndClass.lpszClassName = XplatUI.DefaultClassName;
714 wndClass.lpszMenuName = "";
716 result=Win32RegisterClass(ref wndClass);
718 Win32MessageBox(IntPtr.Zero, "Could not register the "+XplatUI.DefaultClassName+" window class, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
721 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);
723 if (FosterParent==IntPtr.Zero) {
724 Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
727 scroll_height = Win32GetSystemMetrics(SystemMetrics.SM_CYHSCROLL);
728 scroll_width = Win32GetSystemMetrics(SystemMetrics.SM_CXVSCROLL);
730 timer_list = new Hashtable ();
732 #endregion // Constructor & Destructor
734 #region Private Support Methods
736 private static bool RetrieveMessage(ref MSG msg) {
739 if (message_queue.Count == 0) {
743 message = (MSG)message_queue.Dequeue();
749 private static bool StoreMessage(ref MSG msg) {
750 MSG message = new MSG();
753 message_queue.Enqueue(message);
758 internal static String AnsiToString(IntPtr ansi_data) {
759 return (string)Marshal.PtrToStringAnsi(ansi_data);
762 internal static String UnicodeToString(IntPtr unicode_data) {
763 return (string)Marshal.PtrToStringUni(unicode_data);
766 internal static Image DIBtoImage(IntPtr dib_data) {
767 BITMAPINFOHEADER bmi;
778 bmi = (BITMAPINFOHEADER)Marshal.PtrToStructure(dib_data, typeof(BITMAPINFOHEADER));
780 ncolors = (int)bmi.biClrUsed;
782 if (bmi.biBitCount < 24) {
783 ncolors = (int)(1 << bmi.biBitCount);
786 //palettesize = ncolors * 4;
788 imagesize = (int)bmi.biSizeImage;
789 if (imagesize == 0) {
790 imagesize = (int)(((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);
793 switch(bmi.biBitCount) {
794 case 1: { // Monochrome
795 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format1bppIndexed);
796 palette = new int[2];
801 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format4bppIndexed);
802 palette = new int[16];
807 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format8bppIndexed);
808 palette = new int[256];
814 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format32bppArgb);
815 palette = new int[0];
820 throw new Exception("Unexpected number of bits:" + bmi.biBitCount.ToString());
824 if (bmi.biBitCount < 24) {
825 pal = bmp.Palette; // Managed palette
826 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER))), palette, 0, palette.Length);
828 for (int i = 0; i < ncolors; i++) {
829 pal.Entries[i] = Color.FromArgb(palette[i] | unchecked((int)0xff000000));
834 bytesPerLine = (int)((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3);
835 bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
837 imagebits = new byte[bytesPerLine];
839 for (int y = 0; y < bmi.biHeight; y++) {
840 // Copy from source to managed
841 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER)) + palette.Length * 4 + bytesPerLine * y), imagebits, 0, bytesPerLine);
843 // Copy from managed to dest
844 Marshal.Copy(imagebits, 0, (IntPtr)((int)bits.Scan0 + bits.Stride * (bmi.biHeight - 1 - y)), imagebits.Length);
847 bmp.UnlockBits(bits);
852 internal static byte[] ImageToDIB(Image image) {
857 ms = new MemoryStream();
858 image.Save(ms, ImageFormat.Bmp);
859 buffer = ms.GetBuffer();
861 // Filter out the file header
862 retbuf = new byte[buffer.Length];
863 Array.Copy(buffer, 14, retbuf, 0, buffer.Length - 14);
867 internal static IntPtr DupGlobalMem(IntPtr mem) {
873 len = Win32GlobalSize(mem);
874 mem_ptr = Win32GlobalLock(mem);
876 dup = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE, (int)len);
877 dup_ptr = Win32GlobalLock(dup);
879 Win32CopyMemory(dup_ptr, mem_ptr, (int)len);
881 Win32GlobalUnlock(mem);
882 Win32GlobalUnlock(dup);
886 #endregion // Private Support Methods
888 #region Static Properties
889 internal override Keys ModifierKeys {
894 key_state = Keys.None;
896 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
897 if ((state & 0x8000) != 0) {
898 key_state |= Keys.Shift;
900 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
901 if ((state & 0x8000) != 0) {
902 key_state |= Keys.Control;
905 state = Win32GetKeyState(VirtualKeys.VK_MENU);
906 if ((state & 0x8000) != 0) {
907 key_state |= Keys.Alt;
914 internal override MouseButtons MouseButtons {
920 internal override Point MousePosition {
922 return mouse_position;
926 internal override Size MouseHoverSize {
931 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref width, 0);
932 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref height, 0);
933 return new Size(width, height);
937 internal override int MouseHoverTime {
941 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERTIME, 0, ref time, 0);
946 internal override int HorizontalScrollBarHeight {
948 return scroll_height;
952 internal override bool UserClipWontExposeParent {
959 internal override int VerticalScrollBarWidth {
965 internal override int MenuHeight {
967 return Win32GetSystemMetrics(SystemMetrics.SM_CYMENU);
971 internal override bool DropTarget {
978 //throw new NotImplementedException("Need to figure out D'n'D for Win32");
983 internal override int Caption {
985 return Win32GetSystemMetrics(SystemMetrics.SM_CYCAPTION);
989 internal override Size CursorSize {
991 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CYCURSOR));
995 internal override bool DragFullWindows {
1001 internal override Size DragSize {
1003 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXDRAG), Win32GetSystemMetrics(SystemMetrics.SM_CYDRAG));
1007 internal override Size FrameBorderSize {
1009 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXFRAME), Win32GetSystemMetrics(SystemMetrics.SM_CYFRAME));
1013 internal override Size IconSize {
1015 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXICON), Win32GetSystemMetrics(SystemMetrics.SM_CYICON));
1019 internal override Size MaxWindowTrackSize {
1021 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMAXTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMAXTRACK));
1025 internal override Size MinimizedWindowSize {
1027 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINIMIZED), Win32GetSystemMetrics(SystemMetrics.SM_CYMINIMIZED));
1031 internal override Size MinimizedWindowSpacingSize {
1033 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINSPACING), Win32GetSystemMetrics(SystemMetrics.SM_CYMINSPACING));
1037 internal override Size MinimumWindowSize {
1039 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMIN), Win32GetSystemMetrics(SystemMetrics.SM_CYMIN));
1043 internal override Size MinWindowTrackSize {
1045 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMINTRACK));
1049 internal override Size SmallIconSize {
1051 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXSMICON), Win32GetSystemMetrics(SystemMetrics.SM_CYSMICON));
1055 internal override int MouseButtonCount {
1057 return Win32GetSystemMetrics(SystemMetrics.SM_CMOUSEBUTTONS);
1061 internal override bool MouseButtonsSwapped {
1063 return Win32GetSystemMetrics(SystemMetrics.SM_SWAPBUTTON) != 0;
1067 internal override bool MouseWheelPresent {
1069 return Win32GetSystemMetrics(SystemMetrics.SM_MOUSEWHEELPRESENT) != 0;
1073 internal override Rectangle VirtualScreen {
1075 return new Rectangle( Win32GetSystemMetrics(SystemMetrics.SM_XVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_YVIRTUALSCREEN),
1076 Win32GetSystemMetrics(SystemMetrics.SM_CXVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYVIRTUALSCREEN));
1080 internal override Rectangle WorkingArea {
1085 Win32SystemParametersInfo(SPIAction.SPI_GETWORKAREA, 0, ref rect, 0);
1086 return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
1087 //return new Rectangle(0, 0, Win32GetSystemMetrics(SystemMetrics.SM.SM_CXSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYSCREEN));
1091 internal override bool ThemesEnabled {
1093 return XplatUIWin32.themes_enabled;
1098 #endregion // Static Properties
1100 #region Singleton Specific Code
1101 public static XplatUIWin32 GetInstance() {
1102 if (instance==null) {
1103 instance=new XplatUIWin32();
1109 public int Reference {
1116 #region Public Static Methods
1117 internal override IntPtr InitializeDriver() {
1121 internal override void ShutdownDriver(IntPtr token) {
1122 Console.WriteLine("XplatUIWin32 ShutdownDriver called");
1126 internal void Version() {
1127 Console.WriteLine("Xplat version $revision: $");
1130 internal override void AudibleAlert() {
1131 Win32PlaySound("Default", IntPtr.Zero, SndFlags.SND_ALIAS | SndFlags.SND_ASYNC | SndFlags.SND_NOSTOP | SndFlags.SND_NOWAIT);
1134 internal override void GetDisplaySize(out Size size) {
1137 Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
1139 size = new Size(rect.right - rect.left, rect.bottom - rect.top);
1142 internal override void EnableThemes() {
1143 themes_enabled=true;
1146 internal override IntPtr CreateWindow(CreateParams cp) {
1147 IntPtr WindowHandle;
1148 IntPtr ParentHandle;
1153 ParentHandle=cp.Parent;
1155 if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
1156 // We need to use our foster parent window until this poor child gets it's parent assigned
1157 ParentHandle = FosterParent;
1160 if ( ((cp.Style & (int)(WindowStyles.WS_CHILD | WindowStyles.WS_POPUP))==0) && ((cp.ExStyle & (int)WindowExStyles.WS_EX_APPWINDOW) == 0)) {
1161 // If we want to be hidden from the taskbar we need to be 'owned' by
1162 // something not on the taskbar. FosterParent is just that
1163 ParentHandle = FosterParent;
1166 // Since we fake MDI dont tell Windows that this is a real MDI window
1167 if ((cp.ExStyle & (int) WindowExStyles.WS_EX_MDICHILD) != 0) {
1171 WindowHandle = Win32CreateWindow((uint)cp.ExStyle, cp.ClassName, cp.Caption, (uint)cp.Style, cp.X, cp.Y, cp.Width, cp.Height, ParentHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
1173 if (WindowHandle==IntPtr.Zero) {
1174 uint error = Win32GetLastError();
1176 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1179 hwnd.ClientWindow = WindowHandle;
1181 Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1183 return WindowHandle;
1186 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1187 CreateParams create_params = new CreateParams();
1189 create_params.Caption = "";
1190 create_params.X = X;
1191 create_params.Y = Y;
1192 create_params.Width = Width;
1193 create_params.Height = Height;
1195 create_params.ClassName=XplatUI.DefaultClassName;
1196 create_params.ClassStyle = 0;
1197 create_params.ExStyle=0;
1198 create_params.Parent=IntPtr.Zero;
1199 create_params.Param=0;
1201 return CreateWindow(create_params);
1204 internal override void DestroyWindow(IntPtr handle) {
1207 hwnd = Hwnd.ObjectFromHandle(handle);
1208 Win32DestroyWindow(handle);
1213 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1214 // We do nothing, Form has to handle WM_GETMINMAXINFO
1218 internal override FormWindowState GetWindowState(IntPtr handle) {
1221 style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1222 if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1223 return FormWindowState.Maximized;
1224 } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1225 return FormWindowState.Minimized;
1227 return FormWindowState.Normal;
1230 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1232 case FormWindowState.Normal: {
1233 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1237 case FormWindowState.Minimized: {
1238 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1242 case FormWindowState.Maximized: {
1243 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1249 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1251 if ((cp.ExStyle & (int) WindowExStyles.WS_EX_MDICHILD) != 0) {
1255 Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1256 Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1259 internal override double GetWindowTransparency(IntPtr handle)
1261 LayeredWindowAttributes lwa;
1265 if (0 == Win32GetLayeredWindowAttributes (handle, out clrRef, out alpha, out lwa))
1268 return ((double)alpha) / 255.0;
1271 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1272 LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1273 byte opacity = (byte)(transparency*255);
1274 COLORREF clrRef = new COLORREF();
1275 if (key != Color.Empty) {
1279 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1284 Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1287 TransparencySupport support;
1288 bool queried_transparency_support;
1289 internal override TransparencySupport SupportsTransparency() {
1290 if (queried_transparency_support)
1294 support = TransparencySupport.None;
1298 Win32SetLayeredWindowAttributes (IntPtr.Zero, new COLORREF (), 255, LayeredWindowAttributes.LWA_ALPHA);
1300 catch (EntryPointNotFoundException) { flag = false; }
1301 catch { /* swallow everything else */ }
1303 if (flag) support |= TransparencySupport.Set;
1307 LayeredWindowAttributes lwa;
1311 Win32GetLayeredWindowAttributes (IntPtr.Zero, out clrRef, out alpha, out lwa);
1313 catch (EntryPointNotFoundException) { flag = false; }
1314 catch { /* swallow everything else */ }
1316 if (flag) support |= TransparencySupport.Get;
1318 queried_transparency_support = true;
1322 internal override void UpdateWindow(IntPtr handle) {
1323 Win32UpdateWindow(handle);
1326 internal override PaintEventArgs PaintEventStart(IntPtr handle, bool client) {
1329 PaintEventArgs paint_event;
1331 Rectangle clip_rect;
1334 clip_rect = new Rectangle();
1336 ps = new PAINTSTRUCT();
1338 hwnd = Hwnd.ObjectFromHandle(handle);
1341 if (Win32GetUpdateRect(handle, ref rect, false)) {
1342 hdc = Win32BeginPaint(handle, ref ps);
1344 hwnd.drawing_stack.Push (ps);
1346 clip_rect = new Rectangle(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top);
1348 hdc = Win32GetDC(handle);
1349 // FIXME: Add the DC to internal list
1351 hwnd.drawing_stack.Push (null);
1353 clip_rect = new Rectangle(rect.top, rect.left, rect.right-rect.left, rect.bottom-rect.top);
1356 hdc = Win32GetWindowDC (handle);
1358 hwnd.drawing_stack.Push (hdc);
1360 // HACK this in for now
1361 Win32GetWindowRect (handle, out rect);
1362 clip_rect = new Rectangle(0, 0, rect.right-rect.left, rect.bottom-rect.top);
1365 Graphics dc = Graphics.FromHdc(hdc);
1366 hwnd.drawing_stack.Push (dc);
1368 paint_event = new PaintEventArgs(dc, clip_rect);
1373 internal override void PaintEventEnd(IntPtr handle, bool client) {
1376 hwnd = Hwnd.ObjectFromHandle(handle);
1378 Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1382 object o = hwnd.drawing_stack.Pop();
1384 PAINTSTRUCT ps = (PAINTSTRUCT)o;
1385 Win32EndPaint(handle, ref ps);
1388 IntPtr hdc = (IntPtr)hwnd.drawing_stack.Pop();
1389 Win32ReleaseDC(handle, hdc);
1394 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1395 Win32MoveWindow(handle, x, y, width, height, true);
1399 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) {
1404 Win32GetWindowRect(handle, out rect);
1405 width = rect.right - rect.left;
1406 height = rect.bottom - rect.top;
1411 parent = Win32GetParent(handle);
1412 Win32ScreenToClient(parent, ref pt);
1417 Win32GetClientRect(handle, out rect);
1418 client_width = rect.right - rect.left;
1419 client_height = rect.bottom - rect.top;
1423 internal override void Activate(IntPtr handle) {
1424 Win32SetActiveWindow(handle);
1427 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1432 rect.right=rc.Right;
1433 rect.bottom=rc.Bottom;
1434 Win32InvalidateRect(handle, ref rect, clear);
1438 internal override void InvalidateNC (IntPtr handle)
1440 // found this gem at
1441 // http://www.dotnet247.com/247reference/msgs/58/292037.aspx
1442 Win32SetWindowPos(handle, IntPtr.Zero,
1444 SetWindowPosFlags.SWP_NOMOVE |
1445 SetWindowPosFlags.SWP_NOSIZE |
1446 SetWindowPosFlags.SWP_NOZORDER |
1447 SetWindowPosFlags.SWP_NOACTIVATE |
1448 SetWindowPosFlags.SWP_DRAWFRAME);
1451 internal override IntPtr DefWndProc(ref Message msg) {
1452 msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1456 internal override void HandleException(Exception e) {
1457 StackTrace st = new StackTrace(e);
1458 Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1459 Console.WriteLine("{0}{1}", e.Message, st.ToString());
1462 internal override void DoEvents() {
1463 MSG msg = new MSG();
1465 if (override_cursor != IntPtr.Zero) {
1466 Cursor.Current = null;
1469 while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1470 XplatUI.TranslateMessage(ref msg);
1471 XplatUI.DispatchMessage(ref msg);
1475 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1476 return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1479 internal override void PostQuitMessage(int exitCode) {
1480 Win32PostQuitMessage(exitCode);
1483 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
1485 if (wm_nc_registered == null)
1486 wm_nc_registered = new Hashtable ();
1488 TMEFlags flags = TMEFlags.TME_NONCLIENT;
1490 flags |= TMEFlags.TME_HOVER;
1492 flags |= TMEFlags.TME_LEAVE;
1494 if (flags == TMEFlags.TME_NONCLIENT) {
1495 if (wm_nc_registered.Contains (hwnd)) {
1496 wm_nc_registered.Remove (hwnd);
1499 if (!wm_nc_registered.Contains (hwnd)) {
1500 wm_nc_registered.Add (hwnd, flags);
1502 wm_nc_registered [hwnd] = flags;
1507 internal override void RequestNCRecalc(IntPtr handle) {
1508 Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOMOVE);
1511 internal override void ResetMouseHover(IntPtr handle) {
1512 TRACKMOUSEEVENT tme;
1514 tme = new TRACKMOUSEEVENT();
1515 tme.size = Marshal.SizeOf(tme);
1517 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1518 Win32TrackMouseEvent(ref tme);
1522 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1523 return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
1526 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
1530 if (RetrieveMessage(ref msg)) {
1535 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
1537 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
1543 // We need to fake WM_MOUSE_ENTER/WM_MOUSE_LEAVE
1544 switch (msg.message) {
1545 case Msg.WM_LBUTTONDOWN: {
1546 mouse_state |= MouseButtons.Left;
1550 case Msg.WM_MBUTTONDOWN: {
1551 mouse_state |= MouseButtons.Middle;
1555 case Msg.WM_RBUTTONDOWN: {
1556 mouse_state |= MouseButtons.Right;
1560 case Msg.WM_LBUTTONUP: {
1561 mouse_state &= ~MouseButtons.Left;
1565 case Msg.WM_MBUTTONUP: {
1566 mouse_state &= ~MouseButtons.Middle;
1570 case Msg.WM_RBUTTONUP: {
1571 mouse_state &= ~MouseButtons.Right;
1575 case Msg.WM_ASYNC_MESSAGE: {
1576 XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
1580 case Msg.WM_MOUSEMOVE: {
1581 if (msg.hwnd != prev_mouse_hwnd) {
1582 TRACKMOUSEEVENT tme;
1584 // The current message will be sent out next time around
1585 StoreMessage(ref msg);
1587 // This is the message we want to send at this point
1588 msg.message = Msg.WM_MOUSE_ENTER;
1590 prev_mouse_hwnd = msg.hwnd;
1592 tme = new TRACKMOUSEEVENT();
1593 tme.size = Marshal.SizeOf(tme);
1594 tme.hWnd = msg.hwnd;
1595 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
1596 Win32TrackMouseEvent(ref tme);
1602 case Msg.WM_NCMOUSEMOVE: {
1603 if (wm_nc_registered == null || !wm_nc_registered.Contains (msg.hwnd))
1606 TRACKMOUSEEVENT tme;
1608 tme = new TRACKMOUSEEVENT ();
1609 tme.size = Marshal.SizeOf(tme);
1610 tme.hWnd = msg.hwnd;
1611 tme.dwFlags = (TMEFlags)wm_nc_registered[msg.hwnd];
1612 Win32TrackMouseEvent (ref tme);
1616 case Msg.WM_DROPFILES: {
1617 return Win32DnD.HandleWMDropFiles(ref msg);
1620 case Msg.WM_MOUSELEAVE: {
1621 prev_mouse_hwnd = IntPtr.Zero;
1622 msg.message=Msg.WM_MOUSE_LEAVE;
1626 case Msg.WM_TIMER: {
1627 Timer timer=(Timer)timer_list[(int)msg.wParam];
1629 if (timer != null) {
1639 internal override bool TranslateMessage(ref MSG msg) {
1640 return Win32TranslateMessage(ref msg);
1643 internal override IntPtr DispatchMessage(ref MSG msg) {
1644 return Win32DispatchMessage(ref msg);
1647 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
1649 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1651 } else if (!Bottom) {
1652 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1654 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
1660 internal override bool SetTopmost(IntPtr hWnd, IntPtr hWndOwner, bool Enabled) {
1662 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1665 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
1670 internal override bool Text(IntPtr handle, string text) {
1671 Win32SetWindowText(handle, text);
1675 internal override bool GetText(IntPtr handle, out string text) {
1678 sb = new StringBuilder(256);
1679 Win32GetWindowText(handle, sb, sb.Capacity);
1680 text = sb.ToString();
1684 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
1687 if (Control.FromHandle (handle) is Form) {
1690 f = (Form)Control.FromHandle (handle);
1691 WindowPlacementFlags flags = WindowPlacementFlags.SW_SHOWNORMAL;
1692 switch (f.WindowState) {
1693 case FormWindowState.Normal: flags = WindowPlacementFlags.SW_SHOWNORMAL; break;
1694 case FormWindowState.Minimized: flags = WindowPlacementFlags.SW_MINIMIZE; break;
1695 case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
1698 if (Hwnd.ObjectFromHandle (handle).no_activate)
1699 flags |= WindowPlacementFlags.SW_SHOWNOACTIVATE;
1701 Win32ShowWindow (handle, flags);
1704 if (Hwnd.ObjectFromHandle (handle).no_activate)
1705 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
1707 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
1711 Win32ShowWindow (handle, WindowPlacementFlags.SW_HIDE);
1716 internal override bool IsEnabled(IntPtr handle) {
1717 return IsWindowEnabled (handle);
1720 internal override bool IsVisible(IntPtr handle) {
1721 return IsWindowVisible (handle);
1724 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
1725 Control c = Control.FromHandle (handle);
1726 if (parent == IntPtr.Zero) {
1728 Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
1732 SetVisible (handle, c.is_visible, true);
1734 if (parent == IntPtr.Zero)
1735 return Win32SetParent (handle, FosterParent);
1737 return Win32SetParent(handle, parent);
1740 // If we ever start using this, we should probably replace FosterParent with IntPtr.Zero
1741 internal override IntPtr GetParent(IntPtr handle) {
1742 return Win32GetParent(handle);
1745 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
1747 Win32SetCapture(hWnd);
1749 if (ConfineToHwnd != IntPtr.Zero) {
1751 Win32GetWindowRect (ConfineToHwnd, out window_rect);
1752 Win32GetClipCursor (out clipped_cursor_rect);
1753 Win32ClipCursor (ref window_rect);
1757 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
1759 GrabConfined = grab_confined;
1760 GrabArea = grab_area;
1763 internal override void UngrabWindow(IntPtr hWnd) {
1764 if (!(clipped_cursor_rect.top == 0 && clipped_cursor_rect.bottom == 0 && clipped_cursor_rect.left == 0 && clipped_cursor_rect.right == 0)) {
1765 Win32ClipCursor (ref clipped_cursor_rect);
1766 clipped_cursor_rect = new RECT ();
1769 Win32ReleaseCapture();
1770 grab_hwnd = IntPtr.Zero;
1773 internal override bool CalculateWindowRect(ref Rectangle ClientRect, int Style, int ExStyle, Menu menu, out Rectangle WindowRect) {
1776 rect.left=ClientRect.Left;
1777 rect.top=ClientRect.Top;
1778 rect.right=ClientRect.Right;
1779 rect.bottom=ClientRect.Bottom;
1781 if (!Win32AdjustWindowRectEx(ref rect, Style, menu != null, ExStyle)) {
1782 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
1786 WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
1790 internal override void SetCursor(IntPtr window, IntPtr cursor) {
1791 Win32SetCursor(cursor);
1795 internal override void ShowCursor(bool show) {
1796 Win32ShowCursor(show);
1799 internal override void OverrideCursor(IntPtr cursor) {
1800 Win32SetCursor(cursor);
1803 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
1805 Bitmap cursor_bitmap;
1813 // Win32 only allows creation cursors of a certain size
1814 if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
1815 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1816 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
1818 cursor_bitmap = bitmap;
1822 width = cursor_bitmap.Width;
1823 height = cursor_bitmap.Height;
1825 cursor_bits = new Byte[(width / 8) * height];
1826 mask_bits = new Byte[(width / 8) * height];
1828 for (int y = 0; y < height; y++) {
1829 for (int x = 0; x < width; x++) {
1830 pixel = cursor_bitmap.GetPixel(x, y);
1832 if (pixel == cursor_pixel) {
1833 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1836 pixel = cursor_mask.GetPixel(x, y);
1838 if (pixel == mask_pixel) {
1839 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
1844 cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
1849 [MonoTODO("Define the missing cursors")]
1850 internal override IntPtr DefineStdCursor(StdCursor id) {
1852 case StdCursor.AppStarting: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
1853 case StdCursor.Arrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1854 case StdCursor.Cross: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
1855 case StdCursor.Default: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
1856 case StdCursor.Hand: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
1857 case StdCursor.Help: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
1858 case StdCursor.HSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1859 case StdCursor.IBeam: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
1860 case StdCursor.No: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
1861 case StdCursor.NoMove2D: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1862 case StdCursor.NoMoveHoriz: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1863 case StdCursor.NoMoveVert: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1864 case StdCursor.PanEast: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1865 case StdCursor.PanNE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1866 case StdCursor.PanNorth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1867 case StdCursor.PanNW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1868 case StdCursor.PanSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1869 case StdCursor.PanSouth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1870 case StdCursor.PanSW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1871 case StdCursor.PanWest: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1872 case StdCursor.SizeAll: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
1873 case StdCursor.SizeNESW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
1874 case StdCursor.SizeNS: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
1875 case StdCursor.SizeNWSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
1876 case StdCursor.SizeWE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
1877 case StdCursor.UpArrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
1878 case StdCursor.VSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
1879 case StdCursor.WaitCursor: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
1881 throw new NotImplementedException ();
1884 internal override void DestroyCursor(IntPtr cursor) {
1885 if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
1886 Win32DestroyCursor(cursor);
1891 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
1898 internal override void SetCursorPos(IntPtr handle, int x, int y) {
1899 Win32SetCursorPos(x, y);
1902 internal override Region GetClipRegion(IntPtr hwnd) {
1905 region = new Region();
1907 Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
1912 internal override void SetClipRegion(IntPtr hwnd, Region region) {
1913 Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
1916 internal override void EnableWindow(IntPtr handle, bool Enable) {
1917 Win32EnableWindow(handle, Enable);
1920 internal override void EndLoop(System.Threading.Thread thread) {
1924 internal override object StartLoop(System.Threading.Thread thread) {
1928 internal override void SetModal(IntPtr handle, bool Modal) {
1929 // we do nothing on Win32
1932 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
1935 Win32GetCursorPos(out pt);
1937 if (handle!=IntPtr.Zero) {
1938 Win32ScreenToClient(handle, ref pt);
1945 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
1947 POINT pnt = new POINT();
1951 Win32ScreenToClient (handle, ref pnt);
1957 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {
1958 POINT pnt = new POINT();
1963 Win32ClientToScreen(handle, ref pnt);
1969 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
1972 Win32GetWindowRect(handle, out rect);
1973 x -= rect.left + SystemInformation.FrameBorderSize.Width;
1974 y -= rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
1978 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {
1981 Win32GetWindowRect(handle, out rect);
1982 x += rect.left + SystemInformation.FrameBorderSize.Width;
1983 y += rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
1987 internal override void SendAsyncMethod (AsyncMethodData method)
1989 Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
1992 internal override void SetTimer (Timer timer)
1996 index = timer.GetHashCode();
1999 timer_list[index]=timer;
2002 Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero);
2005 internal override void KillTimer (Timer timer)
2009 index = timer.GetHashCode();
2011 Win32KillTimer(FosterParent, index);
2014 timer_list.Remove(index);
2018 private void SetMdiStyles (CreateParams cp)
2020 cp.Style = (int)WindowStyles.WS_CHILD | (int)WindowStyles.WS_CLIPCHILDREN | (int)WindowStyles.WS_CLIPSIBLINGS;
2024 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
2025 Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
2026 caret_visible = false;
2029 internal override void DestroyCaret(IntPtr hwnd) {
2030 Win32DestroyCaret();
2033 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
2034 Win32SetCaretPos(x, y);
2037 internal override void CaretVisible(IntPtr hwnd, bool visible) {
2039 if (!caret_visible) {
2040 Win32ShowCaret(hwnd);
2041 caret_visible = true;
2044 if (caret_visible) {
2045 Win32HideCaret(hwnd);
2046 caret_visible = false;
2051 internal override IntPtr GetFocus() {
2052 return Win32GetFocus();
2055 internal override void SetFocus(IntPtr hwnd) {
2056 Win32SetFocus(hwnd);
2059 internal override IntPtr GetActive() {
2060 return Win32GetActiveWindow();
2063 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
2067 tm = new TEXTMETRIC();
2069 dc = Win32GetDC(IntPtr.Zero);
2070 Win32SelectObject(dc, font.ToHfont());
2071 if (Win32GetTextMetrics(dc, ref tm) == false) {
2072 Win32ReleaseDC(IntPtr.Zero, dc);
2077 Win32ReleaseDC(IntPtr.Zero, dc);
2079 ascent = tm.tmAscent;
2080 descent = tm.tmDescent;
2085 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
2089 rect.left = rectangle.X;
2090 rect.top = rectangle.Y;
2091 rect.right = rectangle.Right;
2092 rect.bottom = rectangle.Bottom;
2094 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));
2095 Win32UpdateWindow(hwnd);
2098 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
2099 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));
2102 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
2105 nid = new NOTIFYICONDATA();
2107 nid.cbSize = (uint)Marshal.SizeOf(nid);
2110 nid.uCallbackMessage = (uint)Msg.WM_USER;
2111 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2115 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2119 nid.hIcon = icon.Handle;
2120 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2125 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
2128 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
2131 nid = new NOTIFYICONDATA();
2133 nid.cbSize = (uint)Marshal.SizeOf(nid);
2134 nid.hIcon = icon.Handle;
2137 nid.uCallbackMessage = (uint)Msg.WM_USER;
2138 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2142 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2146 nid.hIcon = icon.Handle;
2147 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2150 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2153 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
2156 nid = new NOTIFYICONDATA();
2158 nid.cbSize = (uint)Marshal.SizeOf(nid);
2163 Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
2167 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
2168 // Nothing to do on Win32
2171 internal override void SetMenu(IntPtr handle, Menu menu) {
2172 // Trigger WM_NCCALC
2173 Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2176 internal override Point GetMenuOrigin(IntPtr handle) {
2177 return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2180 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2181 Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : icon.Handle); // 1 = large icon (0 would be small)
2184 internal override void ClipboardClose(IntPtr handle) {
2185 if (handle != clip_magic) {
2186 throw new ArgumentException("handle is not a valid clipboard handle");
2188 Win32CloseClipboard();
2191 internal override int ClipboardGetID(IntPtr handle, string format) {
2192 if (handle != clip_magic) {
2193 throw new ArgumentException("handle is not a valid clipboard handle");
2195 if (format == "Text" ) return 1;
2196 else if (format == "Bitmap" ) return 2;
2197 else if (format == "MetaFilePict" ) return 3;
2198 else if (format == "SymbolicLink" ) return 4;
2199 else if (format == "DataInterchangeFormat" ) return 5;
2200 else if (format == "Tiff" ) return 6;
2201 else if (format == "OEMText" ) return 7;
2202 else if (format == "DeviceIndependentBitmap" ) return 8;
2203 else if (format == "Palette" ) return 9;
2204 else if (format == "PenData" ) return 10;
2205 else if (format == "RiffAudio" ) return 11;
2206 else if (format == "WaveAudio" ) return 12;
2207 else if (format == "UnicodeText" ) return 13;
2208 else if (format == "EnhancedMetafile" ) return 14;
2209 else if (format == "FileDrop" ) return 15;
2210 else if (format == "Locale" ) return 16;
2212 return (int)Win32RegisterClipboardFormat(format);
2215 internal override IntPtr ClipboardOpen(bool primary_selection) {
2216 // Win32 does not have primary selection
2217 Win32OpenClipboard(FosterParent);
2221 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2226 if (handle != clip_magic) {
2234 format = Win32EnumClipboardFormats(format);
2238 } while (format != 0);
2241 result = new int[count];
2245 format = Win32EnumClipboardFormats(format);
2247 result[count++] = (int)format;
2249 } while (format != 0);
2255 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2260 if (handle != clip_magic) {
2261 throw new ArgumentException("handle is not a valid clipboard handle");
2264 hmem = Win32GetClipboardData((uint)type);
2265 if (hmem == IntPtr.Zero) {
2269 data = Win32GlobalLock(hmem);
2270 if (data == IntPtr.Zero) {
2271 uint error = Win32GetLastError();
2272 Console.WriteLine("Error: {0}", error);
2278 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2279 obj = AnsiToString(data);
2280 } else switch ((ClipboardFormats)type) {
2281 case ClipboardFormats.CF_TEXT: {
2282 obj = AnsiToString(data);
2286 case ClipboardFormats.CF_DIB: {
2287 obj = DIBtoImage(data);
2291 case ClipboardFormats.CF_UNICODETEXT: {
2292 obj = UnicodeToString(data);
2297 if (converter != null && !converter(type, data, out obj)) {
2303 Win32GlobalUnlock(hmem);
2309 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
2314 if (handle != clip_magic) {
2315 throw new ArgumentException("handle is not a valid clipboard handle");
2320 Win32EmptyClipboard();
2325 if (obj is string) {
2326 type = (int)ClipboardFormats.CF_UNICODETEXT;
2327 } else if (obj is Image) {
2328 type = (int)ClipboardFormats.CF_DIB;
2332 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2333 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2334 Win32SetClipboardData((uint)type, hmem);
2336 } else switch((ClipboardFormats)type) {
2337 case ClipboardFormats.CF_UNICODETEXT: {
2338 hmem = Marshal.StringToHGlobalUni((string)obj);
2339 Win32SetClipboardData((uint)type, hmem);
2343 case ClipboardFormats.CF_TEXT: {
2344 hmem = Marshal.StringToHGlobalAnsi((string)obj);
2345 Win32SetClipboardData((uint)type, hmem);
2349 case ClipboardFormats.CF_BITMAP:
2350 case ClipboardFormats.CF_DIB: {
2351 data = ImageToDIB((Image)obj);
2353 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2354 hmem_ptr = Win32GlobalLock(hmem);
2355 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2356 Win32GlobalUnlock(hmem);
2357 Win32SetClipboardData((uint)ClipboardFormats.CF_DIB, hmem);
2362 if (converter != null && converter(ref type, obj, out data)) {
2363 hmem = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
2364 hmem_ptr = Win32GlobalLock(hmem);
2365 Marshal.Copy(data, 0, hmem_ptr, data.Length);
2366 Win32GlobalUnlock(hmem);
2367 Win32SetClipboardData((uint)type, hmem);
2374 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
2376 Win32DnD.RegisterDropTarget(hwnd);
2378 Win32DnD.UnregisterDropTarget(hwnd);
2382 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
2383 return Win32DnD.StartDrag(hwnd, data, allowedEffects);
2386 // XXX this doesn't work at all for FrameStyle.Dashed - it draws like Thick, and in the Thick case
2387 // the corners are drawn incorrectly.
2388 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
2392 COLORREF clrRef = new COLORREF();
2394 // If we want the standard hatch pattern we would
2395 // need to create a brush
2397 clrRef.R = backColor.R;
2398 clrRef.G = backColor.G;
2399 clrRef.B = backColor.B;
2402 pen = Win32CreatePen (style == FrameStyle.Thick ? PenStyle.PS_SOLID : PenStyle.PS_DASH,
2403 style == FrameStyle.Thick ? 4 : 2, ref clrRef);
2405 hdc = Win32GetDC(IntPtr.Zero);
2406 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2407 oldpen = Win32SelectObject(hdc, pen);
2409 Win32MoveToEx(hdc, rectangle.Left, rectangle.Top, IntPtr.Zero);
2410 if ((rectangle.Width > 0) && (rectangle.Height > 0)) {
2411 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2412 Win32LineTo(hdc, rectangle.Right, rectangle.Bottom);
2413 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2414 Win32LineTo(hdc, rectangle.Left, rectangle.Top);
2416 if (rectangle.Width > 0) {
2417 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
2419 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
2423 Win32SelectObject(hdc, oldpen);
2424 Win32DeleteObject(pen);
2426 Win32ReleaseDC(IntPtr.Zero, hdc);
2429 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
2434 COLORREF clrRef = new COLORREF();
2439 Win32ClientToScreen(IntPtr.Zero, ref pt);
2441 // If we want the standard hatch pattern we would
2442 // need to create a brush
2444 clrRef.R = backColor.R;
2445 clrRef.G = backColor.G;
2446 clrRef.B = backColor.B;
2449 pen = Win32CreatePen(PenStyle.PS_SOLID, 1, ref clrRef);
2451 hdc = Win32GetDC(IntPtr.Zero);
2452 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2453 oldpen = Win32SelectObject(hdc, pen);
2455 Win32MoveToEx(hdc, pt.x + start.X, pt.y + start.Y, IntPtr.Zero);
2456 Win32LineTo(hdc, pt.x + end.X, pt.y + end.Y);
2458 Win32SelectObject(hdc, oldpen);
2459 Win32DeleteObject(pen);
2461 Win32ReleaseDC(IntPtr.Zero, hdc);
2464 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
2469 rect.left = rectangle.Left;
2470 rect.top = rectangle.Top;
2471 rect.right = rectangle.Right;
2472 rect.bottom = rectangle.Bottom;
2477 COLORREF clrRef = new COLORREF();
2479 clrRef.R = backColor.R;
2480 clrRef.G = backColor.G;
2481 clrRef.B = backColor.B;
2484 brush = Win32CreateSolidBrush (clrRef);
2486 hdc = Win32GetDC(IntPtr.Zero);
2487 oldbrush = Win32SelectObject(hdc, brush);
2489 Win32PatBlt (hdc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, PatBltRop.DSTINVERT);
2491 Win32SelectObject(hdc, oldbrush);
2492 Win32DeleteObject(brush);
2494 Win32ReleaseDC(IntPtr.Zero, hdc);
2497 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
2506 Win32ClientToScreen(handle, ref pt);
2508 // If we want the standard hatch pattern we would
2509 // need to create a brush
2512 pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
2514 hdc = Win32GetDC(IntPtr.Zero);
2515 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
2516 oldpen = Win32SelectObject(hdc, pen);
2518 Control c = Control.FromHandle (handle);
2521 Win32GetWindowRect (c.Handle, out window_rect);
2522 Region r = new Region (new Rectangle(window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top));
2523 Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
2526 Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
2527 if ((rect.Width > 0) && (rect.Height > 0)) {
2528 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2529 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
2530 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2531 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
2533 if (rect.Width > 0) {
2534 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
2536 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
2540 Win32SelectObject(hdc, oldpen);
2541 Win32DeleteObject(pen);
2543 Win32ExtSelectClipRgn(hdc, IntPtr.Zero, (int) ClipCombineMode.RGN_COPY);
2545 Win32ReleaseDC(IntPtr.Zero, hdc);
2548 internal override SizeF GetAutoScaleSize(Font font) {
2551 string magic_string = "The quick brown fox jumped over the lazy dog.";
2552 double magic_number = 44.549996948242189;
2554 g = Graphics.FromHwnd(FosterParent);
2556 width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
2557 return new SizeF(width, font.Height);
2560 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2561 return Win32SendMessage(hwnd, message, wParam, lParam);
2564 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
2565 return Win32PostMessage(hwnd, message, wParam, lParam);
2568 internal override int SendInput (IntPtr hwnd, Queue keys) {
2569 INPUT[] inputs = new INPUT[keys.Count];
2570 const Int32 INPUT_KEYBOARD = 1;
2573 while (keys.Count > 0) {
2574 MSG msg = (MSG)keys.Dequeue();
2577 inputs[i].ki.wScan = 0;
2578 inputs[i].ki.time = 0;
2579 inputs[i].ki.dwFlags = (Int32)(msg.message == Msg.WM_KEYUP ? InputFlags.KEYEVENTF_KEYUP : 0);
2580 inputs[i].ki.wVk = (short)msg.wParam.ToInt32();
2581 inputs[i].type = INPUT_KEYBOARD;
2584 returns = Win32SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
2586 return (int) returns;
2589 internal override int KeyboardSpeed {
2591 Console.WriteLine ("KeyboardSpeed: need to query Windows");
2594 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
2600 internal override int KeyboardDelay {
2602 Console.WriteLine ("KeyboardDelay: need to query Windows");
2605 // Return values must range from 0 to 4, 0 meaning 250ms,
2606 // and 4 meaning 1000 ms.
2612 internal override event EventHandler Idle;
2614 #endregion // Public Static Methods
2616 #region Win32 Imports
2617 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
2618 private extern static uint Win32GetLastError();
2620 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2621 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);
2623 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
2624 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
2626 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2627 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
2629 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2630 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
2632 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
2633 internal extern static bool Win32TranslateMessage(ref MSG msg);
2635 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2636 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
2638 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
2639 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
2641 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2642 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2644 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
2645 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
2647 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2648 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
2650 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2651 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
2653 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
2654 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
2656 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2657 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
2659 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2660 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
2662 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
2663 private extern static IntPtr Win32ShowCursor(bool bShow);
2665 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
2666 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
2668 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
2669 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
2671 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
2672 private extern static bool Win32DestroyCursor(IntPtr hCursor);
2674 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2675 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2677 //[DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2678 //private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
2680 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
2681 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
2683 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
2684 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
2686 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
2687 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
2689 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
2690 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2692 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
2693 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
2695 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
2696 private extern static IntPtr Win32GetDC(IntPtr hWnd);
2698 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
2699 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
2701 //[DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
2702 //private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
2704 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
2705 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
2707 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2708 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
2710 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2711 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
2713 //[DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
2714 //private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
2716 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
2717 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
2719 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
2720 private extern static IntPtr Win32ReleaseCapture();
2722 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
2723 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
2725 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
2726 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
2728 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
2729 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
2731 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
2732 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
2734 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
2735 private extern static IntPtr Win32GetParent(IntPtr hWnd);
2737 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
2738 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
2740 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
2741 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
2743 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
2744 private extern static bool Win32GetCursorPos(out POINT lpPoint);
2746 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
2747 private extern static bool Win32SetCursorPos(int x, int y);
2749 //[DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
2750 //private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
2752 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
2753 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
2755 //[DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
2756 //private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
2758 [DllImport ("gdi32.dll", EntryPoint="CreateSolidBrush", CallingConvention=CallingConvention.StdCall)]
2759 private extern static IntPtr Win32CreateSolidBrush(COLORREF clrRef);
2761 [DllImport ("gdi32.dll", EntryPoint="PatBlt", CallingConvention=CallingConvention.StdCall)]
2762 private extern static int Win32PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, PatBltRop dwRop);
2764 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
2765 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
2767 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
2768 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
2770 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2771 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
2773 [DllImport ("user32.dll", EntryPoint="GetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
2774 private extern static uint Win32GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out LayeredWindowAttributes pwdFlags);
2776 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
2777 private extern static bool Win32DeleteObject(IntPtr o);
2779 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
2780 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
2782 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
2783 private extern static IntPtr Win32GetDesktopWindow();
2785 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
2786 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
2788 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
2789 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
2791 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
2792 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
2794 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
2795 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
2797 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
2798 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
2800 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
2801 internal extern static IntPtr Win32GetFocus();
2803 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
2804 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
2806 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
2807 private extern static bool Win32DestroyCaret();
2809 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
2810 private extern static bool Win32ShowCaret(IntPtr hwnd);
2812 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
2813 private extern static bool Win32HideCaret(IntPtr hwnd);
2815 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
2816 private extern static bool Win32SetCaretPos(int X, int Y);
2818 //[DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
2819 //private extern static uint Win32GetCaretBlinkTime();
2821 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2822 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
2824 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
2825 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
2827 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2828 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
2830 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2831 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
2833 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2834 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
2836 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2837 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2839 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2840 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2842 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2843 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2845 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
2846 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
2848 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
2849 private extern static IntPtr Win32GetActiveWindow();
2851 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
2852 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
2854 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2855 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
2857 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
2858 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
2860 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
2861 private extern static bool IsWindowEnabled(IntPtr hwnd);
2863 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
2864 private extern static bool IsWindowVisible(IntPtr hwnd);
2866 //[DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
2867 //private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
2869 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2870 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
2872 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2873 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
2875 [DllImport ("user32.dll", EntryPoint="SendInput", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2876 private extern static UInt32 Win32SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray)] INPUT[] inputs, Int32 cbSize);
2878 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2879 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
2881 //[DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2882 //private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
2884 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2885 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
2887 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
2888 private extern static bool Win32OpenClipboard(IntPtr hwnd);
2890 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
2891 private extern static bool Win32EmptyClipboard();
2893 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
2894 private extern static uint Win32RegisterClipboardFormat(string format);
2896 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
2897 private extern static bool Win32CloseClipboard();
2899 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
2900 private extern static uint Win32EnumClipboardFormats(uint format);
2902 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
2903 private extern static IntPtr Win32GetClipboardData(uint format);
2905 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
2906 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
2908 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
2909 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
2911 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
2912 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
2914 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
2915 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
2917 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
2918 internal extern static uint Win32GlobalSize(IntPtr hMem);
2920 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
2921 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
2923 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
2924 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
2926 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
2927 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
2929 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
2930 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
2932 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
2933 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
2935 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
2936 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
2938 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
2939 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
2941 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
2942 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
2944 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
2945 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
2947 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
2948 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
2950 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
2951 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
2953 [DllImport("gdi32.dll", EntryPoint = "ExcludeClipRect", CallingConvention = CallingConvention.StdCall)]
2954 internal extern static int Win32ExcludeClipRect (IntPtr hdc, int left, int top, int right, int bottom);
2956 [DllImport ("gdi32.dll", EntryPoint="ExtSelectClipRgn", CallingConvention=CallingConvention.StdCall)]
2957 internal extern static int Win32ExtSelectClipRgn(IntPtr hdc, IntPtr hrgn, int mode);
2959 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
2960 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
2962 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
2963 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
2965 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
2966 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
2968 [DllImport ("user32.dll", EntryPoint="ClipCursor", CallingConvention=CallingConvention.StdCall)]
2969 internal extern static bool Win32ClipCursor (ref RECT lpRect);
2971 [DllImport ("user32.dll", EntryPoint="GetClipCursor", CallingConvention=CallingConvention.StdCall)]
2972 internal extern static bool Win32GetClipCursor (out RECT lpRect);