仿Office的程序载入窗体

  初次接触启动界面记不清是在哪一年了,估计是小学四年级第一次打开Office Word的时候吧,更记不清楚当时的启动界面是长啥样了。后来随着使用的软件越来越多,也见到各式各样的启动界面。下面就列举了两个平常本人平常最常见的窗体,其实windows系统在启动的过程中,有Window字样并且有动画效果的那个节面也算是一个启动界面。

  其目的很明显,就是程序启动之后,由于加载主界面的时间过长而导致用户体验不佳,于是往往在显示主界面之前多显示一个不带windows窗体元素的窗体,来显示应用程序加载的进度或者直接是一个静态的视图,作用在于就是跟用户反映程序是有响应的并且正在运行当中,从而提高用户体验。下面是我的载入窗

主要是仿照了Office 2013的风格

  简约明了。其中窗体的底色,字体,文字颜色可以更改,左上角的制造商,中间的软件名称,左下角的进度信息都可以更改。不过暂时还没有把图标附加到左上角而已。

窗体的设计如下所示

  载入窗的制造商,软件名称这些信息通过构造函数传参进行设置,此外默认的构造函数被屏蔽了

 1         private LoadingForm()
 2         {
 3             InitializeComponent();
 4         }
 5 
 6         public LoadingForm(string logoStr, string appNameStr, string iniMsgStr):this()
 7         {
 8             this.lbLogo.Text = logoStr;
 9             AppName = appNameStr;
10             this.lbMsg.Text = iniMsgStr;
11             this.picLoading.Width = this.Width;
12 
13             this.MouseDown+=new MouseEventHandler(LoadingForm_MouseDown);
14             foreach (Control con in this.Controls)
15             {
16                 if (con.Equals(this.btnClose)) continue;
17                 con.MouseDown += new MouseEventHandler(LoadingForm_MouseDown);
18             }
19         }

 

  对窗体的打开并非用单纯的Show或者ShowDialog,因为在主线程上Show的话,本身主线程要加载时就会阻塞,这样再阻塞的线程上show的话,窗体难以显示。如果用ShowDialog的也不行,虽然它可以让窗体显示出来,但是调用了ShowDialog之后直到窗体关闭了才返回。就是说载入窗关闭了之后才执行载入加载之类的操作,这样显得毫无意义。

  这里只是用来了一个异步去ShowDialog。代码如下

1         public void ShowLoading()
2         {
3             Action callback = new Action(delegate()
4             {
5                 if (!this.IsDisposed)
6                     this.ShowDialog();
7             });
8             callback.BeginInvoke(null, null);
9         }

 

  这里在ShowDialog之前还多作了一个判断,在于关闭窗体的方法时释放了资源,因此在几个地方都要注意,关闭窗体的处理如下

 1         public void CloseLoading()
 2         {
 3             if (!this.IsDisposed && this.IsHandleCreated)
 4             {
 5                 this.Invoke((Action)delegate
 6                 {
 7                     this.Close();
 8                     this.Dispose();
 9 
10                 });
11             }
12         }

 

  由于调用了Invoke,这个需要判断当前窗体是否已经是显示了出来,否则就会因为窗体的句柄不存在而抛出异常。

  在窗体上显示的进度信息,只是通过了一个外放的属性实现,其判断的用意跟关闭窗体时的一样。

 1         public string Message {
 2             get { return this.lbMsg.Text; }
 3             set 
 4             {
 5                 if (!this.IsDisposed&&this.IsHandleCreated)
 6                 {
 7                     this.Invoke((Action)delegate()
 8                     {
 9                         this.lbMsg.Text = value;
10                     });
11                 }
12             }
13         }

 

为了窗体能具备可拖拽的效果,还额外加了一下的代码

 1         [DllImport("user32.dll")]
 2         private  static extern bool ReleaseCapture();
 3         [DllImport("user32.dll")]
 4         private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
 5         private const int WM_SYSCOMMAND = 0x0112;
 6         private const int SC_MOVE = 0xF010;
 7         private const int HTCAPTION = 0x0002;
 8 
 9         protected void LoadingForm_MouseDown(object sender, MouseEventArgs e)
