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

ano*_*ous 12 c# wpf location window sidebar

我目前正在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;

        }
Run Code Online (Sandbox Code Playgroud)

并得到以下错误:

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

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

有帮助吗?

dkn*_*ack 16

描述

您可以使用ScreenSystem.Windows.Forms.

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

样品

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;
}
Run Code Online (Sandbox Code Playgroud)

更多信息


Kur*_*oMe 5

您可以在不使用引用win表单程序集的情况下执行此操作SystemParameters.在窗口XAML背后的代码中:

MainWindow() {
    InitializeComponents();

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

SystemParameters文档