C#当窗体大小改变时,窗体中的控件大小也随之改变
2010-09-13 09:33
381 查看
第一种:推荐
在窗体中加上如下代码即可实现,但窗体点击放大按钮时却不能改变控件大小。
private Size beforeResizeSize = Size.Empty;
protected override void OnResizeBegin(EventArgs e)
{
base.OnResizeBegin(e);
beforeResizeSize = this.Size;
}
protected override void OnResizeEnd(EventArgs e)
{
base.OnResizeEnd(e);
//窗口resize之后的大小
Size endResizeSize = this.Size;
//获得变化比例
float percentWidth = (float)endResizeSize.Width / beforeResizeSize.Width;
float percentHeight = (float)endResizeSize.Height / beforeResizeSize.Height;
foreach (Control control in this.Controls)
{
if (control is DataGridView)
continue;
//按比例改变控件大小
control.Width = (int)(control.Width * percentWidth);
control.Height = (int)(control.Height * percentHeight);
//为了不使控件之间覆盖 位置也要按比例变化
control.Left = (int)(control.Left * percentWidth);
control.Top = (int)(control.Top * percentHeight);
}
}
第二种:效果很差
在加载事件中写 AutoScale(this);
//设置窗口控件随窗口大小改变而改变
public new void AutoScale(Form frm)
{
frm.Tag = frm.Width.ToString() + "," + frm.Height.ToString();
frm.SizeChanged += new EventHandler(frm_SizeChanged);
}
public void frm_SizeChanged(object sender, EventArgs e)
{
string[] tmp = ((Form)sender).Tag.ToString().Split(',');
float width = (float)((Form)sender).Width / (float)Convert.ToInt32(tmp[0]);
float height = (float)((Form)sender).Height / (float)Convert.ToInt32(tmp[1]);
((Form)sender).Tag = ((Form)sender).Width.ToString() + "," + ((Form)sender).Height;
string str = ((Form)sender).Tag.ToString();
// int font_size = Int32.Parse(str.Substring(0, str.IndexOf(','))) / 100;//也可使字体随之改变
float tempWidth=0F;
float tempHeight=0F;
foreach (Control control in ((Form)sender).Controls)
{
if (control is DataGridView)
continue;
if (control is TextBox)
{
tempHeight = height;
tempWidth = width;
}
if (control is Button)
{
if (this.WindowState == FormWindowState.Maximized)
tempHeight -= 0.4F;
else
tempHeight += 0.2F;
control.Scale(new SizeF(tempWidth, tempHeight));
}
else
{
control.Scale(new SizeF(width, height));
}
}
}
第三种:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Collections;
namespace
WindowsApplication3
{
public
partial
class
Form1 : Form
{
/*
******************设定程序中可能要用到的用以存储初始数据的动态数组及相关私有变量*****************************
*/
private
ArrayList InitialCrl
=
new
ArrayList();
//
用以存储窗体中所有的控件名称
private
ArrayList CrlLocationX
=
new
ArrayList();
//
用以存储窗体中所有的控件原始位置
private
ArrayList CrlLocationY
=
new
ArrayList();
//
用以存储窗体中所有的控件原始位置
private
ArrayList CrlSizeWidth
=
new
ArrayList();
//
用以存储窗体中所有的控件原始的水平尺寸
private
ArrayList CrlSizeHeight
=
new
ArrayList();
//
用以存储窗体中所有的控件原始的垂直尺寸
private
int
FormSizeWidth;
//
用以存储窗体原始的水平尺寸
private
int
FormSizeHeight;
//
用以存储窗体原始的垂直尺寸
private
double
FormSizeChangedX;
//
用以存储相关父窗体/容器的水平变化量
private
double
FormSizeChangedY;
//
用以存储相关父窗体/容器的垂直变化量
private
int
Wcounter
=
0
;
//
为防止递归遍历控件时产生混乱,故专门设定一个全局计数器
/*
**************************************************************************************************************
*/
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
GetInitialFormSize();
//
this.AutoScroll = true;
//
this.SetAutoSizeMode(FormSizeWidth,FormSizeHeight);
//
this.AutoScrollMinSize.Width = FormSizeWidth;
//
this.AutoScrollMinSize.Height = FormSizeHeight;
GetAllCrlLocation(
this
);
GetAllCrlSize(
this
);
}
public
void
GetAllCrlLocation(Control CrlContainer)
//
获得并存储窗体中各控件的初始位置
{
foreach
(Control iCrl
in
CrlContainer.Controls)
{
if
(iCrl.Controls.Count
>
0
)
GetAllCrlLocation(iCrl);
InitialCrl.Add(iCrl);
CrlLocationX.Add(iCrl.Location.X);
CrlLocationY.Add(iCrl.Location.Y);
}
}
public
void
GetAllCrlSize(Control CrlContainer)
//
获得并存储窗体中各控件的初始尺寸
{
foreach
(Control iCrl
in
CrlContainer.Controls)
{
if
(iCrl.Controls.Count
>
0
)
GetAllCrlSize(iCrl);
CrlSizeWidth.Add(iCrl.Width);
CrlSizeHeight.Add(iCrl.Height);
}
}
public
void
GetInitialFormSize()
//
获得并存储窗体的初始尺寸
{
FormSizeWidth
=
this
.Size.Width;
FormSizeHeight
=
this
.Size.Height;
}
private
void
Form1_SizeChanged(
object
sender, EventArgs e)
{
//
MessageBox.Show("窗体尺寸改变");
Wcounter
=
0
;
int
counter
=
0
;
if
(
this
.Size.Width
<
FormSizeWidth
||
this
.Size.Height
<
FormSizeHeight)
//
如果窗体的大小在改变过程中小于窗体尺寸的初始值,则窗体中的各个控件自动重置为初始尺寸,且窗体自动添加滚动条
{
foreach
(Control iniCrl
in
InitialCrl)
{
iniCrl.Width
=
(
int
)CrlSizeWidth[counter];
iniCrl.Height
=
(
int
)CrlSizeHeight[counter];
Point point
=
new
Point();
point.X
=
(
int
)CrlLocationX[counter];
point.Y
=
(
int
)CrlLocationY[counter];
iniCrl.Bounds
=
new
Rectangle(point, iniCrl.Size);
counter
++
;
}
this
.AutoScroll
=
true
;
}
else
//
否则,重新设定窗体中所有控件的大小(窗体内所有控件的大小随窗体大小的变化而变化)
{
this
.AutoScroll
=
false
;
ResetAllCrlState(
this
);
}
}
public
void
ResetAllCrlState(Control CrlContainer)
//
重新设定窗体中各控件的状态(在与原状态的对比中计算而来)
{
FormSizeChangedX
=
(
double
)
this
.Size.Width
/
(
double
)FormSizeWidth;
FormSizeChangedY
=
(
double
)
this
.Size.Height
/
(
double
)FormSizeHeight;
foreach
(Control kCrl
in
CrlContainer.Controls)
{
/*
string name = kCrl.Name.ToString();
MessageBox.Show(name);
MessageBox.Show(Wcounter.ToString());
*/
if
(kCrl.Controls.Count
>
0
)
{
ResetAllCrlState(kCrl);
}
Point point
=
new
Point();
point.X
=
(
int
)((
int
)CrlLocationX[Wcounter]
*
FormSizeChangedX);
point.Y
=
(
int
)((
int
)CrlLocationY[Wcounter]
*
FormSizeChangedY);
kCrl.Width
=
(
int
)((
int
)CrlSizeWidth[Wcounter]
*
FormSizeChangedX);
kCrl.Height
=
(
int
)((
int
)CrlSizeHeight[Wcounter]
*
FormSizeChangedY);
kCrl.Bounds
=
new
Rectangle(point, kCrl.Size);
Wcounter
++
;
//
MessageBox.Show(Wcounter.ToString());
}
}
}
}
在窗体中加上如下代码即可实现,但窗体点击放大按钮时却不能改变控件大小。
private Size beforeResizeSize = Size.Empty;
protected override void OnResizeBegin(EventArgs e)
{
base.OnResizeBegin(e);
beforeResizeSize = this.Size;
}
protected override void OnResizeEnd(EventArgs e)
{
base.OnResizeEnd(e);
//窗口resize之后的大小
Size endResizeSize = this.Size;
//获得变化比例
float percentWidth = (float)endResizeSize.Width / beforeResizeSize.Width;
float percentHeight = (float)endResizeSize.Height / beforeResizeSize.Height;
foreach (Control control in this.Controls)
{
if (control is DataGridView)
continue;
//按比例改变控件大小
control.Width = (int)(control.Width * percentWidth);
control.Height = (int)(control.Height * percentHeight);
//为了不使控件之间覆盖 位置也要按比例变化
control.Left = (int)(control.Left * percentWidth);
control.Top = (int)(control.Top * percentHeight);
}
}
第二种:效果很差
在加载事件中写 AutoScale(this);
//设置窗口控件随窗口大小改变而改变
public new void AutoScale(Form frm)
{
frm.Tag = frm.Width.ToString() + "," + frm.Height.ToString();
frm.SizeChanged += new EventHandler(frm_SizeChanged);
}
public void frm_SizeChanged(object sender, EventArgs e)
{
string[] tmp = ((Form)sender).Tag.ToString().Split(',');
float width = (float)((Form)sender).Width / (float)Convert.ToInt32(tmp[0]);
float height = (float)((Form)sender).Height / (float)Convert.ToInt32(tmp[1]);
((Form)sender).Tag = ((Form)sender).Width.ToString() + "," + ((Form)sender).Height;
string str = ((Form)sender).Tag.ToString();
// int font_size = Int32.Parse(str.Substring(0, str.IndexOf(','))) / 100;//也可使字体随之改变
float tempWidth=0F;
float tempHeight=0F;
foreach (Control control in ((Form)sender).Controls)
{
if (control is DataGridView)
continue;
if (control is TextBox)
{
tempHeight = height;
tempWidth = width;
}
if (control is Button)
{
if (this.WindowState == FormWindowState.Maximized)
tempHeight -= 0.4F;
else
tempHeight += 0.2F;
control.Scale(new SizeF(tempWidth, tempHeight));
}
else
{
control.Scale(new SizeF(width, height));
}
}
}
第三种:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Collections;
namespace
WindowsApplication3
{
public
partial
class
Form1 : Form
{
/*
******************设定程序中可能要用到的用以存储初始数据的动态数组及相关私有变量*****************************
*/
private
ArrayList InitialCrl
=
new
ArrayList();
//
用以存储窗体中所有的控件名称
private
ArrayList CrlLocationX
=
new
ArrayList();
//
用以存储窗体中所有的控件原始位置
private
ArrayList CrlLocationY
=
new
ArrayList();
//
用以存储窗体中所有的控件原始位置
private
ArrayList CrlSizeWidth
=
new
ArrayList();
//
用以存储窗体中所有的控件原始的水平尺寸
private
ArrayList CrlSizeHeight
=
new
ArrayList();
//
用以存储窗体中所有的控件原始的垂直尺寸
private
int
FormSizeWidth;
//
用以存储窗体原始的水平尺寸
private
int
FormSizeHeight;
//
用以存储窗体原始的垂直尺寸
private
double
FormSizeChangedX;
//
用以存储相关父窗体/容器的水平变化量
private
double
FormSizeChangedY;
//
用以存储相关父窗体/容器的垂直变化量
private
int
Wcounter
=
0
;
//
为防止递归遍历控件时产生混乱,故专门设定一个全局计数器
/*
**************************************************************************************************************
*/
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
GetInitialFormSize();
//
this.AutoScroll = true;
//
this.SetAutoSizeMode(FormSizeWidth,FormSizeHeight);
//
this.AutoScrollMinSize.Width = FormSizeWidth;
//
this.AutoScrollMinSize.Height = FormSizeHeight;
GetAllCrlLocation(
this
);
GetAllCrlSize(
this
);
}
public
void
GetAllCrlLocation(Control CrlContainer)
//
获得并存储窗体中各控件的初始位置
{
foreach
(Control iCrl
in
CrlContainer.Controls)
{
if
(iCrl.Controls.Count
>
0
)
GetAllCrlLocation(iCrl);
InitialCrl.Add(iCrl);
CrlLocationX.Add(iCrl.Location.X);
CrlLocationY.Add(iCrl.Location.Y);
}
}
public
void
GetAllCrlSize(Control CrlContainer)
//
获得并存储窗体中各控件的初始尺寸
{
foreach
(Control iCrl
in
CrlContainer.Controls)
{
if
(iCrl.Controls.Count
>
0
)
GetAllCrlSize(iCrl);
CrlSizeWidth.Add(iCrl.Width);
CrlSizeHeight.Add(iCrl.Height);
}
}
public
void
GetInitialFormSize()
//
获得并存储窗体的初始尺寸
{
FormSizeWidth
=
this
.Size.Width;
FormSizeHeight
=
this
.Size.Height;
}
private
void
Form1_SizeChanged(
object
sender, EventArgs e)
{
//
MessageBox.Show("窗体尺寸改变");
Wcounter
=
0
;
int
counter
=
0
;
if
(
this
.Size.Width
<
FormSizeWidth
||
this
.Size.Height
<
FormSizeHeight)
//
如果窗体的大小在改变过程中小于窗体尺寸的初始值,则窗体中的各个控件自动重置为初始尺寸,且窗体自动添加滚动条
{
foreach
(Control iniCrl
in
InitialCrl)
{
iniCrl.Width
=
(
int
)CrlSizeWidth[counter];
iniCrl.Height
=
(
int
)CrlSizeHeight[counter];
Point point
=
new
Point();
point.X
=
(
int
)CrlLocationX[counter];
point.Y
=
(
int
)CrlLocationY[counter];
iniCrl.Bounds
=
new
Rectangle(point, iniCrl.Size);
counter
++
;
}
this
.AutoScroll
=
true
;
}
else
//
否则,重新设定窗体中所有控件的大小(窗体内所有控件的大小随窗体大小的变化而变化)
{
this
.AutoScroll
=
false
;
ResetAllCrlState(
this
);
}
}
public
void
ResetAllCrlState(Control CrlContainer)
//
重新设定窗体中各控件的状态(在与原状态的对比中计算而来)
{
FormSizeChangedX
=
(
double
)
this
.Size.Width
/
(
double
)FormSizeWidth;
FormSizeChangedY
=
(
double
)
this
.Size.Height
/
(
double
)FormSizeHeight;
foreach
(Control kCrl
in
CrlContainer.Controls)
{
/*
string name = kCrl.Name.ToString();
MessageBox.Show(name);
MessageBox.Show(Wcounter.ToString());
*/
if
(kCrl.Controls.Count
>
0
)
{
ResetAllCrlState(kCrl);
}
Point point
=
new
Point();
point.X
=
(
int
)((
int
)CrlLocationX[Wcounter]
*
FormSizeChangedX);
point.Y
=
(
int
)((
int
)CrlLocationY[Wcounter]
*
FormSizeChangedY);
kCrl.Width
=
(
int
)((
int
)CrlSizeWidth[Wcounter]
*
FormSizeChangedX);
kCrl.Height
=
(
int
)((
int
)CrlSizeHeight[Wcounter]
*
FormSizeChangedY);
kCrl.Bounds
=
new
Rectangle(point, kCrl.Size);
Wcounter
++
;
//
MessageBox.Show(Wcounter.ToString());
}
}
}
}
相关文章推荐
- C#当窗体大小改变时,窗体中的控件大小也随之改变
- C#当窗体大小改变时,窗体中的控件大小也随之改变
- C#当窗体大小改变时,窗体中的控件大小也随之改变
- C#当窗体大小改变时,窗体中的控件大小也随之改变
- C#当窗体大小改变时,窗体中的控件大小也随之改变
- C#当窗体大小改变时,窗体中的控件大小也随之改变
- C#当窗体大小改变时,窗体中的控件大小也随之改变
- c#当窗体大小改变时,窗体中的控件大小也随之改变
- C#当窗体大小改变时,控件的位置和大小随之改变
- C#实现窗体控件随窗体大小改变(包括字体大小)
- C# WinForm控件随窗体大小改变自动适应改变
- 4000 c#实现窗体大小改变时,控件大小也跟着改变,实现窗体拉伸重绘
- C#实现窗体控件随窗体大小改变(包括字体大小)
- 【C#】窗体控件随窗体大小改变(包括字体大小)
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- c# 控件随窗体大小改变
- C#中怎么使控件随着窗体的大小改变而改变
- C#控件随窗体大小改变而改变
- C# 窗体控件随窗体改变大小
- C# WinForm控件随窗体大小改变自动适应改变