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;
38 using System.Threading;
42 namespace System.Windows.Forms {
43 internal class XplatUIWin32 : XplatUIDriver {
44 #region Local Variables
45 private static XplatUIWin32 instance;
46 private static int ref_count;
47 private static IntPtr FosterParent;
49 internal static MouseButtons mouse_state;
50 internal static Point mouse_position;
51 internal static bool grab_confined;
52 internal static IntPtr grab_hwnd;
53 internal static Rectangle grab_area;
54 internal static WndProc wnd_proc;
55 internal static IntPtr prev_mouse_hwnd;
56 internal static bool caret_visible;
58 internal static bool themes_enabled;
59 private Hashtable timer_list;
60 private static Queue message_queue;
61 private static IntPtr clip_magic = new IntPtr(27051977);
62 private static int scroll_width;
63 private static int scroll_height;
64 private static Hashtable wm_nc_registered;
65 private static RECT clipped_cursor_rect;
66 private Hashtable registered_classes;
67 private Hwnd HwndCreating; // the Hwnd we are currently creating (see CreateWindow)
69 #endregion // Local Variables
71 #region Private Structs
72 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
73 private struct WNDCLASS {
75 internal WndProc lpfnWndProc;
76 internal int cbClsExtra;
77 internal int cbWndExtra;
78 internal IntPtr hInstance;
79 internal IntPtr hIcon;
80 internal IntPtr hCursor;
81 internal IntPtr hbrBackground;
82 [MarshalAs(UnmanagedType.LPWStr)]
83 internal string lpszMenuName;
84 [MarshalAs(UnmanagedType.LPWStr)]
85 internal string lpszClassName;
88 [StructLayout(LayoutKind.Sequential)]
89 internal struct RECT {
95 public RECT (int left, int top, int right, int bottom)
100 this.bottom = bottom;
103 #region Instance Properties
104 public int Height { get { return bottom - top; } }
105 public int Width { get { return right - left; } }
106 public Size Size { get { return new Size (Width, Height); } }
107 public Point Location { get { return new Point (left, top); } }
110 #region Instance Methods
111 public Rectangle ToRectangle ()
113 return Rectangle.FromLTRB (left, top, right, bottom);
116 public static RECT FromRectangle (Rectangle rectangle)
118 return new RECT (rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
121 public override int GetHashCode ()
123 return left ^ ((top << 13) | (top >> 0x13))
124 ^ ((Width << 0x1a) | (Width >> 6))
125 ^ ((Height << 7) | (Height >> 0x19));
128 public override string ToString ()
130 return String.Format("RECT left={0}, top={1}, right={2}, bottom={3}, width={4}, height={5}", left, top, right, bottom, right-left, bottom-top);
134 #region Operator overloads
135 public static implicit operator Rectangle (RECT rect)
137 return Rectangle.FromLTRB (rect.left, rect.top, rect.right, rect.bottom);
140 public static implicit operator RECT (Rectangle rect)
142 return new RECT (rect.Left, rect.Top, rect.Right, rect.Bottom);
147 internal enum SPIAction {
148 SPI_GETACTIVEWINDOWTRACKING = 0x1000,
149 SPI_GETACTIVEWNDTRKTIMEOUT = 0x2002,
150 SPI_GETANIMATION = 0x0048,
151 SPI_GETCARETWIDTH = 0x2006,
152 SPI_GETCOMBOBOXANIMATION = 0x1004,
153 SPI_GETDRAGFULLWINDOWS = 0x0026,
154 SPI_GETDROPSHADOW = 0x1024,
155 SPI_GETFONTSMOOTHING = 0x004A,
156 SPI_GETFONTSMOOTHINGCONTRAST = 0x200C,
157 SPI_GETFONTSMOOTHINGTYPE = 0x200A,
158 SPI_GETGRADIENTCAPTIONS = 0x1008,
159 SPI_GETHOTTRACKING = 0x100E,
160 SPI_GETICONTITLEWRAP = 0x0019,
161 SPI_GETKEYBOARDSPEED = 0x000A,
162 SPI_GETKEYBOARDDELAY = 0x0016,
163 SPI_GETKEYBOARDCUES = 0x100A,
164 SPI_GETKEYBOARDPREF = 0x0044,
165 SPI_GETLISTBOXSMOOTHSCROLLING = 0x1006,
166 SPI_GETMENUANIMATION = 0x1002,
167 SPI_GETMENUDROPALIGNMENT = 0x001B,
168 SPI_GETMENUFADE = 0x1012,
169 SPI_GETMENUSHOWDELAY = 0x006A,
170 SPI_GETMOUSESPEED = 0x0070,
171 SPI_GETSELECTIONFADE = 0x1014,
172 SPI_GETSNAPTODEFBUTTON = 0x005F,
173 SPI_GETTOOLTIPANIMATION = 0x1016,
174 SPI_GETWORKAREA = 0x0030,
175 SPI_GETMOUSEHOVERWIDTH = 0x0062,
176 SPI_GETMOUSEHOVERHEIGHT = 0x0064,
177 SPI_GETMOUSEHOVERTIME = 0x0066,
178 SPI_GETUIEFFECTS = 0x103E,
179 SPI_GETWHEELSCROLLLINES = 0x0068
182 internal enum WindowPlacementFlags {
186 SW_SHOWMINIMIZED = 2,
187 SW_SHOWMAXIMIZED = 3,
189 SW_SHOWNOACTIVATE = 4,
192 SW_SHOWMINNOACTIVE = 7,
196 SW_FORCEMINIMIZE = 11,
200 [StructLayout(LayoutKind.Sequential)]
201 private struct WINDOWPLACEMENT {
202 internal uint length;
204 internal WindowPlacementFlags showCmd;
205 internal POINT ptMinPosition;
206 internal POINT ptMaxPosition;
207 internal RECT rcNormalPosition;
210 [StructLayout(LayoutKind.Sequential)]
211 internal struct NCCALCSIZE_PARAMS {
215 internal IntPtr lppos;
219 private enum TMEFlags {
220 TME_HOVER = 0x00000001,
221 TME_LEAVE = 0x00000002,
222 TME_NONCLIENT = 0x00000010,
223 TME_QUERY = unchecked((int)0x40000000),
224 TME_CANCEL = unchecked((int)0x80000000)
227 [StructLayout(LayoutKind.Sequential)]
228 private struct TRACKMOUSEEVENT {
230 internal TMEFlags dwFlags;
231 internal IntPtr hWnd;
232 internal int dwHoverTime;
235 [StructLayout(LayoutKind.Sequential)]
236 private struct PAINTSTRUCT {
239 internal RECT rcPaint;
240 internal int fRestore;
241 internal int fIncUpdate;
242 internal int Reserved1;
243 internal int Reserved2;
244 internal int Reserved3;
245 internal int Reserved4;
246 internal int Reserved5;
247 internal int Reserved6;
248 internal int Reserved7;
249 internal int Reserved8;
252 [StructLayout(LayoutKind.Sequential)]
253 internal struct KEYBDINPUT {
255 internal short wScan;
256 internal Int32 dwFlags;
258 internal UIntPtr dwExtraInfo;
261 [StructLayout(LayoutKind.Sequential)]
262 internal struct MOUSEINPUT {
265 internal Int32 mouseData;
266 internal Int32 dwFlags;
268 internal UIntPtr dwExtraInfo;
271 [StructLayout(LayoutKind.Sequential)]
272 internal struct HARDWAREINPUT {
274 internal short wParamL;
275 internal short wParamH;
278 [StructLayout (LayoutKind.Sequential)]
279 internal struct ICONINFO {
281 internal Int32 xHotspot;
282 internal Int32 yHotspot;
283 internal IntPtr hbmMask;
284 internal IntPtr hbmColor;
287 [StructLayout(LayoutKind.Explicit)]
288 internal struct INPUT {
293 internal MOUSEINPUT mi;
296 internal KEYBDINPUT ki;
299 internal HARDWAREINPUT hi;
302 [StructLayout (LayoutKind.Sequential)]
303 public struct ANIMATIONINFO {
304 internal uint cbSize;
305 internal int iMinAnimate;
308 internal enum InputFlags {
309 KEYEVENTF_EXTENDEDKEY = 0x0001,
310 KEYEVENTF_KEYUP = 0x0002,
311 KEYEVENTF_SCANCODE = 0x0003,
312 KEYEVENTF_UNICODE = 0x0004,
315 internal enum ClassStyle {
316 CS_VREDRAW = 0x00000001,
317 CS_HREDRAW = 0x00000002,
318 CS_KEYCVTWINDOW = 0x00000004,
319 CS_DBLCLKS = 0x00000008,
320 CS_OWNDC = 0x00000020,
321 CS_CLASSDC = 0x00000040,
322 CS_PARENTDC = 0x00000080,
323 CS_NOKEYCVT = 0x00000100,
324 CS_NOCLOSE = 0x00000200,
325 CS_SAVEBITS = 0x00000800,
326 CS_BYTEALIGNCLIENT = 0x00001000,
327 CS_BYTEALIGNWINDOW = 0x00002000,
328 CS_GLOBALCLASS = 0x00004000,
331 CS_DROPSHADOW = 0x00020000
334 internal enum SetWindowPosZOrder {
342 internal enum SetWindowPosFlags {
343 SWP_ASYNCWINDOWPOS = 0x4000,
344 SWP_DEFERERASE = 0x2000,
345 SWP_DRAWFRAME = 0x0020,
346 SWP_FRAMECHANGED = 0x0020,
347 SWP_HIDEWINDOW = 0x0080,
348 SWP_NOACTIVATE = 0x0010,
349 SWP_NOCOPYBITS = 0x0100,
351 SWP_NOOWNERZORDER = 0x0200,
352 SWP_NOREDRAW = 0x0008,
353 SWP_NOREPOSITION = 0x0200,
354 SWP_NOENDSCHANGING = 0x0400,
356 SWP_NOZORDER = 0x0004,
357 SWP_SHOWWINDOW = 0x0040
360 internal enum GetSysColorIndex {
362 COLOR_BACKGROUND = 1,
363 COLOR_ACTIVECAPTION = 2,
364 COLOR_INACTIVECAPTION = 3,
367 COLOR_WINDOWFRAME = 6,
369 COLOR_WINDOWTEXT = 8,
370 COLOR_CAPTIONTEXT = 9,
371 COLOR_ACTIVEBORDER = 10,
372 COLOR_INACTIVEBORDER = 11,
373 COLOR_APPWORKSPACE = 12,
374 COLOR_HIGHLIGHT = 13,
375 COLOR_HIGHLIGHTTEXT = 14,
377 COLOR_BTNSHADOW = 16,
380 COLOR_INACTIVECAPTIONTEXT = 19,
381 COLOR_BTNHIGHLIGHT = 20,
382 COLOR_3DDKSHADOW = 21,
388 COLOR_GRADIENTACTIVECAPTION = 27,
389 COLOR_GRADIENTINACTIVECAPTION = 28,
390 COLOR_MENUHIGHLIGHT = 29,
396 COLOR_3DHIGHLIGHT = 20,
397 COLOR_3DHILIGHT = 20,
398 COLOR_BTNHILIGHT = 20,
399 COLOR_MAXVALUE = 24,/* Maximum value */
402 private enum LoadCursorType {
411 IDC_SIZENWSE = 32642,
412 IDC_SIZENESW = 32643,
418 IDC_APPSTARTING = 32650,
423 private enum AncestorType {
430 private enum WindowLong {
441 private enum LogBrushStyle {
450 BS_DIBPATTERN8X8 = 8,
455 private enum LogBrushHatch {
456 HS_HORIZONTAL = 0, /* ----- */
457 HS_VERTICAL = 1, /* ||||| */
458 HS_FDIAGONAL = 2, /* \\\\\ */
459 HS_BDIAGONAL = 3, /* ///// */
460 HS_CROSS = 4, /* +++++ */
461 HS_DIAGCROSS = 5, /* xxxxx */
464 internal struct COLORREF {
471 [StructLayout(LayoutKind.Sequential)]
472 private struct LOGBRUSH {
473 internal LogBrushStyle lbStyle;
474 internal COLORREF lbColor;
475 internal LogBrushHatch lbHatch;
478 [StructLayout(LayoutKind.Sequential)]
479 internal struct TEXTMETRIC {
480 internal int tmHeight;
481 internal int tmAscent;
482 internal int tmDescent;
483 internal int tmInternalLeading;
484 internal int tmExternalLeading;
485 internal int tmAveCharWidth;
486 internal int tmMaxCharWidth;
487 internal int tmWeight;
488 internal int tmOverhang;
489 internal int tmDigitizedAspectX;
490 internal int tmDigitizedAspectY;
491 internal short tmFirstChar;
492 internal short tmLastChar;
493 internal short tmDefaultChar;
494 internal short tmBreakChar;
495 internal byte tmItalic;
496 internal byte tmUnderlined;
497 internal byte tmStruckOut;
498 internal byte tmPitchAndFamily;
499 internal byte tmCharSet;
502 public enum TernaryRasterOperations : uint
504 SRCCOPY = 0x00CC0020,
505 SRCPAINT = 0x00EE0086,
507 SRCINVERT = 0x00660046,
508 SRCERASE = 0x00440328,
509 NOTSRCCOPY = 0x00330008,
510 NOTSRCERASE = 0x001100A6,
511 MERGECOPY = 0x00C000CA,
512 MERGEPAINT = 0x00BB0226,
513 PATCOPY = 0x00F00021,
514 PATPAINT = 0x00FB0A09,
515 PATINVERT = 0x005A0049,
516 DSTINVERT = 0x00550009,
517 BLACKNESS = 0x00000042,
518 WHITENESS = 0x00FF0062
522 private enum ScrollWindowExFlags {
524 SW_SCROLLCHILDREN = 0x0001,
525 SW_INVALIDATE = 0x0002,
527 SW_SMOOTHSCROLL = 0x0010
530 internal enum SystemMetrics {
547 SM_CXFULLSCREEN = 16,
548 SM_CYFULLSCREEN = 17,
549 SM_CYKANJIWINDOW = 18,
550 SM_MOUSEPRESENT = 19,
569 SM_CXICONSPACING = 38,
570 SM_CYICONSPACING = 39,
571 SM_MENUDROPALIGNMENT = 40,
574 SM_CMOUSEBUTTONS = 43,
575 SM_CXFIXEDFRAME = SM_CXDLGFRAME,
576 SM_CYFIXEDFRAME = SM_CYDLGFRAME,
577 SM_CXSIZEFRAME = SM_CXFRAME,
578 SM_CYSIZEFRAME = SM_CYFRAME,
582 SM_CXMINSPACING = 47,
583 SM_CYMINSPACING = 48,
606 SM_MIDEASTENABLED = 74,
607 SM_MOUSEWHEELPRESENT = 75,
608 SM_XVIRTUALSCREEN = 76,
609 SM_YVIRTUALSCREEN = 77,
610 SM_CXVIRTUALSCREEN = 78,
611 SM_CYVIRTUALSCREEN = 79,
613 SM_SAMEDISPLAYFORMAT = 81,
615 SM_CXFOCUSBORDER = 83,
616 SM_CYFOCUSBORDER = 84,
622 // We'll only support _WIN32_IE < 0x0500 for now
623 internal enum NotifyIconMessage {
624 NIM_ADD = 0x00000000,
625 NIM_MODIFY = 0x00000001,
626 NIM_DELETE = 0x00000002,
630 internal enum NotifyIconFlags {
631 NIF_MESSAGE = 0x00000001,
632 NIF_ICON = 0x00000002,
633 NIF_TIP = 0x00000004,
634 NIF_STATE = 0x00000008,
635 NIF_INFO = 0x00000010
638 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
639 internal struct NOTIFYICONDATA {
640 internal uint cbSize;
641 internal IntPtr hWnd;
643 internal NotifyIconFlags uFlags;
644 internal uint uCallbackMessage;
645 internal IntPtr hIcon;
646 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
647 internal string szTip;
648 internal int dwState;
649 internal int dwStateMask;
650 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
651 internal string szInfo;
652 internal int uTimeoutOrVersion;
653 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
654 internal string szInfoTitle;
655 internal ToolTipIcon dwInfoFlags;
659 internal enum DCExFlags {
660 DCX_WINDOW = 0x00000001,
661 DCX_CACHE = 0x00000002,
662 DCX_NORESETATTRS = 0x00000004,
663 DCX_CLIPCHILDREN = 0x00000008,
664 DCX_CLIPSIBLINGS = 0x00000010,
665 DCX_PARENTCLIP = 0x00000020,
666 DCX_EXCLUDERGN = 0x00000040,
667 DCX_INTERSECTRGN = 0x00000080,
668 DCX_EXCLUDEUPDATE = 0x00000100,
669 DCX_INTERSECTUPDATE = 0x00000200,
670 DCX_LOCKWINDOWUPDATE = 0x00000400,
671 DCX_USESTYLE = 0x00010000,
672 DCX_VALIDATE = 0x00200000
675 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
676 internal struct CLIENTCREATESTRUCT {
677 internal IntPtr hWindowMenu;
678 internal uint idFirstChild;
681 private enum ClassLong : int {
683 GCL_HBRBACKGROUND = -10,
687 GCL_CBWNDEXTRA = -18,
688 GCL_CBCLSEXTRA = -20,
696 internal enum GAllocFlags : uint {
698 GMEM_MOVEABLE = 0x0002,
699 GMEM_NOCOMPACT = 0x0010,
700 GMEM_NODISCARD = 0x0020,
701 GMEM_ZEROINIT = 0x0040,
702 GMEM_MODIFY = 0x0080,
703 GMEM_DISCARDABLE = 0x0100,
704 GMEM_NOT_BANKED = 0x1000,
706 GMEM_DDESHARE = 0x2000,
707 GMEM_NOTIFY = 0x4000,
708 GMEM_LOWER = GMEM_NOT_BANKED,
709 GMEM_VALID_FLAGS = 0x7F72,
710 GMEM_INVALID_HANDLE = 0x8000,
711 GHND = (GMEM_MOVEABLE | GMEM_ZEROINIT),
712 GPTR = (GMEM_FIXED | GMEM_ZEROINIT)
715 internal enum ROP2DrawMode : int {
735 internal enum PenStyle : int {
747 internal enum PatBltRop : int {
749 PATINVERT = 0x5a0049,
750 DSTINVERT = 0x550009,
751 BLACKNESS = 0x000042,
752 WHITENESS = 0xff0062,
755 internal enum StockObject : int {
762 HOLLOW_BRUSH = NULL_BRUSH,
767 ANSI_FIXED_FONT = 11,
770 DEVICE_DEFAULT_FONT = 14,
771 DEFAULT_PALETTE = 15,
772 SYSTEM_FIXED_FONT = 16
775 internal enum HatchStyle : int {
785 internal enum SndFlags : int {
788 SND_NODEFAULT = 0x0002,
792 SND_NOWAIT = 0x00002000,
793 SND_ALIAS = 0x00010000,
794 SND_ALIAS_ID = 0x00110000,
795 SND_FILENAME = 0x00020000,
796 SND_RESOURCE = 0x00040004,
798 SND_APPLICATION = 0x0080,
802 internal enum LayeredWindowAttributes : int {
807 public enum ACLineStatus : byte {
813 public enum BatteryFlag : byte {
818 NoSystemBattery = 128,
822 [StructLayout (LayoutKind.Sequential)]
823 public class SYSTEMPOWERSTATUS {
824 public ACLineStatus _ACLineStatus;
825 public BatteryFlag _BatteryFlag;
826 public Byte _BatteryLifePercent;
827 public Byte _Reserved1;
828 public Int32 _BatteryLifeTime;
829 public Int32 _BatteryFullLifeTime;
833 #region Constructor & Destructor
834 private XplatUIWin32() {
835 // Handle singleton stuff first
838 mouse_state = MouseButtons.None;
839 mouse_position = Point.Empty;
841 grab_confined = false;
842 grab_area = Rectangle.Empty;
844 message_queue = new Queue();
846 themes_enabled = false;
848 wnd_proc = new WndProc(InternalWndProc);
850 FosterParent=Win32CreateWindow(WindowExStyles.WS_EX_TOOLWINDOW, "static", "Foster Parent Window", WindowStyles.WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
852 if (FosterParent==IntPtr.Zero) {
853 Win32MessageBox(IntPtr.Zero, "Could not create foster window, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
856 scroll_height = Win32GetSystemMetrics(SystemMetrics.SM_CYHSCROLL);
857 scroll_width = Win32GetSystemMetrics(SystemMetrics.SM_CXVSCROLL);
859 timer_list = new Hashtable ();
860 registered_classes = new Hashtable ();
862 #endregion // Constructor & Destructor
864 #region Private Support Methods
866 private string RegisterWindowClass (int classStyle)
870 lock (registered_classes) {
871 class_name = (string)registered_classes[classStyle];
873 if (class_name != null)
876 class_name = string.Format ("Mono.WinForms.{0}.{1}", System.Threading.Thread.GetDomainID ().ToString (), classStyle);
880 wndClass.style = classStyle;
881 wndClass.lpfnWndProc = wnd_proc;
882 wndClass.cbClsExtra = 0;
883 wndClass.cbWndExtra = 0;
884 wndClass.hbrBackground = (IntPtr)(GetSysColorIndex.COLOR_WINDOW + 1);
885 wndClass.hCursor = Win32LoadCursor (IntPtr.Zero, LoadCursorType.IDC_ARROW);
886 wndClass.hIcon = IntPtr.Zero;
887 wndClass.hInstance = IntPtr.Zero;
888 wndClass.lpszClassName = class_name;
889 wndClass.lpszMenuName = "";
891 bool result = Win32RegisterClass (ref wndClass);
894 Win32MessageBox (IntPtr.Zero, "Could not register the window class, win32 error " + Win32GetLastError ().ToString (), "Oops", 0);
896 registered_classes[classStyle] = class_name;
902 private static bool RetrieveMessage(ref MSG msg) {
905 if (message_queue.Count == 0) {
909 message = (MSG)message_queue.Dequeue();
915 private static bool StoreMessage(ref MSG msg) {
916 MSG message = new MSG();
919 message_queue.Enqueue(message);
924 internal static String AnsiToString(IntPtr ansi_data) {
925 return (string)Marshal.PtrToStringAnsi(ansi_data);
928 internal static String UnicodeToString(IntPtr unicode_data) {
929 return (string)Marshal.PtrToStringUni(unicode_data);
932 internal static Image DIBtoImage(IntPtr dib_data) {
933 BITMAPINFOHEADER bmi;
944 bmi = (BITMAPINFOHEADER)Marshal.PtrToStructure(dib_data, typeof(BITMAPINFOHEADER));
946 ncolors = (int)bmi.biClrUsed;
948 if (bmi.biBitCount < 24) {
949 ncolors = (int)(1 << bmi.biBitCount);
952 //palettesize = ncolors * 4;
954 imagesize = (int)bmi.biSizeImage;
955 if (imagesize == 0) {
956 imagesize = (int)(((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) * bmi.biHeight);
959 switch(bmi.biBitCount) {
960 case 1: { // Monochrome
961 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format1bppIndexed);
962 palette = new int[2];
967 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format4bppIndexed);
968 palette = new int[16];
973 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format8bppIndexed);
974 palette = new int[256];
980 bmp = new Bitmap(bmi.biWidth, bmi.biHeight, PixelFormat.Format32bppArgb);
981 palette = new int[0];
986 throw new Exception("Unexpected number of bits:" + bmi.biBitCount.ToString());
990 if (bmi.biBitCount < 24) {
991 pal = bmp.Palette; // Managed palette
992 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER))), palette, 0, palette.Length);
994 for (int i = 0; i < ncolors; i++) {
995 pal.Entries[i] = Color.FromArgb(palette[i] | unchecked((int)0xff000000));
1000 bytesPerLine = (int)((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3);
1001 bits = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
1003 imagebits = new byte[bytesPerLine];
1005 for (int y = 0; y < bmi.biHeight; y++) {
1006 // Copy from source to managed
1007 Marshal.Copy((IntPtr)((int)dib_data + Marshal.SizeOf(typeof(BITMAPINFOHEADER)) + palette.Length * 4 + bytesPerLine * y), imagebits, 0, bytesPerLine);
1009 // Copy from managed to dest
1010 Marshal.Copy(imagebits, 0, (IntPtr)((int)bits.Scan0 + bits.Stride * (bmi.biHeight - 1 - y)), imagebits.Length);
1013 bmp.UnlockBits(bits);
1018 internal static byte[] ImageToDIB(Image image) {
1023 ms = new MemoryStream();
1024 image.Save(ms, ImageFormat.Bmp);
1025 buffer = ms.GetBuffer();
1027 // Filter out the file header
1028 retbuf = new byte[buffer.Length];
1029 Array.Copy(buffer, 14, retbuf, 0, buffer.Length - 14);
1033 internal static IntPtr DupGlobalMem(IntPtr mem) {
1039 len = Win32GlobalSize(mem);
1040 mem_ptr = Win32GlobalLock(mem);
1042 dup = Win32GlobalAlloc(GAllocFlags.GMEM_MOVEABLE, (int)len);
1043 dup_ptr = Win32GlobalLock(dup);
1045 Win32CopyMemory(dup_ptr, mem_ptr, (int)len);
1047 Win32GlobalUnlock(mem);
1048 Win32GlobalUnlock(dup);
1053 private int GetSystemParametersInfoInt (SPIAction spi)
1057 Win32SystemParametersInfo (spi, 0, ref value, 0);
1062 private bool GetSystemParametersInfoBool (SPIAction spi)
1066 Win32SystemParametersInfo (spi, 0, ref value, 0);
1070 #endregion // Private Support Methods
1072 #region Static Properties
1073 internal override int ActiveWindowTrackingDelay {
1074 get { return GetSystemParametersInfoInt (SPIAction.SPI_GETACTIVEWNDTRKTIMEOUT); }
1077 internal override int CaretWidth {
1079 // Supported on 2k, XP, 2k3 +
1080 if (Environment.OSVersion.Version.Major < 5)
1081 throw new NotSupportedException ();
1083 return GetSystemParametersInfoInt (SPIAction.SPI_GETCARETWIDTH);
1087 internal override int FontSmoothingContrast {
1089 // Supported on XP, 2k3 +
1090 if (Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0))
1091 throw new NotSupportedException ();
1093 return GetSystemParametersInfoInt (SPIAction.SPI_GETFONTSMOOTHINGCONTRAST);
1097 internal override int FontSmoothingType {
1099 // Supported on XP, 2k3 +
1100 if (Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0))
1101 throw new NotSupportedException ();
1103 return GetSystemParametersInfoInt (SPIAction.SPI_GETFONTSMOOTHINGTYPE);
1107 internal override int HorizontalResizeBorderThickness {
1108 get { return Win32GetSystemMetrics (SystemMetrics.SM_CXSIZEFRAME); }
1111 internal override bool IsActiveWindowTrackingEnabled {
1112 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETACTIVEWINDOWTRACKING); }
1115 internal override bool IsComboBoxAnimationEnabled {
1116 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETCOMBOBOXANIMATION); }
1119 internal override bool IsDropShadowEnabled {
1121 // Supported on XP, 2k3 +
1122 if (Environment.OSVersion.Version.Major < 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0))
1123 throw new NotSupportedException ();
1125 return GetSystemParametersInfoBool (SPIAction.SPI_GETDROPSHADOW);
1129 internal override bool IsFontSmoothingEnabled {
1130 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETFONTSMOOTHING); }
1133 internal override bool IsHotTrackingEnabled {
1134 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETHOTTRACKING); }
1137 internal override bool IsIconTitleWrappingEnabled {
1138 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETICONTITLEWRAP); }
1141 internal override bool IsKeyboardPreferred {
1142 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETKEYBOARDPREF); }
1145 internal override bool IsListBoxSmoothScrollingEnabled {
1146 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETLISTBOXSMOOTHSCROLLING); }
1149 internal override bool IsMenuAnimationEnabled {
1150 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETMENUANIMATION); }
1153 internal override bool IsMenuFadeEnabled {
1154 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETMENUFADE); }
1157 internal override bool IsMinimizeRestoreAnimationEnabled {
1159 ANIMATIONINFO ai = new ANIMATIONINFO ();
1160 ai.cbSize = (uint)Marshal.SizeOf (ai);
1162 Win32SystemParametersInfo (SPIAction.SPI_GETANIMATION, 0, ref ai, 0);
1163 return ai.iMinAnimate == 0 ? false : true;
1167 internal override bool IsSelectionFadeEnabled {
1168 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETSELECTIONFADE); }
1171 internal override bool IsSnapToDefaultEnabled {
1172 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETSNAPTODEFBUTTON); }
1175 internal override bool IsTitleBarGradientEnabled {
1176 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETGRADIENTCAPTIONS); }
1179 internal override bool IsToolTipAnimationEnabled {
1180 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETTOOLTIPANIMATION); }
1183 internal override Size MenuBarButtonSize {
1185 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXMENUSIZE),
1186 Win32GetSystemMetrics (SystemMetrics.SM_CYMENUSIZE));
1190 public override Size MenuButtonSize {
1193 Win32GetSystemMetrics (SystemMetrics.SM_CXMENUSIZE),
1194 Win32GetSystemMetrics (SystemMetrics.SM_CYMENUSIZE));
1198 internal override int MenuShowDelay {
1199 get { return GetSystemParametersInfoInt (SPIAction.SPI_GETMENUSHOWDELAY); }
1202 internal override int MouseSpeed {
1203 get { return GetSystemParametersInfoInt (SPIAction.SPI_GETMOUSESPEED); }
1206 internal override LeftRightAlignment PopupMenuAlignment {
1207 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETMENUDROPALIGNMENT) == true ? LeftRightAlignment.Left : LeftRightAlignment.Right; }
1210 internal override PowerStatus PowerStatus {
1212 SYSTEMPOWERSTATUS p = new SYSTEMPOWERSTATUS ();
1214 Win32GetSystemPowerStatus (p);
1216 PowerStatus ps = new PowerStatus ((BatteryChargeStatus)p._BatteryFlag, p._BatteryFullLifeTime, (float)p._BatteryLifePercent / 255f, p._BatteryLifeTime, (PowerLineStatus)p._ACLineStatus);
1222 internal override int SizingBorderWidth {
1223 get { return Win32GetSystemMetrics (SystemMetrics.SM_CXSIZEFRAME); }
1226 internal override Size SmallCaptionButtonSize {
1228 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXSMSIZE),
1229 Win32GetSystemMetrics (SystemMetrics.SM_CYSMSIZE));
1233 internal override bool UIEffectsEnabled {
1234 get { return GetSystemParametersInfoBool (SPIAction.SPI_GETUIEFFECTS); }
1237 internal override int VerticalResizeBorderThickness {
1238 get { return Win32GetSystemMetrics (SystemMetrics.SM_CYSIZEFRAME); }
1241 internal override void RaiseIdle (EventArgs e)
1243 int id = Thread.CurrentThread.ManagedThreadId;
1244 if (Idle_Threads != null &&
1245 Idle_Threads.ContainsKey (id) &&
1246 Idle_Threads [id] != null)
1248 Idle_Threads [id] (this, e);
1252 internal override Keys ModifierKeys {
1257 key_state = Keys.None;
1259 state = Win32GetKeyState(VirtualKeys.VK_SHIFT);
1260 if ((state & 0x8000) != 0) {
1261 key_state |= Keys.Shift;
1263 state = Win32GetKeyState(VirtualKeys.VK_CONTROL);
1264 if ((state & 0x8000) != 0) {
1265 key_state |= Keys.Control;
1268 state = Win32GetKeyState(VirtualKeys.VK_MENU);
1269 if ((state & 0x8000) != 0) {
1270 key_state |= Keys.Alt;
1277 internal override MouseButtons MouseButtons {
1283 internal override Point MousePosition {
1285 return mouse_position;
1289 internal override Size MouseHoverSize {
1294 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref width, 0);
1295 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERWIDTH, 0, ref height, 0);
1296 return new Size(width, height);
1300 internal override int MouseHoverTime {
1304 Win32SystemParametersInfo(SPIAction.SPI_GETMOUSEHOVERTIME, 0, ref time, 0);
1309 internal override int MouseWheelScrollDelta {
1312 Win32SystemParametersInfo(SPIAction.SPI_GETWHEELSCROLLLINES, 0, ref delta, 0);
1317 internal override int HorizontalScrollBarHeight {
1319 return scroll_height;
1323 internal override bool UserClipWontExposeParent {
1330 internal override int VerticalScrollBarWidth {
1332 return scroll_width;
1336 internal override int MenuHeight {
1338 return Win32GetSystemMetrics(SystemMetrics.SM_CYMENU);
1342 internal override Size Border3DSize {
1344 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXEDGE),
1345 Win32GetSystemMetrics (SystemMetrics.SM_CYEDGE));
1349 internal override Size BorderSize {
1351 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXBORDER),
1352 Win32GetSystemMetrics (SystemMetrics.SM_CYBORDER));
1356 internal override bool DropTarget {
1363 //throw new NotImplementedException("Need to figure out D'n'D for Win32");
1368 internal override Size CaptionButtonSize {
1370 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXSIZE),
1371 Win32GetSystemMetrics (SystemMetrics.SM_CYSIZE));
1375 internal override int CaptionHeight {
1377 return Win32GetSystemMetrics(SystemMetrics.SM_CYCAPTION);
1381 internal override Size CursorSize {
1383 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CYCURSOR));
1387 internal override bool DragFullWindows {
1390 Win32SystemParametersInfo (SPIAction.SPI_GETDRAGFULLWINDOWS, 0, ref full, 0);
1395 internal override Size DragSize {
1397 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXDRAG), Win32GetSystemMetrics(SystemMetrics.SM_CYDRAG));
1401 internal override Size DoubleClickSize {
1403 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXDOUBLECLK),
1404 Win32GetSystemMetrics (SystemMetrics.SM_CYDOUBLECLK));
1408 internal override int DoubleClickTime {
1410 return Win32GetDoubleClickTime ();
1414 internal override Size FixedFrameBorderSize {
1416 return new Size (Win32GetSystemMetrics (SystemMetrics.SM_CXFIXEDFRAME),
1417 Win32GetSystemMetrics (SystemMetrics.SM_CYFIXEDFRAME));
1421 internal override Size FrameBorderSize {
1423 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXFRAME), Win32GetSystemMetrics(SystemMetrics.SM_CYFRAME));
1427 internal override Size IconSize {
1429 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXICON), Win32GetSystemMetrics(SystemMetrics.SM_CYICON));
1433 internal override Size MaxWindowTrackSize {
1435 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMAXTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMAXTRACK));
1439 internal override bool MenuAccessKeysUnderlined {
1442 Win32SystemParametersInfo (SPIAction.SPI_GETKEYBOARDCUES, 0, ref underlined, 0);
1443 return (underlined != 0);
1447 internal override Size MinimizedWindowSize {
1449 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINIMIZED), Win32GetSystemMetrics(SystemMetrics.SM_CYMINIMIZED));
1453 internal override Size MinimizedWindowSpacingSize {
1455 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINSPACING), Win32GetSystemMetrics(SystemMetrics.SM_CYMINSPACING));
1459 internal override Size MinimumWindowSize {
1461 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMIN), Win32GetSystemMetrics(SystemMetrics.SM_CYMIN));
1465 internal override Size MinWindowTrackSize {
1467 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXMINTRACK), Win32GetSystemMetrics(SystemMetrics.SM_CYMINTRACK));
1471 internal override Size SmallIconSize {
1473 return new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXSMICON), Win32GetSystemMetrics(SystemMetrics.SM_CYSMICON));
1477 internal override int MouseButtonCount {
1479 return Win32GetSystemMetrics(SystemMetrics.SM_CMOUSEBUTTONS);
1483 internal override bool MouseButtonsSwapped {
1485 return Win32GetSystemMetrics(SystemMetrics.SM_SWAPBUTTON) != 0;
1489 internal override bool MouseWheelPresent {
1491 return Win32GetSystemMetrics(SystemMetrics.SM_MOUSEWHEELPRESENT) != 0;
1495 internal override Rectangle VirtualScreen {
1497 return new Rectangle( Win32GetSystemMetrics(SystemMetrics.SM_XVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_YVIRTUALSCREEN),
1498 Win32GetSystemMetrics(SystemMetrics.SM_CXVIRTUALSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYVIRTUALSCREEN));
1502 internal override Rectangle WorkingArea {
1507 Win32SystemParametersInfo(SPIAction.SPI_GETWORKAREA, 0, ref rect, 0);
1508 return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
1509 //return new Rectangle(0, 0, Win32GetSystemMetrics(SystemMetrics.SM.SM_CXSCREEN), Win32GetSystemMetrics(SystemMetrics.SM_CYSCREEN));
1513 internal override bool ThemesEnabled {
1515 return XplatUIWin32.themes_enabled;
1519 internal override bool RequiresPositiveClientAreaSize {
1525 public override int ToolWindowCaptionHeight {
1527 return Win32GetSystemMetrics (SystemMetrics.SM_CYSMCAPTION);
1531 public override Size ToolWindowCaptionButtonSize {
1534 Win32GetSystemMetrics (SystemMetrics.SM_CXSMSIZE),
1535 Win32GetSystemMetrics (SystemMetrics.SM_CYSMSIZE));
1538 #endregion // Static Properties
1540 #region Singleton Specific Code
1541 public static XplatUIWin32 GetInstance() {
1542 if (instance==null) {
1543 instance=new XplatUIWin32();
1549 public int Reference {
1556 #region Public Static Methods
1557 internal override IntPtr InitializeDriver() {
1561 internal override void ShutdownDriver(IntPtr token) {
1562 Console.WriteLine("XplatUIWin32 ShutdownDriver called");
1566 internal void Version() {
1567 Console.WriteLine("Xplat version $revision: $");
1570 string GetSoundAlias (AlertType alert)
1573 case AlertType.Error:
1574 return "SystemHand";
1575 case AlertType.Question:
1576 return "SystemQuestion";
1577 case AlertType.Warning:
1578 return "SystemExclamation";
1579 case AlertType.Information:
1580 return "SystemAsterisk";
1582 return "SystemDefault";
1586 internal override void AudibleAlert(AlertType alert) {
1587 Win32PlaySound(GetSoundAlias (alert), IntPtr.Zero, SndFlags.SND_ALIAS_ID | SndFlags.SND_ASYNC | SndFlags.SND_NOSTOP | SndFlags.SND_NOWAIT);
1590 internal override void BeginMoveResize (IntPtr handle) {
1593 internal override void GetDisplaySize(out Size size) {
1596 Win32GetWindowRect(Win32GetDesktopWindow(), out rect);
1598 size = new Size(rect.right - rect.left, rect.bottom - rect.top);
1601 internal override void EnableThemes() {
1602 themes_enabled=true;
1605 internal override IntPtr CreateWindow(CreateParams cp) {
1606 IntPtr WindowHandle;
1607 IntPtr ParentHandle;
1612 ParentHandle=cp.Parent;
1614 if ((ParentHandle==IntPtr.Zero) && (cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
1615 // We need to use our foster parent window until this poor child gets it's parent assigned
1616 ParentHandle = FosterParent;
1619 if ( ((cp.Style & (int)(WindowStyles.WS_CHILD | WindowStyles.WS_POPUP))==0) && ((cp.ExStyle & (int)WindowExStyles.WS_EX_APPWINDOW) == 0)) {
1620 // If we want to be hidden from the taskbar we need to be 'owned' by
1621 // something not on the taskbar. FosterParent is just that
1622 ParentHandle = FosterParent;
1626 if (cp.HasWindowManager) {
1627 location = Hwnd.GetNextStackedFormLocation (cp, Hwnd.ObjectFromHandle (cp.Parent));
1629 location = new Point (cp.X, cp.Y);
1632 string class_name = RegisterWindowClass (cp.ClassStyle);
1633 HwndCreating = hwnd;
1635 // We cannot actually send the WS_EX_MDICHILD flag to Windows because we
1636 // are faking MDI, not uses Windows' version.
1637 if ((cp.WindowExStyle & WindowExStyles.WS_EX_MDICHILD) == WindowExStyles.WS_EX_MDICHILD)
1638 cp.WindowExStyle ^= WindowExStyles.WS_EX_MDICHILD;
1640 WindowHandle = Win32CreateWindow (cp.WindowExStyle, class_name, cp.Caption, cp.WindowStyle, location.X, location.Y, cp.Width, cp.Height, ParentHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
1642 HwndCreating = null;
1644 if (WindowHandle==IntPtr.Zero) {
1645 int error = Marshal.GetLastWin32Error ();
1647 Win32MessageBox(IntPtr.Zero, "Error : " + error.ToString(), "Failed to create window, class '"+cp.ClassName+"'", 0);
1650 hwnd.ClientWindow = WindowHandle;
1652 Win32SetWindowLong(WindowHandle, WindowLong.GWL_USERDATA, (uint)ThemeEngine.Current.DefaultControlBackColor.ToArgb());
1654 return WindowHandle;
1657 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1658 CreateParams create_params = new CreateParams();
1660 create_params.Caption = "";
1661 create_params.X = X;
1662 create_params.Y = Y;
1663 create_params.Width = Width;
1664 create_params.Height = Height;
1666 create_params.ClassName=XplatUI.GetDefaultClassName (GetType ());
1667 create_params.ClassStyle = 0;
1668 create_params.ExStyle=0;
1669 create_params.Parent=IntPtr.Zero;
1670 create_params.Param=0;
1672 return CreateWindow(create_params);
1675 internal override void DestroyWindow(IntPtr handle) {
1678 hwnd = Hwnd.ObjectFromHandle(handle);
1679 Win32DestroyWindow(handle);
1684 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1685 // We do nothing, Form has to handle WM_GETMINMAXINFO
1689 internal override FormWindowState GetWindowState(IntPtr handle) {
1692 style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
1693 if ((style & (uint)WindowStyles.WS_MAXIMIZE) != 0) {
1694 return FormWindowState.Maximized;
1695 } else if ((style & (uint)WindowStyles.WS_MINIMIZE) != 0) {
1696 return FormWindowState.Minimized;
1698 return FormWindowState.Normal;
1701 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1703 case FormWindowState.Normal: {
1704 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_RESTORE);
1708 case FormWindowState.Minimized: {
1709 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MINIMIZE);
1713 case FormWindowState.Maximized: {
1714 Win32ShowWindow(hwnd, WindowPlacementFlags.SW_MAXIMIZE);
1720 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1722 Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
1723 Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
1726 // Certain window data is cached, so changes you make using SetWindowLong
1727 // will not take effect until you call the SetWindowPos function. Specifically,
1728 // if you change any of the frame styles, you must call SetWindowPos with
1729 // the SWP_FRAMECHANGED flag for the cache to be updated properly.
1730 if (cp.control is Form)
1731 XplatUI.RequestNCRecalc (handle);
1734 internal override double GetWindowTransparency(IntPtr handle)
1736 LayeredWindowAttributes lwa;
1740 if (0 == Win32GetLayeredWindowAttributes (handle, out clrRef, out alpha, out lwa))
1743 return ((double)alpha) / 255.0;
1746 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1747 LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
1748 byte opacity = (byte)(transparency*255);
1749 COLORREF clrRef = new COLORREF();
1750 if (key != Color.Empty) {
1754 lwa = (LayeredWindowAttributes)( (int)lwa | (int)LayeredWindowAttributes.LWA_COLORKEY );
1759 Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
1762 TransparencySupport support;
1763 bool queried_transparency_support;
1764 internal override TransparencySupport SupportsTransparency() {
1765 if (queried_transparency_support)
1769 support = TransparencySupport.None;
1773 Win32SetLayeredWindowAttributes (IntPtr.Zero, new COLORREF (), 255, LayeredWindowAttributes.LWA_ALPHA);
1775 catch (EntryPointNotFoundException) { flag = false; }
1776 catch { /* swallow everything else */ }
1778 if (flag) support |= TransparencySupport.Set;
1782 LayeredWindowAttributes lwa;
1786 Win32GetLayeredWindowAttributes (IntPtr.Zero, out clrRef, out alpha, out lwa);
1788 catch (EntryPointNotFoundException) { flag = false; }
1789 catch { /* swallow everything else */ }
1791 if (flag) support |= TransparencySupport.Get;
1793 queried_transparency_support = true;
1797 internal override void UpdateWindow(IntPtr handle) {
1798 Win32UpdateWindow(handle);
1801 internal override PaintEventArgs PaintEventStart(ref Message msg, IntPtr handle, bool client) {
1804 PaintEventArgs paint_event;
1806 Rectangle clip_rect;
1809 clip_rect = new Rectangle();
1811 ps = new PAINTSTRUCT();
1813 hwnd = Hwnd.ObjectFromHandle(msg.HWnd);
1816 if (Win32GetUpdateRect(msg.HWnd, ref rect, false)) {
1817 if (handle != msg.HWnd) {
1818 // We need to validate the window where the paint message
1819 // was generated, otherwise we'll never stop getting paint
1821 Win32GetClientRect (msg.HWnd, out rect);
1822 Win32ValidateRect (msg.HWnd, ref rect);
1823 hdc = Win32GetDC (handle);
1825 hdc = Win32BeginPaint (handle, ref ps);
1829 hdc = Win32GetDC(handle);
1831 clip_rect = rect.ToRectangle ();
1833 hdc = Win32GetWindowDC (handle);
1835 // HACK this in for now
1836 Win32GetWindowRect (handle, out rect);
1837 clip_rect = new Rectangle (0, 0, rect.Width, rect.Height);
1840 // If we called BeginPaint, store the PAINTSTRUCT,
1841 // otherwise store hdc, so that PaintEventEnd can know
1842 // whether to call EndPaint or ReleaseDC.
1843 if (ps.hdc != IntPtr.Zero) {
1844 hwnd.drawing_stack.Push (ps);
1846 hwnd.drawing_stack.Push (hdc);
1849 Graphics dc = Graphics.FromHdc(hdc);
1850 hwnd.drawing_stack.Push (dc);
1852 paint_event = new PaintEventArgs(dc, clip_rect);
1857 internal override void PaintEventEnd(ref Message m, IntPtr handle, bool client) {
1860 hwnd = Hwnd.ObjectFromHandle(m.HWnd);
1862 Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1865 object o = hwnd.drawing_stack.Pop();
1867 IntPtr hdc = (IntPtr) o;
1868 Win32ReleaseDC (handle, hdc);
1869 } else if (o is PAINTSTRUCT) {
1870 PAINTSTRUCT ps = (PAINTSTRUCT) o;
1871 Win32EndPaint (handle, ref ps);
1876 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1877 Win32MoveWindow(handle, x, y, width, height, true);
1881 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) {
1886 Win32GetWindowRect(handle, out rect);
1887 width = rect.right - rect.left;
1888 height = rect.bottom - rect.top;
1893 parent = Win32GetAncestor (handle, AncestorType.GA_PARENT);
1894 if (parent != IntPtr.Zero && parent != Win32GetDesktopWindow ())
1895 Win32ScreenToClient(parent, ref pt);
1900 Win32GetClientRect(handle, out rect);
1901 client_width = rect.right - rect.left;
1902 client_height = rect.bottom - rect.top;
1906 internal override void Activate(IntPtr handle) {
1907 Win32SetActiveWindow(handle);
1908 // delayed timer enabled
1910 foreach (Timer t in timer_list.Values) {
1911 if (t.Enabled && t.window == IntPtr.Zero) {
1913 int id = t.GetHashCode ();
1914 Win32SetTimer(handle, id, (uint)t.Interval, IntPtr.Zero);
1920 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
1925 rect.right=rc.Right;
1926 rect.bottom=rc.Bottom;
1927 Win32InvalidateRect(handle, ref rect, clear);
1931 internal override void InvalidateNC (IntPtr handle)
1933 // found this gem at
1934 // http://www.dotnet247.com/247reference/msgs/58/292037.aspx
1935 Win32SetWindowPos(handle, IntPtr.Zero,
1937 SetWindowPosFlags.SWP_NOMOVE |
1938 SetWindowPosFlags.SWP_NOSIZE |
1939 SetWindowPosFlags.SWP_NOZORDER |
1940 SetWindowPosFlags.SWP_NOACTIVATE |
1941 SetWindowPosFlags.SWP_DRAWFRAME);
1944 private IntPtr InternalWndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
1946 if (HwndCreating != null && HwndCreating.ClientWindow == IntPtr.Zero)
1947 HwndCreating.ClientWindow = hWnd;
1948 return NativeWindow.WndProc (hWnd, msg, wParam, lParam);
1951 internal override IntPtr DefWndProc(ref Message msg) {
1952 msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
1956 internal override void HandleException(Exception e) {
1957 StackTrace st = new StackTrace(e);
1958 Win32MessageBox(IntPtr.Zero, e.Message+st.ToString(), "Exception", 0);
1959 Console.WriteLine("{0}{1}", e.Message, st.ToString());
1962 internal override void DoEvents() {
1963 MSG msg = new MSG();
1965 while (GetMessage(ref msg, IntPtr.Zero, 0, 0, false)) {
1966 Message m = Message.Create (msg.hwnd, (int)msg.message, msg.wParam, msg.lParam);
1968 if (Application.FilterMessage (ref m))
1971 XplatUI.TranslateMessage(ref msg);
1972 XplatUI.DispatchMessage(ref msg);
1976 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1977 return Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, flags);
1980 internal override void PostQuitMessage(int exitCode) {
1981 Win32PostQuitMessage(exitCode);
1984 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
1986 if (wm_nc_registered == null)
1987 wm_nc_registered = new Hashtable ();
1989 TMEFlags flags = TMEFlags.TME_NONCLIENT;
1991 flags |= TMEFlags.TME_HOVER;
1993 flags |= TMEFlags.TME_LEAVE;
1995 if (flags == TMEFlags.TME_NONCLIENT) {
1996 if (wm_nc_registered.Contains (hwnd)) {
1997 wm_nc_registered.Remove (hwnd);
2000 if (!wm_nc_registered.Contains (hwnd)) {
2001 wm_nc_registered.Add (hwnd, flags);
2003 wm_nc_registered [hwnd] = flags;
2008 internal override void RequestNCRecalc(IntPtr handle) {
2009 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);
2012 internal override void ResetMouseHover(IntPtr handle) {
2013 TRACKMOUSEEVENT tme;
2015 tme = new TRACKMOUSEEVENT();
2016 tme.size = Marshal.SizeOf(tme);
2018 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
2019 Win32TrackMouseEvent(ref tme);
2023 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
2024 return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax, true);
2027 private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
2029 bool CheckIdle = blocking;
2034 if (RetrieveMessage(ref msg)) {
2038 if (blocking && !CheckIdle) {
2039 result = Win32GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
2041 result = Win32PeekMessage(ref msg, hWnd, wFilterMin, wFilterMax, (uint)PeekMessageFlags.PM_REMOVE);
2046 goto ProcessNextMessage;
2053 // We need to fake WM_MOUSE_ENTER
2054 switch (msg.message) {
2055 case Msg.WM_LBUTTONDOWN: {
2056 mouse_state |= MouseButtons.Left;
2060 case Msg.WM_MBUTTONDOWN: {
2061 mouse_state |= MouseButtons.Middle;
2065 case Msg.WM_RBUTTONDOWN: {
2066 mouse_state |= MouseButtons.Right;
2070 case Msg.WM_LBUTTONUP: {
2071 mouse_state &= ~MouseButtons.Left;
2075 case Msg.WM_MBUTTONUP: {
2076 mouse_state &= ~MouseButtons.Middle;
2080 case Msg.WM_RBUTTONUP: {
2081 mouse_state &= ~MouseButtons.Right;
2085 case Msg.WM_ASYNC_MESSAGE: {
2086 XplatUIDriverSupport.ExecuteClientMessage((GCHandle)msg.lParam);
2090 case Msg.WM_MOUSEMOVE: {
2091 if (msg.hwnd != prev_mouse_hwnd) {
2092 TRACKMOUSEEVENT tme;
2094 mouse_state = Control.FromParamToMouseButtons ((int)msg.lParam.ToInt32());
2096 // The current message will be sent out next time around
2097 StoreMessage(ref msg);
2099 // This is the message we want to send at this point
2100 msg.message = Msg.WM_MOUSE_ENTER;
2102 prev_mouse_hwnd = msg.hwnd;
2104 tme = new TRACKMOUSEEVENT();
2105 tme.size = Marshal.SizeOf(tme);
2106 tme.hWnd = msg.hwnd;
2107 tme.dwFlags = TMEFlags.TME_LEAVE | TMEFlags.TME_HOVER;
2108 Win32TrackMouseEvent(ref tme);
2114 case Msg.WM_NCMOUSEMOVE: {
2115 if (wm_nc_registered == null || !wm_nc_registered.Contains (msg.hwnd))
2118 mouse_state = Control.FromParamToMouseButtons ((int)msg.lParam.ToInt32 ());
2120 TRACKMOUSEEVENT tme;
2122 tme = new TRACKMOUSEEVENT ();
2123 tme.size = Marshal.SizeOf(tme);
2124 tme.hWnd = msg.hwnd;
2125 tme.dwFlags = (TMEFlags)wm_nc_registered[msg.hwnd];
2126 Win32TrackMouseEvent (ref tme);
2130 case Msg.WM_DROPFILES: {
2131 return Win32DnD.HandleWMDropFiles(ref msg);
2134 case Msg.WM_MOUSELEAVE: {
2135 prev_mouse_hwnd = IntPtr.Zero;
2139 case Msg.WM_TIMER: {
2140 Timer timer=(Timer)timer_list[(int)msg.wParam];
2142 if (timer != null) {
2152 internal override bool TranslateMessage(ref MSG msg) {
2153 return Win32TranslateMessage(ref msg);
2156 internal override IntPtr DispatchMessage(ref MSG msg) {
2157 return Win32DispatchMessage(ref msg);
2160 internal override bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom) {
2162 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOP, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2164 } else if (!Bottom) {
2165 Win32SetWindowPos(hWnd, AfterhWnd, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2167 Win32SetWindowPos(hWnd, (IntPtr)SetWindowPosZOrder.HWND_BOTTOM, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2173 internal override bool SetTopmost(IntPtr hWnd, bool Enabled) {
2175 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
2178 Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
2183 internal override bool SetOwner(IntPtr hWnd, IntPtr hWndOwner) {
2184 Win32SetWindowLong(hWnd, WindowLong.GWL_HWNDPARENT, (uint) hWndOwner);
2188 internal override bool Text(IntPtr handle, string text) {
2189 Win32SetWindowText(handle, text);
2193 internal override bool GetText(IntPtr handle, out string text) {
2196 sb = new StringBuilder(256);
2197 Win32GetWindowText(handle, sb, sb.Capacity);
2198 text = sb.ToString();
2202 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
2205 Control c = Control.FromHandle (handle);
2209 f = (Form)Control.FromHandle (handle);
2210 WindowPlacementFlags flags = WindowPlacementFlags.SW_SHOWNORMAL;
2211 switch (f.WindowState) {
2212 case FormWindowState.Normal: flags = WindowPlacementFlags.SW_SHOWNORMAL; break;
2213 case FormWindowState.Minimized: flags = WindowPlacementFlags.SW_MINIMIZE; break;
2214 case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
2217 if (!f.ActivateOnShow)
2218 flags = WindowPlacementFlags.SW_SHOWNOACTIVATE;
2220 Win32ShowWindow (handle, flags);
2223 if (c.ActivateOnShow)
2224 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
2226 Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
2230 Win32ShowWindow (handle, WindowPlacementFlags.SW_HIDE);
2235 internal override bool IsEnabled(IntPtr handle) {
2236 return IsWindowEnabled (handle);
2239 internal override bool IsKeyLocked (VirtualKeys key)
2241 return (Win32GetKeyState (key) & 1) == 1;
2244 internal override bool IsVisible(IntPtr handle) {
2245 return IsWindowVisible (handle);
2248 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
2249 Control c = Control.FromHandle (handle);
2250 if (parent == IntPtr.Zero) {
2252 Win32ShowWindow(handle, WindowPlacementFlags.SW_HIDE);
2256 SetVisible (handle, c.is_visible, true);
2259 // The Win32SetParent is lame, it can very well move the window
2260 // ref: http://groups.google.com/group/microsoft.public.vb.winapi/browse_thread/thread/1b82ccc54231ecee/afa82835bfc0422a%23afa82835bfc0422a
2261 // Here we save the position before changing the parent, and if it has changed afterwards restore it.
2262 // Another possibility would be to intercept WM_WINDOWPOSCHANGING and restore the coords there, but this would require plumbing in weird places
2263 // (either inside Control or add handling to InternalWndProc)
2264 // We also need to remove WS_CHILD if making the window parent-less, and add it if we're parenting it.
2267 WindowStyles style, new_style;
2269 Win32GetWindowRect (handle, out rect);
2270 style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
2272 if (parent == IntPtr.Zero) {
2273 new_style = style & ~WindowStyles.WS_CHILD;
2274 result = Win32SetParent (handle, FosterParent);
2276 new_style = style | WindowStyles.WS_CHILD;
2277 result = Win32SetParent (handle, parent);
2279 if (style != new_style && c is Form) {
2280 Win32SetWindowLong (handle, WindowLong.GWL_STYLE, (uint) new_style);
2282 Win32GetWindowRect (handle, out rect2);
2283 if (rect.top != rect2.top && rect.left != rect2.left && c is Form) {
2284 Win32SetWindowPos (handle, IntPtr.Zero, rect.top, rect.left, rect.Width, rect.Height, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOREDRAW | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOENDSCHANGING | SetWindowPosFlags.SWP_NOACTIVATE);
2289 // If we ever start using this, we should probably replace FosterParent with IntPtr.Zero
2290 internal override IntPtr GetParent(IntPtr handle) {
2291 return Win32GetParent(handle);
2294 // This is a nop on win32 and x11
2295 internal override IntPtr GetPreviousWindow(IntPtr handle) {
2299 internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
2301 Win32SetCapture(hWnd);
2303 if (ConfineToHwnd != IntPtr.Zero) {
2305 Win32GetWindowRect (ConfineToHwnd, out window_rect);
2306 Win32GetClipCursor (out clipped_cursor_rect);
2307 Win32ClipCursor (ref window_rect);
2311 internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
2313 GrabConfined = grab_confined;
2314 GrabArea = grab_area;
2317 internal override void UngrabWindow(IntPtr hWnd) {
2318 if (!(clipped_cursor_rect.top == 0 && clipped_cursor_rect.bottom == 0 && clipped_cursor_rect.left == 0 && clipped_cursor_rect.right == 0)) {
2319 Win32ClipCursor (ref clipped_cursor_rect);
2320 clipped_cursor_rect = new RECT ();
2323 Win32ReleaseCapture();
2324 grab_hwnd = IntPtr.Zero;
2327 internal override bool CalculateWindowRect(ref Rectangle ClientRect, CreateParams cp, Menu menu, out Rectangle WindowRect) {
2330 rect.left=ClientRect.Left;
2331 rect.top=ClientRect.Top;
2332 rect.right=ClientRect.Right;
2333 rect.bottom=ClientRect.Bottom;
2335 if (!Win32AdjustWindowRectEx(ref rect, cp.Style, menu != null, cp.ExStyle)) {
2336 WindowRect = new Rectangle(ClientRect.Left, ClientRect.Top, ClientRect.Width, ClientRect.Height);
2340 WindowRect = new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
2344 internal override void SetCursor(IntPtr window, IntPtr cursor) {
2345 Win32SetCursor(cursor);
2349 internal override void ShowCursor(bool show) {
2350 Win32ShowCursor(show);
2353 internal override void OverrideCursor(IntPtr cursor) {
2354 Win32SetCursor(cursor);
2357 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
2359 Bitmap cursor_bitmap;
2367 // Win32 only allows creation cursors of a certain size
2368 if ((bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)) || (bitmap.Width != Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR))) {
2369 cursor_bitmap = new Bitmap(bitmap, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
2370 cursor_mask = new Bitmap(mask, new Size(Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR), Win32GetSystemMetrics(SystemMetrics.SM_CXCURSOR)));
2372 cursor_bitmap = bitmap;
2376 width = cursor_bitmap.Width;
2377 height = cursor_bitmap.Height;
2379 cursor_bits = new Byte[(width / 8) * height];
2380 mask_bits = new Byte[(width / 8) * height];
2382 for (int y = 0; y < height; y++) {
2383 for (int x = 0; x < width; x++) {
2384 pixel = cursor_bitmap.GetPixel(x, y);
2386 if (pixel == cursor_pixel) {
2387 cursor_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
2390 pixel = cursor_mask.GetPixel(x, y);
2392 if (pixel == mask_pixel) {
2393 mask_bits[y * width / 8 + x / 8] |= (byte)(0x80 >> (x % 8));
2398 cursor = Win32CreateCursor(IntPtr.Zero, xHotSpot, yHotSpot, width, height, mask_bits, cursor_bits);
2403 internal override Bitmap DefineStdCursorBitmap (StdCursor id)
2405 // We load the cursor, create a bitmap, draw the cursor onto the bitmap and return the bitmap.
2406 IntPtr cursor = DefineStdCursor (id);
2407 // Windows only have one possible cursor size!
2408 int width = Win32GetSystemMetrics (SystemMetrics.SM_CXCURSOR);
2409 int height = Win32GetSystemMetrics (SystemMetrics.SM_CYCURSOR);
2410 Bitmap bmp = new Bitmap (width, height);
2411 Graphics gc = Graphics.FromImage (bmp);
2412 IntPtr hdc = gc.GetHdc ();
2413 Win32DrawIcon (hdc, 0, 0, cursor);
2414 gc.ReleaseHdc (hdc);
2419 [MonoTODO("Define the missing cursors")]
2420 internal override IntPtr DefineStdCursor(StdCursor id) {
2422 case StdCursor.AppStarting: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_APPSTARTING);
2423 case StdCursor.Arrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2424 case StdCursor.Cross: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_CROSS);
2425 case StdCursor.Default: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
2426 case StdCursor.Hand: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HAND);
2427 case StdCursor.Help: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_HELP);
2428 case StdCursor.HSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2429 case StdCursor.IBeam: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_IBEAM);
2430 case StdCursor.No: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_NO);
2431 case StdCursor.NoMove2D: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2432 case StdCursor.NoMoveHoriz: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2433 case StdCursor.NoMoveVert: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2434 case StdCursor.PanEast: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2435 case StdCursor.PanNE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2436 case StdCursor.PanNorth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2437 case StdCursor.PanNW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2438 case StdCursor.PanSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2439 case StdCursor.PanSouth: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2440 case StdCursor.PanSW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2441 case StdCursor.PanWest: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2442 case StdCursor.SizeAll: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEALL);
2443 case StdCursor.SizeNESW: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENESW);
2444 case StdCursor.SizeNS: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENS);
2445 case StdCursor.SizeNWSE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZENWSE);
2446 case StdCursor.SizeWE: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_SIZEWE);
2447 case StdCursor.UpArrow: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_UPARROW);
2448 case StdCursor.VSplit: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW); // FIXME
2449 case StdCursor.WaitCursor: return Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_WAIT);
2451 throw new NotImplementedException ();
2454 internal override void DestroyCursor(IntPtr cursor) {
2455 if ((cursor.ToInt32() < (int)LoadCursorType.First) || (cursor.ToInt32() > (int)LoadCursorType.Last)) {
2456 Win32DestroyCursor(cursor);
2461 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
2462 ICONINFO ii = new ICONINFO ();
2464 if (!Win32GetIconInfo (cursor, out ii))
2465 throw new Win32Exception ();
2469 hotspot_x = ii.xHotspot;
2470 hotspot_y = ii.yHotspot;
2473 internal override void SetCursorPos(IntPtr handle, int x, int y) {
2474 Win32SetCursorPos(x, y);
2477 internal override Region GetClipRegion(IntPtr hwnd) {
2480 region = new Region();
2482 Win32GetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)));
2487 internal override void SetClipRegion(IntPtr hwnd, Region region) {
2489 Win32SetWindowRgn (hwnd, IntPtr.Zero, true);
2491 Win32SetWindowRgn(hwnd, region.GetHrgn(Graphics.FromHwnd(hwnd)), true);
2494 internal override void EnableWindow(IntPtr handle, bool Enable) {
2495 Win32EnableWindow(handle, Enable);
2498 internal override void EndLoop(System.Threading.Thread thread) {
2502 internal override object StartLoop(System.Threading.Thread thread) {
2506 internal override void SetModal(IntPtr handle, bool Modal) {
2507 // we do nothing on Win32
2510 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
2513 Win32GetCursorPos(out pt);
2515 if (handle!=IntPtr.Zero) {
2516 Win32ScreenToClient(handle, ref pt);
2523 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
2525 POINT pnt = new POINT();
2529 Win32ScreenToClient (handle, ref pnt);
2535 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {
2536 POINT pnt = new POINT();
2541 Win32ClientToScreen(handle, ref pnt);
2547 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
2550 Win32GetWindowRect(handle, out rect);
2551 x -= rect.left + SystemInformation.FrameBorderSize.Width;
2552 y -= rect.top + SystemInformation.FrameBorderSize.Height;
2554 WindowStyles style = (WindowStyles) Win32GetWindowLong (handle, WindowLong.GWL_STYLE);
2555 if (CreateParams.IsSet (style, WindowStyles.WS_CAPTION)) {
2556 y -= ThemeEngine.Current.CaptionHeight;
2560 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {
2563 Win32GetWindowRect(handle, out rect);
2564 x += rect.left + SystemInformation.FrameBorderSize.Width;
2565 y += rect.top + SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight;
2569 internal override void SendAsyncMethod (AsyncMethodData method)
2571 Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
2574 internal override void SetTimer (Timer timer)
2578 index = timer.GetHashCode();
2581 timer_list[index]=timer;
2584 if (Win32SetTimer(FosterParent, index, (uint)timer.Interval, IntPtr.Zero) != IntPtr.Zero)
2585 timer.window = FosterParent;
2587 timer.window = IntPtr.Zero;
2590 internal override void KillTimer (Timer timer)
2594 index = timer.GetHashCode();
2596 Win32KillTimer(timer.window, index);
2599 timer_list.Remove(index);
2603 internal override void CreateCaret(IntPtr hwnd, int width, int height) {
2604 Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
2605 caret_visible = false;
2608 internal override void DestroyCaret(IntPtr hwnd) {
2609 Win32DestroyCaret();
2612 internal override void SetCaretPos(IntPtr hwnd, int x, int y) {
2613 Win32SetCaretPos(x, y);
2616 internal override void CaretVisible(IntPtr hwnd, bool visible) {
2618 if (!caret_visible) {
2619 Win32ShowCaret(hwnd);
2620 caret_visible = true;
2623 if (caret_visible) {
2624 Win32HideCaret(hwnd);
2625 caret_visible = false;
2630 internal override IntPtr GetFocus() {
2631 return Win32GetFocus();
2634 internal override void SetFocus(IntPtr hwnd) {
2635 Win32SetFocus(hwnd);
2638 internal override IntPtr GetActive() {
2639 return Win32GetActiveWindow();
2642 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
2647 tm = new TEXTMETRIC();
2649 dc = Win32GetDC (IntPtr.Zero);
2650 prevobj = Win32SelectObject (dc, font.ToHfont ());
2652 if (Win32GetTextMetrics (dc, ref tm) == false) {
2653 prevobj = Win32SelectObject (dc, prevobj);
2654 Win32DeleteObject (prevobj);
2655 Win32ReleaseDC (IntPtr.Zero, dc);
2660 prevobj = Win32SelectObject (dc, prevobj);
2661 Win32DeleteObject (prevobj);
2662 Win32ReleaseDC (IntPtr.Zero, dc);
2664 ascent = tm.tmAscent;
2665 descent = tm.tmDescent;
2670 internal override void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children) {
2674 rect.left = rectangle.X;
2675 rect.top = rectangle.Y;
2676 rect.right = rectangle.Right;
2677 rect.bottom = rectangle.Bottom;
2679 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));
2680 Win32UpdateWindow(hwnd);
2683 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children) {
2684 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));
2687 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
2690 nid = new NOTIFYICONDATA();
2692 nid.cbSize = (uint)Marshal.SizeOf(nid);
2695 nid.uCallbackMessage = (uint)Msg.WM_USER;
2696 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2700 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2704 nid.hIcon = icon.Handle;
2705 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2710 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_ADD, ref nid);
2713 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
2716 nid = new NOTIFYICONDATA();
2718 nid.cbSize = (uint)Marshal.SizeOf(nid);
2719 nid.hIcon = icon.Handle;
2722 nid.uCallbackMessage = (uint)Msg.WM_USER;
2723 nid.uFlags = NotifyIconFlags.NIF_MESSAGE;
2727 nid.uFlags |= NotifyIconFlags.NIF_TIP;
2731 nid.hIcon = icon.Handle;
2732 nid.uFlags |= NotifyIconFlags.NIF_ICON;
2735 return Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2738 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
2741 nid = new NOTIFYICONDATA();
2743 nid.cbSize = (uint)Marshal.SizeOf(nid);
2748 Win32Shell_NotifyIcon(NotifyIconMessage.NIM_DELETE, ref nid);
2751 internal override void SystrayBalloon(IntPtr hwnd, int timeout, string title, string text, ToolTipIcon icon)
2755 nid = new NOTIFYICONDATA();
2757 nid.cbSize = (uint)Marshal.SizeOf(nid);
2760 nid.uFlags = NotifyIconFlags.NIF_INFO;
2761 nid.uTimeoutOrVersion = timeout;
2762 nid.szInfoTitle = title;
2764 nid.dwInfoFlags = icon;
2766 Win32Shell_NotifyIcon(NotifyIconMessage.NIM_MODIFY, ref nid);
2769 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
2770 // Nothing to do on Win32
2773 internal override void SetMenu(IntPtr handle, Menu menu) {
2774 // Trigger WM_NCCALC
2775 Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
2778 internal override Point GetMenuOrigin(IntPtr handle) {
2779 Form form = Control.FromHandle (handle) as Form;
2782 if (form.FormBorderStyle == FormBorderStyle.None)
2785 int bordersize = (form.Width - form.ClientSize.Width) / 2;
2787 if (form.FormBorderStyle == FormBorderStyle.FixedToolWindow || form.FormBorderStyle == FormBorderStyle.SizableToolWindow)
2788 return new Point (bordersize, bordersize + SystemInformation.ToolWindowCaptionHeight);
2790 return new Point (bordersize, bordersize + SystemInformation.CaptionHeight);
2793 return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
2796 internal override void SetIcon(IntPtr hwnd, Icon icon) {
2797 Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : icon.Handle); // 1 = large icon (0 would be small)
2800 internal override void ClipboardClose(IntPtr handle) {
2801 if (handle != clip_magic) {
2802 throw new ArgumentException("handle is not a valid clipboard handle");
2804 Win32CloseClipboard();
2807 internal override int ClipboardGetID(IntPtr handle, string format) {
2808 if (handle != clip_magic) {
2809 throw new ArgumentException("handle is not a valid clipboard handle");
2811 if (format == "Text" ) return 1;
2812 else if (format == "Bitmap" ) return 2;
2813 else if (format == "MetaFilePict" ) return 3;
2814 else if (format == "SymbolicLink" ) return 4;
2815 else if (format == "DataInterchangeFormat" ) return 5;
2816 else if (format == "Tiff" ) return 6;
2817 else if (format == "OEMText" ) return 7;
2818 else if (format == "DeviceIndependentBitmap" ) return 8;
2819 else if (format == "Palette" ) return 9;
2820 else if (format == "PenData" ) return 10;
2821 else if (format == "RiffAudio" ) return 11;
2822 else if (format == "WaveAudio" ) return 12;
2823 else if (format == "UnicodeText" ) return 13;
2824 else if (format == "EnhancedMetafile" ) return 14;
2825 else if (format == "FileDrop" ) return 15;
2826 else if (format == "Locale" ) return 16;
2828 return (int)Win32RegisterClipboardFormat(format);
2831 internal override IntPtr ClipboardOpen(bool primary_selection) {
2832 // Win32 does not have primary selection
2833 Win32OpenClipboard(FosterParent);
2837 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
2842 if (handle != clip_magic) {
2850 format = Win32EnumClipboardFormats(format);
2854 } while (format != 0);
2857 result = new int[count];
2861 format = Win32EnumClipboardFormats(format);
2863 result[count++] = (int)format;
2865 } while (format != 0);
2871 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
2876 if (handle != clip_magic) {
2877 throw new ArgumentException("handle is not a valid clipboard handle");
2880 hmem = Win32GetClipboardData((uint)type);
2881 if (hmem == IntPtr.Zero) {
2885 data = Win32GlobalLock(hmem);
2886 if (data == IntPtr.Zero) {
2887 uint error = Win32GetLastError();
2888 Console.WriteLine("Error: {0}", error);
2894 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2895 obj = AnsiToString(data);
2896 } else switch ((ClipboardFormats)type) {
2897 case ClipboardFormats.CF_TEXT: {
2898 obj = AnsiToString(data);
2902 case ClipboardFormats.CF_DIB: {
2903 obj = DIBtoImage(data);
2907 case ClipboardFormats.CF_UNICODETEXT: {
2908 obj = UnicodeToString(data);
2913 if (converter != null && !converter(type, data, out obj)) {
2919 Win32GlobalUnlock(hmem);
2925 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter, bool copy)
2929 if (handle != clip_magic) {
2930 throw new ArgumentException("handle is not a valid clipboard handle");
2935 if (!Win32EmptyClipboard())
2936 throw new ExternalException("Win32EmptyClipboard");
2941 if (obj is string) {
2942 type = (int)ClipboardFormats.CF_UNICODETEXT;
2943 } else if (obj is Image) {
2944 type = (int)ClipboardFormats.CF_DIB;
2948 if (type == DataFormats.GetFormat(DataFormats.Rtf).Id) {
2949 data = StringToAnsi ((string)obj);
2950 } else switch((ClipboardFormats)type) {
2951 case ClipboardFormats.CF_UNICODETEXT: {
2952 data = StringToUnicode ((string)obj);
2956 case ClipboardFormats.CF_TEXT: {
2957 data = StringToAnsi ((string)obj);
2961 case ClipboardFormats.CF_BITMAP:
2962 case ClipboardFormats.CF_DIB: {
2963 data = ImageToDIB ((Image)obj);
2964 type = (int)ClipboardFormats.CF_DIB;
2969 if (converter != null && !converter(ref type, obj, out data)) {
2970 data = null; // ensure that a failed conversion leaves null.
2976 SetClipboardData ((uint)type, data);
2980 internal static byte[] StringToUnicode (string text)
2982 return Encoding.Unicode.GetBytes (text + "\0");
2985 internal static byte[] StringToAnsi (string text)
2987 // FIXME, follow the behaviour of the previous code using UTF-8,
2988 // but this should be 'ANSI' on Windows, i.e. the current code page.
2989 // Does Encoding.Default work on Windows?
2990 return Encoding.UTF8.GetBytes (text + "\0");
2993 private void SetClipboardData (uint type, byte[] data)
2995 if (data.Length == 0)
2996 // Shouldn't call Win32SetClipboard with NULL, as, from MSDN:
2997 // "This parameter can be NULL, indicating that the window provides data
2998 // in the specified clipboard format (renders the format) upon request."
2999 // and I don't think we support that...
3000 // Note this is unrelated to the fact that passing a null obj to
3001 // ClipboardStore is actually a request to empty the clipboard!
3003 IntPtr hmem = CopyToMoveableMemory (data);
3004 if (hmem == IntPtr.Zero)
3005 // As above, should not call with null.
3006 // (Not that CopyToMoveableMemory should ever return null!)
3007 throw new ExternalException ("CopyToMoveableMemory failed.");
3008 if (Win32SetClipboardData (type, hmem) == IntPtr.Zero)
3009 throw new ExternalException ("Win32SetClipboardData");
3013 /// Creates a memory block with GlobalAlloc(GMEM_MOVEABLE), copies the data
3014 /// into it, and returns the handle to the memory.
3017 /// <param name="data">The data. Must not be null or zero-length —
3018 /// see the exception notes.</param>
3020 /// <returns>The *handle* to the allocated GMEM_MOVEABLE block.</returns>
3022 /// <exception cref="T:System.ArgumentException">The data was null or zero
3023 /// length. This is disallowed since a zero length allocation can't be made
3025 /// <exception cref="T:System.ComponentModel.Win32Exception">The allocation,
3026 /// or locking (handle->pointer) failed.
3027 /// Either out of memory or the handle table is full (256 max currently).
3028 /// Note Win32Exception is a subclass of ExternalException so this is OK in
3029 /// the documented Clipboard interface.
3031 internal static IntPtr CopyToMoveableMemory (byte[] data)
3033 if (data == null || data.Length == 0)
3034 // detect this before GlobalAlloc does.
3035 throw new ArgumentException ("Can't create a zero length memory block.");
3037 IntPtr hmem = Win32GlobalAlloc (GAllocFlags.GMEM_MOVEABLE | GAllocFlags.GMEM_DDESHARE, data.Length);
3038 if (hmem == IntPtr.Zero)
3039 throw new Win32Exception ();
3040 IntPtr hmem_ptr = Win32GlobalLock (hmem);
3041 if (hmem_ptr == IntPtr.Zero) // If the allocation was valid this shouldn't occur.
3042 throw new Win32Exception ();
3043 Marshal.Copy (data, 0, hmem_ptr, data.Length);
3044 Win32GlobalUnlock (hmem);
3049 internal override void SetAllowDrop(IntPtr hwnd, bool allowed) {
3051 Win32DnD.RegisterDropTarget(hwnd);
3053 Win32DnD.UnregisterDropTarget(hwnd);
3057 internal override DragDropEffects StartDrag(IntPtr hwnd, object data, DragDropEffects allowedEffects) {
3058 return Win32DnD.StartDrag(hwnd, data, allowedEffects);
3061 // XXX this doesn't work at all for FrameStyle.Dashed - it draws like Thick, and in the Thick case
3062 // the corners are drawn incorrectly.
3063 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
3067 COLORREF clrRef = new COLORREF();
3069 // If we want the standard hatch pattern we would
3070 // need to create a brush
3072 clrRef.R = backColor.R;
3073 clrRef.G = backColor.G;
3074 clrRef.B = backColor.B;
3077 pen = Win32CreatePen (style == FrameStyle.Thick ? PenStyle.PS_SOLID : PenStyle.PS_DASH,
3078 style == FrameStyle.Thick ? 4 : 2, ref clrRef);
3080 hdc = Win32GetDC(IntPtr.Zero);
3081 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
3082 oldpen = Win32SelectObject(hdc, pen);
3084 Win32MoveToEx(hdc, rectangle.Left, rectangle.Top, IntPtr.Zero);
3085 if ((rectangle.Width > 0) && (rectangle.Height > 0)) {
3086 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
3087 Win32LineTo(hdc, rectangle.Right, rectangle.Bottom);
3088 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
3089 Win32LineTo(hdc, rectangle.Left, rectangle.Top);
3091 if (rectangle.Width > 0) {
3092 Win32LineTo(hdc, rectangle.Right, rectangle.Top);
3094 Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
3098 Win32SelectObject(hdc, oldpen);
3099 Win32DeleteObject(pen);
3101 Win32ReleaseDC(IntPtr.Zero, hdc);
3104 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
3109 COLORREF clrRef = new COLORREF();
3114 Win32ClientToScreen(IntPtr.Zero, ref pt);
3116 // If we want the standard hatch pattern we would
3117 // need to create a brush
3119 clrRef.R = backColor.R;
3120 clrRef.G = backColor.G;
3121 clrRef.B = backColor.B;
3124 pen = Win32CreatePen(PenStyle.PS_SOLID, 1, ref clrRef);
3126 hdc = Win32GetDC(IntPtr.Zero);
3127 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
3128 oldpen = Win32SelectObject(hdc, pen);
3130 Win32MoveToEx(hdc, pt.x + start.X, pt.y + start.Y, IntPtr.Zero);
3131 Win32LineTo(hdc, pt.x + end.X, pt.y + end.Y);
3133 Win32SelectObject(hdc, oldpen);
3134 Win32DeleteObject(pen);
3136 Win32ReleaseDC(IntPtr.Zero, hdc);
3139 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
3144 rect.left = rectangle.Left;
3145 rect.top = rectangle.Top;
3146 rect.right = rectangle.Right;
3147 rect.bottom = rectangle.Bottom;
3152 COLORREF clrRef = new COLORREF();
3154 clrRef.R = backColor.R;
3155 clrRef.G = backColor.G;
3156 clrRef.B = backColor.B;
3159 brush = Win32CreateSolidBrush (clrRef);
3161 hdc = Win32GetDC(IntPtr.Zero);
3162 oldbrush = Win32SelectObject(hdc, brush);
3164 Win32PatBlt (hdc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, PatBltRop.DSTINVERT);
3166 Win32SelectObject(hdc, oldbrush);
3167 Win32DeleteObject(brush);
3169 Win32ReleaseDC(IntPtr.Zero, hdc);
3172 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
3181 Win32ClientToScreen(handle, ref pt);
3183 // If we want the standard hatch pattern we would
3184 // need to create a brush
3187 pen = Win32CreatePen(PenStyle.PS_SOLID, line_width, IntPtr.Zero);
3189 hdc = Win32GetDC(IntPtr.Zero);
3190 Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
3191 oldpen = Win32SelectObject(hdc, pen);
3193 Control c = Control.FromHandle (handle);
3196 Win32GetWindowRect (c.Handle, out window_rect);
3197 Region r = new Region (new Rectangle(window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top));
3198 Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
3201 Win32MoveToEx(hdc, pt.x + rect.Left, pt.y + rect.Top, IntPtr.Zero);
3202 if ((rect.Width > 0) && (rect.Height > 0)) {
3203 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
3204 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Bottom);
3205 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
3206 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Top);
3208 if (rect.Width > 0) {
3209 Win32LineTo(hdc, pt.x + rect.Right, pt.y + rect.Top);
3211 Win32LineTo(hdc, pt.x + rect.Left, pt.y + rect.Bottom);
3215 Win32SelectObject(hdc, oldpen);
3216 Win32DeleteObject(pen);
3218 Win32ExtSelectClipRgn(hdc, IntPtr.Zero, (int) ClipCombineMode.RGN_COPY);
3220 Win32ReleaseDC(IntPtr.Zero, hdc);
3223 internal override SizeF GetAutoScaleSize(Font font) {
3226 string magic_string = "The quick brown fox jumped over the lazy dog.";
3227 double magic_number = 44.549996948242189;
3229 g = Graphics.FromHwnd(FosterParent);
3231 width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
3232 return new SizeF(width, font.Height);
3235 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
3236 return Win32SendMessage(hwnd, message, wParam, lParam);
3239 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
3240 return Win32PostMessage(hwnd, message, wParam, lParam);
3243 internal override int SendInput (IntPtr hwnd, Queue keys) {
3244 INPUT[] inputs = new INPUT[keys.Count];
3245 const Int32 INPUT_KEYBOARD = 1;
3248 while (keys.Count > 0) {
3249 MSG msg = (MSG)keys.Dequeue();
3252 inputs[i].ki.wScan = 0;
3253 inputs[i].ki.time = 0;
3254 inputs[i].ki.dwFlags = (Int32)(msg.message == Msg.WM_KEYUP ? InputFlags.KEYEVENTF_KEYUP : 0);
3255 inputs[i].ki.wVk = (short)msg.wParam.ToInt32();
3256 inputs[i].type = INPUT_KEYBOARD;
3259 returns = Win32SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
3261 return (int) returns;
3264 internal override int KeyboardSpeed {
3267 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDSPEED, 0, ref speed, 0);
3269 // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
3275 internal override int KeyboardDelay {
3278 Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDDELAY, 0, ref delay, 0);
3280 // Return values must range from 0 to 4, 0 meaning 250ms,
3281 // and 4 meaning 1000 ms.
3287 private class WinBuffer
3290 public IntPtr bitmap;
3292 public WinBuffer (IntPtr hdc, IntPtr bitmap)
3295 this.bitmap = bitmap;
3299 internal override void CreateOffscreenDrawable (IntPtr handle, int width, int height, out object offscreen_drawable)
3301 Graphics destG = Graphics.FromHwnd (handle);
3302 IntPtr destHdc = destG.GetHdc ();
3304 IntPtr srcHdc = Win32CreateCompatibleDC (destHdc);
3305 IntPtr srcBmp = Win32CreateCompatibleBitmap (destHdc, width, height);
3306 Win32SelectObject (srcHdc, srcBmp);
3308 offscreen_drawable = new WinBuffer (srcHdc, srcBmp);
3310 destG.ReleaseHdc (destHdc);
3313 internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
3315 return Graphics.FromHdc (((WinBuffer)offscreen_drawable).hdc);
3318 internal override void BlitFromOffscreen (IntPtr dest_handle, Graphics dest_dc, object offscreen_drawable, Graphics offscreen_dc, Rectangle r)
3320 WinBuffer wb = (WinBuffer)offscreen_drawable;
3322 IntPtr destHdc = dest_dc.GetHdc ();
3323 Win32BitBlt (destHdc, r.Left, r.Top, r.Width, r.Height, wb.hdc, r.Left, r.Top, TernaryRasterOperations.SRCCOPY);
3324 dest_dc.ReleaseHdc (destHdc);
3327 internal override void DestroyOffscreenDrawable (object offscreen_drawable)
3329 WinBuffer wb = (WinBuffer)offscreen_drawable;
3331 Win32DeleteObject (wb.bitmap);
3332 Win32DeleteDC (wb.hdc);
3335 internal override void SetForegroundWindow (IntPtr handle)
3337 Win32SetForegroundWindow(handle);
3340 #endregion // Public Static Methods
3342 #region Win32 Imports
3343 [DllImport ("kernel32.dll", EntryPoint="GetLastError", CallingConvention=CallingConvention.StdCall)]
3344 private extern static uint Win32GetLastError();
3346 [DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3347 internal extern static IntPtr Win32CreateWindow(WindowExStyles dwExStyle, string lpClassName, string lpWindowName, WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lParam);
3349 [DllImport ("user32.dll", EntryPoint="DestroyWindow", CallingConvention=CallingConvention.StdCall)]
3350 internal extern static bool Win32DestroyWindow(IntPtr hWnd);
3352 [DllImport ("user32.dll", EntryPoint="PeekMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3353 internal extern static bool Win32PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
3355 [DllImport ("user32.dll", EntryPoint="GetMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3356 internal extern static bool Win32GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
3358 [DllImport ("user32.dll", EntryPoint="TranslateMessage", CallingConvention=CallingConvention.StdCall)]
3359 internal extern static bool Win32TranslateMessage(ref MSG msg);
3361 [DllImport ("user32.dll", EntryPoint="DispatchMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3362 internal extern static IntPtr Win32DispatchMessage(ref MSG msg);
3364 [DllImport ("user32.dll", EntryPoint="MoveWindow", CallingConvention=CallingConvention.StdCall)]
3365 internal extern static bool Win32MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
3367 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
3368 internal extern static bool Win32SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
3370 [DllImport ("user32.dll", EntryPoint="SetWindowPos", CallingConvention=CallingConvention.StdCall)]
3371 internal extern static bool Win32SetWindowPos(IntPtr hWnd, SetWindowPosZOrder pos, int x, int y, int cx, int cy, SetWindowPosFlags Flags);
3373 [DllImport ("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3374 internal extern static bool Win32SetWindowText(IntPtr hWnd, string lpString);
3376 [DllImport ("user32.dll", EntryPoint="GetWindowTextW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3377 internal extern static bool Win32GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
3379 [DllImport ("user32.dll", EntryPoint="SetParent", CallingConvention=CallingConvention.StdCall)]
3380 internal extern static IntPtr Win32SetParent(IntPtr hWnd, IntPtr hParent);
3382 [DllImport ("user32.dll", EntryPoint="RegisterClassW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3383 private extern static bool Win32RegisterClass(ref WNDCLASS wndClass);
3385 [DllImport ("user32.dll", EntryPoint="LoadCursorW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3386 private extern static IntPtr Win32LoadCursor(IntPtr hInstance, LoadCursorType type);
3388 [DllImport ("user32.dll", EntryPoint="ShowCursor", CallingConvention=CallingConvention.StdCall)]
3389 private extern static IntPtr Win32ShowCursor(bool bShow);
3391 [DllImport ("user32.dll", EntryPoint="SetCursor", CallingConvention=CallingConvention.StdCall)]
3392 private extern static IntPtr Win32SetCursor(IntPtr hCursor);
3394 [DllImport ("user32.dll", EntryPoint="CreateCursor", CallingConvention=CallingConvention.StdCall)]
3395 private extern static IntPtr Win32CreateCursor(IntPtr hInstance, int xHotSpot, int yHotSpot, int nWidth, int nHeight, Byte[] pvANDPlane, Byte[] pvORPlane);
3397 [DllImport ("user32.dll", EntryPoint="DestroyCursor", CallingConvention=CallingConvention.StdCall)]
3398 private extern static bool Win32DestroyCursor(IntPtr hCursor);
3400 [DllImport ("user32.dll", EntryPoint = "DrawIcon", CallingConvention = CallingConvention.StdCall)]
3401 private extern static bool Win32DrawIcon (IntPtr hDC, int X, int Y, IntPtr hIcon);
3403 [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3404 private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
3406 //[DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3407 //private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
3409 [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
3410 private extern static IntPtr Win32PostQuitMessage(int nExitCode);
3412 [DllImport ("user32.dll", EntryPoint="UpdateWindow", CallingConvention=CallingConvention.StdCall)]
3413 private extern static IntPtr Win32UpdateWindow(IntPtr hWnd);
3415 [DllImport ("user32.dll", EntryPoint="GetUpdateRect", CallingConvention=CallingConvention.StdCall)]
3416 private extern static bool Win32GetUpdateRect(IntPtr hWnd, ref RECT rect, bool erase);
3418 [DllImport ("user32.dll", EntryPoint="BeginPaint", CallingConvention=CallingConvention.StdCall)]
3419 private extern static IntPtr Win32BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
3421 [DllImport ("user32.dll", EntryPoint = "ValidateRect", CallingConvention = CallingConvention.StdCall)]
3422 private extern static IntPtr Win32ValidateRect (IntPtr hWnd, ref RECT rect);
3424 [DllImport ("user32.dll", EntryPoint="EndPaint", CallingConvention=CallingConvention.StdCall)]
3425 private extern static bool Win32EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
3427 [DllImport ("user32.dll", EntryPoint="GetDC", CallingConvention=CallingConvention.StdCall)]
3428 private extern static IntPtr Win32GetDC(IntPtr hWnd);
3430 [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
3431 private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
3433 //[DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
3434 //private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
3436 [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
3437 private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
3439 [DllImport ("user32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3440 private extern static IntPtr Win32MessageBox(IntPtr hParent, string pText, string pCaption, uint uType);
3442 [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
3443 private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
3445 //[DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
3446 //private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
3448 [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
3449 private extern static IntPtr Win32SetCapture(IntPtr hWnd);
3451 [DllImport ("user32.dll", EntryPoint="ReleaseCapture", CallingConvention=CallingConvention.StdCall)]
3452 private extern static IntPtr Win32ReleaseCapture();
3454 [DllImport ("user32.dll", EntryPoint="GetWindowRect", CallingConvention=CallingConvention.StdCall)]
3455 private extern static IntPtr Win32GetWindowRect(IntPtr hWnd, out RECT rect);
3457 [DllImport ("user32.dll", EntryPoint="GetClientRect", CallingConvention=CallingConvention.StdCall)]
3458 private extern static IntPtr Win32GetClientRect(IntPtr hWnd, out RECT rect);
3460 [DllImport ("user32.dll", EntryPoint="ScreenToClient", CallingConvention=CallingConvention.StdCall)]
3461 private extern static bool Win32ScreenToClient(IntPtr hWnd, ref POINT pt);
3463 [DllImport ("user32.dll", EntryPoint="ClientToScreen", CallingConvention=CallingConvention.StdCall)]
3464 private extern static bool Win32ClientToScreen(IntPtr hWnd, ref POINT pt);
3466 // This function returns the parent OR THE OWNER!
3467 // Use GetAncestor to only get the parent.
3468 [DllImport ("user32.dll", EntryPoint="GetParent", CallingConvention=CallingConvention.StdCall)]
3469 private extern static IntPtr Win32GetParent(IntPtr hWnd);
3471 [DllImport ("user32.dll", EntryPoint = "GetAncestor", CallingConvention = CallingConvention.StdCall)]
3472 private extern static IntPtr Win32GetAncestor (IntPtr hWnd, AncestorType flags);
3474 [DllImport ("user32.dll", EntryPoint="SetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3475 private extern static IntPtr Win32SetActiveWindow(IntPtr hWnd);
3477 [DllImport ("user32.dll", EntryPoint="AdjustWindowRectEx", CallingConvention=CallingConvention.StdCall)]
3478 private extern static bool Win32AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
3480 [DllImport ("user32.dll", EntryPoint="GetCursorPos", CallingConvention=CallingConvention.StdCall)]
3481 private extern static bool Win32GetCursorPos(out POINT lpPoint);
3483 [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
3484 private extern static bool Win32SetCursorPos(int x, int y);
3486 //[DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
3487 //private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
3489 [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
3490 private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
3492 //[DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
3493 //private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
3495 [DllImport ("gdi32.dll", EntryPoint="CreateSolidBrush", CallingConvention=CallingConvention.StdCall)]
3496 private extern static IntPtr Win32CreateSolidBrush(COLORREF clrRef);
3498 [DllImport ("gdi32.dll", EntryPoint="PatBlt", CallingConvention=CallingConvention.StdCall)]
3499 private extern static int Win32PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, PatBltRop dwRop);
3501 [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
3502 private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
3504 [DllImport ("user32.dll", EntryPoint="GetWindowLong", CallingConvention=CallingConvention.StdCall)]
3505 private extern static uint Win32GetWindowLong(IntPtr hwnd, WindowLong index);
3507 [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
3508 private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
3510 [DllImport ("user32.dll", EntryPoint="GetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
3511 private extern static uint Win32GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out LayeredWindowAttributes pwdFlags);
3513 [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
3514 public extern static bool Win32DeleteObject(IntPtr o);
3516 [DllImport ("user32.dll", EntryPoint="GetKeyState", CallingConvention=CallingConvention.StdCall)]
3517 private extern static short Win32GetKeyState(VirtualKeys nVirtKey);
3519 [DllImport ("user32.dll", EntryPoint="GetDesktopWindow", CallingConvention=CallingConvention.StdCall)]
3520 private extern static IntPtr Win32GetDesktopWindow();
3522 [DllImport ("user32.dll", EntryPoint="SetTimer", CallingConvention=CallingConvention.StdCall)]
3523 private extern static IntPtr Win32SetTimer(IntPtr hwnd, int nIDEvent, uint uElapse, IntPtr timerProc);
3525 [DllImport ("user32.dll", EntryPoint="KillTimer", CallingConvention=CallingConvention.StdCall)]
3526 private extern static IntPtr Win32KillTimer(IntPtr hwnd, int nIDEvent);
3528 [DllImport ("user32.dll", EntryPoint="ShowWindow", CallingConvention=CallingConvention.StdCall)]
3529 private extern static IntPtr Win32ShowWindow(IntPtr hwnd, WindowPlacementFlags nCmdShow);
3531 [DllImport ("user32.dll", EntryPoint="EnableWindow", CallingConvention=CallingConvention.StdCall)]
3532 private extern static IntPtr Win32EnableWindow(IntPtr hwnd, bool Enabled);
3534 [DllImport ("user32.dll", EntryPoint="SetFocus", CallingConvention=CallingConvention.StdCall)]
3535 internal extern static IntPtr Win32SetFocus(IntPtr hwnd);
3537 [DllImport ("user32.dll", EntryPoint="GetFocus", CallingConvention=CallingConvention.StdCall)]
3538 internal extern static IntPtr Win32GetFocus();
3540 [DllImport ("user32.dll", EntryPoint="CreateCaret", CallingConvention=CallingConvention.StdCall)]
3541 internal extern static bool Win32CreateCaret(IntPtr hwnd, IntPtr hBitmap, int nWidth, int nHeight);
3543 [DllImport ("user32.dll", EntryPoint="DestroyCaret", CallingConvention=CallingConvention.StdCall)]
3544 private extern static bool Win32DestroyCaret();
3546 [DllImport ("user32.dll", EntryPoint="ShowCaret", CallingConvention=CallingConvention.StdCall)]
3547 private extern static bool Win32ShowCaret(IntPtr hwnd);
3549 [DllImport ("user32.dll", EntryPoint="HideCaret", CallingConvention=CallingConvention.StdCall)]
3550 private extern static bool Win32HideCaret(IntPtr hwnd);
3552 [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
3553 private extern static bool Win32SetCaretPos(int X, int Y);
3555 //[DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
3556 //private extern static uint Win32GetCaretBlinkTime();
3558 [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3559 internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
3561 [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
3562 internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
3564 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3565 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3567 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3568 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3570 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3571 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
3573 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3574 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3576 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3577 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3579 //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3580 //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3582 [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
3583 private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
3585 [DllImport ("user32.dll", EntryPoint="GetActiveWindow", CallingConvention=CallingConvention.StdCall)]
3586 private extern static IntPtr Win32GetActiveWindow();
3588 [DllImport ("user32.dll", EntryPoint="GetSystemMetrics", CallingConvention=CallingConvention.StdCall)]
3589 private extern static int Win32GetSystemMetrics(SystemMetrics nIndex);
3591 [DllImport ("shell32.dll", EntryPoint="Shell_NotifyIconW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3592 private extern static bool Win32Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NOTIFYICONDATA lpData);
3594 [DllImport ("gdi32.dll", EntryPoint="CreateRectRgn", CallingConvention=CallingConvention.StdCall)]
3595 internal extern static IntPtr Win32CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
3597 [DllImport ("user32.dll", EntryPoint="IsWindowEnabled", CallingConvention=CallingConvention.StdCall)]
3598 private extern static bool IsWindowEnabled(IntPtr hwnd);
3600 [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
3601 private extern static bool IsWindowVisible(IntPtr hwnd);
3603 //[DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
3604 //private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
3606 [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3607 private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3609 [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3610 private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
3612 [DllImport ("user32.dll", EntryPoint="SendInput", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3613 private extern static UInt32 Win32SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray)] INPUT[] inputs, Int32 cbSize);
3615 [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3616 private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
3618 //[DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3619 //private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
3621 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3622 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
3624 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3625 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref bool value, uint fWinIni);
3627 [DllImport ("user32.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
3628 private extern static bool Win32SystemParametersInfo (SPIAction uiAction, uint uiParam, ref ANIMATIONINFO value, uint fWinIni);
3630 [DllImport ("user32.dll", EntryPoint="OpenClipboard", CallingConvention=CallingConvention.StdCall)]
3631 private extern static bool Win32OpenClipboard(IntPtr hwnd);
3633 [DllImport ("user32.dll", EntryPoint="EmptyClipboard", CallingConvention=CallingConvention.StdCall)]
3634 private extern static bool Win32EmptyClipboard();
3636 [DllImport ("user32.dll", EntryPoint="RegisterClipboardFormatW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
3637 private extern static uint Win32RegisterClipboardFormat(string format);
3639 [DllImport ("user32.dll", EntryPoint="CloseClipboard", CallingConvention=CallingConvention.StdCall)]
3640 private extern static bool Win32CloseClipboard();
3642 [DllImport ("user32.dll", EntryPoint="EnumClipboardFormats", CallingConvention=CallingConvention.StdCall)]
3643 private extern static uint Win32EnumClipboardFormats(uint format);
3645 [DllImport ("user32.dll", EntryPoint="GetClipboardData", CallingConvention=CallingConvention.StdCall)]
3646 private extern static IntPtr Win32GetClipboardData(uint format);
3648 [DllImport ("user32.dll", EntryPoint="SetClipboardData", CallingConvention=CallingConvention.StdCall)]
3649 private extern static IntPtr Win32SetClipboardData(uint format, IntPtr handle);
3651 [DllImport ("kernel32.dll", EntryPoint="GlobalAlloc", CallingConvention=CallingConvention.StdCall)]
3652 internal extern static IntPtr Win32GlobalAlloc(GAllocFlags Flags, int dwBytes);
3654 [DllImport ("kernel32.dll", EntryPoint="CopyMemory", CallingConvention=CallingConvention.StdCall)]
3655 internal extern static void Win32CopyMemory(IntPtr Destination, IntPtr Source, int length);
3657 [DllImport ("kernel32.dll", EntryPoint="GlobalFree", CallingConvention=CallingConvention.StdCall)]
3658 internal extern static IntPtr Win32GlobalFree(IntPtr hMem);
3660 [DllImport ("kernel32.dll", EntryPoint="GlobalSize", CallingConvention=CallingConvention.StdCall)]
3661 internal extern static uint Win32GlobalSize(IntPtr hMem);
3663 [DllImport ("kernel32.dll", EntryPoint="GlobalLock", CallingConvention=CallingConvention.StdCall)]
3664 internal extern static IntPtr Win32GlobalLock(IntPtr hMem);
3666 [DllImport ("kernel32.dll", EntryPoint="GlobalUnlock", CallingConvention=CallingConvention.StdCall)]
3667 internal extern static IntPtr Win32GlobalUnlock(IntPtr hMem);
3669 [DllImport ("gdi32.dll", EntryPoint="SetROP2", CallingConvention=CallingConvention.StdCall)]
3670 internal extern static int Win32SetROP2(IntPtr hdc, ROP2DrawMode fnDrawMode);
3672 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3673 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, ref POINT lpPoint);
3675 [DllImport ("gdi32.dll", EntryPoint="MoveToEx", CallingConvention=CallingConvention.StdCall)]
3676 internal extern static bool Win32MoveToEx(IntPtr hdc, int x, int y, IntPtr lpPoint);
3678 [DllImport ("gdi32.dll", EntryPoint="LineTo", CallingConvention=CallingConvention.StdCall)]
3679 internal extern static bool Win32LineTo(IntPtr hdc, int x, int y);
3681 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3682 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, ref COLORREF color);
3684 [DllImport ("gdi32.dll", EntryPoint="CreatePen", CallingConvention=CallingConvention.StdCall)]
3685 internal extern static IntPtr Win32CreatePen(PenStyle fnPenStyle, int nWidth, IntPtr color);
3687 [DllImport ("gdi32.dll", EntryPoint="GetStockObject", CallingConvention=CallingConvention.StdCall)]
3688 internal extern static IntPtr Win32GetStockObject(StockObject fnObject);
3690 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3691 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, IntPtr color);
3693 [DllImport ("gdi32.dll", EntryPoint="CreateHatchBrush", CallingConvention=CallingConvention.StdCall)]
3694 internal extern static IntPtr Win32CreateHatchBrush(HatchStyle fnStyle, ref COLORREF color);
3696 [DllImport("gdi32.dll", EntryPoint = "ExcludeClipRect", CallingConvention = CallingConvention.StdCall)]
3697 internal extern static int Win32ExcludeClipRect (IntPtr hdc, int left, int top, int right, int bottom);
3699 [DllImport ("gdi32.dll", EntryPoint="ExtSelectClipRgn", CallingConvention=CallingConvention.StdCall)]
3700 internal extern static int Win32ExtSelectClipRgn(IntPtr hdc, IntPtr hrgn, int mode);
3702 [DllImport ("winmm.dll", EntryPoint="PlaySoundW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3703 internal extern static IntPtr Win32PlaySound(string pszSound, IntPtr hmod, SndFlags fdwSound);
3705 [DllImport ("user32.dll", EntryPoint="GetDoubleClickTime", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3706 private extern static int Win32GetDoubleClickTime ();
3708 [DllImport ("user32.dll", EntryPoint="SetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3709 internal extern static int Win32SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
3711 [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
3712 internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
3714 [DllImport ("user32.dll", EntryPoint="ClipCursor", CallingConvention=CallingConvention.StdCall)]
3715 internal extern static bool Win32ClipCursor (ref RECT lpRect);
3717 [DllImport ("user32.dll", EntryPoint="GetClipCursor", CallingConvention=CallingConvention.StdCall)]
3718 internal extern static bool Win32GetClipCursor (out RECT lpRect);
3720 [DllImport ("gdi32.dll", EntryPoint="BitBlt", CallingConvention=CallingConvention.StdCall)]
3721 internal static extern bool Win32BitBlt (IntPtr hObject, int nXDest, int nYDest, int nWidth,
3722 int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
3724 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3725 internal static extern IntPtr Win32CreateCompatibleDC (IntPtr hdc);
3727 [DllImport ("gdi32.dll", EntryPoint="DeleteDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
3728 internal static extern bool Win32DeleteDC (IntPtr hdc);
3730 [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleBitmap", CallingConvention=CallingConvention.StdCall)]
3731 internal static extern IntPtr Win32CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight);
3733 [DllImport ("kernel32.dll", EntryPoint = "GetSystemPowerStatus", CallingConvention = CallingConvention.StdCall)]
3734 internal static extern Boolean Win32GetSystemPowerStatus (SYSTEMPOWERSTATUS sps);
3736 [DllImport ("user32.dll", EntryPoint = "GetIconInfo", CallingConvention = CallingConvention.StdCall)]
3737 internal static extern bool Win32GetIconInfo (IntPtr hIcon, out ICONINFO piconinfo);
3739 [DllImport ("user32.dll", EntryPoint="SetForegroundWindow", CallingConvention=CallingConvention.StdCall)]
3740 extern static bool Win32SetForegroundWindow(IntPtr hWnd);