How to remove dotted focus rectangle from tab control? [closed]

泪湿孤枕 提交于 2019-12-04 06:58:16

问题


I'm trying to remove dotted focus rectangle from my custom Tab Control. I've tried everything and I could not remove that rectangle.

As you can see in the picture, the focus rectangle is disturbing in my application design.

Please help!


回答1:


To remove the focus cue, you have to set UserPaint to true, and then paint the entire tab control yourself, including the borders, text, backgrounds, highlighting, hot-tracking, etc.

The following code only paints the tab text and the background:

public class TC2 : TabControl {
    public TC2() {
        this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);
        var g = e.Graphics;

        TabPage currentTab = this.SelectedTab;
        for (int i = 0; i < TabPages.Count; i++) {
            TabPage tp = TabPages[i];
            Rectangle r = GetTabRect(i);
            Brush b = (tp == currentTab ? Brushes.LightSteelBlue : Brushes.LightGray);
            g.FillRectangle(b, r);
            TextRenderer.DrawText(g, tp.Text, tp.Font, r, tp.ForeColor);
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e) {
        base.OnPaintBackground(e);
    }
}


来源:https://stackoverflow.com/questions/31486507/how-to-remove-dotted-focus-rectangle-from-tab-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!