初次接觸啟動界面記不清是在哪一年了,估計是小學四年級第一次打開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 }