예제 #1
0
        private void WmDpiChanged(ref Message m)
        {
            var oldDeviceDpi = _deviceDpi;
            var newDeviceDpi = Win32.SignedHIWORD(m.WParam);

            var suggestedRect = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));

            ScaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(Handle);

            _deviceDpi = newDeviceDpi;

            var maxSizeState = MaximumSize;
            var minSizeState = MinimumSize;

            MinimumSize = Size.Empty;
            MaximumSize = Size.Empty;

            User32.SetWindowPos(Handle, IntPtr.Zero, suggestedRect.left, suggestedRect.top, suggestedRect.Width, suggestedRect.Height, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOACTIVATE);

            var scaleFactor = (float)newDeviceDpi / oldDeviceDpi;

            MinimumSize = DpiHelper.CalcScaledSize(minSizeState, new SizeF(scaleFactor, scaleFactor));
            MaximumSize = DpiHelper.CalcScaledSize(maxSizeState, new SizeF(scaleFactor, scaleFactor));

            OnWmDpiChanged(oldDeviceDpi, newDeviceDpi, suggestedRect.ToRectangle());
        }
예제 #2
0
        protected override void OnCreateControl()
        {
            base.OnCreateControl();

            if (!DesignMode)
            {
                if (ScaleFactor != 1.0f)
                {
                    Scale(new SizeF(ScaleFactor, ScaleFactor));
                }

                var currentScreenScaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(Handle);

                var primaryScreenScaleFactor = DpiHelper.GetScreenDpi(Screen.PrimaryScreen) / 96f;

                if (primaryScreenScaleFactor != 1.0f)
                {
                    Font = new Font(Font.FontFamily, (float)Math.Round(Font.Size / primaryScreenScaleFactor), Font.Style);
                }

                Font = new Font(Font.FontFamily, (float)Math.Round(Font.Size * currentScreenScaleFactor), Font.Style);


                CorrectFormPostion();
            }

            _isLoaded = true;
        }
예제 #3
0
        protected override void OnHandleCreated(EventArgs e)
        {
            ScaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(Handle);



            base.OnHandleCreated(e);

            CheckResetDPIAutoScale(true);

            CorrectFormPostion();
        }
        protected override bool GetScreenPoint(CefBrowser browser, int viewX, int viewY, ref int screenX, ref int screenY)
        {
            var scaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(_owner.HostWindowHandle);


            var pt = new POINT((int)(viewX * scaleFactor), (int)(viewY * scaleFactor));

            User32.ClientToScreen(_owner.HostWindowHandle, ref pt);

            screenX = pt.x;
            screenY = pt.y;

            return(true);
        }
예제 #5
0
        private void WmDpiChanged(ref Message m)
        {
            var oldDeviceDpi = _deviceDpi;
            var newDeviceDpi = Win32.SignedHIWORD(m.WParam);

            var suggestedRect = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));

            ScaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(Handle);

            _deviceDpi = newDeviceDpi;

            User32.SetWindowPos(Handle, IntPtr.Zero, suggestedRect.left, suggestedRect.top, suggestedRect.Width, suggestedRect.Height, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOACTIVATE);

            OnWmDpiChanged(oldDeviceDpi, newDeviceDpi, suggestedRect.ToRectangle());
        }
        protected override bool GetScreenInfo(CefBrowser browser, CefScreenInfo screenInfo)
        {
            var handle = _owner.HostWindowHandle;

            var screen = Screen.FromHandle(handle);

            screenInfo.DeviceScaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(handle);

            GetViewRect(browser, out var rectView);

            screenInfo.Rectangle = rectView;

            screenInfo.AvailableRectangle = rectView;//new CefRectangle(screen.WorkingArea.X, screen.WorkingArea.Y, screen.WorkingArea.Width, screen.WorkingArea.Height);

            return(true);
        }
        protected override void GetViewRect(CefBrowser browser, out CefRectangle rect)
        {
            var handle = _owner.HostWindowHandle;

            var scaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(handle);


            var clientRect = new RECT();

            rect = new CefRectangle();


            User32.GetClientRect(handle, ref clientRect);

            rect.X = rect.Y = 0;


            if (User32.IsIconic(handle) || clientRect.Width == 0 || clientRect.Height == 0)
            {
                var placement = new WINDOWPLACEMENT();

                User32.GetWindowPlacement(_owner.HostWindowHandle, ref placement);

                clientRect = placement.rcNormalPosition;

                rect.Width = (int)(clientRect.Width / scaleFactor);

                rect.Height = (int)(clientRect.Height / scaleFactor);
            }
            else
            {
                rect.Width  = (int)(clientRect.Width / scaleFactor);
                rect.Height = (int)(clientRect.Height / scaleFactor);
            }



            if (clientRect.Width != _view_width || clientRect.Height != _view_height)
            {
                _view_width  = clientRect.Width;
                _view_height = clientRect.Height;

                DiscardDeviceResources();
            }
        }
