仿Office的程序载入窗体
初次接觸啟動(dòng)界面記不清是在哪一年了,估計(jì)是小學(xué)四年級第一次打開Office Word的時(shí)候吧,更記不清楚當(dāng)時(shí)的啟動(dòng)界面是長啥樣了。后來隨著使用的軟件越來越多,也見到各式各樣的啟動(dòng)界面。下面就列舉了兩個(gè)平常本人平常最常見的窗體,其實(shí)windows系統(tǒng)在啟動(dòng)的過程中,有Window字樣并且有動(dòng)畫效果的那個(gè)節(jié)面也算是一個(gè)啟動(dòng)界面。
其目的很明顯,就是程序啟動(dòng)之后,由于加載主界面的時(shí)間過長而導(dǎo)致用戶體驗(yàn)不佳,于是往往在顯示主界面之前多顯示一個(gè)不帶windows窗體元素的窗體,來顯示應(yīng)用程序加載的進(jìn)度或者直接是一個(gè)靜態(tài)的視圖,作用在于就是跟用戶反映程序是有響應(yīng)的并且正在運(yùn)行當(dāng)中,從而提高用戶體驗(yàn)。下面是我的載入窗
主要是仿照了Office 2013的風(fēng)格
簡約明了。其中窗體的底色,字體,文字顏色可以更改,左上角的制造商,中間的軟件名稱,左下角的進(jìn)度信息都可以更改。不過暫時(shí)還沒有把圖標(biāo)附加到左上角而已。
窗體的設(shè)計(jì)如下所示
載入窗的制造商,軟件名稱這些信息通過構(gòu)造函數(shù)傳參進(jìn)行設(shè)置,此外默認(rèn)的構(gòu)造函數(shù)被屏蔽了
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,因?yàn)樵谥骶€程上Show的話,本身主線程要加載時(shí)就會阻塞,這樣再阻塞的線程上show的話,窗體難以顯示。如果用ShowDialog的也不行,雖然它可以讓窗體顯示出來,但是調(diào)用了ShowDialog之后直到窗體關(guān)閉了才返回。就是說載入窗關(guān)閉了之后才執(zhí)行載入加載之類的操作,這樣顯得毫無意義。
這里只是用來了一個(gè)異步去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之前還多作了一個(gè)判斷,在于關(guān)閉窗體的方法時(shí)釋放了資源,因此在幾個(gè)地方都要注意,關(guān)閉窗體的處理如下
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 }?
由于調(diào)用了Invoke,這個(gè)需要判斷當(dāng)前窗體是否已經(jīng)是顯示了出來,否則就會因?yàn)榇绑w的句柄不存在而拋出異常。
在窗體上顯示的進(jìn)度信息,只是通過了一個(gè)外放的屬性實(shí)現(xiàn),其判斷的用意跟關(guān)閉窗體時(shí)的一樣。
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 }?
調(diào)用的形式如下
LoadingForm frm = new LoadingForm("HopeGi", "猴健工具集", "啟動(dòng)中...");frm.ShowLoading();//一系列操作frm.Message = "加載界面...";//一系列操作frm.CloseLoading();?
?
最近出了點(diǎn)狀況,電腦很久也沒碰了,前進(jìn)的步伐緩了下來。能寫出來的博客也不咋的,各位有什么好的建議和意見盡管提,謝謝!最后附上源碼,可是用到的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?
總結(jié)
以上是生活随笔為你收集整理的仿Office的程序载入窗体的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 安全网关之三:IPTables
- 下一篇: 【推荐】国外优秀Drupal答疑网站
