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

14

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

这是我想做的事情的示例:

http://prntscr.com/5tfkz

如何有效地实现这样的功能?


@dknaack

我尝试了这段代码:

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;

        }

并出现以下错误:

错误 1:类型“System.Drawing.Size”在未被引用的程序集中定义。您必须添加对程序集“System.Drawing,版本=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

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

有帮助吗?

6个回答

17

描述

您可以使用System.Windows.Forms中的Screen。 因此,添加对System.Windows.Forms.dllSystem.Drawing.dll的引用。 然后在MainWindow_Loaded方法中更改LeftHeight属性。

示例

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

更多信息


好的,我刚试了一下,现在构建没有任何问题了。唯一的问题是,它不在屏幕的侧面!看这里:http://prntscr.com/5th4q - 再次感谢 - anonymous
似乎事件没有被触发。您可以设置断点以查看方法是否被调用。请查看我的更新答案 this.Loaded += new RoutedEventHandler(MainWindow_Loaded); - dknaack
其实,我找到问题了。我的窗口里有一些空白...哈哈。谢谢你的帮助!-最佳答案- - anonymous
还有一件事...我怎样才能把侧边栏移到右边而不是左边? - anonymous
这个方法或事件在哪里? - Nathan McKaskle
显示剩余4条评论

5
你可以使用 SystemParameters 来完成此操作,而无需引用 Win Forms 框架。在窗口 XAML 的代码后台中:
MainWindow() {
    InitializeComponents();

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

SystemParameters 文档


System.Windows.Forms.Screen.PrimaryScreen 在你有多个屏幕的情况下更为适用。 - MikroDel
MikroDel有何优势? 我使用三个显示器,两个并排放置,第三个则在这两个的正中间上方。 通过SystemParameters,我实际上得到了我需要的一切。 - Steven Borges

2
在您的XAML代码中:
WindowStartupLocation="Manual" 

在构造函数中:
 Left = System.Windows.SystemParameters.PrimaryScreenWidth - Width
 Top=0

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

WindowStartupLocation = WindowStartupLocation.Manual; 是一个很好的提示。 - MikroDel

0

使用startPosition属性或location属性


1
startPosition属性没有屏幕边缘位置。 - anonymous
我现在无法尝试这段代码,但是在表单类中不是有一个this.position属性吗?你可以设置自定义点,例如:this.position = new Point(x, y)。 - yoozz
@yoz cia 这个问题是关于 WPF 窗口的。 - Eugene Cheverda
私有 void SetWindowPosition(){ Left = SystemParameters.PrimaryScreenWidth - (double)GetValue(WidthProperty); Top = 0; } - yoozz

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

1
什么?这是HTML,不是XAML/WPF/C#。你需要阅读问题,而不仅仅是标题。 - Richard Robertson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接