예제 #8
0
        private void CorrectFormPostion()
        {
            if (StartPosition == FormStartPosition.CenterParent && Owner != null)
            {
                Location = new Point(Owner.Location.X + Owner.Width / 2 - Width / 2,
                                     Owner.Location.Y + Owner.Height / 2 - Height / 2);
            }
            else if (StartPosition == FormStartPosition.CenterScreen || (StartPosition == FormStartPosition.CenterParent && Owner == null))
            {
                var currentScreen = Screen.FromPoint(MousePosition);

                var initScreen = Screen.FromHandle(Handle);

                var w = RealFormRectangle.Width;
                var h = RealFormRectangle.Height;

                var screenLeft = 0;
                var screenTop  = 0;

                if (DpiHelper.IsPerMonitorV2Awareness)
                {
                    var factor = DpiHelper.GetScaleFactorForCurrentWindow(Handle);


                    //var screenDpi = DpiHelper.GetScreenDpiFromPoint(MousePosition);

                    //var screenScaleFactor = (screenDpi / 96f) / ScaleFactor;

                    var bounds = GetScaledBounds(RealFormRectangle, new SizeF(factor, factor), BoundsSpecified.Size);

                    w = bounds.Width;
                    h = bounds.Height;
                }

                if (currentScreen.DeviceName != initScreen.DeviceName)
                {
                }

                screenLeft += currentScreen.WorkingArea.Left;
                screenTop  += currentScreen.WorkingArea.Top;

                User32.SetWindowPos(Handle, IntPtr.Zero, screenLeft + (currentScreen.WorkingArea.Width - w) / 2, screenTop + (currentScreen.WorkingArea.Height - h) / 2, RealFormRectangle.Width, RealFormRectangle.Height, SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOZORDER);
            }
        }
예제 #9
0
        protected override void OnHandleCreated(EventArgs e)
        {
            try
            {
                UxTheme.SetWindowTheme(Handle, string.Empty, string.Empty);
                User32.DisableProcessWindowsGhosting();

                ScaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(Handle);
            }
            catch { }

            base.OnHandleCreated(e);



            CheckResetDPIAutoScale(true);

            ResumeLayout();
        }
예제 #10
0
        protected override void OnDraggableRegionsChanged(CefBrowser browser, CefFrame frame, CefDraggableRegion[] regions)
        {
            if (_owner.WebView.DraggableRegion != null)
            {
                _owner.WebView.DraggableRegion.Dispose();
                _owner.WebView.DraggableRegion = null;
            }

            if (regions.Length > 0)
            {
                var scaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(_owner.WebView.BrowserHost.GetWindowHandle());
                //DpiHelper.GetScaleFactorForCurrentWindow(_owner.WebView.BrowserHost.GetWindowHandle());


                //var targetRegion = _core.DraggableRegion = new Region();


                foreach (var region in regions)
                {
                    var rect = new Rectangle((int)(region.Bounds.X * scaleFactor), (int)(region.Bounds.Y * scaleFactor), (int)(region.Bounds.Width * scaleFactor), (int)(region.Bounds.Height * scaleFactor));

                    if (_owner.WebView.DraggableRegion == null)
                    {
                        _owner.WebView.DraggableRegion = new Region(rect);
                    }
                    else
                    {
                        if (region.Draggable)
                        {
                            _owner.WebView.DraggableRegion.Union(rect);
                        }
                        else
                        {
                            _owner.WebView.DraggableRegion.Exclude(rect);
                        }
                    }
                }
            }
        }
        protected override void OnPaint(CefBrowser browser, CefPaintElementType type, CefRectangle[] dirtyRects, IntPtr buffer, int width, int height)
        {
            if (width <= 0 || height <= 0)
            {
                return;
            }

            var handle = _owner.HostWindowHandle;

            CreateDeviceResource();

            _renderTarget.BeginDraw();


            if (type == CefPaintElementType.View)
            {
                var bmp = _renderTarget.CreateBitmap(new Vortice.Mathematics.Size(width, height), buffer, width * 4, new BitmapProperties(new PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)));

                if (!_isPopupShown)
                {
                    _renderTarget.Clear(Color.Transparent);
                }

                _renderTarget.DrawBitmap(bmp);

                bmp.Dispose();
            }
            else if (type == CefPaintElementType.Popup)
            {
                var bmp = _renderTarget.CreateBitmap(new Vortice.Mathematics.Size(width, height), buffer, width * 4, new BitmapProperties(new PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)));

                if (_cachedPopupImage != null)
                {
                    _cachedPopupImage.Dispose();
                    _cachedPopupImage = null;
                    GC.Collect();
                }

                _cachedPopupImage = _renderTarget.CreateSharedBitmap(bmp, new BitmapProperties
                {
                    PixelFormat = new PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)
                });

                bmp.Dispose();
            }

            if (_cachedPopupImage != null && _isPopupShown && _popupRect.HasValue)
            {
                var scaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(_owner.HostWindowHandle);
                var x           = _popupRect.Value.X * scaleFactor;
                var y           = _popupRect.Value.Y * scaleFactor;

                var popupWidth  = _popupRect.Value.Width * scaleFactor;
                var popupHeight = _popupRect.Value.Height * scaleFactor;

                var right  = x + popupWidth;
                var bottom = y + popupHeight;

                _renderTarget.DrawBitmap(_cachedPopupImage, new Vortice.RawRectF(x, y, right, bottom), 1f, BitmapInterpolationMode.Linear, new Vortice.RawRectF(0, 0, popupWidth, popupHeight));
            }

            if (_renderTarget.EndDraw().Failure)
            {
                DiscardDeviceResources();
            }

            unsafe
            {
                var rect = new RECT();
                User32.GetWindowRect(handle, ref rect);

                var newLocation = new POINT(rect.left, rect.top);
                var newSize     = new SIZE(rect.Width, rect.Height);
                var zeroPoint   = new POINT(0, 0);

                if (rect.Width == 0 || rect.Height == 0)
                {
                    return;
                }

                var blend = new BLENDFUNCTION
                {
                    BlendOp             = AcSrcOver,
                    BlendFlags          = 0,
                    SourceConstantAlpha = 255,
                    AlphaFormat         = AcSrcAlpha
                };

                var ulwi = new UPDATELAYEREDWINDOWINFO
                {
                    cbSize = Marshal.SizeOf(typeof(UPDATELAYEREDWINDOWINFO)),
                    hdcDst = _screenDC,
                    pptDst = &newLocation,
                    psize  = &newSize,
                    hdcSrc = _memDC,
                    pptSrc = &zeroPoint,
                    //crKey = new COLORREF(0),
                    pblend   = &blend,
                    dwFlags  = BlendFlags.ULW_ALPHA,
                    prcDirty = null
                };

                User32.UpdateLayeredWindowIndirect(_owner.HostWindowHandle, ref ulwi);
            }

            GC.Collect();
        }
