当前位置:   article > 正文

devexpress带计时器的等待窗口

devexpress带计时器的等待窗口

在执行一个时间较长的操作时,往往需要设计一个等待窗口,网上找了很多,都没有计时功能,今天自己写一个。

运用到的技术有:devexpress的SplashScreenManager,线程调用。

直接上效果图!!!!!!

接下来是设计思路:

1.设计等待窗口 

等待窗口设计器代码:

  1. namespace IMS_TOOLS.UI.UserForm
  2. {
  3. partial class WaitForm1
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9. /// <summary>
  10. /// Clean up any resources being used.
  11. /// </summary>
  12. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13. protected override void Dispose(bool disposing)
  14. {
  15. if (disposing && (components != null))
  16. {
  17. components.Dispose();
  18. }
  19. base.Dispose(disposing);
  20. }
  21. #region Windows Form Designer generated code
  22. /// <summary>
  23. /// Required method for Designer support - do not modify
  24. /// the contents of this method with the code editor.
  25. /// </summary>
  26. private void InitializeComponent()
  27. {
  28. this.progressPanel1 = new DevExpress.XtraWaitForm.ProgressPanel();
  29. this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
  30. this.labelControl1 = new DevExpress.XtraEditors.LabelControl();
  31. this.tableLayoutPanel1.SuspendLayout();
  32. this.SuspendLayout();
  33. //
  34. // progressPanel1
  35. //
  36. this.progressPanel1.Appearance.BackColor = System.Drawing.Color.Transparent;
  37. this.progressPanel1.Appearance.Options.UseBackColor = true;
  38. this.progressPanel1.AppearanceCaption.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
  39. this.progressPanel1.AppearanceCaption.Options.UseFont = true;
  40. this.progressPanel1.AppearanceDescription.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
  41. this.progressPanel1.AppearanceDescription.Options.UseFont = true;
  42. this.progressPanel1.Caption = "请稍后";
  43. this.progressPanel1.Description = "正在加载...";
  44. this.progressPanel1.ImageHorzOffset = 20;
  45. this.progressPanel1.Location = new System.Drawing.Point(0, 16);
  46. this.progressPanel1.Margin = new System.Windows.Forms.Padding(0, 3, 0, 3);
  47. this.progressPanel1.Name = "progressPanel1";
  48. this.progressPanel1.Size = new System.Drawing.Size(246, 35);
  49. this.progressPanel1.TabIndex = 0;
  50. this.progressPanel1.Text = "progressPanel1";
  51. //
  52. // tableLayoutPanel1
  53. //
  54. this.tableLayoutPanel1.AutoSize = true;
  55. this.tableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
  56. this.tableLayoutPanel1.BackColor = System.Drawing.Color.Transparent;
  57. this.tableLayoutPanel1.ColumnCount = 1;
  58. this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
  59. this.tableLayoutPanel1.Controls.Add(this.progressPanel1, 0, 0);
  60. this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
  61. this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
  62. this.tableLayoutPanel1.Name = "tableLayoutPanel1";
  63. this.tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(0, 13, 0, 13);
  64. this.tableLayoutPanel1.RowCount = 1;
  65. this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
  66. this.tableLayoutPanel1.Size = new System.Drawing.Size(246, 67);
  67. this.tableLayoutPanel1.TabIndex = 1;
  68. //
  69. // labelControl1
  70. //
  71. this.labelControl1.Location = new System.Drawing.Point(207, 36);
  72. this.labelControl1.Name = "labelControl1";
  73. this.labelControl1.Size = new System.Drawing.Size(19, 14);
  74. this.labelControl1.TabIndex = 2;
  75. this.labelControl1.Text = "0秒";
  76. //
  77. // WaitForm1
  78. //
  79. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  80. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  81. this.AutoSize = true;
  82. this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
  83. this.ClientSize = new System.Drawing.Size(246, 67);
  84. this.Controls.Add(this.labelControl1);
  85. this.Controls.Add(this.tableLayoutPanel1);
  86. this.DoubleBuffered = true;
  87. this.Name = "WaitForm1";
  88. this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
  89. this.Text = "Form1";
  90. this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.WaitForm1_FormClosed);
  91. this.Load += new System.EventHandler(this.WaitForm1_Load);
  92. this.tableLayoutPanel1.ResumeLayout(false);
  93. this.ResumeLayout(false);
  94. this.PerformLayout();
  95. }
  96. #endregion
  97. private DevExpress.XtraWaitForm.ProgressPanel progressPanel1;
  98. private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
  99. private DevExpress.XtraEditors.LabelControl labelControl1;
  100. }
  101. }

 等待窗口代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using DevExpress.XtraWaitForm;
  5. using System.Threading;
  6. namespace IMS_TOOLS.UI.UserForm
  7. {
  8. public partial class WaitForm1 : WaitForm
  9. {
  10. // 启动新线程用于计时
  11. System.Threading.Timer myTimer;
  12. long TimeCount;
  13. delegate void SetValue();
  14. // 线程执行计时任务
  15. private void TimerUp(object state)
  16. {
  17. if (this.IsHandleCreated)
  18. {
  19. this.Invoke(new SetValue(ShowTime));
  20. TimeCount++;
  21. }
  22. }
  23. public void ShowTime()
  24. {
  25. TimeSpan t = new TimeSpan(0, 0, (int)TimeCount);
  26. labelControl1.Text = t.TotalSeconds + "秒";
  27. }
  28. public WaitForm1()
  29. {
  30. InitializeComponent();
  31. progressPanel1.AutoHeight = true;
  32. }
  33. private void WaitForm1_Load(object sender, EventArgs e)
  34. {
  35. myTimer = new System.Threading.Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, 1000);
  36. // 计时开始
  37. TimeCount = 0;
  38. myTimer.Change(0, 1000);
  39. }
  40. // 计时结束
  41. private void WaitForm1_FormClosed(object sender, FormClosedEventArgs e)
  42. {
  43. if (myTimer != null)
  44. {
  45. myTimer.Change(Timeout.Infinite, 1000);
  46. myTimer = null;
  47. }
  48. }
  49. }
  50. }

