当前位置: 移动技术网 > IT编程>开发语言>.net > C#之WinForm界面分辨率问题

C#之WinForm界面分辨率问题

2018年12月11日  | 移动技术网IT编程  | 我要评论

全民超人汉考克2,这该死的爱情,1984年鼠票

  在做上一个c#小工具的时候,当时为了处理界面最大化,分辨率问题,只是简单的用各种···panle控价简单随意的处理控件的大小位置,字体什么的就随缘了(貌似有点不负责任啊,嘿嘿~)。

  所以在开始第二个c#小工具的时候,就又想到了这个问题,下面就贴代码啦↓↓↓

 

public form1()
{
  initializecomponent();
  x = this.width;
  y = this.height;
  settag(this);
}
private float x;//定义当前窗体的宽度
private float y;//定义当前窗体的高度
private void settag(control cons)
{
  foreach (control con in cons.controls)
  {
    con.tag = con.width + ";" + con.height + ";" + con.left + ";" + con.top + ";" + con.font.size;
    if (con.controls.count > 0)
    {
      settag(con);
    }
  }
}
private void setcontrols(float newx, float newy, control cons)
{
  //遍历窗体中的控件,重新设置控件的值
  foreach (control con in cons.controls)
  {
    //获取控件的tag属性值,并分割后存储字符串数组
    if (con.tag != null)
    {
      string[] mytag = con.tag.tostring().split(new char[] { ';' });
      //根据窗体缩放的比例确定控件的值
      con.width = convert.toint32(system.convert.tosingle(mytag[0]) * newx);//宽度
      con.height = convert.toint32(system.convert.tosingle(mytag[1]) * newy);//高度
      con.left = convert.toint32(system.convert.tosingle(mytag[2]) * newx);//左边距
      con.top = convert.toint32(system.convert.tosingle(mytag[3]) * newy);//顶边距
      single currentsize = system.convert.tosingle(mytag[4]) * newy;//字体大小
      con.font = new font(con.font.name, currentsize, con.font.style, con.font.unit);
      if (con.controls.count > 0)
      {
        setcontrols(newx, newy, con);
      }
    }
  }
}

private void form1_resize(object sender, eventargs e)   
{
  float newx = (this.width) / x;
  float newy = (this.height) / y;
  setcontrols(newx, newy, this);
}

 

嗯,这个就到这就结束了。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网