首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#标签的副文本

C#标签的副文本
EN

Stack Overflow用户
提问于 2009-10-15 11:54:50
回答 4查看 7.1K关注 0票数 2

我需要在Winforms的C# labels .Text属性中添加一些潜文本。有什么简单的方法可以不用自己写控件就做到这一点吗?

下面是HTML中的示例

代码语言:javascript
复制
<sub>1a</sub>

谢谢,保罗。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-10-15 12:41:08

下面是一个已经编写的控件的链接,它就是这样做的:

http://www.freshnova.com/C-Tutorials/superscript-subscript-label.html

更新:您也可以只使用RichTextBox (使用BorderStyle none、BackColor控件和ReadOnly true)。RichTextBox的SelectionCharOffset允许您以像素为单位指定基线上方或下方的文本高度,然后在每次设置SelectionCharOffset后设置其SelectedText属性,以便在一个框中混合和匹配不同位置的文本。

票数 6
EN

Stack Overflow用户

发布于 2009-10-15 11:58:42

不,你必须编写你自己的控件。

只需在项目中创建一个新的用户控件,并向其添加两个标签即可。然后添加一些用于获取/设置标签值的服务器端属性。

然后只需将用户控件添加到页面。

票数 3
EN

Stack Overflow用户

发布于 2009-10-16 11:48:46

如果有人担心,我写了我自己的.

代码语言:javascript
复制
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

namespace XXX.UI.Custom
{
/// <summary>
/// A label which is capable of subscript.
/// 
/// Version: 1.
/// Author: XXX.
/// Date: 15/10/2009.
/// Changes: Initial version.
/// </summary>
public class SubscriptLabel : Label
{
    #region Vars

    // Vars.
    private char _subMark = '`';
    private SolidBrush _brush = new SolidBrush(Color.Black);
    private StringFormat _stringFormat = new StringFormat(StringFormat.GenericTypographic);

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the subscript marker.
    /// </summary>
    /// <value>The subscript marker.</value>
    [Description("Marker for start/end of subscript text."),
    Category("Appearance"),
    Browsable(true)]
    public char SubscriptMarker
    {
        get
        {
            return _subMark;
        }
        set
        {
            _subMark = value;
            Invalidate();
        }
    }

    #endregion

    #region Methods

    #region Public

    /// <summary>
    /// Initializes a new instance of the <see cref="SubscriptLabel"/> class.
    /// </summary>
    public SubscriptLabel()
    {
        // Setup text mode.
        _stringFormat.Alignment = StringAlignment.Near;
        _stringFormat.HotkeyPrefix = HotkeyPrefix.Show;
        _stringFormat.LineAlignment = StringAlignment.Near;
        _stringFormat.Trimming = StringTrimming.EllipsisCharacter;
        _stringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.DisplayFormatControl
            | StringFormatFlags.NoClip | StringFormatFlags.NoFontFallback
            | StringFormatFlags.NoWrap;
    }

    #endregion

    #region Private

    /// <summary>
    /// Measures the Y DSW.
    /// </summary>
    /// <param name="graphics">The graphics.</param>
    /// <param name="text">The text.</param>
    /// <param name="font">The font.</param>
    /// <returns>The size.</returns>
    private SizeF MeasureDSW(Graphics graphics, string text,
        Font font)
    {
        // Init.
        graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

        // Vars.
        StringFormat stringFormat = new StringFormat(StringFormat.GenericTypographic);
        SizeF size = new SizeF();

        // Init.
        stringFormat.Alignment = StringAlignment.Near;
        stringFormat.HotkeyPrefix = HotkeyPrefix.Show;
        stringFormat.LineAlignment = StringAlignment.Near;
        stringFormat.Trimming = StringTrimming.None;
        stringFormat.HotkeyPrefix = HotkeyPrefix.None;
        stringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoClip
            | StringFormatFlags.NoWrap;

        // The string size.
        size = graphics.MeasureString(text, Font,
            Width, stringFormat);

        // Init.
        graphics.TextRenderingHint = TextRenderingHint.SystemDefault;

        return size;
    }

    /// <summary>
    /// The pain method.
    /// </summary>
    /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"/> that contains the event data.</param>
    protected override void OnPaint(PaintEventArgs e)
    {
        // Ensure that we have some text to draw.
        if (!string.IsNullOrEmpty(Text))
        {
            // Init.
            float currentX = 0f;
            float currentY = 0f;
            string[] splittedText = Text.Split(SubscriptMarker);

            // Setup graphics.
            e.Graphics.CompositingQuality = CompositingQuality.Default;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.SmoothingMode = SmoothingMode.Default;
            e.Graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
            e.Graphics.CompositingMode = CompositingMode.SourceOver;

            // Loop around the splitted text.
            for (int i = 0; i < splittedText.Length; i++)
            {
                // Vars.
                int drawSubscript = i % 2;

                // Are we to draw the subscript?
                if (drawSubscript > 0)
                {
                    DrawText(e.Graphics, ref currentX,
                        ref currentY, splittedText,
                        i, true);
                }
                else // Don't draw the subscript?
                {
                    DrawText(e.Graphics, ref currentX,
                        ref currentY, splittedText,
                        i, false);
                }
            }
        }
    }

    /// <summary>
    /// Draws the text onto the control.
    /// </summary>
    /// <param name="graphics">The graphics.</param>
    /// <param name="currentX">The current X.</param>
    /// <param name="currentY">The current Y.</param>
    /// <param name="splittedText">The splitted text.</param>
    /// <param name="i">The current text array position.</param>
    /// <param name="isSubScript">if set to <c>true</c> [is sub script].</param>
    private void DrawText(Graphics graphics, ref float currentX,
        ref float currentY, string[] splittedText,
        int i, bool isSubScript)
    {
        // Vars.
        string[] words = splittedText[i].Split(' ');

        // Loop around all the words.
        foreach (string word in words)
        {
            // Vars.
            string newWord = word + " ";
            float nextPosWord = MeasureDSW(graphics, newWord,
                Font).Width;

            // Are we on the final element?
            if (word == words[words.Length-1] && !isSubScript)
            {
                // Remove the space.
                newWord = newWord.Trim();

                // Re-measure to remove the space.
                nextPosWord = MeasureDSW(graphics, newWord,
                    Font).Width;
            }

            // Are we over the end of the label?
            if ((currentX + nextPosWord) > Width)
            {
                // Add the Y coords.
                currentY += MeasureDSW(graphics, newWord,
                    Font).Height;

                // Reset the X coords.
                currentX = 0;
            }

            // Vars.
            float newCurrentY = currentY;

            // Is this for the sub script characters?
            if (isSubScript)
            {
                // Add offsets.
                newCurrentY += 5 * graphics.DpiY / 96; ;
            }

            // Draw onto the control.
            graphics.DrawString(newWord, Font,
                _brush, currentX, 
                newCurrentY);

            // Add the size onto the X coords.
            currentX += nextPosWord;
        }
    }

    #endregion

    #endregion
}
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1571911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档