无边框窗体最大化显示超出屏幕外的解决方案
相关:
https://stackoverflow.com/questions/12884987/wpf-borderless-window-on-maximize-exceeds-window-height-and-width?rq=1
Solved:
通过修改ResizeMode = ResizeMode.NoResize
可以有效的解决该问题,但需要注意的是在其它窗口状态下ResizeMode需要恢复原先值。具体代码如下:
private ResizeMode _preResizeMode;
private void BtnMax_OnClick(object sender, RoutedEventArgs e)
{
_preResizeMode = this.ResizeMode;
this.ResizeMode = ResizeMode.NoResize;
this.WindowState = WindowState.Maximized;
}
private void BtnNormax_OnClick(object sender, RoutedEventArgs e)
{
this.ResizeMode = _preResizeMode;
this.WindowState = WindowState.Normal;
}
private void BtnMinimal_OnClick(object sender, RoutedEventArgs e)
{
this.ResizeMode = _preResizeMode;
this.WindowState = WindowState.Minimized;
}
无边框窗体不摭挡桌面工具栏的代码
使用P/Invoke的方式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Interop;
namespace testxx22
{
public partial class MainWindow
{
void MainWindow_SourceInitialized(object sender, EventArgs e)
{
IntPtr handle = (new WindowInteropHelper(this)).Handle;
HwndSource.FromHwnd(handle).AddHook(WindowProc);
}
private static 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 static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
{
var 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)
{
var 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);
//下两行是用来控制不摭挡工具栏
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
}
Marshal.StructureToPtr(mmi, lParam, true);
}
/// <summary>