Пример #1
0
        //dirtyRect是绝对坐标(在多屏的情况下)
        private void SetHBitmap(IntPtr hBitmap, Rectangle newWindowBounds, Point drawAt, Rectangle dirtyRect, byte opacity)
        {
            // IntPtr screenDc = Win32.GDI32.GetDC(IntPtr.Zero);

            IntPtr memDc     = NativeMethods.CreateCompatibleDC(IntPtr.Zero);
            IntPtr oldBitmap = IntPtr.Zero;

            try
            {
                oldBitmap = NativeMethods.SelectObject(memDc, hBitmap);

                var winSize = new NativeMethods.Size(newWindowBounds.Width, newWindowBounds.Height);
                var winPos  = new NativeMethods.Point(newWindowBounds.X, newWindowBounds.Y);

                var drawBmpAt = new NativeMethods.Point(drawAt.X, drawAt.Y);
                var blend     = new NativeMethods.BLENDFUNCTION {
                    BlendOp = AC_SRC_OVER, BlendFlags = 0, SourceConstantAlpha = opacity, AlphaFormat = AC_SRC_ALPHA
                };

                var updateInfo = new NativeMethods.UPDATELAYEREDWINDOWINFO
                {
                    cbSize  = (uint)Marshal.SizeOf(typeof(NativeMethods.UPDATELAYEREDWINDOWINFO)),
                    dwFlags = ULW_ALPHA,
                    hdcDst  = IntPtr.Zero,
                    hdcSrc  = memDc
                };
                //NativeMethods.GetDC(IntPtr.Zero);//IntPtr.Zero; //ScreenDC

                // dirtyRect.X -= _bounds.X;
                // dirtyRect.Y -= _bounds.Y;

                //dirtyRect.Offset(-_bounds.X, -_bounds.Y);
                var dirRect = new NativeMethods.RECT(dirtyRect.X, dirtyRect.Y, dirtyRect.Right, dirtyRect.Bottom);

                unsafe
                {
                    updateInfo.pblend   = &blend;
                    updateInfo.pptDst   = &winPos;
                    updateInfo.psize    = &winSize;
                    updateInfo.pptSrc   = &drawBmpAt;
                    updateInfo.prcDirty = &dirRect;
                }

                NativeMethods.UpdateLayeredWindowIndirect(Handle, ref updateInfo);
                // Debug.Assert(NativeMethods.GetLastError() == 0);

                //NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, GDI32.ULW_ALPHA);
            }
            finally
            {
                //GDI32.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    NativeMethods.SelectObject(memDc, oldBitmap);
                    //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak.
                    //Win32.DeleteObject(hBitmap);
                }
                NativeMethods.DeleteDC(memDc);
            }
        }
Пример #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hWnd">被覆盖控件句柄。</param>
        internal void CreateParamsInternal(IntPtr hWnd)
        {
            IntPtr hParent = NativeMethods.GetParent(hWnd);
            RECT rect = new RECT();
            NativeMethods.Point point = new NativeMethods.Point();

            NativeMethods.GetWindowRect(hWnd, ref rect);
            point.x = rect.Left;
            point.y = rect.Top;
            NativeMethods.ScreenToClient(hParent, ref point);

            int dwStyle =
                SS.SS_OWNERDRAW |
                WS.WS_CHILD |
                WS.WS_CLIPSIBLINGS |
                WS.WS_OVERLAPPED | WS.WS_VISIBLE;
            int dwExStyle = WS_EX.WS_EX_TOPMOST | WS_EX.WS_EX_TOOLWINDOW;
            _createParams = new CreateParams();

            _createParams.Parent = hParent;
            _createParams.ClassName = ClassName.STATIC;
            _createParams.Caption = null;
            _createParams.Style = dwStyle;
            _createParams.ExStyle = dwExStyle;
            _createParams.X = point.x;
            _createParams.Y = point.y;
            _createParams.Width = rect.Right - rect.Left;
            _createParams.Height = rect.Bottom - rect.Top;
        }
