如何在c#winforms中添加上标幂运算符

mik*_*ile 4 c# winforms

我知道可以使用unicode值将square运算符添加到标签中.(如何在.NET GUI标签中显示上标字符?).有没有办法为标签添加任何力量?我的应用程序需要显示多项式函数,即x ^ 7 + x ^ 6等.

谢谢,迈克

dig*_*All 9

您可以使用(伟大的)HtmlRenderer并构建自己的支持html的标签控件.

这是一个例子:

public class HtmlPoweredLabel : Control
{
    protected override void OnPaint(PaintEventArgs e)
    {
        string html = string.Format(System.Globalization.CultureInfo.InvariantCulture,
        "<div style=\"font-family:{0}; font-size:{1}pt;\">{2}</div>",
        this.Font.FontFamily.Name,
        this.Font.SizeInPoints,
        this.Text);

        var topLeftCorner = new System.Drawing.PointF(0, 0);
        var size = this.Size;

        HtmlRenderer.HtmlRender.Render(e.Graphics, html, topLeftCorner, size);

        base.OnPaint(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

// add an HtmlPoweredLabel to you form using designer or programmatically,
// then set the text in this way:
this.htmlPoweredLabel.Text = "y = x<sup>7</sup> + x<sup>6</sup>";
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

请注意,此代码将您的html包装到div部分,该部分将字体系列和大小设置为控件使用的字体系列和大小.因此,您可以通过更改Font标签的属性来更改大小和字体.