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

发布于 2025-01-02 13:36:46 字数 1174 浏览 4 评论 0原文

我目前正在用 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,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:

http://prntscr.com/5tfkz

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我只土不豪 2025-01-09 13:36:46

描述

您可以使用System.Windows.Forms 中的Screen

因此,添加对 System.Windows.Forms.dll 和 System.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;
}

更多信息

Description

You can use Screen from System.Windows.Forms.

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.

Sample

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

More Information

家住魔仙堡 2025-01-09 13:36:46

您可以使用 SystemParameters 来执行此操作,而无需引用 win 表单程序集。在窗口 XAML 的代码后面:

MainWindow() {
    InitializeComponents();

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

SystemParameters 文档

You can do this without referencing win forms assemblies by using SystemParameters. In the code behind for your window XAML:

MainWindow() {
    InitializeComponents();

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

SystemParameters documentation

风情万种。 2025-01-09 13:36:46

在你的 xaml 中:

WindowStartupLocation="Manual" 

在构造函数中:

 Left = System.Windows.SystemParameters.PrimaryScreenWidth - Width
 Top=0

in your xaml :

WindowStartupLocation="Manual" 

in the constructor :

 Left = System.Windows.SystemParameters.PrimaryScreenWidth - Width
 Top=0
不回头走下去 2025-01-09 13:36:46
public MainWindow()
{
    InitializeComponent();
    WindowStartupLocation = WindowStartupLocation.Manual;
    Left =  System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width - Width;
}
public MainWindow()
{
    InitializeComponent();
    WindowStartupLocation = WindowStartupLocation.Manual;
    Left =  System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width - Width;
}
初见终念 2025-01-09 13:36:46

使用 startPosition 属性或 location 属性

use startPosition property or location property

水波映月 2025-01-09 13:36:46
<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>
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文