Пример #3
0
        private void FoundAndDrawWindowRect()
        {
            NativeMethods.Point lpPoint = new NativeMethods.Point();
            lpPoint.x = Control.MousePosition.X;
            lpPoint.y = Control.MousePosition.Y;
            IntPtr num = NativeMethods.ChildWindowFromPointEx(NativeMethods.GetDesktopWindow(), lpPoint, 3U);

            if (!(num != IntPtr.Zero))
            {
                return;
            }
            IntPtr hWnd = num;

            while (true)
            {
                NativeMethods.ScreenToClient(hWnd, ref lpPoint);
                hWnd = NativeMethods.ChildWindowFromPointEx(num, lpPoint, 1U);
                if (!(hWnd == IntPtr.Zero) && !(hWnd == num))
                {
                    num       = hWnd;
                    lpPoint.x = Control.MousePosition.X;
                    lpPoint.y = Control.MousePosition.Y;
                }
                else
                {
                    break;
                }
            }
            RECT lpRect = new RECT();

            NativeMethods.GetWindowRect(num, ref lpRect);
            this.imageProcessBox1.SetSelectRect(new Rectangle(lpRect.Left, lpRect.Top, lpRect.Right - lpRect.Left, lpRect.Bottom - lpRect.Top));
        }
Пример #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hWnd">被覆盖控件句柄。</param>
        internal void CheckBounds(IntPtr hWnd)
        {
            RECT controlRect = new RECT();
            RECT maskRect = new RECT();

            NativeMethods.GetWindowRect(base.Handle, ref maskRect);
            NativeMethods.GetWindowRect(hWnd, ref controlRect);

            uint uFlag =
                SWP.SWP_NOACTIVATE |
                SWP.SWP_NOOWNERZORDER |
                SWP.SWP_NOZORDER;

            if (!NativeMethods.EqualRect(ref controlRect, ref maskRect))
            {
                NativeMethods.Point point = new NativeMethods.Point(controlRect.Left, controlRect.Top);
                IntPtr hParent = NativeMethods.GetParent(base.Handle);
                NativeMethods.ScreenToClient(hParent, ref point);

                NativeMethods.SetWindowPos(
                    base.Handle,
                    IntPtr.Zero,
                    point.x,
                    point.y,
                    controlRect.Right - controlRect.Left,
                    controlRect.Bottom - controlRect.Top,
                    uFlag);
            }
        }
Пример #5
0
            private void DrawUpDownButton()
            {
                bool mouseOver       = false;
                bool mousePress      = LeftKeyPressed();
                bool mouseInUpButton = false;

                Rectangle clipRect = _upDownButton.ClientRectangle;

                RECT windowRect = new RECT();

                NativeMethods.Point cursorPoint = new NativeMethods.Point();
                NativeMethods.GetCursorPos(ref cursorPoint);
                NativeMethods.GetWindowRect(_upDownButtonWnd, ref windowRect);

                mouseOver = NativeMethods.PtInRect(ref windowRect, cursorPoint);

                cursorPoint.x -= windowRect.Left;
                cursorPoint.y -= windowRect.Top;

                mouseInUpButton = cursorPoint.y < clipRect.Height / 2;

                using (Graphics g = Graphics.FromHwnd(_upDownButtonWnd))
                {
                    UpDownButtonPaintEventArgs e =
                        new UpDownButtonPaintEventArgs(
                            g,
                            clipRect,
                            mouseOver,
                            mousePress,
                            mouseInUpButton);
                    _owner.OnPaintUpDownButton(e);
                }
            }
