格式的文本在WinForm标签
-
08-06-2019 - |
题
是否有可能格式的某些文本在WinForm标签,而不是破坏文本为多个标签吗?请无视HTML tags在标签的文字;这只是用来获取我的观点。
例如:
Dim myLabel As New Label
myLabel.Text = "This is <b>bold</b> text. This is <i>italicized</i> text."
这将产生的文本的标签中:
这个是 大胆 的文本。这个是 斜体 的文本。
解决方案
这是不可能与它的标签,因为它是。标签必须有一个字体,完全一种尺寸和一个脸。你有几个选择:
- 使用单独的标签
- 创建一个新的控制来源的类做其自己的绘图通过GDI+和使用,而不是标签;这可能是你最好的选择,因为它给你的完全控制指令控制的格式文本
- 利用一个第三方签控制,这将让你插入HTML段(有一堆检演示);这将是其他人的执行情况#2.
其他提示
不是真的,但是你可以伪造它有一个只读是没有边界。是支持丰富的文本格式(rtf)。
另一种变通办法,迟到缔约方:如果你不想使用一个第三方控制,而你只是希望提请注意一些文字在你的标签, 和 你确定与所强调的,你可以使用 LinkLabel.
注意到许多考虑这个'性犯罪',但是如果你不是设计一些对终端用户消耗量,然后它可能是你在准备有关于你的良心。
诀窍是增加无障碍链接到这份文本,你想要强调,然后在全球设定链接的颜色匹配的其余部分的标签。你可以设置几乎所有必要属性,在设计时除了 Links.Add()
一块,但是在这里,他们在码:
linkLabel1.Text = "You are accessing a government system, and all activity " +
"will be logged. If you do not wish to continue, log out now.";
linkLabel1.AutoSize = false;
linkLabel1.Size = new Size(365, 50);
linkLabel1.TextAlign = ContentAlignment.MiddleCenter;
linkLabel1.Links.Clear();
linkLabel1.Links.Add(20, 17).Enabled = false; // "government system"
linkLabel1.Links.Add(105, 11).Enabled = false; // "log out now"
linkLabel1.LinkColor = linkLabel1.ForeColor;
linkLabel1.DisabledLinkColor = linkLabel1.ForeColor;
结果是:
工作方案对我来说-使用的定义RichEditBox.与权利的属性,它将被看作是简单的标签,与大胆的支持。
1) 首先,增加自定义RichTextLabel类残疾插入:
public class RichTextLabel : RichTextBox
{
public RichTextLabel()
{
base.ReadOnly = true;
base.BorderStyle = BorderStyle.None;
base.TabStop = false;
base.SetStyle(ControlStyles.Selectable, false);
base.SetStyle(ControlStyles.UserMouse, true);
base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
base.MouseEnter += delegate(object sender, EventArgs e)
{
this.Cursor = Cursors.Default;
};
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x204) return; // WM_RBUTTONDOWN
if (m.Msg == 0x205) return; // WM_RBUTTONUP
base.WndProc(ref m);
}
}
2) 分你一句话与性的标志,确定那个词应该是大胆或没有:
private void AutocompleteItemControl_Load(object sender, EventArgs e)
{
RichTextLabel rtl = new RichTextLabel();
rtl.Font = new Font("MS Reference Sans Serif", 15.57F);
StringBuilder sb = new StringBuilder();
sb.Append(@"{\rtf1\ansi ");
foreach (var wordPart in wordParts)
{
if (wordPart.IsSelected)
{
sb.Append(@"\b ");
}
sb.Append(ConvertString2RTF(wordPart.WordPart));
if (wordPart.IsSelected)
{
sb.Append(@"\b0 ");
}
}
sb.Append(@"}");
rtl.Rtf = sb.ToString();
rtl.Width = this.Width;
this.Controls.Add(rtl);
}
3) 添加功能把你的文本,以有效rtf(unicode支持!) :
private string ConvertString2RTF(string input)
{
//first take care of special RTF chars
StringBuilder backslashed = new StringBuilder(input);
backslashed.Replace(@"\", @"\\");
backslashed.Replace(@"{", @"\{");
backslashed.Replace(@"}", @"\}");
//then convert the string char by char
StringBuilder sb = new StringBuilder();
foreach (char character in backslashed.ToString())
{
if (character <= 0x7f)
sb.Append(character);
else
sb.Append("\\u" + Convert.ToUInt32(character) + "?");
}
return sb.ToString();
}
就像一个魅力我!解决方案,汇编自:
- 创建该文本作为RTF文件,在写字板
- 创建丰富的文字控制的无疆界和编辑=false
- 添加RTF文件的项目作为一个资源
在Form1_load做
myRtfControl.Rtf=Resource1.MyRtfControlText
AutoRichLabel
我解决这个问题通过建立一个 UserControl
包含一个 TransparentRichTextBox
这是只读。的 TransparentRichTextBox
是一个 RichTextBox
允许被透明:
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;
}
}
}
最终 UserControl
作为包装的 TransparentRichTextBox
.不幸的是,我不得不限制它 AutoSize
在我自己的方式,因为 AutoSize
的 RichTextBox
成为了打破。
AutoRichLabel.设计师。cs:
partial class AutoRichLabel
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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:
/// <summary>
/// <para>An auto sized label with the ability to display text with formattings by using the Rich Text Format.</para>
/// <para></para>
/// <para>Short RTF syntax examples: </para>
/// <para></para>
/// <para>Paragraph: </para>
/// <para>{\pard This is a paragraph!\par}</para>
/// <para></para>
/// <para>Bold / Italic / Underline: </para>
/// <para>\b bold text\b0</para>
/// <para>\i italic text\i0</para>
/// <para>\ul underline text\ul0</para>
/// <para></para>
/// <para>Alternate color using color table: </para>
/// <para>{\colortbl ;\red0\green77\blue187;}{\pard The word \cf1 fish\cf0 is blue.\par</para>
/// <para></para>
/// <para>Additional information: </para>
/// <para>Always wrap every text in a paragraph. </para>
/// <para>Different tags can be stacked (i.e. \pard\b\i Bold and Italic\i0\b0\par)</para>
/// <para>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)</para>
/// <para>Full specification: http://www.biblioscape.com/rtf15_spec.htm </para>
/// </summary>
public partial class AutoRichLabel : UserControl
{
/// <summary>
/// The rich text content.
/// <para></para>
/// <para>Short RTF syntax examples: </para>
/// <para></para>
/// <para>Paragraph: </para>
/// <para>{\pard This is a paragraph!\par}</para>
/// <para></para>
/// <para>Bold / Italic / Underline: </para>
/// <para>\b bold text\b0</para>
/// <para>\i italic text\i0</para>
/// <para>\ul underline text\ul0</para>
/// <para></para>
/// <para>Alternate color using color table: </para>
/// <para>{\colortbl ;\red0\green77\blue187;}{\pard The word \cf1 fish\cf0 is blue.\par</para>
/// <para></para>
/// <para>Additional information: </para>
/// <para>Always wrap every text in a paragraph. </para>
/// <para>Different tags can be stacked (i.e. \pard\b\i Bold and Italic\i0\b0\par)</para>
/// <para>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)</para>
/// <para>Full specification: http://www.biblioscape.com/rtf15_spec.htm </para>
/// </summary>
[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.
}
}
/// <summary>
/// Dynamic width of the control.
/// </summary>
[Browsable(false)]
public new int Width
{
get
{
return base.Width;
}
}
/// <summary>
/// Dynamic height of the control.
/// </summary>
[Browsable(false)]
public new int Height
{
get
{
return base.Height;
}
}
/// <summary>
/// The measured width based on the content.
/// </summary>
public int DesiredWidth { get; private set; }
/// <summary>
/// The measured height based on the content.
/// </summary>
public int DesiredHeight { get; private set; }
/// <summary>
/// Determines the text will be word wrapped. This is true, when the maximum size has been set.
/// </summary>
public bool WordWrap { get; private set; }
/// <summary>
/// Constructor.
/// </summary>
public AutoRichLabel()
{
InitializeComponent();
}
/// <summary>
/// Overrides the width and height with the measured width and height
/// </summary>
public void Fit()
{
base.Width = this.DesiredWidth;
base.Height = this.DesiredHeight;
}
/// <summary>
/// Will be called when the rich text content of the control changes.
/// </summary>
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.
}
}
该法的丰富的文本格式很简单:
段落:
{\pard This is a paragraph!\par}
大胆/斜体/下划线的案文:
\b bold text\b0
\i italic text\i0
\ul underline text\ul0
交替使用的颜色颜色表:
{\colortbl ;\red0\green77\blue187;}
{\pard The word \cf1 fish\cf0 is blue.\par
但请注意:总是包裹的每一个文本中的段落。此外,不同的标记可以叠(即 \pard\b\i Bold and Italic\i0\b0\par
)和空间字背后的一个标签是容忽视。所以如果你需要一个空间背后,插入两个空间(即 \pard The word \bBOLD\0 is bold.\par
).来逃脱 \
或 {
或 }
, 请用一个领先 \
.更多信息,有一个 完全说明的丰富文本格式在线.
使用这个相当简单的语法可以生产的东西喜欢你可以看到在第一图像。将丰富案文内容,这是附加的 RtfContent
酒店的我 AutoRichLabel
在第一图像是:
{\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}
如果你想要启用词包裹,请设置的最大宽度到期望的大小。然而,这将解决的宽度,最大宽度,即使案文更短。
玩得开心!
有一个很好的文章,从2009年在代码项目名为"一个专业HTML渲染你会用"它实现了类似的东西是什么原来的海报希望。
我使用它成功地在几个项目。
非常简单的解决办法:
- 加2上的标签形式,LabelA和LabelB
- 去属性LabelA和码头到左边。
- 去属性LabelB和码头到左。
- 设置的字体大胆LabelA.
现在LabelB将移取决于长度的案文LabelA.
这就是全部。
我还想找出如果这是可能的。
当我们无法找到一个解决方案,我们采取组件的SuperLabel'控制它允许HTML markup在一个标签。
意识到这是一个古老的问题,我的答案是,更多的人,像我,他仍然可以寻找这样的解决方案和偶然发现这个问题。
除了已经提到的,DevExpress的 LabelControl 是一个标签,支持这种行为- 这里的演示.唉,这是部分支付图书馆。
如果你想找免费的解决方案,我相信 HTML渲染 是一个最好的事情。
一FlowLayoutPanel工作以及对于您的问题。如果添加标签的流动小组和格式中的每一标签的字体和利润的性质,然后你可以有不同的字体样式。漂亮的快速和容易的解决方案得到工作。
是的。你可以实现,采用HTML呈现。你看,点击链接: https://htmlrenderer.codeplex.com/ 我希望这是有用的。