
正文
winfrom创建转圈等待窗体
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
第一步:创建一个WaitForm
public partial class WaitForm : Form
{
private int count = -;
private ArrayList images = new ArrayList();
public Bitmap[] bitmap = new Bitmap[];
private int _value = ;
private Color _circleColor = Color.Red;
private float _circleSize = 0.8f;
private bool disposed = false;
public WaitForm()
{
InitializeComponent();
Instance = this;
ShowInTaskbar = false;
} public Color CircleColor
{
get { return _circleColor; }
set
{
_circleColor = value;
Invalidate();
}
} public float CircleSize
{
get { return _circleSize; }
set
{
if (value <= 0.0F)
_circleSize = 0.05F;
else
_circleSize = value > 4.0F ? 4.0F : value;
Invalidate();
}
} public Bitmap DrawCircle(int j)
{
const float angle = 360.0F / ; Bitmap map = new Bitmap(, );
Graphics g = Graphics.FromImage(map); g.TranslateTransform( / 2.0F, / 2.0F);
g.RotateTransform(angle * _value);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.AntiAlias;
int[] a = new int[] { , , , , , , , };
for (int i = ; i <= ; i++)
{
int alpha = a[(i + j - ) % ];
Color drawColor = Color.FromArgb(alpha, _circleColor);
using (SolidBrush brush = new SolidBrush(drawColor))
{
float sizeRate = 3.5F / _circleSize;
float size = / ( * sizeRate); float diff = ( / 10.0F) - size; float x = ( / 80.0F) + diff;
float y = ( / 80.0F) + diff;
g.FillEllipse(brush,x,y, size, size);
g.RotateTransform(angle);
}
}
return map;
} public void Draw()
{
for (int j = ; j < ; j++)
{
bitmap[ - j] = DrawCircle(j);
}
}
protected override void OnResize(EventArgs e)
{
SetNewSize();
base.OnResize(e);
} protected override void OnSizeChanged(EventArgs e)
{
SetNewSize();
base.OnSizeChanged(e);
} private void SetNewSize()
{
int size = Math.Max(Width, Height);
Size = new Size(size, size);
} public void set()
{
for (int i = ; i < ; i++)
{
Draw(); //Bitmap map = new Bitmap((bitmap[i]), new Size(300, 300)); images.Add(bitmap[i]);
}
pictureBoxWait.Image = (Image)images[];
pictureBoxWait.Size = pictureBoxWait.Image.Size; }
private void pictureBox_Click(object sender, EventArgs e)
{
this.Visible = false;
base.Dispose();
} private void Timer_Tick(object sender, EventArgs e)
{
Invoke(new Action(() =>
{
foreach (Bitmap item in bitmap)
{
if (item != null)
{
item.Dispose();
}
}
images.Clear();
set();
count = (count + ) % ;
pictureBoxWait.Image = (Image)images[count];
})); }
public void ShowForm()
{
Invoke(new Action(() =>
{
Activate();
Show();
}));
}
public void StopWait()
{
Invoke(new Action(() => { Hide(); })); }
private void button1_Click(object sender, EventArgs e)
{
this.Visible = false;
base.Dispose();
}
public static WaitForm Instance = null;
private void WaitForm_Load(object sender, EventArgs e)
{
Invoke(new Action(() =>
{ pictureBoxWait.Left = Width / - ;
pictureBoxWait.Top = Height / - ;
pictureBoxWait.Width = ;
pictureBoxWait.Height = ;
})); } private void WaitForm_KeyDown(object sender, KeyEventArgs e)
{
Invoke(new Action(() =>
{
if (e.KeyCode == Keys.T)
{
this.Close();
}
})); }
}
第二步:使用方法
在MainForm 初始化地方开启线程调用
tdWait = new Thread(() => { Application.Run(new WaitForm()); });
tdWait.IsBackground = true;
tdWait.Start();
第三步:在需要使用的地方使用
WaitForm.Instance.ShowForm();
AppointModels appModels = new AppointModels();
ResultModels result = HISManager.DownloadAppointData(JsonConvert.SerializeObject(appModels));
WaitForm.Instance.StopWait();
第四步:在MainForm Close 的地方关闭线程
if (tdWait != null)
{
tdWait.Abort();
tdWait = null;
}







