环境
语言:wpf
Framework:4.5.2
系统:win10,win11
CEF版本:86
方法能够达到的结果
说明:此方法主要需要从以下2个方面考虑
- 能够自适应dpi
- H5内容能够自适应dpi
最终达到的效果:
- 设置完成后,主屏窗口能够达到在全屏时不遮挡任务栏
- 设置完成后,当主屏dpi小于等于扩展屏dpi时,能够保证扩展屏的窗口全屏时不遮挡任务栏
- 此方法能够保证窗口和窗口内容跟随dpi
- 此方法能够保证多个屏幕时,窗口能够跟随不同屏幕的dpi自动变化
- 此方法能够保证全屏时,任务栏窗口的按钮以及窗口内的H5内容能够自适应
瑕疵:
这个方法在主屏的dpi大于扩展屏的dpi时,扩展屏的窗口在全屏时还是会遮挡任务栏,原因【注解】:https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/ns-winuser-minmaxinfo?redirectedfrom=MSDN
设置方法
- 设置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感知栏查看状态,状态为:未知
- 重写窗口全屏方法
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;
};
}