Winform 空间位置及大小自适应

文章参考:http://blog.csdn.net/loveme1204/archive/2007/12/24/1964577.aspx

是一篇vb编写的,我只是把它翻译成c#的.

其实Anchor和Dock属性也可以实现,但好象只对容器效果理想,而且字体大小也没有变化.最近论坛上有好多这样的贴子,具体实现代码如下:

[csharp]  view plain copy
  1. private void  setTag(Control cons)  
  2. {  
  3.     foreach (Control con in cons.Controls)  
  4.     {  
  5.         con.Tag = con.Width +":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;  
  6.         if (con.Controls.Count > 0)  
  7.             setTag(con);                  
  8.     }  
  9. }  
  10. private void setControls(float   newx, float  newy, Control  cons)  
  11. {  
  12.     foreach (Control  con in cons .Controls )  
  13.     {  
  14.   
  15.         string[] mytag = con.Tag.ToString().Split(new char[] { ':' });  
  16.         float a = Convert.ToSingle(mytag[0]) * newx;  
  17.         con.Width = (int)a;  
  18.         a=Convert.ToSingle(mytag[1]) * newy;  
  19.         con.Height = (int)(a);  
  20.         a=Convert.ToSingle(mytag[2]) * newx;  
  21.         con.Left = (int)(a);  
  22.         a=Convert.ToSingle(mytag[3]) * newy;  
  23.         con.Top = (int)(a);  
  24.         Single currentSize = Convert.ToSingle (mytag[4]) * newy;  
  25.         con .Font =new Font (con.Font .Name ,currentSize,con.Font .Style ,con.Font .Unit );  
  26.         if(con.Controls .Count >0)  
  27.         {  
  28.             setControls (newx ,newy ,con );  
  29.         }  
  30.     }  
  31.   
  32. }  
  33.   
  34. void Form1_Resize(object sender, EventArgs e)  
  35. {  
  36.    // throw new Exception("The method or operation is not implemented.");  
  37.     float  newx = (this.Width )/ X;  
  38.   //  float newy = (this.Height - this.statusStrip1.Height) / (Y - y);  
  39.     float newy = this.Height / Y;  
  40.     setControls(newx, newy, this);  
  41.     this.Text = this.Width.ToString() +" "this.Height.ToString();  
  42.   
  43. }  
[csharp]  view plain copy
  1. 在Form_Load里面添加:  
[csharp]  view plain copy
  1. this.Resize += new EventHandler(Form1_Resize);   
  2.   
  3. X = this.Width;   
  4. Y = this.Height;   
  5. y = this.statusStrip1.Height;   
  6. setTag (this);  

当然定义了两个全局变量,private float X; private float Y;

细心的你一定会发现还有个y ;这是因为有statusStrip1之类的控件,这是就一定要注意,窗体的实际宽度和高度一定要减去statusStrip1之类.

如果是容器,最好将Anchor和Dock属性也修改过来.

效果还不错.

帖子:http://topic.csdn.net/u/20080516/13/12a39498-314f-4a7b-a210-6988f79da0f4.htm


在上面程序中,如果不用到控件的tag属性,可以正常运行。否则会带来问题,所以,在此基础上改进了一下

1

private void setOriginControlsSize(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.Name.Length == 0)
                    continue;
                string strCtlSize = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                dicControls.Add(con.Name, strCtlSize);
                Console.WriteLine("{0}:{1}", con.Name, strCtlSize);
                if (con.Controls.Count > 0)
                    setOriginControlsSize(con);
            }
        }
        private void setNewControlsSize(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                object objCtlSize;
                Console.WriteLine("{0}", con.Name);
                if (false == dicControls.TryGetValue(con.Name, out objCtlSize))
                    continue;
                Console.WriteLine("{0}", objCtlSize);
                string[] mytag = objCtlSize.ToString().Split(new char[] { ':' });
                float a = Convert.ToSingle(mytag[0]) * newx;
                con.Width = (int)a;
                a = Convert.ToSingle(mytag[1]) * newy;
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * newy;
                con.Font = new System.Drawing.Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setNewControlsSize(newx, newy, con);
                }
            }

        }

需要定义变量 private Dictionary<string, object> dicControls;



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值