窗口根据屏幕分辨率自动调整大小

double xrate ;
double yrate ;

//构造函数中
Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);
            xrate = rect.Width / 1024.0;
            yrate = rect.Height / 738.0;
            controlRsize((Control)this);


//控件大小、位置调整函数
private void controlRsize(Control cr)
      {
         
          if (cr.Controls.Count == 0)
          {
              cr.Width = (int)(cr.Width * xrate);
              cr.Height = (int)(cr.Height * yrate);
              return;
          }
          else
          {
              cr.Width = (int)(cr.Width * xrate);
              cr.Height = (int)(cr.Height * yrate);
              if (cr.Dock==DockStyle.None)
              {
                  cr.Location = new Point((int)(cr.Location.X * xrate),(int)(cr.Location.Y * yrate));
              }
              foreach (Control obj in cr.Controls)
                  controlRsize(obj);
          }
      }

 

你可能感兴趣的:(窗口根据屏幕分辨率自动调整大小)