Пример #6
0
            private void ShowContextMenu()
            {
                if (ContextMenu == null && ContextMenuStrip == null)
                {
                    return;
                }

                NativeMethods.Point p = new NativeMethods.Point();
                NativeMethods.GetCursorPos(ref p);
                NativeMethods.SetForegroundWindow(new HandleRef(_window, _window.Handle));

                if (ContextMenu != null)
                {
                    ContextMenu.GetType().InvokeMember("OnPopup",
                                                       BindingFlags.NonPublic | BindingFlags.InvokeMethod |
                                                       BindingFlags.Instance, null, ContextMenu,
                                                       new object[] { EventArgs.Empty });

                    NativeMethods.TrackPopupMenuEx(new HandleRef(ContextMenu, ContextMenu.Handle), 72, p.X, p.Y, new HandleRef(_window, _window.Handle), IntPtr.Zero);
                    NativeMethods.PostMessage(new HandleRef(_window, _window.Handle), WM_NULL, 0, 0);
                    return;
                }

                ContextMenuStrip?.GetType().InvokeMember("ShowInTaskbar",
                                                         BindingFlags.NonPublic | BindingFlags.InvokeMethod |
                                                         BindingFlags.Instance, null, ContextMenuStrip, new object[] { p.X, p.Y });
            }
Пример #7
0
        public void SetBits()
        {
            //绘制绘图层背景
            Bitmap bitmap = new Bitmap(Main.Width + Main.ShadowWidth * 2, Main.Height + Main.ShadowWidth * 2);
            //Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20);//窗体光泽重绘边界
            Graphics g = Graphics.FromImage(bitmap);

            g.SmoothingMode   = SmoothingMode.HighQuality;   //高质量
            g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量

            DrawShadow(g);
            //ImageDrawRect.DrawRect(g, Properties.Resources.main_light_bkg_top123, ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1);

            if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
            {
                throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
            }
            IntPtr oldBits  = IntPtr.Zero;
            IntPtr screenDC = NativeMethods.GetDC(IntPtr.Zero);
            IntPtr hBitmap  = IntPtr.Zero;
            IntPtr memDc    = NativeMethods.CreateCompatibleDC(screenDC);

            try
            {
                NativeMethods.Point         topLoc     = new NativeMethods.Point(Left, Top);
                NativeMethods.Size          bitMapSize = new NativeMethods.Size(Width, Height);
                NativeMethods.BLENDFUNCTION blendFunc  = new NativeMethods.BLENDFUNCTION();
                NativeMethods.Point         srcLoc     = new NativeMethods.Point(0, 0);

                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                oldBits = NativeMethods.SelectObject(memDc, hBitmap);

                blendFunc.BlendOp             = AC.AC_SRC_OVER;
                blendFunc.SourceConstantAlpha = Byte.Parse("255");
                blendFunc.AlphaFormat         = AC.AC_SRC_ALPHA;
                blendFunc.BlendFlags          = 0;

                NativeMethods.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, NativeMethods.ULW_ALPHA);
            }
            finally
            {
                if (hBitmap != IntPtr.Zero)
                {
                    NativeMethods.SelectObject(memDc, oldBits);
                    NativeMethods.DeleteObject(hBitmap);
                }
                NativeMethods.ReleaseDC(IntPtr.Zero, screenDC);
                NativeMethods.DeleteDC(memDc);
            }
        }
Пример #8
0
            private void ShowContextMenu()
            {
                if (ContextMenuStrip == null)
                {
                    return;
                }

                NativeMethods.Point p = new NativeMethods.Point();
                NativeMethods.GetCursorPos(ref p);
                NativeMethods.SetForegroundWindow(new HandleRef(_window, _window.Handle));

                ContextMenuStrip?.GetType().InvokeMember("ShowInTaskbar",
                                                         BindingFlags.NonPublic | BindingFlags.InvokeMethod |
                                                         BindingFlags.Instance, null, ContextMenuStrip, new object[] { p.X, p.Y });
            }
