/// <overloads>
        /// Initializes a new instance of the <see cref="WpfDrawingSettings"/> class.
        /// </overloads>
        /// <summary>
        /// Initializes a new instance of the <see cref="WpfDrawingSettings"/> class
        /// with the default parameters and settings.
        /// </summary>
        public WpfDrawingSettings()
        {
            _defaultFontName = "Arial";
            _textAsGeometry  = false;
            _optimizePath    = true;
            _includeRuntime  = true;
            _neutralCulture  = CultureInfo.GetCultureInfo("en-us");
            _culture         = CultureInfo.GetCultureInfo("en-us");

            _pixelWidth  = -1;
            _pixelHeight = -1;

            _ensureViewboxSize     = false;
            _ensureViewboxPosition = true;
            _ignoreRootViewbox     = false;
            _wpfVisitors           = new WpfVisitors();
            _properties            = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            _fontSynch       = new object();
            _fontLocations   = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            _fontFamilyNames = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            _fontFamilyMap   = new Dictionary <string, IList <FontFamily> >(StringComparer.OrdinalIgnoreCase);
            _cssVariables    = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            _dpiScale = DpiUtilities.GetSystemScale();
        }
        public ImageCaptureWindow(System.Drawing.Rectangle rect, AreaSelection areaSelection)
        {
            InitializeComponent();

            MouseUp   += OnMouseUp;
            MouseDown += OnMouseDown;
            MouseMove += OnMouseMove;
            Loaded    += OnMainWindowLoaded;
            _scalor    = DpiUtilities.GetVirtualPixelScale(this);

            _areaSelection = areaSelection;
            var monitors = _areaSelection.GetMonitorsInformation();

            _screenShotImage = new ScreenshotImage();

            _screenScalor = DpiUtilities.GetScreenScalingFactor();
            _screenShotImage.SnapShot(rect, _screenScalor);

            WindowStartupLocation = WindowStartupLocation.Manual;

            SourceInitialized += (sender, e) =>
            {
                IntPtr hWnd = new WindowInteropHelper(this).Handle;
                NativeMethods.SetWindowPos(hWnd, (IntPtr)NativeMethods.SetWindowPosInsertAfter.HWND_TOP, rect.Left, rect.Top, rect.Width, rect.Height, 0);
            };

            var bmDesktopSource = ScreenCapture.GetBitmapSource(_screenShotImage.ScreenSnapshotImage);

            BackgroundImage.Fill = new ImageBrush(bmDesktopSource);
            // MagnifierBackgroundImage.Source = bmDesktopSource;
        }
