如何将启动时窗口的位置定位到用户屏幕的右侧?
我目前正在用 C# 创建一个类似侧边栏的 WPF 应用程序。当用户启动应用程序时,我希望窗口自动将其自身定位到用户屏幕的一侧。我尝试了一些方法和谷歌搜索,但没有找到任何帮助。
这是我想做的一个例子:
我怎样才能有效去实现这样的事情吗?
@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
和
错误 2 'System.Drawing.Size' 不包含 'Width' 的定义,也没有扩展名方法“Width”接受类型的第一个参数可以找到“System.Drawing.Size”(是否缺少 using 指令或程序集引用?) C:\Users\Test\Documents\Expression\Blend 4\Projects\WindBar_Prototype_1\WindBar_Prototype_1\MainWindow.xaml.cs 32 78 WindBar_Prototype_1
有帮助吗?
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
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
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
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
描述
您可以使用
System.Windows.Forms
中的Screen
。因此,添加对 System.Windows.Forms.dll 和 System.Drawing.dll 的引用。然后更改
MainWindow_Loaded
方法中的Left
和Height
属性。示例
更多信息
Description
You can use
Screen
fromSystem.Windows.Forms
.So add reference to the
System.Windows.Forms.dll
andSystem.Drawing.dll
. Then change theLeft
andHeight
property in theMainWindow_Loaded
method.Sample
More Information
您可以使用
SystemParameters
来执行此操作,而无需引用 win 表单程序集。在窗口 XAML 的代码后面:SystemParameters 文档
You can do this without referencing win forms assemblies by using
SystemParameters
. In the code behind for your window XAML:SystemParameters documentation
在你的 xaml 中:
在构造函数中:
in your xaml :
in the constructor :
使用 startPosition 属性或 location 属性
use startPosition property or location property