经济适用房能继承吗:C#实现窗体中所有控件跟随窗体尺寸的自由变换
来源:百度文库 编辑:偶看新闻 时间:2024/05/02 13:44:35
/**********************************C#中代码实现控件随窗体的自由变换********************************************/
// 文章出处: 星魂工作室 作者:月云
// 2008.8.4
/**********************************************************************************************************/
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());
}
}
}
}
// 文章出处: 星魂工作室 作者:月云
// 2008.8.4
/**********************************************************************************************************/
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#编程中启动新窗体的问题
在VB中怎样用窗体中的用户控件来拖动无标题的窗体?
怎么在C#中改变窗体标题栏
C#中 如何判断窗体是否关闭
C#中,怎么禁止窗体大小变化??
C#中,怎样制作打开资源管理器窗体??
在vb 6.0中我想在MDI窗体中加载一个TreeView控件.怎么实现啊?
高手请进!c# 多窗体控件如何互相调用
EXCEL单元格中如何插入窗体控件?
在窗体中如何添加MSComm控件
ASP.NET中如何实现窗体的继承?
ASP.NET中如何实现窗体的继承?
ASPNET中如何实现窗体间的ShowModel?
C#中,怎样能让窗体中的内容和GDI绘图的内容随窗体大小改变而缩放?
C# 窗体设计问题
C#窗体切换
请问如何用C#在窗体上实现指定分钟或小时的倒计时时钟??