2.封装SplashScreenManager共通类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using DevExpress.XtraSplashScreen;
  6. namespace IMS_TOOLS.Unit
  7. {
  8. public class SplashFormUtil
  9. {
  10. /// <summary>
  11. /// 设置等待窗口的标题
  12. /// </summary>
  13. /// <returns></returns>
  14. public static void SetCaption(string caption)
  15. {
  16. if (SplashScreenManager.Default != null && !string.IsNullOrEmpty(caption))
  17. {
  18. SplashScreenManager.Default.SetWaitFormCaption(caption);
  19. }
  20. }
  21. /// <summary>
  22. /// 设置等待窗口的描述文字
  23. /// </summary>
  24. /// <returns></returns>
  25. public static void SetDescription(string description)
  26. {
  27. if (SplashScreenManager.Default != null && !string.IsNullOrEmpty(description))
  28. {
  29. SplashScreenManager.Default.SetWaitFormDescription(description);
  30. }
  31. }
  32. /// <summary>
  33. /// 显示等待窗口
  34. /// </summary>
  35. /// <returns></returns>
  36. public static void ShowSplashScreen(Type type)
  37. {
  38. CloseSplashScreen();
  39. SplashScreenManager.ShowForm(null, type, false, false, true);
  40. }
  41. /// <summary>
  42. /// 关闭等待窗口
  43. /// </summary>
  44. /// <returns></returns>
  45. public static void CloseSplashScreen()
  46. {
  47. if (SplashScreenManager.Default != null)
  48. {
  49. SplashScreenManager.CloseForm(true);
  50. }
  51. }
  52. }
  53. }

3.其他类调用

  1. SplashFormUtil.ShowSplashScreen(typeof(WaitForm1));
  2. /***耗时长的操作***/
  3. SplashFormUtil.CloseSplashScreen();

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/309597
推荐阅读
相关标签
  

闽ICP备14008679号