C#利用tabControl控件实现多窗体嵌入及关闭

创建一个主窗体(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);  
               }  
           }  
       } 

==

出处:http://blog.csdn.net/w1wqw/article/details/44356439

posted on   jack_Meng  阅读(16353)  评论(1编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2014-01-18 公司被一条 update 语句干趴了!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