Tab Page Header Location

2019-08-20 01:45发布

问题:

I have a Tab Control in my Form and what I want to achieve is a simple way to get the exact location the Tab Page Header.

I've searched around, but I haven't found anything. Any ideas?

回答1:

This will make the Tab control blink a little but it will return a list of bounding Rectangles for the pages' headers..

SortedDictionary<int, Rectangle> GetTabBounds(TabControl tab)
{
    SortedDictionary<int, Rectangle> bounds = new SortedDictionary<int, Rectangle>();
    TabDrawMode tdm = tab.DrawMode;
    tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
    DrawItemEventHandler mit = (sl, el) => bounds.Add(el.Index, el.Bounds);
    tab.DrawItem += mit;
    tab.Refresh();
    tab.DrawItem -= mit;
    tab.DrawMode = tdm;
    tab.Invalidate();
    return bounds;
}

For a less exact result you may simply want to calculate them from the page index and tab item size..at least if your pages are all in one row.