C# 重繪tabControl,新增關閉按鈕(續)
阿新 • • 發佈:2019-01-03
在上一篇隨筆中,新增關閉按鈕是可以實現 ,但細心一點就會發現,每次關閉一個選項卡,tableControl都會自動跳到第一個頁面,顯然 這不是我們想要的,為此,我修改了部分的程式碼。除此之外,我還添加了一些兩個新的方法,用於建立新的tablePage.以下是我實現 的類
#region 重繪tablecontrol的類(新增關閉頁面功能),ablepage的建立,以及窗體的附加 public class DrawTabControl { TabControl tabControl1 = null; Font font1 = null; //開啟和關閉的頁面索引 int tabindex_show = 0; int tabindex_close = 0; public DrawTabControl() { } /// <summary> /// 初始化 /// </summary> /// <param name="tabcontrol">TacControl控制元件</param> /// <param name="fo">主程式this.Font</param> public DrawTabControl(TabControl tabcontrol,Font fo) { tabControl1 = tabcontrol; font1 = fo; } #region 關於tabcontrol的重繪 const int CLOSE_SIZE = 15;//大小 //tabPage標籤圖片 Bitmap image = new Bitmap("D:\\power_003.png"); public void ClearPage() { //清空控制元件 //this.MainTabControl.TabPages.Clear(); //繪製的方式OwnerDrawFixed表示由窗體繪製大小也一樣 this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed; this.tabControl1.Padding = new System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE - 8); this.tabControl1.DrawItem += new DrawItemEventHandler(this.tabControl1_DrawItem); this.tabControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabControl1_MouseDown); } /// <summary> /// 產生新的窗體時觸發 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tabControl1_DrawItem(object sender, DrawItemEventArgs e) { try { Rectangle myTabRect = this.tabControl1.GetTabRect(e.Index); //先新增TabPage屬性 e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, this.font1, SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2); //再畫一個矩形框 using (Pen p = new Pen(Color.White)) { myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2); myTabRect.Width = CLOSE_SIZE; myTabRect.Height = CLOSE_SIZE; e.Graphics.DrawRectangle(p, myTabRect); } ////填充矩形框 Color recColor = e.State == DrawItemState.Selected ? Color.White : Color.White; using (Brush b = new SolidBrush(recColor)) { e.Graphics.FillRectangle(b, myTabRect); } //畫關閉符號 using (Pen objpen = new Pen(Color.Black)) { ////============================================= //自己畫X ////"\"線 Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3); Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3); e.Graphics.DrawLine(objpen, p1, p2); ////"/"線 Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3); Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3); e.Graphics.DrawLine(objpen, p3, p4); ////============================================= //使用圖片 //Bitmap bt = new Bitmap(image); //Point p5 = new Point(myTabRect.X, 4); //e.Graphics.DrawImage(bt, p5); //e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.font1, objpen.Brush, p5); } e.Graphics.Dispose(); } catch (Exception) { } } /// <summary> /// 滑鼠點選選項卡時的觸發 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tabControl1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { int x = e.X, y = e.Y; //計算關閉區域 Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.SelectedIndex); myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2); myTabRect.Width = CLOSE_SIZE; myTabRect.Height = CLOSE_SIZE; //如果滑鼠在區域內就關閉選項卡,如果不在就顯示選項卡 //如果關閉的選項卡正好開啟著,則關閉當前先項卡,顯示下一個選項卡 bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom; if (isClose == true) { //判斷關閉的頁面索引是否比正在顯示的頁面的索引 //如果關閉的頁面的索引大於或等於顯示正在顯示的頁面索引,剛tabindex_close的值為關閉的頁面的索引; //相反 的,剛tabindex_close則為tabindex_show-1; tabindex_close = this.tabControl1.SelectedIndex >= tabindex_show ? this.tabControl1.SelectedIndex : tabindex_close - 1; this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab); } else { tabindex_close = this.tabControl1.SelectedIndex; tabindex_show = this.tabControl1.SelectedIndex; } if (tabindex_close < tabindex_show) { tabindex_show = tabindex_show - 1; } //顯示頁面 try { this.tabControl1.SelectedTab = this.tabControl1.TabPages[tabindex_show]; } catch (Exception ex) { } Console.WriteLine("顯示頁面" + tabindex_show + "關閉頁面" + tabindex_close); } } #endregion #region 關於tablepage的建立,以及窗體的附加 /// <summary> /// 建立新的選項卡 /// </summary> /// <param name="ParentForm">父窗體</param> /// <param name="ChildForm">子窗體</param> /// <param name="Pagename">頁面名稱</param> public void CreatePage(Form ParentForm,Form ChildForm, string Pagename) { ChildForm.MdiParent = ParentForm; ChildForm.Text = "第二個窗體"; ChildForm.Dock = DockStyle.Fill; ChildForm.TopMost = true; ChildForm.FormBorderStyle = FormBorderStyle.None; TabPage tb1 = AddPage(Pagename); tb1.Controls.Add(ChildForm); this.tabControl1.Controls.Add(tb1); ChildForm.Show(); } /// <summary> /// 新增選項卡 /// </summary> /// <param name="PageName">頁面名稱</param> /// <returns>一個頁面</returns> public TabPage AddPage(string PageName) { TabPage tabPage = new TabPage(); tabPage.Location = new System.Drawing.Point(4, 21); tabPage.Name = PageName; tabPage.Padding = new System.Windows.Forms.Padding(3); tabPage.Size = new System.Drawing.Size(658, 410); tabPage.Text = PageName; tabPage.UseVisualStyleBackColor = true; return tabPage; } #endregion } #endregion