我知道使用unicode值可以將square運算符添加到標籤中(How can I show a superscript character in .NET GUI labels?)。有沒有辦法給標籤添加任何電源?我的應用程序需要顯示多項式函數,即,x^7個+ X^6個如何在c#winforms中添加上標電源運算符
謝謝, 邁克
我知道使用unicode值可以將square運算符添加到標籤中(How can I show a superscript character in .NET GUI labels?)。有沒有辦法給標籤添加任何電源?我的應用程序需要顯示多項式函數,即,x^7個+ X^6個如何在c#winforms中添加上標電源運算符
謝謝, 邁克
可以使用(大)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);
}
}
用例:
// 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
屬性來更改大小和字體。
謝謝,這真的很酷! – mikeythemissile 2013-02-23 16:37:03
你也可以使用原生支持UTF串的力量,這樣做並擴展方法轉換整數(甚至的uint)爲字符串,如:
public static class SomeClass {
private static readonly string superscripts = @"⁰¹²³⁴⁵⁶⁷⁸⁹";
public static string ToSuperscriptNumber(this int @this) {
var sb = new StringBuilder();
Stack<byte> digits = new Stack<byte>();
do {
var digit = (byte)(@this % 10);
digits.Push(digit);
@this /= 10;
} while (@this != 0);
while (digits.Count > 0) {
var digit = digits.Pop();
sb.Append(superscripts[digit]);
}
return sb.ToString();
}
}
,然後使用該擴展方法在某種程度上像這樣的:
public class Etc {
private Label someWinFormsLabel;
public void Foo(int n, int m) {
// we want to write the equation x + x^N + x^M = 0
// where N and M are variables
this.someWinFormsLabel.Text = string.Format(
"x + x{0} + x{1} = 0",
n.ToSuperscriptNumber(),
m.ToSuperscriptNumber()
);
}
// the result of calling Foo(34, 2798) would be the label becoming: x + x³⁴+ x²⁷⁹⁸ = 0
}
依照該思路,並與幾個額外的調整,(如掛鉤到一個文本框的TextChange和諸如此類的事件處理程序),你甚至可以允許用戶編輯這樣的「標補償可用「字符串(通過在用戶界面上的其他按鈕上打開和關閉」上標模式「)。
感謝上標文本列表。複製粘貼爲我工作! – trailmax 2013-07-17 11:13:41
您可以將unicode轉換爲上標,下標和任何其他符號的字符串並添加到字符串中。 例如:如果你想要10^6,你可以在C#或其他代碼中編寫代碼如下:
unicode for power 6 is U + 2076 and power 7 is U + 2077,所以你可以寫x^6 + x^7 as
label1.Text =「X」+(char)0X2076 +「X」+(char)0x2077;
您是否需要將文本編輯或不編輯? – digEmAll 2013-02-23 16:17:52
@digEmAll不,不需要更改 – mikeythemissile 2013-02-23 16:22:16
好吧,那麼你可以檢查我的答案中的解決方案是否符合你的需求;) – digEmAll 2013-02-23 16:30:51