有没有办法让a的一部分label.text变得大胆?
label.text = "asd" + string;
希望该string部分是大胆的.
有可能,这怎么办?
par*_*cle 27
下面的类演示了如何通过重写做OnPaint()在Label类的WinForms的.你可以改进它.但我所做的是|在字符串中使用管道字符()来告诉OnPaint()方法在|粗体之前和正常文本之后打印文本.
class LabelX : Label
{
    protected override void OnPaint(PaintEventArgs e) {
        Point drawPoint = new Point(0, 0);
        string[] ary = Text.Split(new char[] { '|' });
        if (ary.Length == 2) {
            Font normalFont = this.Font;
            Font boldFont = new Font(normalFont, FontStyle.Bold);
            Size boldSize = TextRenderer.MeasureText(ary[0], boldFont);
            Size normalSize = TextRenderer.MeasureText(ary[1], normalFont);
            Rectangle boldRect = new Rectangle(drawPoint, boldSize);
            Rectangle normalRect = new Rectangle(
                boldRect.Right, boldRect.Top, normalSize.Width, normalSize.Height);
            TextRenderer.DrawText(e.Graphics, ary[0], boldFont, boldRect, ForeColor);
            TextRenderer.DrawText(e.Graphics, ary[1], normalFont, normalRect, ForeColor);
        }
        else {
            TextRenderer.DrawText(e.Graphics, Text, Font, drawPoint, ForeColor);                
        }
    }
}
以下是如何使用它:
LabelX x = new LabelX();
Controls.Add(x);
x.Dock = DockStyle.Top;
x.Text = "Hello | World";       
您好将以粗体和世界正常打印.
这是对Simon建议用readonly RichTextBox控件替换Label控件的一个详细说明.
将Label控件替换为RichTextBox控件,位置和大小相同.在以下注释中,控件的名称是rtbResults.
让它只读: rtbResults.ReadOnly = True;
没有国界: rtbResults.BorderStyle = BorderStyle.None;
而不是分配要显示的字符串,Label.Text您将其分配给RichTextBox.Rtf,并应用一些简单的RTF格式.
以下代码是一个示例 - 它显示由Scrabble骗子程序生成的单词,其中高值字母以粗体显示.
  /// <summary>
  /// Method to display the results in the RichTextBox, prefixed with "Results: " and with the 
  /// letters J, Q, X and Z in bold type.
  /// </summary>
  private void DisplayResults(string resultString)
  {
     resultString = MakeSubStringBold(resultString, "J");
     resultString = MakeSubStringBold(resultString, "Q");
     resultString = MakeSubStringBold(resultString, "X");
     resultString = MakeSubStringBold(resultString, "Z");
     rtbResults.Rtf = @"{\rtf1\ansi " + "Results: " + resultString + "}";
  }
  /// <summary>
  /// Method to apply RTF-style formatting to make all occurrences of a substring in a string 
  /// bold. 
  /// </summary>
  private static string MakeSubStringBold(string theString, string subString)
  {
     return theString.Replace(subString, @"\b " + subString + @"\b0 ");
  }
| 归档时间: | 
 | 
| 查看次数: | 68370 次 | 
| 最近记录: |