我有一个具有以下属性的WPF窗口:
-ResizeMode = NoResize
-WindowStyle =无
我完成了普通窗口的所有功能,但是当任务栏的高度发生变化时,我不知道如何使窗口自动调整自身大小(最大化时)。 (类似于Microsoft Visual Studio 2017窗口)。
我可以手动最大化窗口,但是如果我隐藏任务栏,则窗口和屏幕底部之间将有一个空白空间。
工作区域更改时是否触发任何事件?
答案 0 :(得分:1)
对于您的问题,您可以使用 SystemParameters.WorkArea
。
最初设置 MainWindow 的 MaxHeight
。
MaxHeight="{Binding Height, Source={x:Static SystemParameters.WorkArea}}"
注册到 MainWindow 代码隐藏中的 SystemParameters.StaticPropertyChanged
以接收更改并更新您的窗口大小。
SystemParameters.StaticPropertyChanged += (sender, args) =>
{
if (args.PropertyName == nameof(SystemParameters.WorkArea))
{
this.Dispatcher.Invoke(() =>
{
MaxHeight = SystemParameters.WorkArea.Height;
Height = SystemParameters.WorkArea.Height;
WindowState = WindowState.Normal; // Updates the windows new sizes
WindowState = WindowState.Maximized;
});
}
};
答案 1 :(得分:0)
处理窗口的WM_GETMINMAXINFO消息,并进行所需的调整大小操作
public MainWindow()
{
InitializeComponent();
SourceInitialized += new EventHandler(win_SourceInitialized);
}
private void win_SourceInitialized(object sender, EventArgs e)
{
System.IntPtr handle = (new WinInterop.WindowInteropHelper(this)).Handle;
WinInterop.HwndSource.FromHwnd(handle).AddHook(new WinInterop.HwndSourceHook(WindowProc));
}
private const int WM_GETMINMAXINFO = 0x0024;
private static System.IntPtr WindowProc(
System.IntPtr hwnd,
int msg,
System.IntPtr wParam,
System.IntPtr lParam,
ref bool handled)
{
switch (msg)
{
case WM_GETMINMAXINFO: //https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-getminmaxinfo
WmGetMinMaxInfo(hwnd, lParam); // <------ Do what you need to here ---------->
handled = true;
break;
}
return (System.IntPtr)0;
}
请注意,如果您是无边界且不可调整大小的窗口,则可能还需要获取监视器信息(通过Win32 GetMonitorInfo),并将您的应用程序限制在其所在的监视器工作区。在我们的系统上,窗口无法正确调整1900x1200监视器的窗口大小(它太高了,因此我们必须根据“监视器信息”设置MaxHeight,并注意如果通过继续观看来调整任务栏的大小来更改此大小) WM_GETMINMAXINFO消息)。
如果您也遇到这些问题,此博客可能会对此有所帮助: