C# form窗体内控件与背景发生偏离

  窗体的控件由于电脑分辨率的不同,导致控件在不同分辨率下发生偏移。修改思路:根据当前电脑分辨率,修改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)
                {
                    
                }
            }

修改后,显示正常。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姑苏 · 安阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值