C# 如何在任何分辨率下找到屏幕中心
如何在任何分辨率下找到屏幕中心 <>我希望我的程序出现在屏幕的中间- 10(在y轴)。 (我的程序不是最大大小) 我在WinForm中使用C#。您可以将主WinForm的属性设置为C# 如何在任何分辨率下找到屏幕中心,c#,winforms,C#,Winforms,如何在任何分辨率下找到屏幕中心 我希望我的程序出现在屏幕的中间- 10(在y轴)。 (我的程序不是最大大小) 我在WinForm中使用C#。您可以将主WinForm的属性设置为中心屏幕。然后,如果您希望它以某种方式显示在相对于屏幕中心的不同位置,可以使用和属性添加或减去所需的像素数。这可能会对您有所帮助 private void center_Load(object sender, System.EventArgs e) { // Position the Form on The screen
中心屏幕。然后,如果您希望它以某种方式显示在相对于屏幕中心的不同位置,可以使用和属性添加或减去所需的像素数。这可能会对您有所帮助
private void center_Load(object sender, System.EventArgs e)
{
// Position the Form on The screen taking in account
the resolution
//
Rectangle screenRect = Screen.GetBounds(Bounds);
// get the Screen Boundy
ClientSize = new Size((int)(screenRect.Width/2),
(int)(screenRect.Height/2)); // set the size of the form
Location = new
Point(screenRect.Width/2-ClientSize.Width/2,
screenRect.Height/2-ClientSize.Height/2); // Center the Location of
the form.
}
您应该能够这样做:
var screensize = Screen.PrimaryScreen.Bounds;
var programsize = Bounds;
Location = new Point( (screensize.X-programsize.X)/2,
(screensize.Y-programsize.Y)/2 - 10 );
不过,我还是同意@DarinDimitrov的建议。。。如果您需要了解屏幕边界:
System.Windows.Forms.Screen.PrimaryScreen.Bounds
或者,考虑到任务栏:
System.Windows.Forms.Screen.PrimaryScreen.WorkingArea
…或某些变体尝试以下操作:
new Point((this.Width + this.Location.X) / 2 - popupControlContainer1.Width / 2,
(this.Height + this.Location.Y) / 2 - popupControlContainer1.Height / 2)
希望它有帮助您需要的是,如果有两个或多个分辨率不同的屏幕,这可能会有问题……如果有两个或多个分辨率不同的屏幕,这可能会有问题。。。