Пример #9
0
        public void SetBitmap(Bitmap bitmap, byte opacity = 255)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
            }

            // The ideia of this is very simple,
            // 1. Create a compatible DC with screen;
            // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
            // 3. Call the UpdateLayeredWindow.

            IntPtr screenDc  = NativeMethods.GetDC(IntPtr.Zero);
            IntPtr memDc     = NativeMethods.CreateCompatibleDC(screenDc);
            IntPtr hBitmap   = IntPtr.Zero;
            IntPtr oldBitmap = IntPtr.Zero;

            try
            {
                hBitmap   = bitmap.GetHbitmap(Color.FromArgb(0));               // grab a GDI handle from this GDI+ bitmap
                oldBitmap = NativeMethods.SelectObject(memDc, hBitmap);

                var size        = new NativeMethods.Size(bitmap.Width, bitmap.Height);
                var pointSource = new NativeMethods.Point(0, 0);
                var topPos      = new NativeMethods.Point(Left, Top);
                var blend       = new NativeMethods.BLENDFUNCTION();
                blend.BlendOp             = NativeMethods.AC_SRC_OVER;
                blend.BlendFlags          = 0;
                blend.SourceConstantAlpha = opacity;
                blend.AlphaFormat         = NativeMethods.AC_SRC_ALPHA;

                NativeMethods.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend,
                                                  NativeMethods.ULW_ALPHA);
            }
            finally
            {
                NativeMethods.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    NativeMethods.SelectObject(memDc, oldBitmap);
                    //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak.
                    NativeMethods.DeleteObject(hBitmap);
                }
                NativeMethods.DeleteDC(memDc);
            }
        }
Пример #10
0
        public void SetBits()
        {
            if (BackgroundImage != null)
            {
                //绘制绘图层背景
                Bitmap bitmap = new Bitmap(BackgroundImage, base.Width, base.Height);
                if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
                {
                    throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
                }
                IntPtr oldBits  = IntPtr.Zero;
                IntPtr screenDC = NativeMethods.GetDC(IntPtr.Zero);
                IntPtr hBitmap  = IntPtr.Zero;
                IntPtr memDc    = NativeMethods.CreateCompatibleDC(screenDC);

                try
                {
                    NativeMethods.Point         topLoc     = new NativeMethods.Point(Left, Top);
                    NativeMethods.Size          bitMapSize = new NativeMethods.Size(Width, Height);
                    NativeMethods.BLENDFUNCTION blendFunc  = new NativeMethods.BLENDFUNCTION();
                    NativeMethods.Point         srcLoc     = new NativeMethods.Point(0, 0);

                    hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                    oldBits = NativeMethods.SelectObject(memDc, hBitmap);

                    blendFunc.BlendOp             = AC.AC_SRC_OVER;
                    blendFunc.SourceConstantAlpha = Byte.Parse("255");
                    blendFunc.AlphaFormat         = AC.AC_SRC_ALPHA;
                    blendFunc.BlendFlags          = 0;

                    NativeMethods.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, NativeMethods.ULW_ALPHA);
                }
                finally
                {
                    if (hBitmap != IntPtr.Zero)
                    {
                        NativeMethods.SelectObject(memDc, oldBits);
                        NativeMethods.DeleteObject(hBitmap);
                    }
                    NativeMethods.ReleaseDC(IntPtr.Zero, screenDC);
                    NativeMethods.DeleteDC(memDc);
                }
            }
        }
