c# - TabControl.DrawItem 未在用户绘制的 TabControl 上触发

标签 c# winforms tabcontrol

嘿,我一直在尝试绘制自己的 TabControl 以摆脱 3D 阴影,但我没有取得太大的成功。 DrawItem 事件目前未触发。必须自己拍吗?我该怎么做?

代码:

namespace NCPad
{
    public partial class NCE_TabControl : TabControl
    {
        Rectangle TabBoundary;
        RectangleF TabTextBoundary;

        public NCE_TabControl()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.Paint += new PaintEventHandler(this.OnPaint);
            this.DrawItem += new DrawItemEventHandler(this.OnDrawItem);
        }

        protected void OnPaint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.FillRectangle(new SolidBrush(Color.Red), e.ClipRectangle);
        }

        protected void OnDrawItem(object sender, DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            g.FillRectangle(new SolidBrush(Color.Blue), this.TabBoundary);
            MessageBox.Show("hi");
        }

        protected override void OnLayout(LayoutEventArgs levent)
        {
            base.OnLayout(levent);

            this.TabBoundary = this.GetTabRect(0);
            this.TabTextBoundary = (RectangleF)this.GetTabRect(0);
        }
    }
}

最佳答案

对此我不确定,但我相信如果您将 ControlStyles.UserPaint 位指定为 true,则 DrawItem 不会触发。不过,其他 ControlStyles(AllPaintingInWmPaint 和 DoubleBuffer)相互依赖,因此您也需要将它们关闭。但是,不将 UserPaint 位设置为 true 将导致不触发 Paint 事件。我一直在做的是覆盖 OnPaintBackground 方法:

public partial class NCE_TabControl : TabControl
{
    Rectangle TabBoundary;
    RectangleF TabTextBoundary;
    StringFormat format = new StringFormat(); //for tab header text

    public NCE_TabControl()
    {   InitializeComponent();         
        this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
        this.DrawMode = TabDrawMode.OwnerDrawFixed;
        this.format.Alignment = StringAlignment.Center;
        this.format.LineAlignment = StringAlignment.Center;
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        Graphics g = pevent.Graphics;
        g.FillRectangle(new SolidBrush(Color.Red), 0, 0, this.Size.Width, this.Size.Height);

        foreach (TabPage tp in this.TabPages)
        {
            //drawItem
            int index = this.TabPages.IndexOf(tp);

            this.TabBoundary = this.GetTabRect(index);
            this.TabTextBoundary = (RectangleF)this.GetTabRect(index);

            g.FillRectangle(new SolidBrush(Color.LightBlue), this.TabBoundary);
            g.DrawString("tabPage " + index.ToString(), this.Font, new SolidBrush(Color.Black), this.TabTextBoundary, format);
        }
    }
} 

我认为这对您有用,但也可能有其他方法。

关于c# - TabControl.DrawItem 未在用户绘制的 TabControl 上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3115321/

相关文章:

c# - 使用 Windows API 从 C# Windows 窗体应用程序读取非 C# 应用程序文本框

c# - Type 对象属性的 TypeConverter

c# - 验证并打印小数点后两位的 double 值

c# - 无法从 DoubleClick 事件切换 TabItem

C# Linq group by DateTime 忽略小时和分钟

c# - 序列化我的对象时如何加密选定的属性?

c# - 尝试使用 JSON.Net 使用 SmartyStreets JSON... "Cannot deserialize JSON array into type Components"

c# - 重构该代码... Controls.Find 方法

c# - Winforms 选项卡页面上的关闭按钮

c# - 动态添加 TabItems