Beispiel #3
0
        public MonitorInformation GetMonitorInformation(IntPtr hMonitor)
        {
            if (_monitorInfoCache.ContainsKey(hMonitor))
            {
                return(_monitorInfoCache[hMonitor]);
            }

            if (hMonitor == IntPtr.Zero)
            {
                // Get handle to the default monitor
                const int MONITOR_DEFAULTTOPRIMARY = 0x00000001;
                hMonitor = NativeMethods.MonitorFromWindow(IntPtr.Zero, MONITOR_DEFAULTTOPRIMARY);
                if (_monitorInfoCache.ContainsKey(hMonitor))
                {
                    _monitorInfoCache[IntPtr.Zero] = _monitorInfoCache[hMonitor];
                    return(_monitorInfoCache[hMonitor]);
                }
            }

            var monitorInfo = NativeMethods.MONITORINFOEX.New();

            if (!NativeMethods.GetMonitorInfoEx(hMonitor, ref monitorInfo))
            {
                _monitorInfoCache[hMonitor] = null;
                return(null);
            }

            System.Diagnostics.Trace.WriteLine("Monitor:\"" + monitorInfo.deviceName + "\" Handle:" + hMonitor
                                               + " Left:" + monitorInfo.rcMonitor.left + " Top:" + monitorInfo.rcMonitor.top
                                               + " Right:" + monitorInfo.rcMonitor.right + " Bottom:" + monitorInfo.rcMonitor.bottom);

            UInt32 effectiveDPIx, effectiveDPIy;

            //GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_Effective_DPI, out effectiveDPIx, out effectiveDPIy);
            DpiUtilities.GetMonitorEffectiveDpi(hMonitor, out effectiveDPIx, out effectiveDPIy);
            System.Diagnostics.Trace.WriteLine("Effective DPI:" + effectiveDPIx + " " + effectiveDPIy);

            //UInt32 rawDPIx, rawDPIy;
            //GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_Raw_DPI, out rawDPIx, out rawDPIy);
            //System.Diagnostics.Trace.WriteLine("Raw DPI:" + rawDPIx + " " + rawDPIy);

            var monitorInformation = new MonitorInformation()
            {
                deviceName    = monitorInfo.deviceName,
                isPrimary     = (monitorInfo.dwFlags & NativeMethods.MONITORINFOF_PRIMARY) != 0,
                rcMonitor     = monitorInfo.rcMonitor,
                rcWork        = monitorInfo.rcWork,
                scalingFactor = DpiUtilities.GetScreenScalingFactor(monitorInfo.deviceName),
                dpiX          = effectiveDPIx,
                dpiY          = effectiveDPIy,
            };

            _monitorInfoCache[hMonitor] = monitorInformation;
            if (monitorInformation.isPrimary && !_monitorInfoCache.ContainsKey(IntPtr.Zero))
            {
                _monitorInfoCache[IntPtr.Zero] = monitorInformation;
            }
            return(monitorInformation);
        }
        public static void PopupHotkeyHandler()
        {
            // Prevent duplicates
            var popups = GetOpenPopups();

            if (popups != null && popups.Any(p => p is PopupControl))
            {
                return;
            }

            var info = new WinApiProvider.GUITHREADINFO();

            info.cbSize = Marshal.SizeOf(info);
            if (WinApiProvider.GetGUIThreadInfo(0, ref info))
            {
                var hwndFocus = info.hwndFocus;
                var caretRect = GetAccessibleCaretRect(hwndFocus);

                var popup = (Application.Current as App).Host.Services.GetService(typeof(PopupControl)) as PopupControl;

                // Obtain popup handle for placement
                //var popupRect = new WinApiProvider.RECT();
                //WinApiProvider.GetWindowRect(popupHandle, ref popupRect);
                if (!RectValid(caretRect))
                {
                    // Can't accquire caret placement
                    caretRect = GetWinApiCaretRect(hwndFocus);
                    if (!RectValid(caretRect))
                    {
                        caretRect = new WinApiProvider.RECT()
                        {
                            left   = (int)(SystemParameters.PrimaryScreenWidth - popup.Width),
                            top    = (int)(SystemParameters.PrimaryScreenHeight - popup.Height),
                            right  = (int)SystemParameters.PrimaryScreenWidth,
                            bottom = (int)SystemParameters.PrimaryScreenHeight
                        };
                    }
                }

                // https://stackoverflow.com/questions/1918877/how-can-i-get-the-dpi-in-wpf
                // VisualTreeHelper.GetDpi(Visual visual)
                var dpiAtPoint = DpiUtilities.GetDpiForNearestMonitor(caretRect.right, caretRect.bottom);
                popup.HorizontalOffset = caretRect.right * DpiUtilities.DefaultDpiX / dpiAtPoint;
                popup.VerticalOffset   = caretRect.bottom * DpiUtilities.DefaultDpiY / dpiAtPoint;
                popup.IsOpen           = true;

                // OK caret placement
                //WinApiProvider.SetWindowPos(
                //    popup.Handle,
                //    IntPtr.Zero,
                //    caretRect.right,
                //    caretRect.bottom,
                //    (int)popup.Width,
                //    (int)popup.Height,
                //    WinApiProvider.SWP_NOZORDER);
            }
        }
        private void RecalculateSystemScale()
        {
            DpiUtilities.GetSystemEffectiveDpi(out _systemDpiX, out _systemDpiY);

            SystemScale = DpiUtilities.CalculateScale(_systemDpiX, _systemDpiY, 96, 96);

#if (DEBUG)
            System.Diagnostics.Debug.WriteLine("System DPI = " + _systemDpiX.ToString());
#endif
        }
        public static Tuple <double, double, double, double> GetWorkingAreaInSystemScale(Window window)
        {
            var dpiScale    = DpiUtilities.GetSystemScale();
            var workingArea = GetWorkingArea(window);

            return(new Tuple <double, double, double, double>(workingArea.Item1 / dpiScale.X,
                                                              workingArea.Item2 / dpiScale.Y,
                                                              workingArea.Item3 / dpiScale.X,
                                                              workingArea.Item4 / dpiScale.Y));
        }
        public static void PositionWindowOnScreen(Window window, System.Windows.Forms.Screen screen, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment)
        {
            var workingArea = screen.WorkingArea;
            var dpiScale    = DpiUtilities.GetSystemScale();

            double workingLeft   = workingArea.Left / dpiScale.X;
            double workingWidth  = workingArea.Width / dpiScale.X;
            double workingTop    = workingArea.Top / dpiScale.Y;
            double workingHeight = workingArea.Height / dpiScale.Y;

            PositionWindow(window, workingLeft, workingTop, workingWidth, workingHeight, horizontalAlignment, verticalAlignment);
        }
