zoukankan      html  css  js  c++  java
  • C#:控件随着窗体一起变化大小

    在网上看了一些别人写的方法:

    方法一:(自己实践过)

    看见有人说:不要使用Scale这个函数,这个函数很诱人,可以改变控件大小,这个函数用了SizeF这个结构,然而第一改变比如最大化后是对的,但是还原后一切都不是那么回事了。(具体什么情况不是很了解)

     
    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); //每次使用的都是最初始的控件大小,保证准确无误。 } }

    方法二:

     public Form1()
            {
                InitializeComponent();
                Hashtable ht = new Hashtable();//使用哈希表存放位置以及控件在容器中的位置比例
                ht.Add("width", Size.Width);
                ht.Add("height", Size.Height);
                foreach (Control ctrl in this.Controls)
                {
                    ht.Add(ctrl.Name + "X", ctrl.Location.X / (double)Size.Width);//存储控件在容器中的相对比例
                    ht.Add(ctrl.Name + "Y", ctrl.Location.Y / (double)Size.Height);//存储控件在容器中的相对比例
                    ctrl.Tag = ctrl.Size;              
                }
                Tag = ht;
            }
            private void Form1_Resize(object sender, EventArgs e)
            {
                Hashtable scale = (Hashtable)Tag;
                foreach (Control ctrl in this.Controls)
                {
                    ctrl.Left = (int)(Size.Width * (double)scale[ctrl.Name + "X"]);
                    ctrl.Top = (int)(Size.Height * (double)scale[ctrl.Name + "Y"]);
                    ctrl.Width = (int)(Size.Width*1.0f / (int)scale["width"] * ((Size)ctrl.Tag).Width);
                    ctrl.Height = (int)(Size.Height*1.0f / (int)scale["height"] * ((Size)ctrl.Tag).Height);
                    //每次使用的都是最初始的控件大小,保证准确无误。
                }
            }

    以上方法可以实现如题功能,但是如果我们在TextBox 里写入的内容超过text的length,会有一部分文字被隐藏,当我们拉伸窗体后,我们会发现,窗体里控件的size都增大了,但是textbox里隐藏部分的内容还是没有显示出来。这样看起来还是有点小不爽,对吧。那么下面用一个取巧的方法去掉这一瑕疵。

    在执行Resize之前,将TextBox里的value记录下来,待Resize执行之后,重新赋值:

     
     private void Form1_Resize(object sender, EventArgs e)
           
    {
    // 获取TextBox的个数 int textBoxCount = 0; foreach (Control c in Controls) { if (c.GetType().Name == "TextBox") { textBoxCount++; } } //将TextBox里的内容记下来后,设置为空 string[] tmpTextBoxValue = new string[textBoxCount]; int j = 0; foreach (Control c in Controls) { if (c.GetType().Name == "TextBox") { tmpTextBoxValue[j] = c.Text; j++; c.Text = ""; } } //执行resize 事件 float[] scale = (float[])Tag; int i = 2; foreach (Control crl in Controls) { crl.Left = (int)(Size.Width * scale[i++]); crl.Top = (int)(Size.Height * scale[i++]); crl.Width = (int)(Size.Width / (float)scale[0] * ((Size)crl.Tag).Width); crl.Height = (int)(Size.Height / (float)scale[1] * ((Size)crl.Tag).Height); } //重新写入Textbox的值 int m = 0; foreach (Control c in Controls) { if (c.GetType().Name =="TextBox") { //c.Focus(); c.Text = tmpTextBoxValue[m]; m++; } } }

    有取巧的嫌疑,但还是很好的实现了想要的效果。

  • 相关阅读:
    存储过程
    C++学习总结
    Android快速开发系列 10个常用工具类
    SimpleHttpServer的学习之总体架构
    SimpleHttpServer的学习之UML
    SimpleHttpServer的学习(1)
    map用法
    idea 中resources下于java包名相同的包不能导入XML文件问题
    leetcode621
    Yukari's Birthday 枚举+二分 过程注意数据的溢出问题 HDU4430
  • 原文地址:https://www.cnblogs.com/Alvin-x/p/3243383.html
Copyright © 2011-2022 走看看