如何更改动态标签页眉头颜色? [英] How to change the dynamic tab page header color ?

查看:126
本文介绍了如何更改动态标签页眉头颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Windows窗体应用程序中更改标签页标题颜色,即如果我在选择下一个tabpage2时完成tabpage1事件,则在tabcontrol1(tabpage1,tabpage2,tabpage3)中有三个标签。如何更改为tabpage1标题颜色。







请帮帮我

How to change the tab page header color in windows form application,i.e I have a three tabs in tabcontrol1 (tabpage1,tabpage2,tabpage3) if I complete tabpage1 event when i select next tabpage2 . How can I change to tabpage1 Header color.



Please Help Me

推荐答案

如果您尝试更改背景颜色,您无疑会发现它会更改标签页部分的背景颜色,但不会更改标签标题(也就是标题)



您必须重新绘制项目才能实现此目的。例如:将它放在Form_Load事件中
If you try to change the backcolor you have no doubt discovered that it changes the background colour of the tab page section but not the tab header (aka title)

You have to redraw the item to achieve this. For example: Put this in the Form_Load event
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;



然后创建一个方法着色标题


Then create a method to "colour in" the heading

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
    Graphics g = e.Graphics;
    TabPage tp = tabControl1.TabPages[e.Index];

    StringFormat sf = new StringFormat();
    sf.Alignment = StringAlignment.Center;  //optional

    // This is the rectangle to draw "over" the tabpage title
    RectangleF headerRect = new RectangleF(e.Bounds.X, e.Bounds.Y + 2,e.Bounds.Width, e.Bounds.Height - 2);

    // This is the default colour to use for the non-selected tabs
    SolidBrush sb = new SolidBrush(Color.AntiqueWhite);

    // This changes the colour if we're trying to draw the selected tabpage
    if (tabControl1.SelectedIndex == e.Index)
        sb.Color = Color.Aqua;

    // Colour the header of the current tabpage based on what we did above
    g.FillRectangle(sb, e.Bounds);

    //Remember to redraw the text - I'm always using black for title text
    g.DrawString(tp.Text, tabControl1.Font, new SolidBrush(Color.Black), headerRect, sf);
}





您已创建tabControl动态记得添加事件



As you have created the tabControl dynamically remember to add the event

this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);


这篇关于如何更改动态标签页眉头颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