最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#中通过代码改变/设置窗口的位置(Location)

C# crifan 10820浏览 0评论

【问题】

折腾:

C#中获得当前任务栏的位置和高度

的过程中,想要通过程序代码的方式,去设置窗口的位置。

 

注:此处不是要设置启动窗口的位置的.

因为启动窗口的位置,很好设置,直接通过设置对应的属性为以下其中一种即可:

FormStartPosition Enumeration

Member name

Description

Manual

The position of the form is determined by the Location property.

CenterScreen

The form is centered on the current display, and has the dimensions specified in the form’s size.

WindowsDefaultLocation

The form is positioned at the Windows default location and has the dimensions specified in the form’s size.

WindowsDefaultBounds

The form is positioned at the Windows default location and has the bounds determined by Windows default.

CenterParent

The form is centered within the bounds of its parent form.

 

【解决过程】

1.当将计算好的,窗口的位置,赋值给当前的location,结果无法赋值:

Cannot modify the retuan value of ‘System.Windows.Forms.Location’ because it is not a variable

cannot modify location

2.无奈,后来继续尝试,想要通过StartPosition去赋值,结果发现只能写成:

this.StartPosition = FormStartPosition.Manual;

还是无法设置具体的位置。

3.后来参考:

C#指定窗口显示位置

窗体的InitializeComponent中的,对于某个LinkLabel设置位置的方法:

this.lklOpenFile.Location = new System.Drawing.Point(34, 22);

然后去写成:

this.Location = new System.Drawing.Point(xPos, yPos);

然后看看结果如何。

然后就可以实现所需要的,设置窗口到指定位置了:

taskbar bottom effect

 

【总结】

C#中,想要通过程序代码设置窗口的位置的话,是不能直接设置Location.X和Location.Y的。

而只能通过新new一个point类型然后赋值给Location才可以:

this.Location = new System.Drawing.Point(xPos, yPos);

转载请注明:在路上 » 【已解决】C#中通过代码改变/设置窗口的位置(Location)

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 这和窗体不窗体没关系…… 而是将结构体作为某个类的成员后,不能够再对该结构体的成员单独修改,而是只能替换整个结构体才行。
    smallerxie11年前 (2013-07-29)回复
85 queries in 0.168 seconds, using 22.10MB memory