当我点击按钮时,我想要扩大一个窗体,它应该在屏幕的中心,.so我写了下面的代码片段。
private void ord_Click(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.CenterScreen;
this.Size = new Size(1308,599);
this.Show();
}但是当我点击按钮时,窗口会变大,但是半个窗口都不能see.Here了,那就是图片。
我怎样才能摆脱这个问题呢?
我的代码出了什么问题?
发布于 2016-08-28 03:30:04
您可以使用Screen类的PrimaryScreen属性。
//this.StartPosition = FormStartPosition.CenterScreen;
//this.Show();除了设置表单的Size属性外,省略您已经编写的这些行:
private void ord_Click(object sender, EventArgs e)
{
this.Size = new Size(1308,599);
CenterForm();
}创建一个名为CenterForm()的方法,该方法将设置表单的新位置。您可以通过在按钮单击事件中调用此方法来实现此目的。
private void CenterForm()
{
int getWidth = Screen.PrimaryScreen.Bounds.Width;
int getHeight = Screen.PrimaryScreen.Bounds.Height;
int X = getWidth - this.Width;
int Y = getHeight - this.Height;
this.Location = new Point(X / 2, Y / 2);
}注意:当窗体的大小发生变化时,总是记得你的控件。
发布于 2016-08-28 03:33:10
你必须同时计算Size和Location
private void ord_Click(object sender, EventArgs e) {
// Ensure that suggested form size doesn't exceed the screen width and height
this.Size = new System.Drawing.Size(
Screen.GetWorkingArea(this).Width >= 1308 ? 1308 : Screen.GetWorkingArea(this).Width,
Screen.GetWorkingArea(this).Height >= 599 ? 599 : Screen.GetWorkingArea(this).Height);
// locate the form in the center of the working area
this.Location = new System.Drawing.Point(
(Screen.GetWorkingArea(this).Width - Width) / 2,
(Screen.GetWorkingArea(this).Height - Height) / 2);
}https://stackoverflow.com/questions/39184600
复制相似问题