赞
踩
在执行一个时间较长的操作时,往往需要设计一个等待窗口,网上找了很多,都没有计时功能,今天自己写一个。
运用到的技术有:devexpress的SplashScreenManager,线程调用。
直接上效果图!!!!!!
接下来是设计思路:
1.设计等待窗口
等待窗口设计器代码:
- namespace IMS_TOOLS.UI.UserForm
- {
- partial class WaitForm1
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
-
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
-
- #region Windows Form Designer generated code
-
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.progressPanel1 = new DevExpress.XtraWaitForm.ProgressPanel();
- this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
- this.labelControl1 = new DevExpress.XtraEditors.LabelControl();
- this.tableLayoutPanel1.SuspendLayout();
- this.SuspendLayout();
- //
- // progressPanel1
- //
- this.progressPanel1.Appearance.BackColor = System.Drawing.Color.Transparent;
- this.progressPanel1.Appearance.Options.UseBackColor = true;
- this.progressPanel1.AppearanceCaption.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
- this.progressPanel1.AppearanceCaption.Options.UseFont = true;
- this.progressPanel1.AppearanceDescription.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
- this.progressPanel1.AppearanceDescription.Options.UseFont = true;
- this.progressPanel1.Caption = "请稍后";
- this.progressPanel1.Description = "正在加载...";
- this.progressPanel1.ImageHorzOffset = 20;
- this.progressPanel1.Location = new System.Drawing.Point(0, 16);
- this.progressPanel1.Margin = new System.Windows.Forms.Padding(0, 3, 0, 3);
- this.progressPanel1.Name = "progressPanel1";
- this.progressPanel1.Size = new System.Drawing.Size(246, 35);
- this.progressPanel1.TabIndex = 0;
- this.progressPanel1.Text = "progressPanel1";
- //
- // tableLayoutPanel1
- //
- this.tableLayoutPanel1.AutoSize = true;
- this.tableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
- this.tableLayoutPanel1.BackColor = System.Drawing.Color.Transparent;
- this.tableLayoutPanel1.ColumnCount = 1;
- this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
- this.tableLayoutPanel1.Controls.Add(this.progressPanel1, 0, 0);
- this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
- this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
- this.tableLayoutPanel1.Name = "tableLayoutPanel1";
- this.tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(0, 13, 0, 13);
- this.tableLayoutPanel1.RowCount = 1;
- this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
- this.tableLayoutPanel1.Size = new System.Drawing.Size(246, 67);
- this.tableLayoutPanel1.TabIndex = 1;
- //
- // labelControl1
- //
- this.labelControl1.Location = new System.Drawing.Point(207, 36);
- this.labelControl1.Name = "labelControl1";
- this.labelControl1.Size = new System.Drawing.Size(19, 14);
- this.labelControl1.TabIndex = 2;
- this.labelControl1.Text = "0秒";
- //
- // WaitForm1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.AutoSize = true;
- this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
- this.ClientSize = new System.Drawing.Size(246, 67);
- this.Controls.Add(this.labelControl1);
- this.Controls.Add(this.tableLayoutPanel1);
- this.DoubleBuffered = true;
- this.Name = "WaitForm1";
- this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
- this.Text = "Form1";
- this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.WaitForm1_FormClosed);
- this.Load += new System.EventHandler(this.WaitForm1_Load);
- this.tableLayoutPanel1.ResumeLayout(false);
- this.ResumeLayout(false);
- this.PerformLayout();
-
- }
-
- #endregion
-
- private DevExpress.XtraWaitForm.ProgressPanel progressPanel1;
- private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
- private DevExpress.XtraEditors.LabelControl labelControl1;
- }
- }
等待窗口代码:
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using DevExpress.XtraWaitForm;
- using System.Threading;
-
- namespace IMS_TOOLS.UI.UserForm
- {
- public partial class WaitForm1 : WaitForm
- {
- // 启动新线程用于计时
- System.Threading.Timer myTimer;
- long TimeCount;
-
- delegate void SetValue();
-
- // 线程执行计时任务
- private void TimerUp(object state)
- {
- if (this.IsHandleCreated)
- {
- this.Invoke(new SetValue(ShowTime));
- TimeCount++;
- }
- }
-
- public void ShowTime()
- {
- TimeSpan t = new TimeSpan(0, 0, (int)TimeCount);
- labelControl1.Text = t.TotalSeconds + "秒";
- }
-
- public WaitForm1()
- {
- InitializeComponent();
- progressPanel1.AutoHeight = true;
- }
-
- private void WaitForm1_Load(object sender, EventArgs e)
- {
- myTimer = new System.Threading.Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, 1000);
-
- // 计时开始
- TimeCount = 0;
- myTimer.Change(0, 1000);
- }
-
- // 计时结束
- private void WaitForm1_FormClosed(object sender, FormClosedEventArgs e)
- {
- if (myTimer != null)
- {
- myTimer.Change(Timeout.Infinite, 1000);
- myTimer = null;
- }
- }
-
- }
- }
2.封装SplashScreenManager共通类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using DevExpress.XtraSplashScreen;
-
- namespace IMS_TOOLS.Unit
- {
- public class SplashFormUtil
- {
- /// <summary>
- /// 设置等待窗口的标题
- /// </summary>
- /// <returns></returns>
- public static void SetCaption(string caption)
- {
- if (SplashScreenManager.Default != null && !string.IsNullOrEmpty(caption))
- {
- SplashScreenManager.Default.SetWaitFormCaption(caption);
- }
- }
-
- /// <summary>
- /// 设置等待窗口的描述文字
- /// </summary>
- /// <returns></returns>
- public static void SetDescription(string description)
- {
- if (SplashScreenManager.Default != null && !string.IsNullOrEmpty(description))
- {
- SplashScreenManager.Default.SetWaitFormDescription(description);
- }
- }
-
- /// <summary>
- /// 显示等待窗口
- /// </summary>
- /// <returns></returns>
- public static void ShowSplashScreen(Type type)
- {
- CloseSplashScreen();
- SplashScreenManager.ShowForm(null, type, false, false, true);
- }
-
- /// <summary>
- /// 关闭等待窗口
- /// </summary>
- /// <returns></returns>
- public static void CloseSplashScreen()
- {
- if (SplashScreenManager.Default != null)
- {
- SplashScreenManager.CloseForm(true);
- }
- }
- }
- }
3.其他类调用
- SplashFormUtil.ShowSplashScreen(typeof(WaitForm1));
- /***耗时长的操作***/
- SplashFormUtil.CloseSplashScreen();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。