如何在C#WinForms中添加上标幂运算符 [英] How to add superscript power operators in c# winforms

查看:136
本文介绍了如何在C#WinForms中添加上标幂运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know it's possible to add the square operator to a label using its unicode value.(How can I show a superscript character in .NET GUI labels?). Is there a way to add any power to a label? My application needs to display polynomial functions, i.e. x^7 + x^6 etc.

谢谢,
Mike

Thanks, Mike

推荐答案

您可以使用(great) HtmlRenderer

You can use the (great) HtmlRenderer and build you own label control supporting 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);
    }
}

用法示例:

// 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>";

结果:

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

Note that this code wraps your html into a div section that sets the font family and size to the one used by the control. So you can change the size and font by changing the Font property of the label.

这篇关于如何在C#WinForms中添加上标幂运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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