Winform 控件随窗体大小改变而变化
2010-04-21 21:06
483 查看
有如下3种方法:
方法1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MarkPrinter
{
public partial class ResizeTest : Form
{
public float X;
public float Y;
public float y;
public ResizeTest()
{
InitializeComponent();
}
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);
}
}
}
void Form1_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();
}
private void ResizeTest_Load(object sender, EventArgs e)
{
this.Resize += new EventHandler(Form1_Resize);
X = this.Width;
Y = this.Height;
y = this.statusStrip1.Height;
setTag(this);
}
}
}
方法2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MarkPrinter
{
public partial class ResizeTest : Form
{
public float X;
public float Y;
public float y;
public ResizeTest()
{
InitializeComponent();
}
private void ResizeTest_Load(object sender, EventArgs e)
{
AutoScale(this);
}
/// <summary>
/// 控件随窗体自动缩放
/// </summary>
/// <param name="frm"></param>
public static void AutoScale(Form frm)
{
frm.Tag = frm.Width.ToString() + "," + frm.Height.ToString();
frm.SizeChanged += new EventHandler(frm_SizeChanged);
}
static void frm_SizeChanged(object sender, EventArgs e)
{
string[] tmp = ((Form)sender).Tag.ToString().Split(',');
float width = (float)((Form)sender).Width / (float)Convert.ToInt16(tmp[0]);
float heigth = (float)((Form)sender).Height / (float)Convert.ToInt16(tmp[1]);
((Form)sender).Tag = ((Form)sender).Width.ToString() + "," + ((Form)sender).Height;
foreach (Control control in ((Form)sender).Controls)
{
control.Scale(new SizeF(width, heigth));
}
}
}
}
用此方法时,如果界面一开始就要求最大化,在最大化之前就要调用AutoScale方法
方法3
将控件放进容器中, 然后使用控件的Dock属性来获取或设置控件停靠到父容器的哪一个边缘。(略)
方法1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MarkPrinter
{
public partial class ResizeTest : Form
{
public float X;
public float Y;
public float y;
public ResizeTest()
{
InitializeComponent();
}
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);
}
}
}
void Form1_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();
}
private void ResizeTest_Load(object sender, EventArgs e)
{
this.Resize += new EventHandler(Form1_Resize);
X = this.Width;
Y = this.Height;
y = this.statusStrip1.Height;
setTag(this);
}
}
}
方法2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MarkPrinter
{
public partial class ResizeTest : Form
{
public float X;
public float Y;
public float y;
public ResizeTest()
{
InitializeComponent();
}
private void ResizeTest_Load(object sender, EventArgs e)
{
AutoScale(this);
}
/// <summary>
/// 控件随窗体自动缩放
/// </summary>
/// <param name="frm"></param>
public static void AutoScale(Form frm)
{
frm.Tag = frm.Width.ToString() + "," + frm.Height.ToString();
frm.SizeChanged += new EventHandler(frm_SizeChanged);
}
static void frm_SizeChanged(object sender, EventArgs e)
{
string[] tmp = ((Form)sender).Tag.ToString().Split(',');
float width = (float)((Form)sender).Width / (float)Convert.ToInt16(tmp[0]);
float heigth = (float)((Form)sender).Height / (float)Convert.ToInt16(tmp[1]);
((Form)sender).Tag = ((Form)sender).Width.ToString() + "," + ((Form)sender).Height;
foreach (Control control in ((Form)sender).Controls)
{
control.Scale(new SizeF(width, heigth));
}
}
}
}
用此方法时,如果界面一开始就要求最大化,在最大化之前就要调用AutoScale方法
方法3
将控件放进容器中, 然后使用控件的Dock属性来获取或设置控件停靠到父容器的哪一个边缘。(略)
相关文章推荐
- WinForm中控件位置不随窗体大小的变化而改变
- Winform 控件随窗体大小改变而变化
- winform控件随窗体大小变化而变化
- 【NCRE】——Winform窗体控件随窗体大小而变化
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- c#中控件随窗体大小改变的代码(winform开发)
- 在.Net的WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变<收藏>
- C# WinForm控件随窗体大小改变自动适应改变
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变(转)
- winform中,如何控制控件位置随窗体的大小改变而改变
- 在.Net的WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- winform 窗体大小变化时,如何设置使控件一起按照比例变大
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- C# WinForm控件随窗体大小改变自动适应改变
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- winform中,如何控制控件位置随窗体的大小改变而改变
- C#中WinForm实现控件随窗体大小自适应改变
- winform中,如何控制控件位置随窗体的大小改变而改变
- c#,VC++ 在WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变