初始化Form1出现了问题,请帮忙看看这是怎么回事。(无限循环初始化)
public Form1()
{
InitializeComponent();
this.Resize += new EventHandler(Form1_Resize);
res.firstDemo(this);
}
///这个是调用
internal void firstDemo(Form1 form1)
{
firstDemo(form1);
}
我的程序目的是将放大和缩小Form封装成一个类,现在出现了问题,代码在不封装的时候是好用的。。。。。。。。封装类如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace 放大和缩小界面
{
class resize : Form
{
public void firstDemo(Form it)
{
try
{
int count = this.Controls.Count * 2 + 2;
float[] factor = new float[count];
int i = 0;
factor[i++] = Size.Width;
factor[i++] = Size.Height;
foreach (Control ctrl in it.Controls)
{
factor[i++] = ctrl.Location.X / (float)Size.Width;
factor[i++] = ctrl.Location.Y / (float)Size.Height;
ctrl.Tag = ctrl.Size;
}
Tag = factor;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public void resizeStatus(Form it)
{
try
{
float[] scale = (float[])Tag;
int i = 2;
foreach (Control ctrl in it.Controls)
{
ctrl.Left = (int)(Size.Width * scale[i++]);
ctrl.Top = (int)(Size.Height * scale[i++]);
ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
//每次使用的都是最初始的控件大小,保证准确无误。
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}