如何用c#实现在vs2005环境,实现TabControl选中不同TabPage页时,tab页的文字变成红色?或者背景发生变化....

解决方案 »

  1.   

    http://www.codeproject.com/cs/miscctrl/magictabcontrol.asp
    http://www.codeproject.com/cs/miscctrl/flattabcontrol.asp
      

  2.   

    如果要tab页里面的文字变色比较简单,在TabControl的SelectedIndexChanged事件处理程序里设置tabControl1.SelectedTab.ForeColor = Color.Red;就可以但是如果要改变tab页头的文字(标题)就比较麻烦,这个控件本身好像是不提供此功能的,需要重载并改写这个控件
      

  3.   

    http://www.codeproject.com/cs/miscctrl/magictabcontrol.asp
      

  4.   

    以前程序中的一个例子: this.tcMain.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tcMain_DrawItem); /// <summary>
    /// 画选项卡
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void tcMain_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
    if (tcMain.SelectedIndex == e.Index)
    {
    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(216,228,248)),e.Bounds);
    e.Graphics.DrawString(tcMain.TabPages[e.Index].Text,new System.Drawing.Font("宋体",10,System.Drawing.FontStyle.Bold),new SolidBrush(Color.Black),e.Bounds.Left + 4,e.Bounds.Top + 7,StringFormat.GenericDefault);
    }
    else
    {
    e.Graphics.DrawString(tcMain.TabPages[e.Index].Text,e.Font,new SolidBrush(Color.Black),e.Bounds.Left + 5,e.Bounds.Top + 8,StringFormat.GenericDefault);
    }
    }
      

  5.   

    private void MainFormtabControl_SelectedIndexChanged(object sender, EventArgs e)
            {this.MainFormtabControl.SelectedTab.ForeColor = Color.Red;
    .........................
    应用后,tab的标题,选中某个标签页没有变化,该怎么做?
      

  6.   

    this.MainFormtabControl.SelectedTab.ForeColor指的是Tab页的前景色,并不能改变标题的前景色.
    可以按我的例子试一下,是用来改变Tab页标题的.需注意的是,还要修改Tab控件的一个属性的值,具体是什么名字我记不清了.
      

  7.   

    就是想在SelectedIndexChanged事件里,实现改变当 前选中的tab页,tab页头的文字(标题)文字的颜色....
      

  8.   

    this.tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
    {
    if (this.tabControl1.SelectedIndex == e.Index)
    {
    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(216, 228, 248)), e.Bounds);
    e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold), new SolidBrush(Color.Red), e.Bounds.Left + 4, e.Bounds.Top + 7, StringFormat.GenericDefault);
    }
    else
    {
    e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, new SolidBrush(Color.Black), e.Bounds.Left + 5, e.Bounds.Top + 8, StringFormat.GenericDefault);
    }
    }
      

  9.   

    bigrongshu(Simple life @ Innovation) ( ) 信誉:100    Blog 
    private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) {
    if (this.tabControl1.SelectedIndex == e.Index) {
    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0,0, 0)), e.Bounds);//标题头的背景颜色
    e.Graphics.DrawString("好好",new Font("宋体",12),Brushes.Beige,new Point(12,12));

    }
    else {
    e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, new SolidBrush(Color.Black), e.Bounds.Left + 5, e.Bounds.Top + 8, StringFormat.GenericDefault);
    }
    }
      

  10.   

    楼上说的正确,微软的这个控件的确麻烦,那就是说每个tab页要想有这样的变化,都要重绘!
    public Form1()
        {
            TabControl tabControl1 = new TabControl();
            TabPage tabPage1 = new TabPage();        // Allows access to the DrawItem event. 
            tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;        tabControl1.SizeMode = TabSizeMode.Fixed;
            tabControl1.Controls.Add(tabPage1);
            tabControl1.ItemSize = new Size(80, 30);
            tabControl1.Location = new Point(25, 25);
            tabControl1.Size = new Size(250, 250);
            tabPage1.TabIndex = 0;
            ClientSize = new Size(300, 300);
            Controls.Add(tabControl1);        tabArea = tabControl1.GetTabRect(0);
            tabTextArea = (RectangleF)tabControl1.GetTabRect(0);        // Binds the event handler DrawOnTab to the DrawItem event 
            // through the DrawItemEventHandler delegate.
            tabControl1.DrawItem += new DrawItemEventHandler(DrawOnTab);
        }    // Declares the event handler DrawOnTab which is a method that
        // draws a string and Rectangle on the tabPage1 tab.
        private void DrawOnTab(object sender, DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Blue);
            Font font = new Font("Arial", 10.0f);
            SolidBrush brush = new SolidBrush(Color.Red);        g.DrawRectangle(p, tabArea);
            g.DrawString("tabPage1", font, brush, tabTextArea);
        }
      

  11.   

    还有我已经定义了每个标签的内容,如果只是为了选中某个标签标题颜色有变化,都重绘是否很麻烦哦?tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;!!!!!(改成了程序来绘制的模式)
     private void MainFormtabControl_SelectedIndexChanged(object sender, EventArgs e)
            {
                TreeInfo ft = TreeInfo.GetForm();
                ft.DockableAreas = WeifenLuo.WinFormsUI.DockAreas.DockRight;
                ft.editBKMC.Text = this.MainFormtabControl.SelectedTab.Text;
      

  12.   

    tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
    这句要在事件里定义上,并在
    private void MainFormtabControl_SelectedIndexChanged(object sender, EventArgs e)里面加上,效果实现了,只是在页面切换一瞬间,标签那块感觉被 一块颜色突然盖掉...
    .Graphics.FillRectangle(new SolidBrush(Color.FromArgb(212, 208, 200)), e.Bounds);
                    e.Graphics.DrawString(MainFormtabControl.TabPages[e.Index].Text, new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular), new SolidBrush(Color.Red), e.Bounds.Left + 4, e.Bounds.Top + 7, StringFormat.GenericDefault);