Пример #11
0
        public void SetBits()
        {
            Bitmap   image = new Bitmap(this.Main.Width + (this.Main.ShadowWidth * 2), this.Main.Height + (this.Main.ShadowWidth * 2));
            Graphics g     = Graphics.FromImage(image);

            g.SmoothingMode   = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            this.DrawShadow(g);
            if (Image.IsCanonicalPixelFormat(image.PixelFormat) && Image.IsAlphaPixelFormat(image.PixelFormat))
            {
                IntPtr zero    = IntPtr.Zero;
                IntPtr dC      = NativeMethods.GetDC(IntPtr.Zero);
                IntPtr hgdiobj = IntPtr.Zero;
                IntPtr hdc     = NativeMethods.CreateCompatibleDC(dC);
                try
                {
                    NativeMethods.Point         pptDst = new NativeMethods.Point(base.Left, base.Top);
                    NativeMethods.Size          psize  = new NativeMethods.Size(base.Width, base.Height);
                    NativeMethods.BLENDFUNCTION pblend = new NativeMethods.BLENDFUNCTION();
                    NativeMethods.Point         pprSrc = new NativeMethods.Point(0, 0);
                    hgdiobj                    = image.GetHbitmap(Color.FromArgb(0));
                    zero                       = NativeMethods.SelectObject(hdc, hgdiobj);
                    pblend.BlendOp             = 0;
                    pblend.SourceConstantAlpha = byte.Parse("255");
                    pblend.AlphaFormat         = 1;
                    pblend.BlendFlags          = 0;
                    NativeMethods.UpdateLayeredWindow(base.Handle, dC, ref pptDst, ref psize, hdc, ref pprSrc, 0, ref pblend, 2);
                    return;
                }
                finally
                {
                    if (hgdiobj != IntPtr.Zero)
                    {
                        NativeMethods.SelectObject(hdc, zero);
                        NativeMethods.DeleteObject(hgdiobj);
                    }
                    NativeMethods.ReleaseDC(IntPtr.Zero, dC);
                    NativeMethods.DeleteDC(hdc);
                }
            }
            throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
        }
Пример #12
0
        private ScrollBarHistTest ScrollBarHitTest(IntPtr hWnd)
        {
            NativeMethods.Point point = new NativeMethods.Point();
            RECT  rect       = new RECT();
            Point thumbPoint = GetScrollBarThumb();

            int arrowCx = ArrowCx;
            int arrowCy = ArrowCy;

            NativeMethods.GetWindowRect(hWnd, ref rect);
            NativeMethods.OffsetRect(ref rect, -rect.Left, -rect.Top);

            RECT tp = rect;

            NativeMethods.GetCursorPos(ref point);
            NativeMethods.ScreenToClient(hWnd, ref point);

            if (Direction == Orientation.Horizontal)
            {
                if (NativeMethods.PtInRect(ref rect, point))
                {
                    // left arrow
                    tp.Right = arrowCx;
                    if (NativeMethods.PtInRect(ref tp, point))
                    {
                        return(ScrollBarHistTest.LeftArrow);
                    }
                    // right arrow
                    tp.Left  = rect.Right - arrowCx;
                    tp.Right = rect.Right;
                    if (NativeMethods.PtInRect(ref tp, point))
                    {
                        return(ScrollBarHistTest.RightArrow);
                    }

                    // button
                    if (_owner.RightToLeft == RightToLeft.Yes)
                    {
                        tp.Left  = point.y;
                        tp.Right = point.x;
                    }
                    else
                    {
                        tp.Left  = thumbPoint.X;
                        tp.Right = thumbPoint.Y;
                    }
                    if (NativeMethods.PtInRect(ref tp, point))
                    {
                        return(ScrollBarHistTest.Thumb);
                    }
                    // track
                    return(ScrollBarHistTest.Track);
                }
            }
            else
            {
                if (NativeMethods.PtInRect(ref rect, point))
                {
                    // top arrow
                    tp.Bottom = arrowCy;
                    if (NativeMethods.PtInRect(ref tp, point))
                    {
                        return(ScrollBarHistTest.TopArrow);
                    }
                    // bottom arrow
                    tp.Top    = rect.Bottom - arrowCy;
                    tp.Bottom = rect.Bottom;
                    if (NativeMethods.PtInRect(ref tp, point))
                    {
                        return(ScrollBarHistTest.BottomArrow);
                    }
                    // button
                    tp.Top    = thumbPoint.X;
                    tp.Bottom = thumbPoint.Y;
                    if (NativeMethods.PtInRect(ref tp, point))
                    {
                        return(ScrollBarHistTest.Thumb);
                    }
                    // track
                    return(ScrollBarHistTest.Track);
                }
            }
            return(ScrollBarHistTest.None);
        }
