简体   繁体   English

C#tabControl DrawFixed

[英]C# tabControl DrawFixed

I'm using TabControl and I have DrawFixed. 我正在使用TabControl,并且已经有了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 . 该框架为DrawStyle提供了两个选项:Normal和OwnerDrawFixed Normal allows Windows to set the tab size based on the text and font, OwnerDrawFixed means the tab size is completely fixed. Normal允许Windows根据文本和字体设置选项卡大小, OwnerDrawFixed表示选项卡大小是完全固定的。 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. 但是, OwnerDrawFixed使您可以访问OnDrawItem事件,该事件是您用来绘制选项卡本身的方式。

It now seems you've bitten the bullet and set UserPaint to True which means you are now doing all of the drawing. 现在看来,您已经把项目符号设置为True了,这意味着您正在绘制所有图形。 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. 我建议此时将DrawStyle设置回Normal ,然后可以合并一些幕后文字,以使Windows自动控制标签宽度。 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. 因此,在这里我将指出TabControl.GetTabRect(index As Integer) ,该方法可用于获取给定选项卡的边界矩形。 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. 这意味着我不需要使用OwnerDrawFixed即可在其中绘制边界。

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 . 但是,如果仍然希望获得更好的控制,则可以从ControlUserControl开始,完全实现要查看的任何控件,都可以达到80%的方法。 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 . 我本人也考虑过同样的问题,但没有考虑,因为我仍在托管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. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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