窗体的控件由于电脑分辨率的不同,导致控件在不同分辨率下发生偏移。修改思路:根据当前电脑分辨率,修改location和size,让控件自适应当前屏幕。
原图:
将代码放入form_load即可(可根据自己实际情况进行修改),代码如下:
//设置初始窗体的宽高
int oldWidth = 1480;
int oldHeight = 783;
//获取当前窗体的宽高
int newWidth = this.ClientRectangle.Width;
int newHeight = this.ClientRectangle.Height;
//当前屏幕的宽高
Rectangle ScreenArea = System.Windows.Forms.Screen.GetWorkingArea(this);
int curWidth = ScreenArea.Width;
int curHeight = ScreenArea.Height;
//MessageBox.Show("当前窗体的宽高:" + curWidth +" "+ curHeight, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Width = curWidth;
this.Height = curHeight;
//遍历窗体上所有的控件
foreach (Control ctl in this.Controls)
{
if (ctl is Button)
{
//MessageBox.Show("这是按钮", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (ctl is TextBox)
{
if (ctl.Name == "txtUserName")
{
//适应位置
double widthProportion = Math.Round((double)ctl.Location.X / oldWidth, 4);
double heightProportion = Math.Round((double)ctl.Location.Y / oldHeight, 4);
ctl.Location = new Point(Convert.ToInt32(widthProportion * curWidth)-10, Convert.ToInt32(heightProportion * curHeight)-15);
//MessageBox.Show("当前控件的宽高:" + Convert.ToInt32(widthProportion * curWidth) + " " + widthProportion, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//适应大小
double widthSizeProportion = Math.Round((double)ctl.Size.Width / oldWidth, 4);
double heightSizeProportion = Math.Round((double)ctl.Size.Height / oldHeight, 4);
ctl.Size = new Size(Convert.ToInt32(widthSizeProportion*curWidth),Convert.ToInt32(heightSizeProportion*curHeight));
}
if (ctl.Name == "txtPassword")
{
double widthProportion = Math.Round((double)ctl.Location.X / oldWidth, 4);
double heightProportion = Math.Round((double)ctl.Location.Y / oldHeight, 4);
ctl.Location = new Point(Convert.ToInt32(widthProportion * curWidth)-10, Convert.ToInt32(heightProportion * curHeight)-15);
//适应大小
double widthSizeProportion = Math.Round((double)ctl.Size.Width / oldWidth, 4);
double heightSizeProportion = Math.Round((double)ctl.Size.Height / oldHeight, 4);
ctl.Size = new Size(Convert.ToInt32(widthSizeProportion * curWidth), Convert.ToInt32(heightSizeProportion * curHeight));
}
}
if(ctl is Label)
{
if (ctl.Name == "lblLogin")
{
double widthProportion = Math.Round((double)ctl.Location.X / oldWidth, 4);
double heightProportion = Math.Round((double)ctl.Location.Y / oldHeight, 4);
ctl.Location = new Point(Convert.ToInt32(widthProportion * curWidth)-10, Convert.ToInt32(heightProportion * curHeight)-15);
//适应大小
double widthSizeProportion = Math.Round((double)ctl.Size.Width / oldWidth, 4);
double heightSizeProportion = Math.Round((double)ctl.Size.Height / oldHeight, 4);
ctl.Size = new Size(Convert.ToInt32(widthSizeProportion * curWidth), Convert.ToInt32(heightSizeProportion * curHeight));
}
if (ctl.Name == "lblExit")
{
double widthProportion = Math.Round((double)ctl.Location.X / oldWidth, 4);
double heightProportion = Math.Round((double)ctl.Location.Y / oldHeight, 4);
ctl.Location = new Point(Convert.ToInt32(widthProportion * curWidth)-17, Convert.ToInt32(heightProportion * curHeight)-15);
//适应大小
double widthSizeProportion = Math.Round((double)ctl.Size.Width / oldWidth, 4);
double heightSizeProportion = Math.Round((double)ctl.Size.Height / oldHeight, 4);
ctl.Size = new Size(Convert.ToInt32(widthSizeProportion * curWidth), Convert.ToInt32(heightSizeProportion * curHeight));
}
}
if(ctl is ComboBox)
{
}
}
修改后,显示正常。