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

发布于 2024-09-07 11:53:03 字数 1352 浏览 3 评论 0原文

嘿,我一直在尝试绘制自己的 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);
        }
    }
}

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?

Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就像说晚安 2024-09-14 11:53:03

我对此不确定,但我相信如果您将 ControlStyles.UserPaint 位指定为 true,则 DrawItem 将不会触发。不过,其他 ControlStyle(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);
        }
    }
} 

我认为这对您有用,但也可能有其他方法可以做到这一点。

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.

森林散布 2024-09-14 11:53:03

为了触发 DrawItem 事件,请在选项卡控件上设置 DrawMode = OwnerDrawFixed
http://msdn.microsoft.com/en -us/library/system.windows.forms.tabcontrol.drawitem.aspx

In order to get DrawItem event fired, set DrawMode = OwnerDrawFixed on the Tab Control
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.drawitem.aspx

救星 2024-09-14 11:53:03

一种简单的方法是将 TabControl 的 Appearance 属性更改为 ButtonsFlatButtons 并将 DrawMode badk 设置为 <强>正常。

One simple way is to change the TabControl's Appearance property to either Buttons or FlatButtons and set the DrawMode badk to Normal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文