我正在创建一个带有自定义chrome的WPF窗口,所以我设置ResizeMode="NoResize"并WindowStyle="None"实现了自己的chrome.但是,在最大化无边框窗口时会出现问题:它需要整个屏幕.
我找到了以下技巧来解决部分问题:http: //chiafong6799.wordpress.com/2009/02/05/maximizing-a-borderlessno-caption-window/
这成功地限制了窗口大小以防止覆盖任务栏.但是,如果用户的任务栏位于左侧或顶部,则无法使用,因为窗口位于0,0位置.
有没有办法更准确地检索可用区域,或查询用户任务栏的位置,以便我可以相应地定位最大化窗口?
我快速玩了一下,当使用无边框表单设置时,似乎忽略了设置Windows Left和Top属性WindowState.Maximized.
一种解决方法是忽略这些WindowState功能并创建自己的Maximize/ Restore功能
粗略的例子.
public partial class MainWindow : Window
{
private Rect _restoreLocation;
public MainWindow()
{
InitializeComponent();
}
private void MaximizeWindow()
{
_restoreLocation = new Rect { Width = Width, Height = Height, X = Left, Y = Top };
System.Windows.Forms.Screen currentScreen;
currentScreen = System.Windows.Forms.Screen.FromPoint(System.Windows.Forms.Cursor.Position);
Height = currentScreen.WorkingArea.Height;
Width = currentScreen.WorkingArea.Width;
Left = currentScreen.WorkingArea.X;
Top = currentScreen.WorkingArea.Y;
}
private void Restore()
{
Height = _restoreLocation.Height;
Width = _restoreLocation.Width;
Left = _restoreLocation.X;
Top = _restoreLocation.Y;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MaximizeWindow();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
Restore();
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragMove();
}
base.OnMouseMove(e);
}
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="74.608" Width="171.708" ResizeMode="NoResize" WindowStyle="None">
<Grid>
<Button Content="Max" HorizontalAlignment="Left" Margin="0,29,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<Button Content="Restore" HorizontalAlignment="Left" Margin="80,29,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
显然,你想清理这个代码,但它似乎工作的任何地方Taskbar的位置,然而,你可能需要添加一些逻辑,以获得正确的Left,Top如果用户的字体DPI大于100%