两行标签文本 [英] Two line Tab Text

查看:95
本文介绍了两行标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Visual Basic 中看到过这一点(尽管我还没有看到代码,或者如果可能的话,我还没有看到它在设计视图中是如何完成的).有没有办法在 WinForms 中创建两行标签名称?请看附图,我圈出来是为了强调.当然,必须有办法做到这一点.事实上,所附图片是在VB6中完成的,不幸的是我无法访问代码.

I've seen this done in Visual Basic (though I haven't seen the code, or how it is done in design view, if that is possible). Is there way to create a two-line tab name in WinForms? Please see attached picture, I've circled it for emphasis. Surely, there must be a way to do it. In fact the attached picture is done in VB6, which unfortunately I have no access to the code.

注意:如果这是一个坏主意或不是一个好的做法,请告诉我.此外,如果解决方案太复杂且不值得探索,我愿意放弃它,但如果它足够简单,那将对我有很大帮助.

Note: If this is a bad idea or not a good practice, please let me know. Also if the solution is too complicated and not worth exploring, I am open to abandoning it, but if it is simple enough, that would help me a lot.

我在屏幕上的主要空间用完了,因为我们的一些表单上有 12 个选项卡(这无济于事),而且我们的屏幕被限制为最大1280 x 720.非常感谢任何帮助.

I'm running out of prime real estate on the screen, as some of our forms have 12 tabs on them (it can't be helped), and our screens are limited to a maximum of 1280 x 720. Any help is greatly appreciated.

不幸的是,我不允许发布图片,即这是我的第一篇文章.如果这有帮助,我会将它们张贴在其他可能的地方.

Unfortunately I am not allowed to post a picture, i.e. this is my first post. If that helps, I will post them somewhere else where it is possible.

也许此编辑暂时会有所帮助,(抱歉插图不佳,鉴于(发布)编辑器限制,这是我能做的最好的事情):

Maybe this edit will help for the moment, (sorry for the poor illustration, it's the best I can do given the (posting) editor limitations):

|标签 |标签 |标签 |
|No.1 |2号|3号|

| Tab | Tab | Tab |
|No.1 | No.2| No.3|

上面的标签标签(文本)应该在标签名称上有两行,例如Tab 1 No. 1、Tab 2 No. 2 等,其中 Tab 1 在第一行,No.1 在第二行...等

The above tab labels (text) should have two lines on the tab names, e.g. Tab 1 No. 1, Tab 2 No. 2, etc. where Tab 1 is on the first line, and No. 1 is on the second line...etc.

这是图片链接:http://imgur.com/gallery/LZSy5r5/new

推荐答案

完整的解决方案分布在 TabControl 及其 TabPages 上.

The full solution is spread over the TabControl and its TabPages.

要强制使用 NewLine,请在 TabPageText 中插入 \n.

To enforce a NewLine you insert a \n in the TabPage's Text.

tabPage1.Text = "Line one\nLine two";

注意你需要code来设置Text.在 Designer 中设置它不起作用!(它从字面上使用 Backslash 而不是作为转义序列,因为它只提供一个单行字段,你也不能使用 Shift-Enter ..)

Note that you need code to set the Text. Setting it in the Designer won't work! (It takes the Backslash literally instead of as an escape sequence and since it only offers a single line field you can't use Shift-Enter either..)

您仍然可以使用设计器,但必须在代码中强制使用 newline,可能像这样:

You still can use the designer, but will have to enforce the newline in code, maybe like this:

tabPage1.Text = tabPage1.Text.Replace("\\n", "\n");

或者,更好的是,在一个循环中:

Or, bettter, in a loop:

foreach (TabPage tp in tabControl1.TabPages) tp.Text = tp.Text.Replace("\\n", "\n");

为了腾出足够的空间,您可以将 TabItemSize.Height 放大到 40 到 48 像素,当然也取决于 Font!:

To make enough room you enlarge the Tab's ItemSize.Height to say 40 to 48 pixels, of course depending also on the Font!:

tabControl1.ItemSize = new Size(tabControl1.ItemSize.Width,  42);

结果:

请注意,非活动的 TabPageTexts' 下降部分往往会在 Bottom 处被切断,除非您将高度设置得非常大.如果这是一个问题,我想您将不得不所有者绘制"TabControl.听起来比它更难..:

Note that the inactive TabPage's Texts' descenders tend to be cut off at the Bottom unless you make the height really large. If that's a problem, I guess you would have to 'owner-draw' the TabControl.Sounds harder than it is..:

将 Tabcontrol 设置为所有者绘制:

Set the Tabcontrol to owner draw:

tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;

并将这段代码添加到 DrawItem 事件中:

and add this piece of code to the DrawItem event:

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
    if ( e.Index == tabControl1.SelectedIndex)
        e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
    else
        e.Graphics.FillRectangle(SystemBrushes.Control, e.Bounds);

    TabPage tp = tabControl1.TabPages[e.Index];

    Point pt = new Point(e.Bounds.X + 2, e.Bounds.Y + 4);
    e.Graphics.DrawString(tp.Text, tabControl1.Font, SystemBrushes.ControlText, pt);
    e.DrawFocusRectangle();
}

这是一个真正的节省空间;高度降至 34 像素,结果如下:

This is a real space saver; the height is down to 34 pixels resulting in this:

一件好事是,现在您也可以在选项卡中显示活动页面及其背景色,也​​就是说,如果您的页面有不同的背景色..

One of the nice things is that now you could show the the active page with its backcolor in the tab, too, that is if you have different backcolors for your pages..

但是请注意,由于系统不知道您将在 DrawItem 代码中做什么,它将为整个未拆分的文本保留足够的空间,从而导致选项卡太宽.如果需要,您可以通过在每个页面的 Tag 中存储真正的 Text 并使 Text 只要最长线路所需:

Note however, that since the System doesn't know what you will do in the DrawItem code, it will reserve enough room for the whole, unsplit text, resulting in too broad tabs. You can fix this, if you want to, by storing the real Text in the Tag of each page and making the Text only as long as needed for the longest line:

if (tp.Tag == null)
{
   tp.Tag = tp.Text;
   tp.Text = "_____________";  // make large enough!!
}
string s = tp.Tag.ToString();

并在DrawString调用中使用string s

如果你想让它完美,你可以编写一个函数来测量每个 TabPage Text 和它的换行位置,以找出必要的宽度..

If you want to make it perfect you can write a function to measure each TabPage Text and its line break position to find out the necessary width..

这篇关于两行标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