2007-03-26 Everaldo Canuto <everaldo@simios.org>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / XplatUIWin32.cs
index da5f9356b9f6c5b4fb9aae06edb53ade1a117e66..61733946d1ea84ff799c3b760aa81e611ba52552 100644 (file)
@@ -61,8 +61,11 @@ namespace System.Windows.Forms {
                private static IntPtr           clip_magic = new IntPtr(27051977);
                private static int              scroll_width;
                private static int              scroll_height;
+               private static Hashtable        wm_nc_registered;
+               private static RECT             clipped_cursor_rect;
+               private Hashtable               registered_classes;
+               private Hwnd HwndCreating; // the Hwnd we are currently creating (see CreateWindow)
 
-               private static Win32DnD         DnD;
                #endregion      // Local Variables
 
                #region Private Structs
@@ -88,23 +91,67 @@ namespace System.Windows.Forms {
                        internal int            top;
                        internal int            right;
                        internal int            bottom;
-                       public override string ToString() {
+
+                       public RECT (int left, int top, int right, int bottom)
+                       {
+                               this.left = left;
+                               this.top = top;
+                               this.right = right;
+                               this.bottom = bottom;
+                       }
+
+                       #region Instance Properties
+                       public int Height { get { return bottom - top + 1; } }
+                       public int Width { get { return right - left + 1; } }
+                       public Size Size { get { return new Size (Width, Height); } }
+                       public Point Location { get { return new Point (left, top); } }
+                       #endregion
+
+                       #region Instance Methods
+                       public Rectangle ToRectangle ()
+                       {
+                               return Rectangle.FromLTRB (left, top, right, bottom);
+                       }
+
+                       public static RECT FromRectangle (Rectangle rectangle)
+                       {
+                               return new RECT (rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
+                       }
+
+                       public override int GetHashCode ()
+                       {
+                               return left ^ ((top << 13) | (top >> 0x13))
+                                 ^ ((Width << 0x1a) | (Width >> 6))
+                                 ^ ((Height << 7) | (Height >> 0x19));
+                       }
+                       
+                       public override string ToString ()
+                       {
                                return String.Format("RECT left={0}, top={1}, right={2}, bottom={3}, width={4}, height={5}", left, top, right, bottom, right-left, bottom-top);
                        }
+                       #endregion
 
-               }
+                       #region Operator overloads
+                       public static implicit operator Rectangle (RECT rect)
+                       {
+                               return Rectangle.FromLTRB (rect.left, rect.top, rect.right, rect.bottom);
+                       }
 
-               [StructLayout(LayoutKind.Sequential)]
-               internal struct POINT {
-                       internal int            x;
-                       internal int            y;
+                       public static implicit operator RECT (Rectangle rect)
+                       {
+                               return new RECT (rect.Left, rect.Top, rect.Right, rect.Bottom);
+                       }
+                       #endregion
                }
 
                internal enum SPIAction {
+                       SPI_GETKEYBOARDSPEED    = 0x000A,
+                       SPI_GETKEYBOARDDELAY    = 0x0016,
                        SPI_GETWORKAREA         = 0x0030,
                        SPI_GETMOUSEHOVERWIDTH  = 0x0062,
                        SPI_GETMOUSEHOVERHEIGHT = 0x0064,
                        SPI_GETMOUSEHOVERTIME   = 0x0066,
+                       SPI_GETWHEELSCROLLLINES = 0x0068
                }
 
                internal enum WindowPlacementFlags {
@@ -147,6 +194,7 @@ namespace System.Windows.Forms {
                private enum TMEFlags {
                        TME_HOVER               = 0x00000001,
                        TME_LEAVE               = 0x00000002,
+                       TME_NONCLIENT           = 0x00000010,
                        TME_QUERY               = unchecked((int)0x40000000),
                        TME_CANCEL              = unchecked((int)0x80000000)
                }
@@ -176,6 +224,55 @@ namespace System.Windows.Forms {
                        internal int            Reserved8;
                }
 
+               [StructLayout(LayoutKind.Sequential)]
+               internal struct KEYBDINPUT {
+                       internal short wVk;
+                       internal short wScan;
+                       internal Int32 dwFlags;
+                       internal Int32 time;
+                       internal UIntPtr dwExtraInfo;
+               }
+
+               [StructLayout(LayoutKind.Sequential)]
+               internal struct MOUSEINPUT {
+                       internal Int32 dx;
+                       internal Int32 dy;
+                       internal Int32 mouseData;
+                       internal Int32 dwFlags;
+                       internal Int32 time;
+                       internal UIntPtr dwExtraInfo;
+               }
+
+               [StructLayout(LayoutKind.Sequential)]
+               internal struct HARDWAREINPUT {
+                       internal Int32 uMsg;
+                       internal short wParamL;
+                       internal short wParamH;
+               }
+
+               [StructLayout(LayoutKind.Explicit)]
+               internal struct INPUT {
+                       [FieldOffset(0)]
+                       internal Int32 type;
+
+                       [FieldOffset(4)]
+                       internal MOUSEINPUT mi;
+
+                       [FieldOffset(4)]
+                       internal KEYBDINPUT ki;
+
+                       [FieldOffset(4)]
+                       internal HARDWAREINPUT hi;
+               }
+
+
+               internal enum InputFlags {
+                       KEYEVENTF_EXTENDEDKEY   = 0x0001,
+                       KEYEVENTF_KEYUP                 = 0x0002,
+                       KEYEVENTF_SCANCODE              = 0x0003,
+                       KEYEVENTF_UNICODE               = 0x0004,
+               }
+
                internal enum ClassStyle {
                        CS_VREDRAW                      = 0x00000001,
                        CS_HREDRAW                      = 0x00000002,
@@ -190,7 +287,9 @@ namespace System.Windows.Forms {
                        CS_BYTEALIGNCLIENT              = 0x00001000,
                        CS_BYTEALIGNWINDOW              = 0x00002000,
                        CS_GLOBALCLASS                  = 0x00004000,
-                       CS_IME                          = 0x00010000
+                       CS_IME                          = 0x00010000,
+                       // Windows XP+
+                       CS_DROPSHADOW                   = 0x00020000
                }
 
                internal enum SetWindowPosZOrder {
@@ -353,7 +452,26 @@ namespace System.Windows.Forms {
                        internal byte                   tmStruckOut; 
                        internal byte                   tmPitchAndFamily; 
                        internal byte                   tmCharSet; 
-               } 
+               }
+
+               public enum TernaryRasterOperations : uint
+               {
+                       SRCCOPY = 0x00CC0020,
+                       SRCPAINT = 0x00EE0086,
+                       SRCAND = 0x008800C6,
+                       SRCINVERT = 0x00660046,
+                       SRCERASE = 0x00440328,
+                       NOTSRCCOPY = 0x00330008,
+                       NOTSRCERASE = 0x001100A6,
+                       MERGECOPY = 0x00C000CA,
+                       MERGEPAINT = 0x00BB0226,
+                       PATCOPY = 0x00F00021,
+                       PATPAINT = 0x00FB0A09,
+                       PATINVERT = 0x005A0049,
+                       DSTINVERT = 0x00550009,
+                       BLACKNESS = 0x00000042,
+                       WHITENESS = 0x00FF0062
+               }
 
                [Flags]
                private enum ScrollWindowExFlags {
@@ -571,6 +689,14 @@ namespace System.Windows.Forms {
                        PS_ALTERNATE                    = 8
                }
 
+               internal enum PatBltRop : int {
+                       PATCOPY   = 0xf00021,
+                       PATINVERT = 0x5a0049,
+                       DSTINVERT = 0x550009,
+                       BLACKNESS = 0x000042,
+                       WHITENESS = 0xff0062,
+               }
+
                internal enum StockObject : int {
                        WHITE_BRUSH                     = 0,
                        LTGRAY_BRUSH                    = 1,
@@ -628,14 +754,9 @@ namespace System.Windows.Forms {
 
                #region Constructor & Destructor
                private XplatUIWin32() {
-                       WNDCLASS        wndClass;
-                       bool            result;
-
                        // Handle singleton stuff first
                        ref_count=0;
 
-                       DnD = new Win32DnD();
-
                        mouse_state = MouseButtons.None;
                        mouse_position = Point.Empty;
 
@@ -643,23 +764,7 @@ namespace System.Windows.Forms {
 
                        themes_enabled = false;
 
-                       // Prepare 'our' window class
-                       wnd_proc = new WndProc(NativeWindow.WndProc);
-                       wndClass.style = (int)(ClassStyle.CS_OWNDC | ClassStyle.CS_DBLCLKS);
-                       wndClass.lpfnWndProc = wnd_proc;
-                       wndClass.cbClsExtra = 0;
-                       wndClass.cbWndExtra = 0;
-                       wndClass.hbrBackground = IntPtr.Zero;
-                       wndClass.hCursor = Win32LoadCursor(IntPtr.Zero, LoadCursorType.IDC_ARROW);
-                       wndClass.hIcon = IntPtr.Zero;
-                       wndClass.hInstance = IntPtr.Zero;
-                       wndClass.lpszClassName = XplatUI.DefaultClassName;
-                       wndClass.lpszMenuName = "";
-
-                       result=Win32RegisterClass(ref wndClass);
-                       if (result==false) {
-                               Win32MessageBox(IntPtr.Zero, "Could not register the "+XplatUI.DefaultClassName+" window class, win32 error " + Win32GetLastError().ToString(), "Oops", 0);
-                       }
+                       wnd_proc = new WndProc(InternalWndProc);
 
                        FosterParent=Win32CreateWindow((int)WindowExStyles.WS_EX_TOOLWINDOW, "static", "Foster Parent Window", (int)WindowStyles.WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
 
@@ -671,21 +776,46 @@ namespace System.Windows.Forms {
                        scroll_width = Win32GetSystemMetrics(SystemMetrics.SM_CXVSCROLL);
 
                        timer_list = new Hashtable ();
+                       registered_classes = new Hashtable ();
                }
                #endregion      // Constructor & Destructor
 
                #region Private Support Methods
-               private static IntPtr DefWndProc(IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) {
-                       return Win32DefWindowProc(hWnd, msg, wParam, lParam);
-               }
 
-               private static bool MessageWaiting {
-                       get {
-                               if (message_queue.Count == 0) {
-                                       return false;
-                               }
-                               return true;
+               private string RegisterWindowClass (int classStyle)
+               {
+                       string class_name;
+
+                       lock (registered_classes) {
+                               class_name = (string)registered_classes[classStyle];
+
+                               if (class_name != null)
+                                       return class_name;
+
+                               class_name = string.Format ("Mono.WinForms.{0}.{1}", System.Threading.Thread.GetDomainID ().ToString (), classStyle);
+
+                               WNDCLASS wndClass;
+
+                               wndClass.style = classStyle;
+                               wndClass.lpfnWndProc = wnd_proc;
+                               wndClass.cbClsExtra = 0;
+                               wndClass.cbWndExtra = 0;
+                               wndClass.hbrBackground = (IntPtr)(GetSysColorIndex.COLOR_WINDOW + 1);
+                               wndClass.hCursor = Win32LoadCursor (IntPtr.Zero, LoadCursorType.IDC_ARROW);
+                               wndClass.hIcon = IntPtr.Zero;
+                               wndClass.hInstance = IntPtr.Zero;
+                               wndClass.lpszClassName = class_name;
+                               wndClass.lpszMenuName = "";
+
+                               bool result = Win32RegisterClass (ref wndClass);
+
+                               if (result == false)
+                                       Win32MessageBox (IntPtr.Zero, "Could not register the window class, win32 error " + Win32GetLastError ().ToString (), "Oops", 0);
+
+                               registered_classes[classStyle] = class_name;
                        }
+                       
+                       return class_name;
                }
 
                private static bool RetrieveMessage(ref MSG msg) {
@@ -898,6 +1028,14 @@ namespace System.Windows.Forms {
                        }
                }
 
+               internal override int MouseWheelScrollDelta {
+                       get {
+                               int delta = 120;
+                               Win32SystemParametersInfo(SPIAction.SPI_GETWHEELSCROLLLINES, 0, ref delta, 0);
+                               return delta;
+                       }
+               }
+               
                internal override int HorizontalScrollBarHeight {
                        get {
                                return scroll_height;
@@ -1118,12 +1256,14 @@ namespace System.Windows.Forms {
                                ParentHandle = FosterParent;
                        }
 
-                       // Since we fake MDI dont tell Windows that this is a real MDI window
-                       if ((cp.ExStyle & (int) WindowExStyles.WS_EX_MDICHILD) != 0) {
-                               SetMdiStyles (cp);
-                       }
+                       FakeStyles (cp);
+
+                       string class_name = RegisterWindowClass (cp.ClassStyle);
+                       HwndCreating = hwnd;
+
+                       WindowHandle = Win32CreateWindow ((uint)cp.ExStyle, class_name, cp.Caption, (uint)cp.Style, cp.X, cp.Y, cp.Width, cp.Height, ParentHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
 
-                       WindowHandle = Win32CreateWindow((uint)cp.ExStyle, cp.ClassName, cp.Caption, (uint)cp.Style, cp.X, cp.Y, cp.Width, cp.Height, ParentHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
+                       HwndCreating = null;
 
                        if (WindowHandle==IntPtr.Zero) {
                                uint error = Win32GetLastError();
@@ -1203,15 +1343,27 @@ namespace System.Windows.Forms {
 
                internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
 
-                       if ((cp.ExStyle & (int) WindowExStyles.WS_EX_MDICHILD) != 0) {
-                               SetMdiStyles (cp);
-                       }
+                       FakeStyles (cp);
 
                        Win32SetWindowLong(handle, WindowLong.GWL_STYLE, (uint)cp.Style);
                        Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, (uint)cp.ExStyle);
+
+                       if ((cp.ExStyle & (int) WindowExStyles.WS_EX_TOOLWINDOW) > 0)
+                               XplatUI.RequestNCRecalc (handle);
+               }
+
+               internal override double GetWindowTransparency(IntPtr handle)
+               {
+                       LayeredWindowAttributes lwa;
+                       COLORREF clrRef;
+                       byte alpha;
+
+                       if (0 == Win32GetLayeredWindowAttributes (handle, out clrRef, out alpha, out lwa))
+                               return 1.0;
+
+                       return ((double)alpha) / 255.0;
                }
 
-               
                internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
                        LayeredWindowAttributes lwa = LayeredWindowAttributes.LWA_ALPHA;
                        byte opacity = (byte)(transparency*255);
@@ -1228,9 +1380,39 @@ namespace System.Windows.Forms {
                        Win32SetLayeredWindowAttributes(handle, clrRef, opacity, lwa);
                }
 
-               internal override bool SupportsTransparency() {
-                       // We might check with the OS, but I think we're only >=W2k
-                       return true;
+               TransparencySupport support;
+               bool queried_transparency_support;
+               internal override TransparencySupport SupportsTransparency() {
+                       if (queried_transparency_support)
+                               return support;
+
+                       bool flag;
+                       support = TransparencySupport.None;
+
+                       flag = true;
+                       try {
+                               Win32SetLayeredWindowAttributes (IntPtr.Zero, new COLORREF (), 255, LayeredWindowAttributes.LWA_ALPHA);
+                       }
+                       catch (EntryPointNotFoundException) { flag = false; }
+                       catch { /* swallow everything else */ }
+
+                       if (flag) support |= TransparencySupport.Set;
+
+                       flag = true;
+                       try {
+                               LayeredWindowAttributes lwa;
+                               COLORREF clrRef;
+                               byte alpha;
+
+                               Win32GetLayeredWindowAttributes (IntPtr.Zero, out clrRef, out alpha, out lwa);
+                       }
+                       catch (EntryPointNotFoundException) { flag = false; }
+                       catch { /* swallow everything else */ }
+
+                       if (flag) support |= TransparencySupport.Get;
+
+                       queried_transparency_support = true;
+                       return support;
                }
 
                internal override void UpdateWindow(IntPtr handle) {
@@ -1362,6 +1544,13 @@ namespace System.Windows.Forms {
                                          SetWindowPosFlags.SWP_DRAWFRAME);
                }
 
+               private IntPtr InternalWndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
+               {
+                       if (HwndCreating != null && HwndCreating.ClientWindow == IntPtr.Zero)
+                               HwndCreating.ClientWindow = hWnd;
+                       return NativeWindow.WndProc (hWnd, msg, wParam, lParam);
+               }
+
                internal override IntPtr DefWndProc(ref Message msg) {
                        msg.Result=Win32DefWindowProc(msg.HWnd, (Msg)msg.Msg, msg.WParam, msg.LParam);
                        return msg.Result;
@@ -1394,8 +1583,32 @@ namespace System.Windows.Forms {
                        Win32PostQuitMessage(exitCode);
                }
 
+               internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
+               {
+                       if (wm_nc_registered == null)
+                               wm_nc_registered = new Hashtable ();
+                               
+                       TMEFlags flags = TMEFlags.TME_NONCLIENT;
+                       if (hover)
+                               flags |= TMEFlags.TME_HOVER;
+                       if (leave)
+                               flags |= TMEFlags.TME_LEAVE;
+
+                       if (flags == TMEFlags.TME_NONCLIENT) {
+                               if (wm_nc_registered.Contains (hwnd)) {
+                                       wm_nc_registered.Remove (hwnd);
+                               }
+                       } else {
+                               if (!wm_nc_registered.Contains (hwnd)) {
+                                       wm_nc_registered.Add (hwnd, flags);
+                               } else {
+                                       wm_nc_registered [hwnd] = flags;
+                               }
+                       }
+               }
+
                internal override void RequestNCRecalc(IntPtr handle) {
-                       Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOMOVE);
+                       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);
                }
 
                internal override void ResetMouseHover(IntPtr handle) {
@@ -1416,6 +1629,7 @@ namespace System.Windows.Forms {
                private bool GetMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, bool blocking) {
                        bool            result;
 
+                       msg.refobject = 0;
                        if (RetrieveMessage(ref msg)) {
                                return true;
                        }
@@ -1429,7 +1643,7 @@ namespace System.Windows.Forms {
                                }
                        }
 
-                       // We need to fake WM_MOUSE_ENTER/WM_MOUSE_LEAVE
+                       // We need to fake WM_MOUSE_ENTER
                        switch (msg.message) {
                                case Msg.WM_LBUTTONDOWN: {
                                        mouse_state |= MouseButtons.Left;
@@ -1488,13 +1702,26 @@ namespace System.Windows.Forms {
                                        break;
                                }
 
+                               case Msg.WM_NCMOUSEMOVE: {
+                                       if (wm_nc_registered == null || !wm_nc_registered.Contains (msg.hwnd))
+                                               break;
+                                               
+                                       TRACKMOUSEEVENT tme;
+
+                                       tme = new TRACKMOUSEEVENT ();
+                                       tme.size = Marshal.SizeOf(tme);
+                                       tme.hWnd = msg.hwnd;
+                                       tme.dwFlags = (TMEFlags)wm_nc_registered[msg.hwnd];
+                                       Win32TrackMouseEvent (ref tme);
+                                       return result;
+                               }
+
                                case Msg.WM_DROPFILES: {
                                        return Win32DnD.HandleWMDropFiles(ref msg);
                                }
 
                                case Msg.WM_MOUSELEAVE: {
                                        prev_mouse_hwnd = IntPtr.Zero;
-                                       msg.message=Msg.WM_MOUSE_LEAVE;
                                        break;
                                }
 
@@ -1532,15 +1759,20 @@ namespace System.Windows.Forms {
                        return false;
                }
 
-               internal override bool SetTopmost(IntPtr hWnd, IntPtr hWndOwner, bool Enabled) {
+               internal override bool SetTopmost(IntPtr hWnd, bool Enabled) {
                        if (Enabled) {
-                               Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
+                               Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
                                return true;
                        } else {
-                               Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
+                               Win32SetWindowPos(hWnd, SetWindowPosZOrder.HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE);
                                return true;
                        }
                }
+               
+               internal override bool SetOwner(IntPtr hWnd, IntPtr hWndOwner) {
+                       Win32SetWindowLong(hWnd, WindowLong.GWL_HWNDPARENT, (uint) hWndOwner);
+                       return true;
+               }
 
                internal override bool Text(IntPtr handle, string text) {
                        Win32SetWindowText(handle, text);
@@ -1559,7 +1791,8 @@ namespace System.Windows.Forms {
                internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
                {
                        if (visible) {
-                               if (Control.FromHandle (handle) is Form) {
+                               Control c = Control.FromHandle (handle);
+                               if (c is Form) {
                                        Form f;
 
                                        f = (Form)Control.FromHandle (handle);
@@ -1570,16 +1803,16 @@ namespace System.Windows.Forms {
                                                case FormWindowState.Maximized: flags = WindowPlacementFlags.SW_MAXIMIZE; break;
                                        }
                                        
-                                       if (Hwnd.ObjectFromHandle (handle).no_activate)
-                                               flags |= WindowPlacementFlags.SW_SHOWNOACTIVATE;
+                                       if (!f.ActivateOnShow)
+                                               flags = WindowPlacementFlags.SW_SHOWNOACTIVATE;
                                                
                                        Win32ShowWindow (handle, flags);
                                }
                                else {
-                                       if (Hwnd.ObjectFromHandle (handle).no_activate)
-                                               Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
-                                       else
+                                       if (c.ActivateOnShow)
                                                Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNORMAL);
+                                       else
+                                               Win32ShowWindow (handle, WindowPlacementFlags.SW_SHOWNOACTIVATE);
                                }
                        }
                        else {
@@ -1620,6 +1853,13 @@ namespace System.Windows.Forms {
                internal override void GrabWindow(IntPtr hWnd, IntPtr ConfineToHwnd) {
                        grab_hwnd = hWnd;
                        Win32SetCapture(hWnd);
+                       
+                       if (ConfineToHwnd != IntPtr.Zero) {
+                               RECT window_rect;
+                               Win32GetWindowRect (ConfineToHwnd, out window_rect);
+                               Win32GetClipCursor (out clipped_cursor_rect);
+                               Win32ClipCursor (ref window_rect);
+                       }
                }
 
                internal override void GrabInfo(out IntPtr hWnd, out bool GrabConfined, out Rectangle GrabArea) {
@@ -1629,6 +1869,11 @@ namespace System.Windows.Forms {
                }
 
                internal override void UngrabWindow(IntPtr hWnd) {
+                       if (!(clipped_cursor_rect.top == 0 && clipped_cursor_rect.bottom == 0 && clipped_cursor_rect.left == 0 && clipped_cursor_rect.right == 0)) {
+                               Win32ClipCursor (ref clipped_cursor_rect);
+                               clipped_cursor_rect = new RECT ();
+                       }
+                       
                        Win32ReleaseCapture();
                        grab_hwnd = IntPtr.Zero;
                }
@@ -1847,6 +2092,13 @@ namespace System.Windows.Forms {
                        return;
                }
   
+  #if NET_2_0
+               internal override void ShowBalloonTip(int timeout, string tipTitle, string tipText, ToolTipIcon tipIcon)
+               {
+                       // TODO:
+               }
+#endif
+
                internal override void SendAsyncMethod (AsyncMethodData method)
                {
                        Win32PostMessage(FosterParent, Msg.WM_ASYNC_MESSAGE, IntPtr.Zero, (IntPtr)GCHandle.Alloc (method));
@@ -1877,18 +2129,18 @@ namespace System.Windows.Forms {
                                timer_list.Remove(index);
                        }
                }
-
-
-               private void CaretCallback(object sender, EventArgs e) {
-                       Console.WriteLine("CaretCallback hit");
-               }
-
-               private void SetMdiStyles (CreateParams cp)
+               
+               private void FakeStyles (CreateParams cp)
                {
-                       cp.Style = (int)WindowStyles.WS_CHILD | (int)WindowStyles.WS_CLIPCHILDREN | (int)WindowStyles.WS_CLIPSIBLINGS;
-                       cp.ExStyle = 0;
+                       if (cp.HasWindowManager) {
+                               // Remove all styles but WS_VISIBLE.
+                               cp.WindowStyle &= WindowStyles.WS_VISIBLE;
+                               // Set styles that enables us to use the window manager.
+                               cp.WindowStyle |= WindowStyles.WS_CHILD | WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS;
+                               cp.ExStyle = 0;
+                       }
                }
-       
+               
                internal override void CreateCaret(IntPtr hwnd, int width, int height) {
                        Win32CreateCaret(hwnd, IntPtr.Zero, width, height);
                        caret_visible = false;
@@ -2033,38 +2285,7 @@ namespace System.Windows.Forms {
 
 
                internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
-                       uint    style;
-                       uint    exstyle;
-
-                       style = Win32GetWindowLong(handle, WindowLong.GWL_STYLE);
-                       exstyle = Win32GetWindowLong(handle, WindowLong.GWL_EXSTYLE);
-
-
-                       switch (border_style) {
-                               case FormBorderStyle.None: {
-                                       style &= ~(uint)WindowStyles.WS_BORDER;
-                                       exstyle &= ~(uint)WindowExStyles.WS_EX_CLIENTEDGE;
-                                       break;
-                               }
-
-                               case FormBorderStyle.FixedSingle: {
-                                       style |= (uint)WindowStyles.WS_BORDER;
-                                       exstyle &= ~(uint)WindowExStyles.WS_EX_CLIENTEDGE;
-                                       break;
-                               }
-
-                               case FormBorderStyle.Fixed3D: {
-                                       style &= ~(uint)WindowStyles.WS_BORDER;
-                                       exstyle |= (uint)WindowExStyles.WS_EX_CLIENTEDGE;
-                                       break;
-                               }
-                       }
-
-                       Win32SetWindowLong(handle, WindowLong.GWL_STYLE, style);
-                       Win32SetWindowLong(handle, WindowLong.GWL_EXSTYLE, exstyle);
-                       
-                       Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, 
-                               SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOZORDER);
+                       // Nothing to do on Win32
                }
 
                internal override void SetMenu(IntPtr handle, Menu menu) {
@@ -2072,13 +2293,12 @@ namespace System.Windows.Forms {
                        Win32SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE);
                }
 
-
                internal override Point GetMenuOrigin(IntPtr handle) {
                        return new Point(SystemInformation.FrameBorderSize.Width, SystemInformation.FrameBorderSize.Height + ThemeEngine.Current.CaptionHeight);
                }
 
                internal override void SetIcon(IntPtr hwnd, Icon icon) {
-                       Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon.Handle); // 1 = large icon (0 would be small)
+                       Win32SendMessage(hwnd, Msg.WM_SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : icon.Handle);    // 1 = large icon (0 would be small)
                }
 
                internal override void ClipboardClose(IntPtr handle) {
@@ -2283,6 +2503,117 @@ namespace System.Windows.Forms {
                        return Win32DnD.StartDrag(hwnd, data, allowedEffects);
                }
 
+               // XXX this doesn't work at all for FrameStyle.Dashed - it draws like Thick, and in the Thick case
+               // the corners are drawn incorrectly.
+               internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
+                       IntPtr          hdc;
+                       IntPtr          pen;
+                       IntPtr          oldpen;
+                       COLORREF        clrRef = new COLORREF();
+
+                       // If we want the standard hatch pattern we would
+                       // need to create a brush
+
+                       clrRef.R = backColor.R;
+                       clrRef.G = backColor.G;
+                       clrRef.B = backColor.B;
+
+                       // Grab a pen
+                       pen = Win32CreatePen (style == FrameStyle.Thick ? PenStyle.PS_SOLID : PenStyle.PS_DASH,
+                                             style == FrameStyle.Thick ? 4 : 2, ref clrRef);
+
+                       hdc = Win32GetDC(IntPtr.Zero);
+                       Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
+                       oldpen = Win32SelectObject(hdc, pen);
+
+                       Win32MoveToEx(hdc, rectangle.Left, rectangle.Top, IntPtr.Zero);
+                       if ((rectangle.Width > 0) && (rectangle.Height > 0)) {
+                               Win32LineTo(hdc, rectangle.Right, rectangle.Top);
+                               Win32LineTo(hdc, rectangle.Right, rectangle.Bottom);
+                               Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
+                               Win32LineTo(hdc, rectangle.Left, rectangle.Top);
+                       } else {
+                               if (rectangle.Width > 0) {
+                                       Win32LineTo(hdc, rectangle.Right, rectangle.Top);
+                               } else {
+                                       Win32LineTo(hdc, rectangle.Left, rectangle.Bottom);
+                               }
+                       }
+
+                       Win32SelectObject(hdc, oldpen);
+                       Win32DeleteObject(pen);
+
+                       Win32ReleaseDC(IntPtr.Zero, hdc);
+               }
+
+               internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
+                       IntPtr          hdc;
+                       IntPtr          pen;
+                       IntPtr          oldpen;
+                       POINT           pt;
+                       COLORREF        clrRef = new COLORREF();
+
+                       pt = new POINT();
+                       pt.x = 0;
+                       pt.y = 0;
+                       Win32ClientToScreen(IntPtr.Zero, ref pt);
+
+                       // If we want the standard hatch pattern we would
+                       // need to create a brush
+
+                       clrRef.R = backColor.R;
+                       clrRef.G = backColor.G;
+                       clrRef.B = backColor.B;
+
+                       // Grab a pen
+                       pen = Win32CreatePen(PenStyle.PS_SOLID, 1, ref clrRef);
+
+                       hdc = Win32GetDC(IntPtr.Zero);
+                       Win32SetROP2(hdc, ROP2DrawMode.R2_NOT);
+                       oldpen = Win32SelectObject(hdc, pen);
+
+                       Win32MoveToEx(hdc, pt.x + start.X, pt.y + start.Y, IntPtr.Zero);
+                       Win32LineTo(hdc, pt.x + end.X, pt.y + end.Y);
+
+                       Win32SelectObject(hdc, oldpen);
+                       Win32DeleteObject(pen);
+
+                       Win32ReleaseDC(IntPtr.Zero, hdc);
+               }
+
+               internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
+               {
+                       RECT    rect;
+
+                       rect = new RECT();
+                       rect.left = rectangle.Left;
+                       rect.top = rectangle.Top;
+                       rect.right = rectangle.Right;
+                       rect.bottom = rectangle.Bottom;
+
+                       IntPtr          hdc;
+                       IntPtr          brush;
+                       IntPtr          oldbrush;
+                       COLORREF        clrRef = new COLORREF();
+
+                       clrRef.R = backColor.R;
+                       clrRef.G = backColor.G;
+                       clrRef.B = backColor.B;
+
+                       // Grab a brush
+                       brush = Win32CreateSolidBrush (clrRef);
+
+                       hdc = Win32GetDC(IntPtr.Zero);
+                       oldbrush = Win32SelectObject(hdc, brush);
+
+                       Win32PatBlt (hdc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, PatBltRop.DSTINVERT);
+
+                       Win32SelectObject(hdc, oldbrush);
+                       Win32DeleteObject(brush);
+
+                       Win32ReleaseDC(IntPtr.Zero, hdc);
+               }
+
                internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
                        IntPtr          hdc;
                        IntPtr          pen;
@@ -2306,8 +2637,9 @@ namespace System.Windows.Forms {
 
                        Control c = Control.FromHandle (handle);
                        if (c != null) {
-                               Region r = new Region(new Rectangle(c.PointToScreen (c.Location), c.Size));
-
+                               RECT window_rect;
+                               Win32GetWindowRect (c.Handle, out window_rect);
+                               Region r = new Region (new Rectangle(window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top));
                                Win32ExtSelectClipRgn(hdc, r.GetHrgn (Graphics.FromHdc (hdc)), (int) ClipCombineMode.RGN_AND);
                        }
 
@@ -2353,35 +2685,99 @@ namespace System.Windows.Forms {
                        return Win32PostMessage(hwnd, message, wParam, lParam);
                }
 
+               internal override int SendInput (IntPtr hwnd, Queue keys) {
+                       INPUT[] inputs = new INPUT[keys.Count];
+                       const Int32 INPUT_KEYBOARD = 1;
+                       uint returns = 0;
+                       int i = 0;
+                       while (keys.Count > 0) {
+                               MSG msg = (MSG)keys.Dequeue();
+
+                               
+                               inputs[i].ki.wScan = 0;
+                               inputs[i].ki.time = 0;
+                               inputs[i].ki.dwFlags = (Int32)(msg.message == Msg.WM_KEYUP ? InputFlags.KEYEVENTF_KEYUP : 0);
+                               inputs[i].ki.wVk = (short)msg.wParam.ToInt32();
+                               inputs[i].type = INPUT_KEYBOARD;
+                               i++;
+                       }
+                       returns = Win32SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
+
+                       return (int) returns;
+               }
+
                internal override int KeyboardSpeed {
                        get {
-                               Console.WriteLine ("KeyboardSpeed: need to query Windows");
-
+                               int speed = 0;
+                               Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDSPEED, 0, ref speed, 0);
                                //
                                // Return values range from 0 to 31 which map to 2.5 to 30 repetitions per second.
                                //
-                               return 0;
+                               return speed;
                        }
                }
 
                internal override int KeyboardDelay {
                        get {
-                               Console.WriteLine ("KeyboardDelay: need to query Windows");
-
+                               int delay = 1;
+                               Win32SystemParametersInfo(SPIAction.SPI_GETKEYBOARDDELAY, 0, ref delay, 0);
                                //
                                // Return values must range from 0 to 4, 0 meaning 250ms,
                                // and 4 meaning 1000 ms.
                                //
-                               return 1;
+                               return delay;
                        }
                }
-               
-               internal override event EventHandler Idle;
 
-               // Santa's little helper
-               static void Where() {
-                       Console.WriteLine("Here: {0}", new StackTrace().ToString());
+               private class WinBuffer
+               {
+                       public IntPtr hdc;
+                       public IntPtr bitmap;
+
+                       public WinBuffer (IntPtr hdc, IntPtr bitmap)
+                       {
+                               this.hdc = hdc;
+                               this.bitmap = bitmap;
+                       }
+               }
+
+               internal override void CreateOffscreenDrawable (IntPtr handle, int width, int height, out object offscreen_drawable)
+               {
+                       Graphics destG = Graphics.FromHwnd (handle);
+                       IntPtr destHdc = destG.GetHdc ();
+
+                       IntPtr srcHdc = Win32CreateCompatibleDC (destHdc);
+                       IntPtr srcBmp = Win32CreateCompatibleBitmap (destHdc, width, height);
+                       Win32SelectObject (srcHdc, srcBmp);
+
+                       offscreen_drawable = new WinBuffer (srcHdc, srcBmp);
+
+                       destG.ReleaseHdc (destHdc);
                }
+
+               internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
+               {
+                       return Graphics.FromHdc (((WinBuffer)offscreen_drawable).hdc);
+               }
+
+               internal override void BlitFromOffscreen (IntPtr dest_handle, Graphics dest_dc, object offscreen_drawable, Graphics offscreen_dc, Rectangle r)
+               {
+                       WinBuffer wb = (WinBuffer)offscreen_drawable;
+
+                       IntPtr destHdc = dest_dc.GetHdc ();
+                       Win32BitBlt (destHdc, r.Left, r.Top, r.Width, r.Height, wb.hdc, r.Left, r.Top, TernaryRasterOperations.SRCCOPY);
+                       dest_dc.ReleaseHdc (destHdc);
+               }
+
+               internal override void DestroyOffscreenDrawable (object offscreen_drawable)
+               {
+                       WinBuffer wb = (WinBuffer)offscreen_drawable;
+
+                       Win32DeleteObject (wb.bitmap);
+                       Win32DeleteDC (wb.hdc);
+               }
+
+               internal override event EventHandler Idle;
                #endregion      // Public Static Methods
 
                #region Win32 Imports
@@ -2445,8 +2841,8 @@ namespace System.Windows.Forms {
                [DllImport ("user32.dll", EntryPoint="DefWindowProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
                private extern static IntPtr Win32DefWindowProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
 
-               [DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
-               private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
+               //[DllImport ("user32.dll", EntryPoint="DefDlgProcW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
+               //private extern static IntPtr Win32DefDlgProc(IntPtr hWnd, Msg Msg, IntPtr wParam, IntPtr lParam);
 
                [DllImport ("user32.dll", EntryPoint="PostQuitMessage", CallingConvention=CallingConvention.StdCall)]
                private extern static IntPtr Win32PostQuitMessage(int nExitCode);
@@ -2469,8 +2865,8 @@ namespace System.Windows.Forms {
                [DllImport ("user32.dll", EntryPoint="GetWindowDC", CallingConvention=CallingConvention.StdCall)]
                private extern static IntPtr Win32GetWindowDC(IntPtr hWnd);
 
-               [DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
-               private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
+               //[DllImport ("user32.dll", EntryPoint="GetDCEx", CallingConvention=CallingConvention.StdCall)]
+               //private extern static IntPtr Win32GetDCEx(IntPtr hWnd, IntPtr hRgn, DCExFlags flags);
 
                [DllImport ("user32.dll", EntryPoint="ReleaseDC", CallingConvention=CallingConvention.StdCall)]
                private extern static IntPtr Win32ReleaseDC(IntPtr hWnd, IntPtr hDC);
@@ -2481,8 +2877,8 @@ namespace System.Windows.Forms {
                [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
                private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
 
-               [DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
-               private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
+               //[DllImport ("user32.dll", EntryPoint="InvalidateRect", CallingConvention=CallingConvention.StdCall)]
+               //private extern static IntPtr Win32InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
 
                [DllImport ("user32.dll", EntryPoint="SetCapture", CallingConvention=CallingConvention.StdCall)]
                private extern static IntPtr Win32SetCapture(IntPtr hWnd);
@@ -2517,17 +2913,20 @@ namespace System.Windows.Forms {
                [DllImport ("user32.dll", EntryPoint="SetCursorPos", CallingConvention=CallingConvention.StdCall)]
                private extern static bool Win32SetCursorPos(int x, int y);
 
-               [DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
-               private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
+               //[DllImport ("user32.dll", EntryPoint="GetWindowPlacement", CallingConvention=CallingConvention.StdCall)]
+               //private extern static bool Win32GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
 
                [DllImport ("user32.dll", EntryPoint="TrackMouseEvent", CallingConvention=CallingConvention.StdCall)]
                private extern static bool Win32TrackMouseEvent(ref TRACKMOUSEEVENT tme);
 
-               [DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
-               private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
+               //[DllImport ("gdi32.dll", EntryPoint="CreateBrushIndirect", CallingConvention=CallingConvention.StdCall)]
+               //private extern static IntPtr Win32CreateBrushIndirect(ref LOGBRUSH lb);
 
-               [DllImport ("user32.dll", EntryPoint="FillRect", CallingConvention=CallingConvention.StdCall)]
-               private extern static int Win32FillRect(IntPtr hdc, ref RECT rect, IntPtr hbr);
+               [DllImport ("gdi32.dll", EntryPoint="CreateSolidBrush", CallingConvention=CallingConvention.StdCall)]
+               private extern static IntPtr Win32CreateSolidBrush(COLORREF clrRef);
+
+               [DllImport ("gdi32.dll", EntryPoint="PatBlt", CallingConvention=CallingConvention.StdCall)]
+               private extern static int Win32PatBlt(IntPtr hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, PatBltRop dwRop);
 
                [DllImport ("user32.dll", EntryPoint="SetWindowLong", CallingConvention=CallingConvention.StdCall)]
                private extern static uint Win32SetWindowLong(IntPtr hwnd, WindowLong index, uint value);
@@ -2538,6 +2937,9 @@ namespace System.Windows.Forms {
                [DllImport ("user32.dll", EntryPoint="SetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
                private extern static uint Win32SetLayeredWindowAttributes (IntPtr hwnd, COLORREF crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
 
+               [DllImport ("user32.dll", EntryPoint="GetLayeredWindowAttributes", CallingConvention=CallingConvention.StdCall)]
+               private extern static uint Win32GetLayeredWindowAttributes (IntPtr hwnd, out COLORREF pcrKey, out byte pbAlpha, out LayeredWindowAttributes pwdFlags);
+
                [DllImport ("gdi32.dll", EntryPoint="DeleteObject", CallingConvention=CallingConvention.StdCall)]
                private extern static bool Win32DeleteObject(IntPtr o);
 
@@ -2580,8 +2982,8 @@ namespace System.Windows.Forms {
                [DllImport ("user32.dll", EntryPoint="SetCaretPos", CallingConvention=CallingConvention.StdCall)]
                private  extern static bool Win32SetCaretPos(int X, int Y);
 
-               [DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
-               private  extern static uint Win32GetCaretBlinkTime();
+               //[DllImport ("user32.dll", EntryPoint="GetCaretBlinkTime", CallingConvention=CallingConvention.StdCall)]
+               //private  extern static uint Win32GetCaretBlinkTime();
 
                [DllImport ("gdi32.dll", EntryPoint="GetTextMetricsW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
                internal extern static bool Win32GetTextMetrics(IntPtr hdc, ref TEXTMETRIC tm);
@@ -2589,23 +2991,23 @@ namespace System.Windows.Forms {
                [DllImport ("gdi32.dll", EntryPoint="SelectObject", CallingConvention=CallingConvention.StdCall)]
                internal extern static IntPtr Win32SelectObject(IntPtr hdc, IntPtr hgdiobject);
 
-               [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
-               private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
+               //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
+               //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
 
-               [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
-               private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
+               //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
+               //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
 
-               [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
-               private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
+               //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
+               //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, out RECT prcUpdate, ScrollWindowExFlags flags);
 
                [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
                private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
 
-               [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
-               private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
+               //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
+               //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
 
-               [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
-               private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
+               //[DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
+               //private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, ref RECT prcScroll, ref RECT prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
 
                [DllImport ("user32.dll", EntryPoint="ScrollWindowEx", CallingConvention=CallingConvention.StdCall)]
                private extern static bool Win32ScrollWindowEx(IntPtr hwnd, int dx, int dy, IntPtr prcScroll, IntPtr prcClip, IntPtr hrgnUpdate, IntPtr prcUpdate, ScrollWindowExFlags flags);
@@ -2628,8 +3030,8 @@ namespace System.Windows.Forms {
                [DllImport ("user32.dll", EntryPoint="IsWindowVisible", CallingConvention=CallingConvention.StdCall)]
                private extern static bool IsWindowVisible(IntPtr hwnd);
 
-               [DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
-               private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
+               //[DllImport ("user32.dll", EntryPoint="SetClassLong", CallingConvention=CallingConvention.StdCall)]
+               //private extern static bool Win32SetClassLong(IntPtr hwnd, ClassLong nIndex, IntPtr dwNewLong);
 
                [DllImport ("user32.dll", EntryPoint="SendMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
                private extern static IntPtr Win32SendMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
@@ -2637,11 +3039,14 @@ namespace System.Windows.Forms {
                [DllImport ("user32.dll", EntryPoint="PostMessageW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
                private extern static bool Win32PostMessage(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
 
-               [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
-               private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
+               [DllImport ("user32.dll", EntryPoint="SendInput", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
+               private extern static UInt32 Win32SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray)] INPUT[] inputs, Int32 cbSize);
 
                [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
-               private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
+               private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref RECT rect, uint fWinIni);
+               
+               //[DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
+               //private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref uint value, uint fWinIni);
 
                [DllImport ("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
                private extern static bool Win32SystemParametersInfo(SPIAction uiAction, uint uiParam, ref int value, uint fWinIni);
@@ -2726,6 +3131,25 @@ namespace System.Windows.Forms {
 
                [DllImport ("user32.dll", EntryPoint="GetWindowRgn", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode)]
                internal extern static IntPtr Win32GetWindowRgn(IntPtr hWnd, IntPtr hRgn);
+
+               [DllImport ("user32.dll", EntryPoint="ClipCursor", CallingConvention=CallingConvention.StdCall)]
+               internal extern static bool Win32ClipCursor (ref RECT lpRect);
+
+               [DllImport ("user32.dll", EntryPoint="GetClipCursor", CallingConvention=CallingConvention.StdCall)]
+               internal extern static bool Win32GetClipCursor (out RECT lpRect);
+
+               [DllImport ("gdi32.dll", EntryPoint="BitBlt", CallingConvention=CallingConvention.StdCall)]
+               internal static extern bool Win32BitBlt (IntPtr hObject, int nXDest, int nYDest, int nWidth,
+                  int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
+
+               [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
+               internal static extern IntPtr Win32CreateCompatibleDC (IntPtr hdc);
+
+               [DllImport ("gdi32.dll", EntryPoint="DeleteDC", CallingConvention=CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
+               internal static extern bool Win32DeleteDC (IntPtr hdc);
+
+               [DllImport ("gdi32.dll", EntryPoint="CreateCompatibleBitmap", CallingConvention=CallingConvention.StdCall)]
+               internal static extern IntPtr Win32CreateCompatibleBitmap (IntPtr hdc, int nWidth, int nHeight);
                #endregion
        }
 }