【问题标题】:How to set the location of WPF window to the bottom right corner of desktop?如何将WPF窗口的位置设置在桌面的右下角?
【发布时间】:2011-11-29 01:08:17
【问题描述】:

当窗口启动时,我想在TaskBar 的时钟上显示我的窗口。

如何找到桌面右下角的位置?

我使用的代码在 Windows 窗体应用程序中运行良好,但在 WPF 中无法正常运行:

var desktopWorkingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
this.Left = desktopWorkingArea.Right - this.Width;
this.Top = desktopWorkingArea.Bottom - this.Height;

【问题讨论】:

    标签: wpf window location


    【解决方案1】:

    此代码适用于我在 WPF 中的 Display 100% 和 125%

     private void Window_Loaded(object sender, RoutedEventArgs e)
     {
        var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
        this.Left = desktopWorkingArea.Right - this.Width;
        this.Top = desktopWorkingArea.Bottom - this.Height;
     }
    

    简而言之,我使用

    System.Windows.SystemParameters.WorkArea

    而不是

    System.Windows.Forms.Screen.PrimaryScreen.WorkingArea

    【讨论】:

    • 在 Win7 中完美运行。
    • 感谢为我工作。我为像我这样使用 WPF 的新手添加此评论:public MainWindow() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Window_Loaded); } 这就是调用它的方式。
    【解决方案2】:

    要访问桌面矩形,您可以使用Screen class - Screen.PrimaryScreen.WorkingArea 属性是您桌面的矩形。

    您的 WPF 窗口具有 TopLeft 属性以及 WidthHeight,因此您可以相对于桌面位置设置这些属性。

    【讨论】:

    • 这在 Windows 窗体应用程序中运行良好,但在我的 WPF 应用程序中无法正常运行(窗口脱离桌面),我的错误在哪里?(我编辑了我的问题)
    • 我刚刚使用您在编辑中发布的代码创建了示例 WPF 应用程序,它非常适合我(在 WIN7 上)。
    • 我发现了为什么会这样WPF 也是,但我怎样才能在 WPF 中做到这一点而不改变我的显示?
    【解决方案3】:

    如果您希望窗口在大小改变时停留在角落,您可以使用窗口的SizeChanged 事件而不是Loaded。如果窗口将Window.SizeToContent 设置为SizeToContent.Manual 以外的某个值,这将特别方便;在这种情况下,它会在角落里调整以适应内容。

    public MyWindow()
    {
        SizeChanged += (o, e) =>
        {
            var r = SystemParameters.WorkArea;
            Left = r.Right - ActualWidth;
            Top = r.Bottom - ActualHeight;
        };
        InitializeComponent();
    }
    

    还请注意,您应该减去ActualWidthActualHeight(而不是其他一些回复中显示的WidthHeight)以处理更多可能的情况,例如在运行时在SizeToContent 模式之间切换。

    【讨论】:

      【解决方案4】:

      我的代码:

      MainWindow.WindowStartupLocation = WindowStartupLocation.Manual;
      
      MainWindow.Loaded += (s, a) =>
      {
          MainWindow.Height = SystemParameters.WorkArea.Height;
          MainWindow.Width = SystemParameters.WorkArea.Width;
          MainWindow.SetLeft(SystemParameters.WorkArea.Location.X);
          MainWindow.SetTop(SystemParameters.WorkArea.Location.Y);
      };
      

      【讨论】:

        【解决方案5】:

        我通过一个包含名为 MessageDisplay 的标签的新窗口解决了这个问题。窗口附带的代码如下:

        public partial class StatusWindow : Window
        {
            static StatusWindow display;
        
            public StatusWindow()
            {
                InitializeComponent();
            }
        
            static public void DisplayMessage( Window parent, string message )
            {
                if ( display != null )
                    ClearMessage();
                display = new StatusWindow();
                display.Top = parent.Top + 100;
                display.Left = parent.Left + 10;
                display.MessageDisplay.Content = message;
                display.Show();
            }
        
            static public void ClearMessage()
            {
                display.Close();
                display = null;
            }
        }
        

        对于我的应用来说,top和left的设置把这个窗口放在主窗口的菜单下面(在第一个参数中传递给DisplayMessage);

        【讨论】:

          【解决方案6】:

          上述解决方案并不完全适用于我的窗口 - 它太低并且窗口的底部位于任务栏下方和桌面工作区下方。我需要在渲染窗口内容后设置位置:

          private void Window_ContentRendered(object sender, EventArgs e)
          {
              var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
              this.Left = desktopWorkingArea.Right - this.Width - 5;
              this.Top = desktopWorkingArea.Bottom - this.Height - 5;
          }
          

          另外,部分框架不在视野范围内,所以我不得不调整 5。不知道为什么在我的情况下需要这样做。

          【讨论】:

          • 我遇到了类似的问题。顶部很好,但不是我想并排放置的两个窗口的左、右、宽度或高度。
          【解决方案7】:

          @Klaus78 的回答是正确的。但是由于这是谷歌弹出的第一件事,并且如果在屏幕分辨率经常变化的环境中工作,以便您的应用程序在虚拟桌面或虚拟服务器上运行,并且您仍然需要它在屏幕分辨率发生变化时更新其位置我发现链接到SystemEvents.DisplaySettingsChanged 事件是有益的。这是一个使用 rx 的示例,您可以将其放入构造函数中以供查看。

                  Observable
                      .FromEventPattern<EventHandler, EventArgs>(_ => SystemEvents.DisplaySettingsChanged += _, _ => SystemEvents.DisplaySettingsChanged -= _)
                      .Select(_ => SystemParameters.WorkArea)
                      .Do(_ =>
                      {
                          Left = _.Right - Width;
                          Top = _.Bottom - Height;
                      })
                      .Subscribe();
          

          【讨论】:

            猜你喜欢
            • 2020-11-14
            • 2020-02-07
            • 2017-01-27
            • 2016-06-23
            • 2022-06-19
            • 1970-01-01
            • 2012-09-17
            • 2018-08-31
            • 1970-01-01
            相关资源
            最近更新 更多