您的位置:首页 > 编程语言 > C#

C#中怎样使控件随着窗体一起变化大小(常见困难以及修正)

2016-07-26 13:36 447 查看
这个是多么古老的话题啊,呵呵,可惜网上的解决方案都不大好。都是针对VB或者使用Dock和Anchor属性的。我实在看不过去,所以自己总结了一下。

1.使用一些布局面板,比如FlowLayoutPanel,TabLayOutPanel之类,但是缺点就是样式太死了。所以本人不采用。

2.采用Dock和Anchor属性。这类虽然鼠标点几下在属性栏设置一下就好,但是缺乏灵活性。只有上下左右中间几种选择。

3.采用Form_Resize()。这种方法最灵活,思路是窗体变化时,直接重写这个变化函数。

那么如何写呢?第一要将原来窗体的属性以及各个控件的所占位置的比例存入Tag中(Tag可以存任何东西哦!)

废话少说,直接贴代码:

public Form1()

        {

            InitializeComponent();

            int count = this.Controls.Count * 2+2;

            float[] factor = new float[count];

            int i = 0;

            factor[i++] = Size.Width;

            factor[i++] = Size.Height;

            foreach(Control ctrl in this.Controls)

            {

                factor[i++] = ctrl.Location.X / (float)Size.Width;

                factor[i++] = ctrl.Location.Y / (float)Size.Height;

            }

            Tag = factor;

        }//在初始化的时候将一切窗体位置的参数存入Tag

private void Form1_Resize(object sender, EventArgs e)

        {

            float[] scale = (float[])Tag;

            int i = 2;

            foreach (Control ctrl in this.Controls)

            {

                ctrl.Left = (int)(Size.Width * scale[i++]);

                ctrl.Top = (int)(Size.Height * scale[i++]);

                ctrl.Width = (int)(Size.Width / (float)scale[0] * ctrl.Width);

                ctrl.Height = (int)(Size.Height / (float)scale[1] * ctrl.Height);

            }

            scale[0] = Size.Width;

            scale[1] = Size.Height;

            Tag = scale;

        }//改写大小

上面的代码似乎没有问题,但是实际上,每次控件大小的变化有误差,导致多次变化后控件严重变型,红色的代码部分存在严重问题。那么应该如何修改呢?

经过本人的修改与测试,下面的代码准确无误。

        public Form1()

        {

            InitializeComponent();

            int count = this.Controls.Count * 2+2;

            float[] factor = new float[count];

            int i = 0;

            factor[i++] = Size.Width;

            factor[i++] = Size.Height;

            foreach(Control ctrl in this.Controls)

            {

                factor[i++] = ctrl.Location.X / (float)Size.Width;

                factor[i++] = ctrl.Location.Y / (float)Size.Height;

                ctrl.Tag = ctrl.Size;

            }

            Tag = factor;

        }

        private void Form1_Resize(object sender, EventArgs e)

        {

            float[] scale = (float[])Tag;

            int i = 2;

            foreach (Control ctrl in this.Controls)

            {

                ctrl.Left = (int)(Size.Width * scale[i++]);

                ctrl.Top = (int)(Size.Height * scale[i++]);

                ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);

                ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);  

                //每次使用的都是最初始的控件大小,保证准确无误。

            }

        }

4.注意点和误区。千万不要使用Scale这个函数,这个函数很诱人,似乎可以改变控件大小,这个函数用了SizeF这个结构,然而第一改变比如最大化后是对的,但是你还原后一切都不是那么回事了。我也不知道是怎么回事。程序反正是不对的。如果有朋友使用过可以告诉我,谢谢。

5.还有人使用resize32.ocx这个控件。这个控件.net框架似乎不存在。

这个是多么古老的话题啊,呵呵,可惜网上的解决方案都不大好。都是针对VB或者使用Dock和Anchor属性的。我实在看不过去,所以自己总结了一下。

1.使用一些布局面板,比如FlowLayoutPanel,TabLayOutPanel之类,但是缺点就是样式太死了。所以本人不采用。

2.采用Dock和Anchor属性。这类虽然鼠标点几下在属性栏设置一下就好,但是缺乏灵活性。只有上下左右中间几种选择。

3.采用Form_Resize()。这种方法最灵活,思路是窗体变化时,直接重写这个变化函数。

那么如何写呢?第一要将原来窗体的属性以及各个控件的所占位置的比例存入Tag中(Tag可以存任何东西哦!)

废话少说,直接贴代码:

public Form1()

        {

            InitializeComponent();

            int count = this.Controls.Count * 2+2;

            float[] factor = new float[count];

            int i = 0;

            factor[i++] = Size.Width;

            factor[i++] = Size.Height;

            foreach(Control ctrl in this.Controls)

            {

                factor[i++] = ctrl.Location.X / (float)Size.Width;

                factor[i++] = ctrl.Location.Y / (float)Size.Height;

            }

            Tag = factor;

        }//在初始化的时候将一切窗体位置的参数存入Tag

private void Form1_Resize(object sender, EventArgs e)

        {

            float[] scale = (float[])Tag;

            int i = 2;

            foreach (Control ctrl in this.Controls)

            {

                ctrl.Left = (int)(Size.Width * scale[i++]);

                ctrl.Top = (int)(Size.Height * scale[i++]);

                ctrl.Width = (int)(Size.Width / (float)scale[0] * ctrl.Width);

                ctrl.Height = (int)(Size.Height / (float)scale[1] * ctrl.Height);

            }

            scale[0] = Size.Width;

            scale[1] = Size.Height;

            Tag = scale;

        }//改写大小

上面的代码似乎没有问题,但是实际上,每次控件大小的变化有误差,导致多次变化后控件严重变型,红色的代码部分存在严重问题。那么应该如何修改呢?

经过本人的修改与测试,下面的代码准确无误。

        public Form1()

        {

            InitializeComponent();

            int count = this.Controls.Count * 2+2;

            float[] factor = new float[count];

            int i = 0;

            factor[i++] = Size.Width;

            factor[i++] = Size.Height;

            foreach(Control ctrl in this.Controls)

            {

                factor[i++] = ctrl.Location.X / (float)Size.Width;

                factor[i++] = ctrl.Location.Y / (float)Size.Height;

                ctrl.Tag = ctrl.Size;

            }

            Tag = factor;

        }

        private void Form1_Resize(object sender, EventArgs e)

        {

            float[] scale = (float[])Tag;

            int i = 2;

            foreach (Control ctrl in this.Controls)

            {

                ctrl.Left = (int)(Size.Width * scale[i++]);

                ctrl.Top = (int)(Size.Height * scale[i++]);

                ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);

                ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);  

                //每次使用的都是最初始的控件大小,保证准确无误。

            }

        }

4.注意点和误区。千万不要使用Scale这个函数,这个函数很诱人,似乎可以改变控件大小,这个函数用了SizeF这个结构,然而第一改变比如最大化后是对的,但是你还原后一切都不是那么回事了。我也不知道是怎么回事。程序反正是不对的。如果有朋友使用过可以告诉我,谢谢。

5.还有人使用resize32.ocx这个控件。这个控件.net框架似乎不存在。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: