第一步:创建一个WaitForm
public partial class WaitForm : Form { private int count = -1; private ArrayList images = new ArrayList(); public Bitmap[] bitmap = new Bitmap[8]; private int _value = 1; 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 / 8; Bitmap map = new Bitmap(200, 200); Graphics g = Graphics.FromImage(map); g.TranslateTransform(300 / 2.0F, 300 / 2.0F); g.RotateTransform(angle * _value); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.AntiAlias; int[] a = new int[8] { 25, 50, 75, 100, 125, 150, 175, 200 }; for (int i = 1; i <= 8; i++) { int alpha = a[(i + j - 1) % 8]; Color drawColor = Color.FromArgb(alpha, _circleColor); using (SolidBrush brush = new SolidBrush(drawColor)) { float sizeRate = 3.5F / _circleSize; float size = 300 / (6 * sizeRate); float diff = (300 / 10.0F) - size; float x = (300 / 80.0F) + diff; float y = (300 / 80.0F) + diff; g.FillEllipse(brush,x,y, size, size); g.RotateTransform(angle); } } return map; } public void Draw() { for (int j = 0; j < 8; j++) { bitmap[7 - 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 = 0; i < 8; i++) { Draw(); //Bitmap map = new Bitmap((bitmap[i]), new Size(300, 300)); images.Add(bitmap[i]); } pictureBoxWait.Image = (Image)images[0]; 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 + 1) % 8; 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 / 2 - 150; pictureBoxWait.Top = Height / 2 - 150; pictureBoxWait.Width = 300; pictureBoxWait.Height = 300; })); } 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; }