C# 如何定位窗口';s启动时的位置在用户的右侧';谁的屏幕?
我目前正在C#中创建一个类似WPF的边栏应用程序。当用户启动应用程序时,我希望窗口自动将其自身定位到用户屏幕的一侧。我尝试了一些方法和谷歌搜索,但没有找到任何帮助 下面是我尝试做的一个例子: 我怎样才能有效地实现这样的目标C# 如何定位窗口';s启动时的位置在用户的右侧';谁的屏幕?,c#,wpf,window,location,sidebar,C#,Wpf,Window,Location,Sidebar,我目前正在C#中创建一个类似WPF的边栏应用程序。当用户启动应用程序时,我希望窗口自动将其自身定位到用户屏幕的一侧。我尝试了一些方法和谷歌搜索,但没有找到任何帮助 下面是我尝试做的一个例子: 我怎样才能有效地实现这样的目标 @德克纳克 我尝试了以下代码: private void Window_Loaded(object sender, RoutedEventArgs e) { this.Left = System.Windows.Forms.Scr
@德克纳克 我尝试了以下代码:
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\u Prototype\u 1\WindBar\u Prototype\u 1\MainWindow.xaml.cs 32 13 WindBar\u Prototype\u 1
及
错误2“System.Drawing.Size”不包含“Width”的定义,并且找不到接受“System.Drawing.Size”类型的第一个参数的扩展方法“Width”(是否缺少using指令或程序集引用?)C:\Users\Test\Documents\Expression\Blend 4\Projects\WindBar\u Prototype\u 1\WindBar\u Prototype\u 1\MainWindow.xaml.cs 32 78 WindBar\u Prototype\u 1
有什么帮助吗 描述
您可以从System.Windows.Forms
使用Screen
因此,添加对System.Windows.Forms.dll
和System.Drawing.dll
的引用。然后在main窗口\u Loaded
方法中更改Left
和Height
属性
样品
更多信息
public MainWindow()
{
InitializeComponent();
WindowStartupLocation = WindowStartupLocation.Manual;
Left = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width - Width;
}
WindowStartupLocation="Manual"
在构造函数中:
Left = System.Windows.SystemParameters.PrimaryScreenWidth - Width
Top=0
通过使用
SystemParameters
,您可以在不引用win forms程序集的情况下执行此操作。在窗口XAML的代码隐藏中:
MainWindow() {
InitializeComponents();
this.Loaded += new RoutedEventHandler(
delegate(object sender, RoutedEventArgs args) {
Width = 300;
Left = SystemParameters.VirtualScreenLeft;
Height = SystemParameters.VirtualScreenHeight;
}
}
函数myfunc()
{
w1=窗口。打开('http://www.google.com'谷歌','左=0,顶=0,宽=250px,高=250px';
w2=窗口。打开('http://www.yahoomail.com'雅虎邮件','左=1166,顶=0,宽=250px,高=250px';
w3=窗口。打开('http://www.people.com","人物杂志","左=1166,顶=500,宽=250px,高=250px";;
w4=窗口。打开('http://www.time.com‘时间杂志’,‘左=0,顶=500,宽=250px,高=250px’;
w5=窗口。打开('http://www.ew.com‘娱乐周刊’、‘左=550,顶=250,宽=250px,高=250px’;
}
函数myclose()
{
w1.close();w2.close();w3.close();w4.close();w5.close();
w6=window.open('smartwindow.html','mywindow',',',');
}
startPosition属性没有屏幕一侧的位置我现在无法尝试该代码,但表单的class a this.position属性中没有,您可以在其中设置自定义点吗?比如:this.position=new Point(x,y)?@yoz cia问题是关于WPF Window.private void SetWindowPosition(){Left=SystemParameters.PrimaryScreenWidth-(double)GetValue(WidthProperty);Top=0;}@JamesDidzun您是否也引用了System.Drawing.dll
?好的,我刚刚尝试过,构建它时没有任何问题。唯一的问题是,它不在屏幕的一侧!看:-再次感谢你,好像事件没有被触发。您可以设置断点以查看是否调用该方法。查看我的更新答案this.Loaded+=newroutedeventhandler(主窗口已加载)事实上,我解决了这个问题。我在窗户上有一些空的地方。。。哈哈。谢谢你的帮助-最好的答案很乐意帮忙!祝你今天愉快System.Windows.Forms.Screen.PrimaryScreen
更适合您有多个屏幕的情况MikroDel如何更好?我使用一个三联显示器设置,并排并排,第三个在中间两个右上方。使用SystemParameters,我实际上得到了我所需要的一切。WindowStartupLocation=WindowStartupLocation.Manual代码>是一个好的hintHuh吗?这是HTML,不是XAML/WPF/C。你需要阅读问题,而不仅仅是标题。
MainWindow() {
InitializeComponents();
this.Loaded += new RoutedEventHandler(
delegate(object sender, RoutedEventArgs args) {
Width = 300;
Left = SystemParameters.VirtualScreenLeft;
Height = SystemParameters.VirtualScreenHeight;
}
}
<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>