2012-06-25 32 views
3

如何在任何分辨率找到屏幕的中心?如何在任何分辨率找到屏幕的中心

我希望我的程序出现在屏幕-10的中间(在y轴上)。

(我的程序是不是在最大尺寸)

我用C#在winform工作。

+0

你需要的是['Screen.WorkingArea'(HTTP:// MSDN。 microsoft.com/en-us/library/system.windows.forms.screen.workingarea.aspx) – V4Vendetta

回答

4

您可以将主WinForm的StartPosition属性设置为CenterScreen。然后,如果您希望它以相对于此屏幕中心的不同位置出现,您可以使用TopLeft属性来添加或减去所需的像素数。

0

这可以帮助你

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. 
} 
0

你应该能够做到这一点是这样的:

var screensize = Screen.PrimaryScreen.Bounds; 
var programsize = Bounds; 
Location = new Point((screensize.X-programsize.X)/2, 
            (screensize.Y-programsize.Y)/2 - 10); 
+0

这可能会有问题,如果有两个或多个屏幕不同的分辨率... – webber2k6

3

我会去用@ DarinDimitrov的建议,但是...如果你需要知道的屏幕范围:

System.Windows.Forms.Screen.PrimaryScreen.Bounds 

,或者考虑到任务栏:

System.Windows.Forms.Screen.PrimaryScreen.WorkingArea 

...或其变型

+0

这可能会有问题,如果有两个或更多不同分辨率的屏幕...... – webber2k6

0

试试这个:

new Point((this.Width + this.Location.X)/2 - popupControlContainer1.Width/2, 
      (this.Height + this.Location.Y)/2 - popupControlContainer1.Height/2) 

希望它可以帮助