コード例 #1
0
        /// <summary>
        /// Gets the screen-relative point for a point within the coordinate
        /// space of the specified element.
        /// </summary>
        /// <param name='element'>Element relative to which the point is specified.</param>
        /// <param name='point'>Point to translate.</param>
        /// <returns>The point in screen-relative coordinates.</returns>
        public static LHPoint GetScreenRelativePoint(UIElement element, LHPoint point)
        {
            PresentationSource source;

            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            LHPoint result = GetClientRelativePoint(element, point);

            source = PresentationSource.FromVisual(element);

            if (source == null)
            {
                throw new InvalidOperationException("element is not connected to visual tree");
            }

            // Offset from client area origin.
            Microsoft.Test.Win32.NativeStructs.POINT p = new Microsoft.Test.Win32.NativeStructs.POINT(0, 0);
            NativeMethods.ClientToScreen(((HwndSource)source).Handle, ref p);

            // Adjust to High DPI.
            float hFactor, vFactor;

            HighDpiScaleFactors(out hFactor, out vFactor);

            result.X = (int)Math.Round(result.X * hFactor) + p.x;
            result.Y = (int)Math.Round(result.Y * vFactor) + p.y;
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Gets the rectangle that bounds the specified element, relative
        /// to the top-left corner of the screen.
        /// </summary>
        /// <param name='element'>Element to get rectangle for.</param>
        /// <returns>The rectangle that bounds the element.</returns>
        public static Rect GetScreenRelativeRect(UIElement element)
        {
            Microsoft.Test.Win32.NativeStructs.POINT topLeft;
            Rect clientRect;
            PresentationSource source;

            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            source = PresentationSource.FromVisual(element);
            if (source == null)
            {
                throw new InvalidOperationException("Element is not connected to visual tree");
            }

            clientRect = GetClientRelativeRect(element);

            // Adjust to High DPI.
            float hFactor, vFactor;

            HighDpiScaleFactors(out hFactor, out vFactor);

            topLeft = new Microsoft.Test.Win32.NativeStructs.POINT((int)Math.Round(clientRect.Left * hFactor), (int)Math.Round(clientRect.Top * vFactor));
            NativeMethods.ClientToScreen(((HwndSource)source).Handle, ref topLeft);

            return(new Rect(topLeft.x, topLeft.y, clientRect.Width * vFactor, clientRect.Height * vFactor));
        }
コード例 #3
0
ファイル: BitmapCapture.cs プロジェクト: dotnet/wpf-test
        private static Bitmap CreateBitmapFromWindowHandle(IntPtr windowHandle,
                                                           Microsoft.Test.Win32.NativeStructs.RECT r)
        {
            WaitForCompleteRender();
            //System.Diagnostics.Trace.WriteLine(String.Format(
            //    "Capturing bitmap for window in rectangle [{0};{1} - {2};{3}]",
            //    r.left, r.top, r.right, r.bottom));
            GlobalLog.LogStatus(String.Format(
                                    "Capturing bitmap for window in rectangle [{0};{1} - {2};{3}]",
                                    r.left, r.top, r.right, r.bottom));
            new SecurityPermission(PermissionState.Unrestricted).Assert();
            int cx = r.Width;
            int cy = r.Height;

            if (cx <= 0)
            {
                throw new Exception("Window to create bitmap for has <= 0 width.");
            }
            if (cy <= 0)
            {
                throw new Exception("Window to create bitmap for has <= 0 height.");
            }

            IntPtr sourceDC = NativeMethods.GetDC(new HandleRef(new object(), (IntPtr)null));

            Microsoft.Test.Win32.NativeStructs.POINT topLeft = new Microsoft.Test.Win32.NativeStructs.POINT(0, 0); //Get the screen co-ordinates of the client origin.
            NativeMethods.ClientToScreen(windowHandle, ref topLeft);

            if ((int)sourceDC == 0)
            {
                string message = String.Format(
                    "Unable to retrieve device context for window handle {0}.",
                    (int)windowHandle);
                throw new Exception(message);
            }
            try
            {
                Bitmap bitmap = new Bitmap(cx, cy,
                                           System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                Graphics graphics    = Graphics.FromImage(bitmap);
                IntPtr   destination = graphics.GetHdc();

                const int SRCCOPY = 0x00CC0020;
                NativeMethods.SafeBitBlt(destination, 0, 0, cx, cy, sourceDC, topLeft.x, topLeft.y,
                                         SRCCOPY);
                graphics.ReleaseHdc(destination);
                graphics.Dispose();
                return(bitmap);
            }
            finally
            {
//                Win32.ReleaseDC(windowHandle, sourceDC);
                NativeMethods.ReleaseDC(new HandleRef(new object(), windowHandle), new HandleRef(new object(), sourceDC));
            }
        }