Beispiel #8
0
        private void Window_LocationChanged(object sender, EventArgs e)
        {
            if (!_isRepositioning)
            {
                // Position was changed by Windows, e.g. due to screen resizing or app reloading
                // Snap to screen boundaries
                _isRepositioning = true;
                var scale = LayoutUtilities.GetDpiScale(this);
                scale = DpiUtilities.GetSystemScale();
                scale = SystemScale;
                var screen = System.Windows.Forms.Screen.FromPoint(new System.Drawing.Point((int)Left, (int)Top));
                Left = Math.Min(Math.Max(Left, screen.WorkingArea.Left / scale.X), screen.WorkingArea.Right / scale.X - ActualWidth);
                Top  = Math.Min(Math.Max(Top, screen.WorkingArea.Top / scale.Y), screen.WorkingArea.Bottom / scale.Y - ActualHeight);

                // Make sure docking state is correct
                var dockState = ToolsDockState(scale, screen);
                Trace.WriteLine("Window position changed, new position: (" + Left + ", " + Top + "), Current dock state:" + _currentDockState + ", Dock state for position:" + dockState);
                if (dockState != _currentDockState)
                {
                    // Adjust tool window position to match the old dock state
                    switch (_currentDockState)
                    {
                    case DockState.Top:
                        Top = screen.WorkingArea.Top / scale.Y;
                        break;

                    case DockState.Left:
                        Left = screen.WorkingArea.Left / scale.X;
                        break;

                    case DockState.Right:
                        Left = screen.WorkingArea.Right / scale.X - ActualWidth;
                        break;

                    case DockState.Bottom:
                        Top = screen.WorkingArea.Bottom / scale.Y - ActualHeight;
                        break;

                    case DockState.Middle:
                        Left = (screen.WorkingArea.Left + screen.WorkingArea.Right) / 2 / scale.X - ActualWidth / 2;
                        Top  = (screen.WorkingArea.Top + screen.WorkingArea.Bottom) / 2 / scale.Y - ActualHeight / 2;
                        break;
                    }
                    Trace.WriteLine("Adjusted dockState:" + _currentDockState + ", adjusted position: (" + Left + ", " + Top + ")");
                }
                _isRepositioning = false;

                Settings.Default.ToolDocking  = _currentDockState.ToString();
                Settings.Default.ToolPosition = new System.Drawing.Point((int)Left, (int)Top);
                Settings.Default.Save();
            }
        }
        private void RecalculateMonitorScale()
        {
            uint dpiX;
            uint dpiY;

            DpiUtilities.GetWindowEffectiveDpi(_hwnd, out dpiX, out dpiY);

            if (dpiX != lastDpiX && dpiY != lastDpiY)
            {
                VirtualPixelScale = DpiUtilities.CalculateScale(dpiX, dpiY, 96, 96);

                lastDpiX = dpiX;
                lastDpiY = dpiY;
            }
        }
        public static void PositionWindowOnScreenWithOffsetIgnoringSystemDPI(Window window, System.Windows.Forms.Screen screen, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, float hOffset, float vOffset)
        {
            System.IntPtr monitor = NativeMethods.GetMonitorFromWindow(NativeMethods.GetWindowHwnd(window));


            var _screenProps = new ScreenProperties();

            _screenProps.GetMonitorsInformation();
            var    monitorInfo   = _screenProps.GetMonitorInformation(monitor);
            var    workingArea   = monitorInfo.rcWork;
            var    dpiScale      = DpiUtilities.CalculateScale((uint)monitorInfo.dpiX, (uint)monitorInfo.dpiY, 96, 96);
            double workingLeft   = (workingArea.left + hOffset) / dpiScale.X;
            double workingWidth  = workingArea.width / dpiScale.X;
            double workingTop    = (workingArea.top + vOffset) / dpiScale.Y;
            double workingHeight = workingArea.height / dpiScale.Y;

            PositionWindow(window, workingLeft, workingTop, workingWidth, workingHeight, horizontalAlignment, verticalAlignment);
        }
        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (_dragging && e.LeftButton == MouseButtonState.Pressed)
            {
                // Get the current mouse position
                Point  mousePos = e.GetPosition(null);
                Vector diff     = mousePos - _startPoint;

                // Snap to screen boundaries
                _isRepositioning = true;
                var scale = LayoutUtilities.GetDpiScale(this);
                scale = DpiUtilities.GetSystemScale();
                scale = SystemScale;
                //scale = new DpiScale();
                var screen = System.Windows.Forms.Screen.FromPoint(System.Windows.Forms.Control.MousePosition);
                System.Diagnostics.Debug.WriteLine("Screen.Left = " + screen.WorkingArea.Left.ToString());
                Left = Math.Min(Math.Max(Left + diff.X, screen.WorkingArea.Left / scale.X), screen.WorkingArea.Right / scale.X - ActualWidth);
                Top  = Math.Min(Math.Max(Top + diff.Y, screen.WorkingArea.Top / scale.Y), screen.WorkingArea.Bottom / scale.Y - ActualHeight);

                var dockState = ToolsDockState(scale, screen);
                if (dockState == DockState.Left || dockState == DockState.Right)
                {
                    if (dockState == DockState.Right && StackContainer.Orientation != Orientation.Vertical)
                    {
                        // Adjust for snapping to the right
                        Left          += ActualWidth - ActualHeight;
                        _startPoint.X -= ActualWidth - ActualHeight;
                    }
                }
                else
                {
                    if (_currentDockState == DockState.Right && StackContainer.Orientation != Orientation.Horizontal)
                    {
                        // Adjust for snapping from the right
                        Left          += ActualWidth - ActualHeight;
                        _startPoint.X -= ActualWidth - ActualHeight;
                    }
                }
                _isRepositioning = false;
                AdjustOrientation(dockState);
                _currentDockState = dockState;
            }
        }
        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Bitmap CaptureWindowRegion(IntPtr hWnd, int x, int y, int width, int height)
        {
            Bitmap bmCapture = null;
            var    dpiScale  = DpiUtilities.GetSystemScale();

            if (width > 0 && height > 0)
            {
                IntPtr ptrWindowDc = NativeMethods.GetWindowDC(hWnd);
                bmCapture = new Bitmap(width, height,
                                       System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                bmCapture.MakeTransparent();
                Graphics g      = Graphics.FromImage(bmCapture);
                IntPtr   hdcPtr = g.GetHdc();

                NativeMethods.BitBlt(hdcPtr, 0, 0, width, height, ptrWindowDc, x,
                                     y, NativeMethods.TernaryRasterOperations.SRCCOPY | NativeMethods.TernaryRasterOperations.CAPTUREBLT);
                bmCapture.SetResolution((float)(96.0 * dpiScale.X), (float)(96.0 * dpiScale.Y));

                g.ReleaseHdc();
                NativeMethods.ReleaseDC(IntPtr.Zero, ptrWindowDc);
                g.Dispose();
            }
            return(bmCapture);
        }
 /// <summary>
 /// Returns DPI (X, Y)
 /// </summary>
 /// <param name="window"></param>
 /// <returns></returns>
 public static DpiScale GetDpiScale(Window window)
 {
     return(DpiUtilities.GetVirtualPixelScale(window));
 }