【问题标题】:C# WinForm DrawItem being called each time I change tab page每次更改标签页时都会调用 C# WinForm DrawItem
【发布时间】:2018-04-05 00:59:16
【问题描述】:

我有一个标签控件并在我使用的标签标题中放置一个关闭按钮:

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

当程序运行时,每次我在标签之间切换时,标题文本都会再次绘制在当前标签文本的顶部,这导致它非常眼痛

例子:

对比

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

编辑:附带说明,只有当我将标签页外观设置为“按钮”而 DrawMode 属性为“OwnerDrawFixed”时才会发生这种情况,同时将焦点更改为另一个控件会导致当前选择的标签页恢复正常(没有文字重叠)

【问题讨论】:

  • 你应该清除它然后重绘,重绘可能有很多原因(例如将一个窗口移动到控件上),所以你应该清除那里的任何东西并重绘,否则你可以看看在绘制事件参数和(我正在记忆中工作)它应该有一个无效的区域,它正在请求重绘,这可能不是整个事情。
  • 我不确定这就是你的意思,但我尝试了 e.Graphics.Clear() 并且它需要一种颜色来重绘该区域,我使用了正常的背景颜色,但它会导致选项卡创建新页面时页面消失
  • 使用DrawBackground
  • 看看TabControl with Close and Add Button。您可以下载一个工作示例。

标签: c# winforms overlap


【解决方案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();
    }

【讨论】:

  • 谢谢,它可以工作,但现在选项卡标题上有蓝色背景,我尝试更改与选项卡控件有关的所有内容的背景颜色,但没有任何改变。它不是世界末日,但它不适合我表单的配色方案
  • 只需添加:e.Graphics.FillRectangle(new SolidBrush(BackColor), e.Bounds);在 e.DrawBackground(); 之后
猜你喜欢
  • 1970-01-01
  • 2020-02-24
  • 2020-02-21
  • 2017-12-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多