如何将启动时窗口的位置定位在用户屏幕的右侧? [英] How can I position the window's position on startup to the right side of the user's screen?
问题描述
我目前正在用C#创建类似侧边栏的WPF应用程序.当用户启动应用程序时,我希望窗口将其自身自动定位到用户屏幕的侧面.我已经尝试了一些方法和Google搜索,但是没有找到任何帮助.
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.
这是我要做的事的一个例子:
Here's an example of what I'm trying to do:
我如何有效地实现这样的目标?
How can I efficiently go about achieving something like this?
@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,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
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
和
错误2'System.Drawing.Size'不包含'Width'的定义,并且找不到扩展方法'Width'接受类型为'System.Drawing.Size'的第一个参数(您是否缺少使用指令还是程序集引用?)C:\ Users \ Test \ Documents \ Expression \ Blend 4 \ Projects \ WindBar_Prototype_1 \ WindBar_Prototype_1 \ MainWindow.xaml.cs 32 78 WindBar_Prototype_1
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
有帮助吗?
推荐答案
说明
您可以使用System.Windows.Forms
中的Screen
.
因此添加对System.Windows.Forms.dll
和System.Drawing.dll
的引用.然后在MainWindow_Loaded
方法中更改Left
和Height
属性.
So add reference to the System.Windows.Forms.dll
and System.Drawing.dll
. Then change the Left
and Height
property in the MainWindow_Loaded
method.
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;
}
更多信息
- MSDN-屏幕类
- MSDN - Screen Class
More Information
这篇关于如何将启动时窗口的位置定位在用户屏幕的右侧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!