WPF - 通过考虑用户任务栏最大化无边框窗口 [英] WPF - Maximizing borderless window by taking in account the user taskbar

查看:35
本文介绍了WPF - 通过考虑用户任务栏最大化无边框窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有自定义镶边的 WPF 窗口,因此我设置了 ResizeMode="NoResize"WindowStyle="None" 来实现我自己的镶边.但是,最大化无边框窗口时存在一个问题:它占用整个屏幕.

I am creating a WPF window with a custom chrome, so I setted ResizeMode="NoResize" and WindowStyle="None" to implement my own chrome. However, there is an issue while maximizing the borderless window: it takes the whole screen.

我发现以下技巧可以解决部分问题:http://chiafong6799.wordpress.com/2009/02/05/最大化-a-borderlessno-caption-window/

I found the following trick to fix part of the issue: http://chiafong6799.wordpress.com/2009/02/05/maximizing-a-borderlessno-caption-window/

这成功地限制了窗口大小以防止覆盖任务栏.但是,如果用户的任务栏位于左侧或顶部,这将不起作用,因为窗口位于位置 0,0.

This successfully restrain the window size to prevent from covering a taskbar. However, if the user have his taskbar positionned at the left or at the top, this won't work, as the window is at position 0,0.

有什么办法可以更准确地检索可用区域,或者查询用户任务栏的位置,以便我可以相应地定位最大化的窗口?

Is there any way to retrieve more accurately the available area, or to query the user taskbar's position so I can position the maximized window accordingly?

推荐答案

我玩了一会儿,似乎设置了 Windows LeftTop 属性在将 WindowState.Maximized 设置为无边框形式时被忽略.

I had a quick play around and it seems that setting the Windows Left and Top properties is ignored when setting WindowState.Maximized with a borderless form.

一种解决方法是忽略WindowState 函数并创建您自己的Maximize/Restore 函数

One workaround would be to ignore the WindowState functions and create your own Maximize/Restore functions

粗略的例子.

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);
    }
}

XML:

<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>

显然你会想要清理这段代码,但它似乎可以在 Taskbar 所在的任何地方工作,但是你可能需要添加一些逻辑来获得正确的 Left, Top 如果用户字体 DPI 大于 100%

Obviously you will want to clean this code up, but it seems to work wherever the Taskbar is located, However you may need to add some logic to get the correct Left, Top if the users font DPI is larger than 100%

这篇关于WPF - 通过考虑用户任务栏最大化无边框窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