每次更改标签页时都会调用C#WinForm DrawItem

时间:2018-04-05 00:59:17

标签: c# winforms overlap

我有一个标签控件,并在我使用的标签标题中添加了一个关闭按钮:

private void systemRecordTabControl_DrawItem_1(object sender, DrawItemEventArgs e)
    {           
        e.Graphics.DrawImage(Properties.Resources.icon, e.Bounds.Right - 15, e.Bounds.Top + 3, 11, 11);
        e.Graphics.DrawString(systemRecordTabControl.TabPages[e.Index].Text, new Font("Arial", 12, FontStyle.Underline), Brushes.Black, e.Bounds.Left + 12, e.Bounds.Top + 4);
        e.DrawFocusRectangle();
    }

程序运行时,每次在选项卡之间切换时,标题文本会再次在当前选项卡文本的顶部绘制,这会导致眼睛疼痛

的示例:

Eewwwww

VS

Mmmmmm

无论如何我可以阻止这种情况发生吗?我不知道如何检查它是否已经绘制

编辑:作为旁注,只有当我将标签页外观设置为"按钮"而DrawMode属性为" OwnerDrawFixed",同时将焦点更改为另一个控件会导致当前选定的标签页恢复正常(无文字重叠)

1 个答案:

答案 0 :(得分:1)

只需重绘背景:

    private void systemRecordTabControl_DrawItem_1(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground(); //<-- Redraw background       
        e.Graphics.DrawImage(Properties.Resources.icon, e.Bounds.Right - 15, e.Bounds.Top + 3, 11, 11);
        e.Graphics.DrawString(systemRecordTabControl.TabPages[e.Index].Text, new Font("Arial", 12, FontStyle.Underline), Brushes.Black, e.Bounds.Left + 12, e.Bounds.Top + 4);
        e.DrawFocusRectangle();
    }