zoukankan      html  css  js  c++  java
  • C# 正则表达式测试工具与分享窗体自适应类

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

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

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

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

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

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

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Windows.Forms;
      6 using System.Drawing;
      7 using System.Collections;
      8 namespace RegexTool
      9 {
     10     public class AutoResizeForm : Form
     11     {
     12         protected    ArrayList InitialCrl = new ArrayList();//用以存储窗体中所有的控件名称
     13         protected ArrayList CrlLocationX = new ArrayList();//用以存储窗体中所有的控件原始位置
     14         protected ArrayList CrlLocationY = new ArrayList();//用以存储窗体中所有的控件原始位置
     15         protected ArrayList CrlSizeWidth = new ArrayList();//用以存储窗体中所有的控件原始的水平尺寸
     16         protected ArrayList CrlSizeHeight = new ArrayList();//用以存储窗体中所有的控件原始的垂直尺寸
     17         protected int FormSizeWidth;//用以存储窗体原始的水平尺寸
     18         protected int FormSizeHeight;//用以存储窗体原始的垂直尺寸
     19 
     20         protected double FormSizeChangedX;//用以存储相关父窗体/容器的水平变化量
     21         protected double FormSizeChangedY;//用以存储相关父窗体/容器的垂直变化量 
     22         protected int Wcounter = 0;//为防止递归遍历控件时产生混乱,故专门设定一个全局计数器
     23 
     24         public void GetAllCrlLocation(Control CrlContainer)//获得并存储窗体中各控件的初始位置
     25         {
     26             foreach (Control iCrl in CrlContainer.Controls)
     27             {
     28 
     29                 if (iCrl.Controls.Count > 0)
     30                     GetAllCrlLocation(iCrl);
     31                 InitialCrl.Add(iCrl);
     32                 CrlLocationX.Add(iCrl.Location.X);
     33                 CrlLocationY.Add(iCrl.Location.Y);
     34 
     35 
     36             }
     37         }
     38 
     39         public void GetAllCrlSize(Control CrlContainer)//获得并存储窗体中各控件的初始尺寸
     40         {
     41             foreach (Control iCrl in CrlContainer.Controls)
     42             {
     43                 if (iCrl.Controls.Count > 0)
     44                     GetAllCrlSize(iCrl);
     45                 CrlSizeWidth.Add(iCrl.Width);
     46                 CrlSizeHeight.Add(iCrl.Height);
     47             }
     48         }
     49 
     50         public void GetInitialFormSize()//获得并存储窗体的初始尺寸
     51         {
     52 
     53             FormSizeWidth = this.Size.Width;
     54             FormSizeHeight = this.Size.Height;
     55 
     56         }
     57 
     58 
     59 
     60         public void ResetAllCrlState(Control CrlContainer)//重新设定窗体中各控件的状态(在与原状态的对比中计算而来)
     61         {
     62 
     63 
     64             FormSizeChangedX = (double)this.Size.Width / (double)FormSizeWidth;
     65             FormSizeChangedY = (double)this.Size.Height / (double)FormSizeHeight;
     66 
     67             foreach (Control kCrl in CrlContainer.Controls)
     68             {
     69 
     70 
     71 
     72                 if (kCrl.Controls.Count > 0)
     73                 {
     74                     ResetAllCrlState(kCrl);
     75 
     76                 }
     77 
     78 
     79                 Point point = new Point();
     80                 point.X = (int)((int)CrlLocationX[Wcounter] * FormSizeChangedX);
     81                 point.Y = (int)((int)CrlLocationY[Wcounter] * FormSizeChangedY);
     82                 kCrl.Width = (int)((int)CrlSizeWidth[Wcounter] * FormSizeChangedX);
     83                 kCrl.Height = (int)((int)CrlSizeHeight[Wcounter] * FormSizeChangedY);
     84                 kCrl.Bounds = new Rectangle(point, kCrl.Size);
     85                 Wcounter++;
     86                 // MessageBox.Show(Wcounter.ToString());
     87 
     88 
     89             }
     90         }
     91 
     92         public void ChangeSize(Control CrlContainer)
     93         {
     94             Wcounter = 0;
     95             int counter = 0;
     96             if (this.Size.Width < FormSizeWidth || this.Size.Height < FormSizeHeight)
     97             //如果窗体的大小在改变过程中小于窗体尺寸的初始值,则窗体中的各个控件自动重置为初始尺寸,且窗体自动添加滚动条
     98             {
     99 
    100                 foreach (Control iniCrl in InitialCrl)
    101                 {
    102                     iniCrl.Width = (int)CrlSizeWidth[counter];
    103                     iniCrl.Height = (int)CrlSizeHeight[counter];
    104                     Point point = new Point();
    105                     point.X = (int)CrlLocationX[counter];
    106                     point.Y = (int)CrlLocationY[counter];
    107                     iniCrl.Bounds = new Rectangle(point, iniCrl.Size);
    108                     counter++;
    109                 }
    110                 this.AutoScroll = true;
    111             }
    112             else
    113             //否则,重新设定窗体中所有控件的大小(窗体内所有控件的大小随窗体大小的变化而变化)
    114             {
    115                 this.AutoScroll = false;
    116                 ResetAllCrlState(this);
    117             }
    118         
    119         }
    120     }
    121 }
    View Code

    使用方法

    1.在项目里添加类

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

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

    1             GetInitialFormSize();
    2             GetAllCrlLocation(this);
    3             GetAllCrlSize(this);
    View Code

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

    1  ChangeSize(this);
    View Code

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

  • 相关阅读:
    代码管理工具SonarQube的搭建和使用
    WebFlux Logs日志
    WebFlux WebClient异常处理
    WebFlux- WebClient(二)
    WebFlux- WebClient(一)
    WebFlux-Server-Sent Event服务器推送事件
    Reactive Stack
    Flink
    Gradle
    springboot
  • 原文地址:https://www.cnblogs.com/try-wyh/p/3779516.html
Copyright © 2011-2022 走看看