简体   繁体   中英

C# tabControl DrawFixed

I'm using TabControl and I have DrawFixed. I just want only draw the tabs not the panel under it.

tab控制面板

How can I remove it?

Also I'd like to ask, can I change tabs size? I've long text which I'd like see all if it's selected but I'd like see it cropped if it's not active.

I've following in draw event, but it always draws the tab in the same size.

if (e.State == DrawItemState.Selected)
{
    e.Graphics.FillRectangle(Brushes.White, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
}
else
{
    e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
    text = text.Length > 10 ? text.Substring(0, 10) + "..." : text;
}

e.Graphics.DrawString(text, e.Font, Brushes.Black, e.Bounds.Left + 17, e.Bounds.Top + 3);

Thanks in advance.

Chronologically in your question, you have asked how to get rid of that bar at the top. If you insisted on using 'faux' tab pages where the tabs merely control content of the fixed set of controls, then shrinking the height of the tab control to a point where that isn't visible is probably an acceptable solution. I just tried it and with some tweaking it looks mostly what I think you are after. For the record I'd recommend actually using the tab pages as intended, that is as hosts to controls, even if you make a custom control that brings together all the controls you want visible. This will fit the tab paradigm much better.

For the second point you'd like to resize the tabs. Impossible. The framework gives two options for DrawStyle , Normal and OwnerDrawFixed . Normal allows Windows to set the tab size based on the text and font, OwnerDrawFixed means the tab size is completely fixed. There is no more control over this. OwnerDrawFixed however gives you access to the OnDrawItem event which is what you are wanting to use for painting the tabs themselves.

It now seems you've bitten the bullet and set UserPaint to True which means you are now doing all of the drawing. I recommend at this point to set DrawStyle back to Normal , then you can kludge some behind-the-scenes text to have Windows control the tab widths automatically. I will warn this won't be very robust since everyone has different font settings and a few pixels off and nothing will draw right.

So here I'll point out TabControl.GetTabRect(index As Integer) , the method you can use to get the bounding rectangle of a given tab. I use this in a loop over all the tab indices and then do all the drawing I need for the tab within the rectangle provided from each tab. This means I don't need to use OwnerDrawFixed to get the bounds to paint within.

However still if you want better control, you're 80% the way to just implementing exactly whatever control you want to see, starting from either Control or UserControl . A similar look could be achieved from overlapping buttons with some logic to paint and lay them out. Then you could get all the text appearance you want also. I considered the same myself but didn't because I am still hosting TabPages . Since you're free from that it would be even easier...

Just use page text default property it will auto fix tab size for you according to the size of text.. then paint your text by your self .. if you still want additional space for painting image or some thing else then use padding which is the property of tab control not the tab page. i hope it will help full for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM