天天看点

winform label显示html,Formatting text in WinForm Label

AutoRichLabel

winform label显示html,Formatting text in WinForm Label

I was solving this problem by building an UserControl that contains a TransparentRichTextBox that is readonly. The TransparentRichTextBox is a RichTextBox that allows to be transparent:

TransparentRichTextBox.cs:

public class TransparentRichTextBox : RichTextBox

{

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]

static extern IntPtr LoadLibrary(string lpFileName);

protected override CreateParams CreateParams

{

get

{

CreateParams prams = base.CreateParams;

if (TransparentRichTextBox.LoadLibrary("msftedit.dll") != IntPtr.Zero)

{

prams.ExStyle |= 0x020; // transparent

prams.ClassName = "RICHEDIT50W";

}

return prams;

}

}

}

The final UserControl acts as wrapper of the TransparentRichTextBox. Unfortunately, I had to limit it to AutoSize on my own way, because the AutoSize of the RichTextBox became broken.

AutoRichLabel.designer.cs:

partial class AutoRichLabel

{

///

/// Required designer variable.

///

private System.ComponentModel.IContainer components = null;

///

/// Clean up any resources being used.

///

/// true if managed resources should be disposed; otherwise, false.

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Component Designer generated code

///

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

///

private void InitializeComponent()

{

this.rtb = new TransparentRichTextBox();

this.SuspendLayout();

//

// rtb

//

this.rtb.BorderStyle = System.Windows.Forms.BorderStyle.None;

this.rtb.Dock = System.Windows.Forms.DockStyle.Fill;

this.rtb.Location = new System.Drawing.Point(0, 0);

this.rtb.Margin = new System.Windows.Forms.Padding(0);

this.rtb.Name = "rtb";

this.rtb.ReadOnly = true;

this.rtb.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None;

this.rtb.Size = new System.Drawing.Size(46, 30);

this.rtb.TabIndex = 0;

this.rtb.Text = "";

this.rtb.WordWrap = false;

this.rtb.ContentsResized += new System.Windows.Forms.ContentsResizedEventHandler(this.rtb_ContentsResized);

//

// AutoRichLabel

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;

this.BackColor = System.Drawing.Color.Transparent;

this.Controls.Add(this.rtb);

this.Name = "AutoRichLabel";

this.Size = new System.Drawing.Size(46, 30);

this.ResumeLayout(false);

}

#endregion

private TransparentRichTextBox rtb;

}

AutoRichLabel.cs:

///

/// An auto sized label with the ability to display text with formattings by using the Rich Text Format.

/// ­

/// Short RTF syntax examples:

/// ­

/// Paragraph:

/// {\pard This is a paragraph!\par}

/// ­

/// Bold / Italic / Underline:

/// \b bold text\b0

/// \i italic text\i0

/// \ul underline text\ul0

/// ­

/// Alternate color using color table:

/// {\colortbl ;\red0\green77\blue187;}{\pard The word \cf1 fish\cf0 is blue.\par

/// ­

/// Additional information:

/// Always wrap every text in a paragraph.

/// Different tags can be stacked (i.e. \pard\b\i Bold and Italic\i0\b0\par)

/// The space behind a tag is ignored. So if you need a space behind it, insert two spaces (i.e. \pard The word \bBOLD\0 is bold.\par)

/// Full specification: http://www.biblioscape.com/rtf15_spec.htm

///

public partial class AutoRichLabel : UserControl

{

///

/// The rich text content.

/// ­

/// Short RTF syntax examples:

/// ­

/// Paragraph:

/// {\pard This is a paragraph!\par}

/// ­

/// Bold / Italic / Underline:

/// \b bold text\b0

/// \i italic text\i0

/// \ul underline text\ul0

/// ­

/// Alternate color using color table:

/// {\colortbl ;\red0\green77\blue187;}{\pard The word \cf1 fish\cf0 is blue.\par

/// ­

/// Additional information:

/// Always wrap every text in a paragraph.

/// Different tags can be stacked (i.e. \pard\b\i Bold and Italic\i0\b0\par)

/// The space behind a tag is ignored. So if you need a space behind it, insert two spaces (i.e. \pard The word \bBOLD\0 is bold.\par)

/// Full specification: http://www.biblioscape.com/rtf15_spec.htm

///

[Browsable(true)]

public string RtfContent

{

get

{

return this.rtb.Rtf;

}

set

{

this.rtb.WordWrap = false; // to prevent any display bugs, word wrap must be off while changing the rich text content.

this.rtb.Rtf = value.StartsWith(@"{\rtf1") ? value : @"{\rtf1" + value + "}"; // Setting the rich text content will trigger the ContentsResized event.

this.Fit(); // Override width and height.

this.rtb.WordWrap = this.WordWrap; // Set the word wrap back.

}

}

///

/// Dynamic width of the control.

///

[Browsable(false)]

public new int Width

{

get

{

return base.Width;

}

}

///

/// Dynamic height of the control.

///

[Browsable(false)]

public new int Height

{

get

{

return base.Height;

}

}

///

/// The measured width based on the content.

///

public int DesiredWidth { get; private set; }

///

/// The measured height based on the content.

///

public int DesiredHeight { get; private set; }

///

/// Determines the text will be word wrapped. This is true, when the maximum size has been set.

///

public bool WordWrap { get; private set; }

///

/// Constructor.

///

public AutoRichLabel()

{

InitializeComponent();

}

///

/// Overrides the width and height with the measured width and height

///

public void Fit()

{

base.Width = this.DesiredWidth;

base.Height = this.DesiredHeight;

}

///

/// Will be called when the rich text content of the control changes.

///

private void rtb_ContentsResized(object sender, ContentsResizedEventArgs e)

{

this.AutoSize = false; // Disable auto size, else it will break everything

this.WordWrap = this.MaximumSize.Width > 0; // Enable word wrap when the maximum width has been set.

this.DesiredWidth = this.rtb.WordWrap ? this.MaximumSize.Width : e.NewRectangle.Width; // Measure width.

this.DesiredHeight = this.MaximumSize.Height > 0 && this.MaximumSize.Height < e.NewRectangle.Height ? this.MaximumSize.Height : e.NewRectangle.Height; // Measure height.

this.Fit(); // Override width and height.

}

}

The syntax of the rich text format is quite simple:

Paragraph:

{\pard This is a paragraph!\par}

Bold / Italic / Underline text:

\b bold text\b0

\i italic text\i0

\ul underline text\ul0

Alternate color using color table:

{\colortbl ;\red0\green77\blue187;}

{\pard The word \cf1 fish\cf0 is blue.\par

But please note: Always wrap every text in a paragraph. Also, different tags can be stacked (i.e. \pard\b\i Bold and Italic\i0\b0\par) and the space character behind a tag is ignored. So if you need a space behind it, insert two spaces (i.e. \pard The word \bBOLD\0 is bold.\par). To escape \ or { or }, please use a leading \.

For more information there is a full specification of the rich text format online.

Using this quite simple syntax you can produce something like you can see in the first image. The rich text content that was attached to the RtfContent property of my AutoRichLabel in the first image was:

{\colortbl ;\red0\green77\blue187;}

{\pard\b BOLD\b0 \i ITALIC\i0 \ul UNDERLINE\ul0 \\\{\}\par}

{\pard\cf1\b BOLD\b0 \i ITALIC\i0 \ul UNDERLINE\ul0\cf0 \\\{\}\par}

winform label显示html,Formatting text in WinForm Label

If you want to enable word wrap, please set the maximum width to a desired size. However, this will fix the width to the maximum width, even when the text is shorter.

Have fun!