C#利用tabControl控制項實現多表單嵌入及關閉

來源:互聯網
上載者:User

標籤:

建立一個主表單(Formmain)、兩個副表單(Form1,Form2);在主表單中分別添加一個menuStrip控制項、tabControl控制項,並在menu控制項上添加一個主菜單和兩個子功能表 

 繼而,選中tabControl控制項屬性修改 DrawMode = OwnerDrawFixed,再根據如下代碼添加即可:public void Add_TabPage(string str, Form myForm)         {             if (tabControlCheckHave(this.MainTabControl, str))              {                  return;              }             else             {                 MainTabControl.TabPages.Add(str);                 MainTabControl.SelectTab(MainTabControl.TabPages.Count - 1);                                  myForm.FormBorderStyle = FormBorderStyle.None;                 myForm.Dock = DockStyle.Fill;                 myForm.TopLevel = false;                 myForm.Show();                 myForm.Parent = MainTabControl.SelectedTab;             }         }           public bool tabControlCheckHave(System.Windows.Forms.TabControl tab, String tabName)         {             for (int i = 0; i < tab.TabCount; i++)             {                 if (tab.TabPages[i].Text == tabName)                 {                     tab.SelectedIndex = i;                     return true;                 }             }             return false;         }          private void form1ToolStripMenuItem_Click(object sender, EventArgs e)         {             Form1 f1= new Form1();             Add_TabPage("f1中華人民共和國表單  ", f1);         }           private void form2ToolStripMenuItem_Click(object sender, EventArgs e)         {             Form2 f2 = new Form2();             Add_TabPage("f2朝鮮表單  ", f2);         }           private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)         {             /*如果將 DrawMode 屬性設定為 OwnerDrawFixed,            則每當 TabControl 需要繪製它的一個選項卡時,它就會引發 DrawItem 事件*/             try             {                 this.MainTabControl.TabPages[e.Index].BackColor = Color.LightBlue;                 Rectangle tabRect = this.MainTabControl.GetTabRect(e.Index);                 e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, SystemBrushes.ControlText, (float)(tabRect.X + 2), (float)(tabRect.Y + 2));                 using (Pen pen = new Pen(Color.White))                 {                     tabRect.Offset(tabRect.Width - 15, 2);                     tabRect.Width = 15;                     tabRect.Height = 15;                     e.Graphics.DrawRectangle(pen, tabRect);                 }                 Color color = (e.State == DrawItemState.Selected) ? Color.LightBlue : Color.White;                 using (Brush brush = new SolidBrush(color))                 {                     e.Graphics.FillRectangle(brush, tabRect);                 }                 using (Pen pen2 = new Pen(Color.Red))                 {                     Point point = new Point(tabRect.X + 3, tabRect.Y + 3);                     Point point2 = new Point((tabRect.X + tabRect.Width) - 3, (tabRect.Y + tabRect.Height) - 3);                     e.Graphics.DrawLine(pen2, point, point2);                     Point point3 = new Point(tabRect.X + 3, (tabRect.Y + tabRect.Height) - 3);                     Point point4 = new Point((tabRect.X + tabRect.Width) - 3, tabRect.Y + 3);                     e.Graphics.DrawLine(pen2, point3, point4);                 }                 e.Graphics.Dispose();             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);             }         }           private void MainTabControl_MouseDown(object sender, MouseEventArgs e)         {             if (e.Button == MouseButtons.Left)             {  www.2cto.com               int x = e.X;                 int y = e.Y;                   Rectangle tabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);                 tabRect.Offset(tabRect.Width - 0x12, 2);                 tabRect.Width = 15;                 tabRect.Height = 15;                 if ((((x > tabRect.X) && (x < tabRect.Right)) && (y > tabRect.Y)) && (y < tabRect.Bottom))                 {                     this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);                 }             }         }  

C#利用tabControl控制項實現多表單嵌入及關閉

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.