【问题标题】:Set margin for maximized window设置最大化窗口的边距
【发布时间】:2014-03-29 15:34:35
【问题描述】:

我的窗口有WindowStyle="None"ResizeMode="CanResize",所以最大化它会覆盖任务栏。我有一个解决方法,在最大化之前将 MaxWidth 和 MaxHeight 设置为 WorkArea 尺寸,当任务栏向下时它工作得很好,但是当任务栏位于屏幕窗口的左侧时,它的位置仍然是 0,0 并且在它下面。

我想将最大化窗口的 TopLeft 偏移到 WorkArea 的 TopLeft。 Window 具有 margin 属性,但这似乎不起作用。

我尝试了以下操作,但似乎无法移动/偏移最大化的窗口。

 private void mywindow_StateChanged(object sender, EventArgs e)
    {
      if (this.WindowState == WindowState.Maximized)
      {
        MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
        MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
        double ScreenXOffset = System.Windows.SystemParameters.WorkArea.TopLeft.X; 

        mywindow.Left = ScreenXOffset;
        mywindow.Margin = new Thickness(ScreenXOffset,0,0,0);
      }

还有关于解决方法的想法?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    可以将窗口的MaxHeight设置为MaximizedPrimaryScreenHeight

    public MainWindow()
    {
        InitializeComponent();
        this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
    }
    

    您可能还希望设置窗口的MaxWidth

    public MainWindow()
    {
        InitializeComponent();
        this.MaxWidth= SystemParameters.MaximizedPrimaryScreenWidth;
        this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
    }
    

    您也可以为此使用 DynamicResource:

    <Window
        Width="{DynamicResource 
          {x:Static SystemParameters.MaximizedPrimaryScreenWidthKey}}"
        Height="{DynamicResource
          {x:Static SystemParameters.MaximizedPrimaryScreenHeightKey}}"
        WindowStartupLocation="CenterScreen"
        WindowStyle="SingleBorderWindow">
       ...
    </Window>
    

    关于任务栏在左侧的问题,见Maximizing borderless window by taking in account the user taskbar

    【讨论】:

    • 你误会了,我已经设置好了,但问题是当任务栏靠左停靠时,最大化的窗口将转到任务栏下方的 0,0,而左侧有空白空间。跨度>
    • 您可能会发现此链接很有帮助,然后stackoverflow.com/questions/14799691/…
    • 这并没有真正最大化窗口。我仍然有边框,我仍然可以移动它,用户可以将窗口拖到顶部边缘,它会真正最大化。即使我可以截获该消息,我仍然会即兴发挥。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 2015-07-21
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多