专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WinForm

控件随窗口大小而改变(来源于小抽奖系统)

发布时间:2011-06-23 13:52:00 文章来源:www.iduyao.cn 采编人员:星星草
控件随窗口大小而改变(来自小抽奖系统)

一、在做小抽奖系统时,遇到了个问题,就是控件要随着窗口的放大,位置和大小也随着改变,在网上找了很多资料,都是修改Anchor和Dock属性值,但不符合我想要的效果;皇天不负苦心人啊,最后终于让我找到了(如下)

 private float X, 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)
            {

                string[] mytag = con.Tag.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 Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }

        }

        private void FrmClinicalTV_Load(object sender, EventArgs e)
        {
            this.Resize += new EventHandler(FrmClinicalTV_Resize);

            X = this.Width;
            Y = this.Height;
            //   y = this.statusStrip1.Height;
            setTag(this);
        }

        private void FrmClinicalTV_Resize(object sender, EventArgs e)
        {
            // throw new Exception("The method or operation is not implemented.");  
            float newx = (this.Width) / X;
            //  float newy = (this.Height - this.statusStrip1.Height) / (Y - y);  
            float newy = this.Height / Y;
            setControls(newx, newy, this);
            //this.Text = this.Width.ToString() + " " + this.Height.ToString();
        }
View Code

二、在启动winfrom窗体时,界面会出现闪烁的情况,这就会影响用户的体验感了,加入一段代码就可以减少这种情况的出现

 //减少闪烁
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);

 

1楼coder情缘
抽奖用到了什么算法
Re: evangogo
@coder情缘,哈哈,有点麻烦的算法,因为比较小,我直接用switch...case和for循环,我觉得用委托应该也可以,只是委托我不是很熟;整个系统的过程:触发定时器的事件,再随机抽取10个数,然后存到泛型集合,在for循环这个集合,然后switch....case赋值和对比集合里的值...
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: