C# 如何在启动时将窗口的位置定位到用户屏幕的右侧?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9120950/
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-08-09 06:03:51  来源:igfitidea点击:

How can I position the window's position on startup to the right side of the user's screen?

c#wpfwindowlocationsidebar

提问by anonymous

I am currently creating a sidebar-like WPF application in C#. When a user starts the application, I would like the window to automatically position it's self to the side of the user's screen. I have tried a few methods and google searches, but have not found any help.

我目前正在用 C# 创建一个类似侧边栏的 WPF 应用程序。当用户启动应用程序时,我希望窗口自动将其自身定位到用户屏幕的一侧。我尝试了一些方法和谷歌搜索,但没有找到任何帮助。

Here's an example of what I'm trying to do:

这是我正在尝试做的一个例子:

http://prntscr.com/5tfkz

http://prntscr.com/5tfkz

How can I efficiently go about achieving something like this?

我怎样才能有效地实现这样的目标?



@dknaack

@dknaack

I tried this code:

我试过这个代码:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {

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

        }

and got the following errors:

并得到以下错误:

Error 1 The type 'System.Drawing.Size' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. C:\Users\Test\Documents\Expression\Blend 4\Projects\WindBar_Prototype_1\WindBar_Prototype_1\MainWindow.xaml.cs 32 13 WindBar_Prototype_1

错误 1 ​​在未引用的程序集中定义了类型“System.Drawing.Size”。您必须添加对程序集“System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”的引用。C:\Users\Test\Documents\Expression\Blend 4\Projects\WindBar_Prototype_1\WindBar_Prototype_1\MainWindow.xaml.cs 32 13 WindBar_Prototype_1

and

Error 2 'System.Drawing.Size' does not contain a definition for 'Width' and no extension method 'Width' accepting a first argument of type 'System.Drawing.Size' could be found (are you missing a using directive or an assembly reference?) C:\Users\Test\Documents\Expression\Blend 4\Projects\WindBar_Prototype_1\WindBar_Prototype_1\MainWindow.xaml.cs 32 78 WindBar_Prototype_1

错误 2“System.Drawing.Size”不包含“Width”的定义,并且找不到接受“System.Drawing.Size”类型的第一个参数的扩展方法“Width”(您是否缺少 using 指令或程序集参考?)C:\Users\Test\Documents\Expression\Blend 4\Projects\WindBar_Prototype_1\WindBar_Prototype_1\MainWindow.xaml.cs 32 78 WindBar_Prototype_1

Any help?

有什么帮助吗?

采纳答案by dknaack

Description

描述

You can use Screenfrom System.Windows.Forms.

您可以使用ScreenSystem.Windows.Forms.

So add reference to the System.Windows.Forms.dlland System.Drawing.dll. Then change the Leftand Heightproperty in the MainWindow_Loadedmethod.

所以添加对System.Windows.Forms.dlland 的引用System.Drawing.dll。然后更改方法中的LeftandHeight属性MainWindow_Loaded

Sample

样本

public MainWindow()
{
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    this.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right - this.Width;
    this.Top = 0;
    this.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
}

More Information

更多信息

回答by yoozz

use startPosition property or location property

使用 startPosition 属性或 location 属性

回答by Eugene Cheverda

public MainWindow()
{
    InitializeComponent();
    WindowStartupLocation = WindowStartupLocation.Manual;
    Left =  System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width - Width;
}

回答by GameAlchemist

in your xaml :

在您的 xaml 中:

WindowStartupLocation="Manual" 

in the constructor :

在构造函数中:

 Left = System.Windows.SystemParameters.PrimaryScreenWidth - Width
 Top=0

回答by KurToMe

You can do this without referencing win forms assemblies by using SystemParameters. In the code behind for your window XAML:

您可以使用SystemParameters. 在您的窗口 XAML 背后的代码中:

MainWindow() {
    InitializeComponents();

    this.Loaded += new RoutedEventHandler(
      delegate(object sender, RoutedEventArgs args) {
        Width = 300;
        Left = SystemParameters.VirtualScreenLeft;
        Height = SystemParameters.VirtualScreenHeight;
    }
}

SystemParameters documentation

系统参数文档

回答by Jairam Kamath

<body>
<script>
function myfunc()
{
    w1=window.open('http://www.google.com','Google','left=0,top=0,width=250px,height=250px');
    w2=window.open('http://www.yahoomail.com','Yahoo Mail','left=1166,top=0,width=250px,height=250px');
    w3=window.open('http://www.people.com','People Magazine','left=1166,top=500,width=250px,height=250px');
    w4=window.open('http://www.time.com','Time Magazines','left=0,top=500,width=250px,height=250px');
    w5=window.open('http://www.ew.com','Entertainment Weekly','left=550,top=250,width=250px,height=250px');

}

function myclose()
{
 w1.close(); w2.close(); w3.close(); w4.close(); w5.close();
 w6=window.open('smartwindow.html',' mywindow',',');
}
</script>
    <div id="cover">
    <img src="images/abstract.jpeg" id="1" width="500px" height="400px" alt="color defined"onClick="myfunc()"/>
     <input type="button" value="click to close all windows" onClick="myclose()"/>
     </div>
</body>