C# 正则表达式测试工具与分享窗体自适应类

时间:2023-11-18 19:02:56

放假刚回来,自己打算写一个正则表达式的测试工具,因为上次在网上用的一个在线正则表示测试工具就

没有很好的服务自己的,所以才有了现在的想法,想写一个C#开发者用的正则表达式测试工具!期间由于最大化时控件不能同时放大,

所以就找出以学习的时候用的一段代码,原作者是谁我自己也不知道,我把代码分享出来,需要的

可以拿来用!原创作者可以消息我!需要这个工具的也可以留下邮箱!

下面是工具的截图和窗体放大的效果图!

C# 正则表达式测试工具与分享窗体自适应类

下面是控件适应的代码,我把原来的代码简单的放到了类里面,以便可以方便的使用,可能有bug!

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace RegexTool
{
public class AutoResizeForm : Form
{
protected ArrayList InitialCrl = new ArrayList();//用以存储窗体中所有的控件名称
protected ArrayList CrlLocationX = new ArrayList();//用以存储窗体中所有的控件原始位置
protected ArrayList CrlLocationY = new ArrayList();//用以存储窗体中所有的控件原始位置
protected ArrayList CrlSizeWidth = new ArrayList();//用以存储窗体中所有的控件原始的水平尺寸
protected ArrayList CrlSizeHeight = new ArrayList();//用以存储窗体中所有的控件原始的垂直尺寸
protected int FormSizeWidth;//用以存储窗体原始的水平尺寸
protected int FormSizeHeight;//用以存储窗体原始的垂直尺寸 protected double FormSizeChangedX;//用以存储相关父窗体/容器的水平变化量
protected double FormSizeChangedY;//用以存储相关父窗体/容器的垂直变化量
protected int Wcounter = ;//为防止递归遍历控件时产生混乱,故专门设定一个全局计数器 public void GetAllCrlLocation(Control CrlContainer)//获得并存储窗体中各控件的初始位置
{
foreach (Control iCrl in CrlContainer.Controls)
{ if (iCrl.Controls.Count > )
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 > )
GetAllCrlSize(iCrl);
CrlSizeWidth.Add(iCrl.Width);
CrlSizeHeight.Add(iCrl.Height);
}
} public void GetInitialFormSize()//获得并存储窗体的初始尺寸
{ FormSizeWidth = this.Size.Width;
FormSizeHeight = this.Size.Height; } 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)
{ if (kCrl.Controls.Count > )
{
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()); }
} public void ChangeSize(Control CrlContainer)
{
Wcounter = ;
int counter = ;
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);
} }
}
}

使用方法

1.在项目里添加类

2.让需要自适应的窗体继承这个类

3.在窗体的Load事件里添加如下代码

             GetInitialFormSize();
GetAllCrlLocation(this);
GetAllCrlSize(this);

4.在窗体的SizeChanged 事件里头添加如下代码

  ChangeSize(this);

OK ,一切都准备好,build看下效果吧!