【问题标题】:How to get the position of a Windows Form on the screen?如何获取 Windows 窗体在屏幕上的位置?
【发布时间】:2013-01-09 16:05:23
【问题描述】:

我在 Visual Studio C# 2010 中编写一个 WinForms 应用程序,我想找出 WinForm 窗口左上角的位置(窗口的起始位置)。

我该怎么做?

【问题讨论】:

    标签: c# winforms position


    【解决方案1】:

    如果您是从表单本身访问它,那么您可以编写

    int windowHeight = this.Height;
    int windowWidth = this.Width;
    

    获取窗口的宽度和高度。还有

    int windowTop = this.Top; 
    int windowLeft = this.Left;
    

    获取屏幕位置。

    否则,如果您启动表单并从另一个表单访问它

    int w, h, t, l;
    using (Form form = new Form())
    {
        form.Show();
        w = form.Width;
        h = form.Height;
        t = form.Top;
        l = form.Left;
    }
    

    我希望这会有所帮助。

    【讨论】:

    • +1 但可能 int 应该是 double 类型。我正在使用 WPF 不确定它是否不同。
    【解决方案2】:

    Form.Location.XForm.Location.Y 会为您提供左上角的 X 和 Y 坐标。

    【讨论】:

    • 有一个问题:当我输入“控制”时。在您通常希望下拉的下拉菜单中(在键入“.”之后)没有选择“位置”的选项我该怎么办?我是否必须在代码顶部包含一些“使用”?
    • 可能还涉及其他拼写。试试 form.DesktopBoundsform.DesktopLocationform.Locationform.RestoreBoundsform。尺寸,也许其中之一会给你你所寻找的信息。另外值得注意的是,WinForms 属性列表会随着 .NET Framework 版本的演变而演变——4.5 的列表比 2.0 更丰富。
    • 但是所有关于 Form 的内容,如果你想要 Control 那么选择列表包括:control.Boundscontrol.ClientRectanglecontrol.ClientSizecontrol.Locationcontrol.Size,或者只是 control.Leftcontrol.Topcontrol.Widthcontrol.Height。由于 FormControl,因此 ....
    【解决方案3】:

    检查这个: Form.DesktopLocation Property

            int left = this.DesktopLocation.X;
            int top = this.DesktopLocation.Y;
    

    【讨论】:

      【解决方案4】:

      使用Form.Bounds.Top获取“Y”坐标,使用Form.Bounds.Left获取“X”坐标

      【讨论】:

        【解决方案5】:

        我有一个类似的情况,我的 Form2 需要 Form1 的屏幕位置。我通过其构造函数将form1的屏幕位置传递给form2来解决它:

        //Form1

         Point Form1Location;
                Form1Location = this.Location;
                Form2 myform2 = new Form2(Form1Location);
                myform2.Show();
        

        //Form2

         Point Form1Loc;
            public Form2(Point Form1LocationRef)
            {
                Form1Loc = Form1LocationRef;
                InitializeComponent();
        
            }
        

        【讨论】:

          【解决方案6】:

          也是 Left 和 Top 属性的组合(例如表单中的 this.Top)

          【讨论】:

            猜你喜欢
            • 2013-01-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-10
            相关资源
            最近更新 更多