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框架似乎不存在。
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框架似乎不存在。
相关文章推荐
- C#中Invoke和BeginInvoke的区别
- C#中字符串以回车分割解决方案
- C#写二进制文件,只写入byte[]数组的前两个数字
- static 类深入了解
- C#开发教程之利用特性自定义数据导出到Excel
- C#开发教程之ftp操作方法整理
- 深入理解C#中的Delegate
- C# 工具函数
- C# 工具函数
- c# Hashtable 的使用
- C# 隐式接口和显示接口的区别
- C#实现简单获取扫码枪信息代码
- C#后台如何获取客户端访问系统型号
- C# ArrayList 的使用
- 小代码 复习c#
- c# 反射列子
- C# 通过比对哈希码判断两个文件内容是否相同
- C#判断字母大小写
- 学习笔记(C#控制台输出判断(yes/no))
- C#开发学习笔记:动态绑定控件事件