예제 #12
0
        protected override void OnPaint(CefBrowser browser, CefPaintElementType type, CefRectangle[] dirtyRects, IntPtr buffer, int width, int height)
        {
            if (width <= 0 || height <= 0)
            {
                return;
            }

            CreateDeviceResource();

            _renderTarget.BeginDraw();

            if (type == CefPaintElementType.View)
            {
                var bmp = _renderTarget.CreateBitmap(new Vortice.Mathematics.Size(width, height), buffer, width * 4, new BitmapProperties(new PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)));

                if (!_isPopupShown)
                {
                    _renderTarget.Clear(Color.Transparent);
                }

                _renderTarget.DrawBitmap(bmp);


                bmp.Dispose();
            }
            else if (type == CefPaintElementType.Popup)
            {
                var bmp = _renderTarget.CreateBitmap(new Vortice.Mathematics.Size(width, height), buffer, width * 4, new BitmapProperties(new PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)));

                if (_cachedPopupImage != null)
                {
                    _cachedPopupImage.Dispose();
                    _cachedPopupImage = null;
                    GC.Collect();
                }

                _cachedPopupImage = _renderTarget.CreateSharedBitmap(bmp, new BitmapProperties
                {
                    PixelFormat = new PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)
                });

                bmp.Dispose();
            }

            //var layer = _renderTarget.CreateLayer();
            //var layerParameter = new LayerParameters
            //{
            //    ContentBounds = new Vortice.RawRectF(0, 0, width, height),
            //    Opacity = 1
            //};
            //_renderTarget.PushLayer(ref layerParameter, layer);

            if (_cachedPopupImage != null && _isPopupShown && _popupRect.HasValue)
            {
                var scaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(_owner.HostWindowHandle);
                var x           = _popupRect.Value.X * scaleFactor;
                var y           = _popupRect.Value.Y * scaleFactor;

                var popupWidth  = _popupRect.Value.Width * scaleFactor;
                var popupHeight = _popupRect.Value.Height * scaleFactor;

                var right  = x + popupWidth;
                var bottom = y + popupHeight;

                _renderTarget.DrawBitmap(_cachedPopupImage, new Vortice.RawRectF(x, y, right, bottom), 1f, BitmapInterpolationMode.Linear, new Vortice.RawRectF(0, 0, popupWidth, popupHeight));
            }

            //_renderTarget.PopLayer();

            //layer.Dispose();

            if (_renderTarget.EndDraw().Failure)
            {
                DiscardDeviceResources();
            }
        }