wpf 如何将WPF窗口的位置设置为桌面右下角?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7620488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:06:18  来源:igfitidea点击:

How to set the location of WPF window to the bottom right corner of desktop?

wpfwindowlocation

提问by VirtualWorld

I want to show my window on top of the TaskBar's clock when the windows starts.

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

How can I find the bottom right corner location of my desktop?

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

I use this code that works well in windows forms app but does not work correctly in WPF:

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

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

回答by Klaus78

This code works for me in WPF both with Display 100% and 125%

这段代码在 WPF 中对我有用,显示 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;
 }

In brief I use

简而言之,我使用

System.Windows.SystemParameters.WorkArea

System.Windows.SystemParameters.WorkArea

instead of

代替

System.Windows.Forms.Screen.PrimaryScreen.WorkingArea

System.Windows.Forms.Screen.PrimaryScreen.WorkingArea

回答by Amittai Shapira

To access the desktop rectangle, you could use the Screen class- Screen.PrimaryScreen.WorkingAreaproperty is the rectangle of your desktop.

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

Your WPF window has Topand Leftproperties as well as Widthand Height, so you could set those properties relative to the desktop location.

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

回答by Glenn Slayden

You can use the window's SizeChangedevent instead of Loadedif you want the window to stayin the corner when its size changes. This is especially handy if the window has Window.SizeToContentset to some value other than SizeToContent.Manual; in this case it will adjust to fit the content while staying in the corner.

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

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

Note also that you should subtract ActualWidthand ActualHeight(instead of Widthand Heightas shown in some other replies) to handle more possible situations, for example switching between SizeToContentmodes at runtime.

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

回答by Cyclion

My code:

我的代码:

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

回答by George Hahn

I solved this problem with a new window containing a label named MessageDisplay. The code accompanying the window was as follows:

我用一个包含名为 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;
    }
}

For my application, the setting of top and left puts this window below the menu on the main window (passed to DisplayMessage in the first parameter);

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

回答by JMIII

@Klaus78 's answer is correct. But since this is first thing google pops up and if working in environments where screen resolution can change often such that your app runs on virtual desktops or virtual servers and you still need it to update its placement when the screen resolution changes I have found linking to the SystemEvents.DisplaySettingsChanged event to be beneficial. Here is an example using rx and you can put this in your constructor for your view.

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

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

回答by PIntag

This above solutions did not entirely work for my window - it was too low and the bottom part of the window was beneath the taskbar and below the desktop workspace. I needed to set the position after the window content had been rendered:

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

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

Also, part of the frame was out of view, so I had to adjust by 5. Not sure why this is needed in my situation.

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