固定/有限宽度的WPF窗口最大化
在我的WPF窗口上,我设置了固定/有限宽度的WPF窗口最大化,wpf,windows-8,maximize,Wpf,Windows 8,Maximize,在我的WPF窗口上,我设置了 Width="300" MinWidth="300" MaxWidth="300" 当我最大化这个窗口时,它会固定在屏幕的左边框上,但窗口的底部在Windows8任务栏的下面 我试过了 public MainWindow() { ... this.MaxHeight = System.Windows.SystemParameters.WorkArea.Height; } ,但这会在任务栏和我的应用程序之间产生几个像素的可用空间 我想 有一个固定
Width="300" MinWidth="300" MaxWidth="300"
当我最大化这个窗口时,它会固定在屏幕的左边框上,但窗口的底部在Windows8任务栏的下面
我试过了
public MainWindow()
{
...
this.MaxHeight = System.Windows.SystemParameters.WorkArea.Height;
}
,但这会在任务栏和我的应用程序之间产生几个像素的可用空间
我想
- 有一个固定宽度的窗口
- 将其停靠在右侧边界上
- 未在最大化状态下覆盖/进入任务栏下方
Width="300" MinWidth="300" MaxWidth="300" StateChanged="MainWindow_OnStateChanged"
事件处理程序和助手方法:
private bool isMaximized;
private Rect normalBounds;
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState == WindowState.Maximized && !isMaximized)
{
WindowState = WindowState.Normal;
isMaximized = true;
normalBounds = RestoreBounds;
Height = SystemParameters.WorkArea.Height;
MaxHeight = Height;
MinHeight = Height;
Top = 0;
Left = SystemParameters.WorkArea.Right - Width;
SetMovable(false);
}
else if(WindowState == WindowState.Maximized && isMaximized)
{
WindowState = WindowState.Normal;
isMaximized = false;
MaxHeight = Double.PositiveInfinity;
MinHeight = 0;
Top = normalBounds.Top;
Left = normalBounds.Left;
Width = normalBounds.Width;
Height = normalBounds.Height;
SetMovable(true);
}
}
private void SetMovable(bool enable)
{
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
if(enable)
source.RemoveHook(WndProc);
else
source.AddHook(WndProc);
}
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
switch (msg)
{
case WM_SYSCOMMAND:
int command = wParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
handled = true;
break;
}
return IntPtr.Zero;
}
不幸的是,仅使用MinWidth、MaxWidth和WindowsState似乎无法实现这三个要求 但不管怎样,仍然有可能实现类似的目标。您需要做的是模拟窗口的最大化状态。您需要将车窗移动到正确的位置,获得正确的高度,并使其不可移动。 前两部分很简单,最后一部分需要一些更先进的东西 从您拥有的窗口开始,将Width、MaxWidth和MinWidth设置为300,然后将事件处理程序添加到StateChanged
Width="300" MinWidth="300" MaxWidth="300" StateChanged="MainWindow_OnStateChanged"
事件处理程序和助手方法:
private bool isMaximized;
private Rect normalBounds;
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState == WindowState.Maximized && !isMaximized)
{
WindowState = WindowState.Normal;
isMaximized = true;
normalBounds = RestoreBounds;
Height = SystemParameters.WorkArea.Height;
MaxHeight = Height;
MinHeight = Height;
Top = 0;
Left = SystemParameters.WorkArea.Right - Width;
SetMovable(false);
}
else if(WindowState == WindowState.Maximized && isMaximized)
{
WindowState = WindowState.Normal;
isMaximized = false;
MaxHeight = Double.PositiveInfinity;
MinHeight = 0;
Top = normalBounds.Top;
Left = normalBounds.Left;
Width = normalBounds.Width;
Height = normalBounds.Height;
SetMovable(true);
}
}
private void SetMovable(bool enable)
{
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
if(enable)
source.RemoveHook(WndProc);
else
source.AddHook(WndProc);
}
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
switch (msg)
{
case WM_SYSCOMMAND:
int command = wParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
handled = true;
break;
}
return IntPtr.Zero;
}