Cefsharp的窗口设置最大化时不覆盖任务栏

环境

语言:wpf
Framework:4.5.2
系统:win10,win11
CEF版本:86

方法能够达到的结果

说明:此方法主要需要从以下2个方面考虑

  • 能够自适应dpi
  • H5内容能够自适应dpi

最终达到的效果:

  1. 设置完成后,主屏窗口能够达到在全屏时不遮挡任务栏
  2. 设置完成后,当主屏dpi小于等于扩展屏dpi时,能够保证扩展屏的窗口全屏时不遮挡任务栏
  3. 此方法能够保证窗口和窗口内容跟随dpi
  4. 此方法能够保证多个屏幕时,窗口能够跟随不同屏幕的dpi自动变化
  5. 此方法能够保证全屏时,任务栏窗口的按钮以及窗口内的H5内容能够自适应
    瑕疵:
    这个方法在主屏的dpi大于扩展屏的dpi时,扩展屏的窗口在全屏时还是会遮挡任务栏,原因【注解】:https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/ns-winuser-minmaxinfo?redirectedfrom=MSDN

设置方法

  1. 设置dpi感知为false:
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
      <windowsSettings>
          <!-- The combination of below two tags have the following effect : 
         1. Per-Monitor for >= Windows 10 Anniversary Update
         2. System < Windows 10 Anniversary Update -->
          <!-- <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness> -->
          <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">false</dpiAware>
      </windowsSettings>
  </application>

设置完成后,启动后可以在进程的dpi感知栏查看状态,状态为:未知
在这里插入图片描述

  1. 重写窗口全屏方法
public partial class MainWindow : Window
{
	public MainWindow ()
	{
		InitializeComponent();
		this.MaxCoverTaskbarNot();
	}
}
public static class WindowCoverTaskbarExtensions
{
    /// <summary>
    /// 窗口最大化时不覆盖任务栏
    /// </summary>
    /// <param name="window"></param>
    public static System.Windows.Window MaxCoverTaskbarNot(this System.Windows.Window window)
    {
        new WindowCoverTaskbar(window).Set();
        return window;
    }

    
}

public class WindowCoverTaskbar
{
    private readonly Window _window;
    private HwndSource _source;
    public WindowCoverTaskbar(Window window)
    {
        _window = window;
        window.SourceInitialized += WindowOnSourceInitialized;
    }

    public void Set()
    {

    }
    private void WindowOnSourceInitialized(object sender, EventArgs e)
    {
        IntPtr handle = new WindowInteropHelper(_window).Handle;
        _source= HwndSource.FromHwnd(handle);
        _source.AddHook(new HwndSourceHook(WindowProc));
    }
    private  IntPtr WindowProc(
        IntPtr hwnd,
        int msg,
        IntPtr wParam,
        IntPtr lParam,
        ref bool handled)
    {
        switch (msg)
        {
            case 0x0024:
                WmGetMinMaxInfo(hwnd, lParam);
                handled = true;
                break;
        }

        return (IntPtr)0;
    }
    private void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
    {

        MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));

        // Adjust the maximized size and position to fit the work area of the correct monitor
        int MONITOR_DEFAULTTONEAREST = 0x00000002;
        IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);

        if (monitor != IntPtr.Zero)
        {
            MONITORINFO monitorInfo = new MONITORINFO();
            GetMonitorInfo(monitor, monitorInfo);
            RECT rcWorkArea = monitorInfo.rcWork;
            RECT rcMonitorArea = monitorInfo.rcMonitor;

            //显示任务栏
            mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
            mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);

            if (rcWorkArea.left == 0)
            {
                //主屏
                mmi.ptMaxSize.x = rcWorkArea.Width; // (int)dpiSize.X;
                mmi.ptMaxSize.y = rcWorkArea.Height; //  (int)dpiSize.Y;
            }
            else
            {
                //扩展屏
                if (mmi.ptMaxSize.x < rcWorkArea.Width)
                {
                    //如果主屏dpi大于扩展屏dpi,则全屏覆盖任务栏
                    //不处理,使用默认的代码
                    //这里不能设置完成是由于微软存在一定的设置问题【关注注解】:https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/ns-winuser-minmaxinfo?redirectedfrom=MSDN
                }
                else
                {
                    //如果主屏dpi小于扩展屏dpi,则全屏不覆盖任务栏
                    mmi.ptMaxSize.x = rcWorkArea.Width;
                    mmi.ptMaxSize.y = rcWorkArea.Height;
                }
            }
        }

        Marshal.StructureToPtr(mmi, lParam, true);
    }
    [DllImport("user32")]
    internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);

    /// <summary>
    /// 
    /// </summary>
    [DllImport("User32")]
    internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
    /// <summary>
    /// </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    internal class MONITORINFO
    {
        /// <summary>
        /// </summary>            
        public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));

        /// <summary>
        /// </summary>            
        public RECT rcMonitor = new RECT();

        /// <summary>
        /// </summary>            
        public RECT rcWork = new RECT();

        /// <summary>
        /// </summary>            
        public int dwFlags = 0;
    }


    /// <summary> Win32 </summary>
    [StructLayout(LayoutKind.Sequential, Pack = 0)]
    internal struct RECT
    {
        /// <summary> Win32 </summary>
        public int left;
        /// <summary> Win32 </summary>
        public int top;
        /// <summary> Win32 </summary>
        public int right;
        /// <summary> Win32 </summary>
        public int bottom;

        /// <summary> Win32 </summary>
        public static readonly RECT Empty = new RECT();

        /// <summary> Win32 </summary>
        public int Width
        {
            get { return Math.Abs(right - left); }  // Abs needed for BIDI OS
        }
        /// <summary> Win32 </summary>
        public int Height
        {
            get { return bottom - top; }
        }

        /// <summary> Win32 </summary>
        public RECT(int left, int top, int right, int bottom)
        {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }


        /// <summary> Win32 </summary>
        public RECT(RECT rcSrc)
        {
            left = rcSrc.left;
            top = rcSrc.top;
            right = rcSrc.right;
            bottom = rcSrc.bottom;
        }

        /// <summary> Win32 </summary>
        public bool IsEmpty
        {
            get
            {
                // BUGBUG : On Bidi OS (hebrew arabic) left > right
                return left >= right || top >= bottom;
            }
        }
        /// <summary> Return a user friendly representation of this struct </summary>
        public override string ToString()
        {
            if (this == Empty) { return "RECT {Empty}"; }
            return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }";
        }

        /// <summary> Determine if 2 RECT are equal (deep compare) </summary>
        public override bool Equals(object obj)
        {
            if (!(obj is RECT)) { return false; }
            return this == (RECT)obj;
        }

        /// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
        public override int GetHashCode()
        {
            return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
        }


        /// <summary> Determine if 2 RECT are equal (deep compare)</summary>
        public static bool operator ==(RECT rect1, RECT rect2)
        {
            return rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right && rect1.bottom == rect2.bottom;
        }

        /// <summary> Determine if 2 RECT are different(deep compare)</summary>
        public static bool operator !=(RECT rect1, RECT rect2)
        {
            return !(rect1 == rect2);
        }


    }
    /// <summary>
    /// POINT aka POINTAPI
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal struct POINT
    {
        /// <summary>
        /// x coordinate of point.
        /// </summary>
        public int x;
        /// <summary>
        /// y coordinate of point.
        /// </summary>
        public int y;

        /// <summary>
        /// Construct a point of coordinates (x,y).
        /// </summary>
        public POINT(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct MINMAXINFO
    {
        public POINT ptReserved;
        public POINT ptMaxSize;
        public POINT ptMaxPosition;
        public POINT ptMinTrackSize;
        public POINT ptMaxTrackSize;
    };
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值