Пример #13
0
        public void SetBits()
        {
            if (shadow_rect.Width > 0 && shadow_rect.Height > 0)
            {
                using (Bitmap bitmap = new Bitmap(shadow_rect.Width, shadow_rect.Height))
                {
                    using (Graphics g = Graphics.FromImage(bitmap))
                    {
                        g.SmoothingMode      = SmoothingMode.AntiAlias;
                        g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                        g.CompositingQuality = CompositingQuality.HighQuality;
                        g.PixelOffsetMode    = PixelOffsetMode.HighQuality; //高像素偏移质量

                        using (GraphicsPath path = rect.CreateRoundedRectanglePath(Main.Radius, Main.RoundStyle))
                        {
                            g.FillPath(Main.ShadowColor, path);
                            bitmap.StackBlur(Main.ShadowWidth);

                            g.CompositingMode = CompositingMode.SourceCopy;
                            g.FillPath(TransparentsolidBrush, path);
                        }
                    }


                    //绘制绘图层背景
                    //if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
                    //    throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
                    IntPtr oldBits  = IntPtr.Zero;
                    IntPtr screenDC = NativeMethods.GetDC(IntPtr.Zero);
                    IntPtr hBitmap  = IntPtr.Zero;
                    IntPtr memDc    = NativeMethods.CreateCompatibleDC(screenDC);

                    try
                    {
                        NativeMethods.Point         topLoc     = new NativeMethods.Point(Left, Top);
                        NativeMethods.Size          bitMapSize = new NativeMethods.Size(Width, Height);
                        NativeMethods.BLENDFUNCTION blendFunc  = new NativeMethods.BLENDFUNCTION();
                        NativeMethods.Point         srcLoc     = new NativeMethods.Point(0, 0);

                        hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                        oldBits = NativeMethods.SelectObject(memDc, hBitmap);

                        blendFunc.BlendOp             = AC.AC_SRC_OVER;
                        blendFunc.SourceConstantAlpha = Byte.Parse("255");
                        blendFunc.AlphaFormat         = AC.AC_SRC_ALPHA;
                        blendFunc.BlendFlags          = 0;

                        NativeMethods.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, NativeMethods.ULW_ALPHA);
                    }
                    finally
                    {
                        if (hBitmap != IntPtr.Zero)
                        {
                            NativeMethods.SelectObject(memDc, oldBits);
                            NativeMethods.DeleteObject(hBitmap);
                        }
                        NativeMethods.ReleaseDC(IntPtr.Zero, screenDC);
                        NativeMethods.DeleteDC(memDc);
                    }
                }
            }
        }
Пример #14
0
      private void ShowContextMenu() {
        if (ContextMenu == null && ContextMenuStrip == null)
          return;

        NativeMethods.Point p = new NativeMethods.Point();
        NativeMethods.GetCursorPos(ref p);
        NativeMethods.SetForegroundWindow(
          new HandleRef(window, window.Handle));

        if (ContextMenu != null) {
          ContextMenu.GetType().InvokeMember("OnPopup",
            BindingFlags.NonPublic | BindingFlags.InvokeMethod |
            BindingFlags.Instance, null, ContextMenu,
            new Object[] { System.EventArgs.Empty });

          NativeMethods.TrackPopupMenuEx(
            new HandleRef(ContextMenu, ContextMenu.Handle), 72,
            p.x, p.y, new HandleRef(window, window.Handle),
            IntPtr.Zero);

          NativeMethods.PostMessage(
            new HandleRef(window, window.Handle), WM_NULL, 0, 0);
          return;
        }

        if (ContextMenuStrip != null)
          ContextMenuStrip.GetType().InvokeMember("ShowInTaskbar",
            BindingFlags.NonPublic | BindingFlags.InvokeMethod |
            BindingFlags.Instance, null, ContextMenuStrip,
            new Object[] { p.x, p.y });
      }