TabControl.DrawItem 不会在用户绘制的 TabControl 上触发 [英] TabControl.DrawItem not firing on user painted TabControl

查看:52
本文介绍了TabControl.DrawItem 不会在用户绘制的 TabControl 上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hey, I've been trying to paint my own TabControl to get rid of the 3D Shadow but I am not having much success. The DrawItem event isn't firing at the moment. Do I have to shoot it myself? How do I do that?

代码:

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 方法:

I'm not sure on this, but I believe if you specify the ControlStyles.UserPaint bit to true, then the DrawItem won't fire. The other ControlStyles (AllPaintingInWmPaint and DoubleBuffer) have dependencies on each other, though, so you would need to leave them off as well. However, not setting the UserPaint bit to true will result in the Paint event not getting fired. What I have been doing is overriding the OnPaintBackground method:

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);
        }
    }
} 

我认为这对您有用,但可能还有其他方法可以做到.

I think this will work for you, but there may be other methods of doing it as well.

这篇关于TabControl.DrawItem 不会在用户绘制的 TabControl 上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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