10         {
11 
12             ReleaseCapture();
13             SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
14         }

 

调用的形式如下

            LoadingForm frm = new LoadingForm("HopeGi", "猴健工具集", "启动中...");
            frm.ShowLoading();
            //一系列操作
            frm.Message = "加载界面...";
            //一系列操作
            frm.CloseLoading();

 

 

  最近出了点状况,电脑很久也没碰了,前进的步伐缓了下来。能写出来的博客也不咋的,各位有什么好的建议和意见尽管提,谢谢!最后附上源码,可是用到的gif图片没附带上来

  1     public partial class LoadingForm : Form
  2     {
  3 
  4         [DllImport("user32.dll")]
  5         private  static extern bool ReleaseCapture();
  6         [DllImport("user32.dll")]
  7         private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  8         private const int WM_SYSCOMMAND = 0x0112;
  9         private const int SC_MOVE = 0xF010;
 10         private const int HTCAPTION = 0x0002;
 11 
 12         private LoadingForm()
 13         {
 14             InitializeComponent();
 15         }
 16 
 17         public LoadingForm(string logoStr, string appNameStr, string iniMsgStr):this()
 18         {
 19             this.lbLogo.Text = logoStr;
 20             AppName = appNameStr;
 21             this.lbMsg.Text = iniMsgStr;
 22             this.picLoading.Width = this.Width;
 23 
 24             this.MouseDown+=new MouseEventHandler(LoadingForm_MouseDown);
 25             foreach (Control con in this.Controls)
 26             {
 27                 if (con.Equals(this.btnClose)) continue;
 28                 con.MouseDown += new MouseEventHandler(LoadingForm_MouseDown);
 29             }
 30         }
 31 
 32         public string Message {
 33             get { return this.lbMsg.Text; }
 34             set 
 35             {
 36                 if (!this.IsDisposed&&this.IsHandleCreated)
 37                 {
 38                     this.Invoke((Action)delegate()
 39                     {
 40                         this.lbMsg.Text = value;
 41                     });
 42                 }
 43             }
 44         }
 45 
 46         public void ShowLoading()
 47         {
 48             Action callback = new Action(delegate()
 49             {
 50                 if (!this.IsDisposed)
 51                     this.ShowDialog();
 52             });
 53             callback.BeginInvoke(null, null);
 54         }
 55 
 56         public void CloseLoading()
 57         {
 58             if (!this.IsDisposed && this.IsHandleCreated)
 59             {
 60                 this.Invoke((Action)delegate
 61                 {
 62                     this.Close();
 63                     this.Dispose();
 64 
 65                 });
 66             }
 67         }
 68 
 69 
 70         private string AppName {
 71             get { return this.lbAppName.Text; }
 72             set {
 73                 this.lbAppName.Text = value;
 74                 this.lbAppName.Location = new Point((this.Width - this.lbAppName.Width) / 2, this.lbAppName.Location.Y);
 75             }
 76         }
 77 
 78         private void btnClose_Click(object sender, EventArgs e)
 79         {
 80             this.CloseLoading();
 81             Environment.Exit(1);
 82         }
 83 
 84         protected void LoadingForm_MouseDown(object sender, MouseEventArgs e)
 85         {
 86 
 87             ReleaseCapture();
 88             SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
 89         }
 90 
 91         #region Windows Form Designer generated code
 92 
 93         /// <summary>
 94         /// Required method for Designer support - do not modify
 95         /// the contents of this method with the code editor.
 96         /// </summary>
 97         private void InitializeComponent()
 98         {
 99             this.lbLogo = new System.Windows.Forms.Label();
100             this.lbAppName = new System.Windows.Forms.Label();
101             this.lbMsg = new System.Windows.Forms.Label();
102             this.btnClose = new System.Windows.Forms.PictureBox();
103             this.picLoading = new System.Windows.Forms.PictureBox();
104             ((System.ComponentModel.ISupportInitialize)(this.btnClose)).BeginInit();
105             ((System.ComponentModel.ISupportInitialize)(this.picLoading)).BeginInit();
106             this.SuspendLayout();
107             // 
108             // lbLogo
109             // 
110             this.lbLogo.AutoSize = true;
111             this.lbLogo.Font = new System.Drawing.Font("楷体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
112             this.lbLogo.ForeColor = System.Drawing.Color.White;
113             this.lbLogo.Location = new System.Drawing.Point(13, 13);
114             this.lbLogo.Name = "lbLogo";
115             this.lbLogo.Size = new System.Drawing.Size(44, 16);
116             this.lbLogo.TabIndex = 0;
117             this.lbLogo.Text = "LOGO";
118             // 
119             // lbAppName
120             // 
121             this.lbAppName.AutoSize = true;
122             this.lbAppName.Font = new System.Drawing.Font("黑体", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
123             this.lbAppName.ForeColor = System.Drawing.Color.White;
124             this.lbAppName.Location = new System.Drawing.Point(138, 88);
125             this.lbAppName.Name = "lbAppName";
126             this.lbAppName.Size = new System.Drawing.Size(148, 35);
127             this.lbAppName.TabIndex = 1;
128             this.lbAppName.Text = "AppName\r\n";
129             // 
130             // lbMsg
131             // 
132             this.lbMsg.AutoSize = true;
133             this.lbMsg.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
134             this.lbMsg.ForeColor = System.Drawing.Color.White;
135             this.lbMsg.Location = new System.Drawing.Point(16, 187);
136             this.lbMsg.Name = "lbMsg";
137             this.lbMsg.Size = new System.Drawing.Size(49, 14);
138             this.lbMsg.TabIndex = 3;
139             this.lbMsg.Text = "label1";
140             // 
141             // btnClose
142             // 
143             this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
144             this.btnClose.Cursor = System.Windows.Forms.Cursors.Hand;
145             this.btnClose.Image = global::AllTypeTest.Properties.Resources.Delete;
146             this.btnClose.Location = new System.Drawing.Point(388, 11);
147             this.btnClose.Name = "btnClose";
148             this.btnClose.Size = new System.Drawing.Size(24, 24);
149             this.btnClose.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
150             this.btnClose.TabIndex = 4;
151             this.btnClose.TabStop = false;
152             this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
153             // 
154             // picLoading
155             // 
156             this.picLoading.Image = global::AllTypeTest.Properties.Resources.download;
157             this.picLoading.Location = new System.Drawing.Point(0, 126);
158             this.picLoading.Name = "picLoading";
159             this.picLoading.Size = new System.Drawing.Size(420, 20);
160             this.picLoading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
161             this.picLoading.TabIndex = 2;
162             this.picLoading.TabStop = false;
163             // 
164             // LoadingForm
165             // 
166             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
167             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
168             this.BackColor = System.Drawing.Color.ForestGreen;
169             this.ClientSize = new System.Drawing.Size(424, 211);
170             this.Controls.Add(this.btnClose);
171             this.Controls.Add(this.lbMsg);
172             this.Controls.Add(this.picLoading);
173             this.Controls.Add(this.lbAppName);
174             this.Controls.Add(this.lbLogo);
175             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
176             this.Name = "LoadingForm";
177             this.ShowInTaskbar = false;
178             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
179             this.Text = "LoadingForm";
180             this.TopMost = true;
181             ((System.ComponentModel.ISupportInitialize)(this.btnClose)).EndInit();
182             ((System.ComponentModel.ISupportInitialize)(this.picLoading)).EndInit();
183             this.ResumeLayout(false);
184             this.PerformLayout();
185 
186         }
187 
188         #endregion
189 
190         private System.Windows.Forms.Label lbLogo;
191         private System.Windows.Forms.Label lbAppName;
192         private System.Windows.Forms.PictureBox picLoading;
193         private System.Windows.Forms.Label lbMsg;
194         private System.Windows.Forms.PictureBox btnClose;
195         
196     }
LoadingForm

 

posted @ 2014-01-08 17:31  猴健居士  阅读(1883)  评论(6编辑  收藏  举报