我需要在Winforms C#labels .Text属性中加入一些子文本。如果没有自己的控制,有没有简单的方法呢?
这将是HTML
中的示例<sub>1a</sub>
谢谢, 保罗。
答案 0 :(得分:6)
这是一个指向已完成的控件的链接:
http://www.freshnova.com/C-Tutorials/superscript-subscript-label.html
更新:您也可以使用RichTextBox(BorderStyle none,BackColor控件和ReadOnly为true)。 RichTextBox的SelectionCharOffset
允许您指定基线上方或下方的文本高度(以像素为单位),然后在每个SelectedText
设置后设置其SelectionCharOffset
属性,以混合匹配不同的文本在一个方框中的位置。
答案 1 :(得分:3)
不,你必须自己编写控件。
只需在项目中创建一个新的用户控件,并为其添加两个标签。然后添加一些服务器端属性以获取/设置标签值。
然后只需将用户控件添加到页面中。
答案 2 :(得分:1)
如果有人感到困扰,我最终写了自己的......
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
}
}
答案 3 :(得分:0)
有人在此post中提到了使用“字符图”工具。如果标签带有静态文本,则最快。
转到“开始”->“程序”->“附件”->“系统工具”->“角色映射”。 现在,从列出的字符中选择该字符²。点击“选择”, 单击“复制”,然后尝试通过粘贴更改任何控件的文本 这个角色。应该可以。
Button1.Text = "mm²"