Winforms 不同字体大小的标签
基本上,我想实现以下目标:Winforms 不同字体大小的标签,winforms,label,Winforms,Label,基本上,我想实现以下目标: 但是我不知道怎么做,我试着用两个标签来组合它们,但结果不是很好。创建一个从标签继承的新类,并重写void OnPaint(PaintEventArgs e)方法来更改默认的渲染行为: public class MyLabel : Label { protected override void OnPaint(PaintEventArgs e) { e.Graphics.DrawString("A", Font, new SolidBr
但是我不知道怎么做,我试着用两个标签来组合它们,但结果不是很好。创建一个从标签继承的新类,并重写void OnPaint(PaintEventArgs e)方法来更改默认的渲染行为:
public class MyLabel : Label
{
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawString("A", Font, new SolidBrush(ForeColor), 10, 10);
e.Graphics.DrawString("B", new Font(Font.FontFamily, 20), new SolidBrush(ForeColor), 50, 10);
}
}
因此,“B”将比“a”大两倍。您可以用同样的方法实现目标,但您必须计算子字符串(“145”、“54”)的位置并绘制它们。您需要从不同的角度绘制文本,即基线:
公共类MyLabel
继承标签
_
公共重写属性自动调整为布尔值
收到
返回错误
结束
设置(值为布尔值)
'MyBase.AutoSize=value
端集
端属性
受保护的覆盖子OnPaint(如PaintEventArgs)
'MyBase.OnPaint(e)
Dim fromLine作为整数=Me.ClientSize.Height*0.75
尺寸g为图形=e.图形
Dim fontParts()作为字符串=Me.Text.Split(“.”)
使用bigFont作为新字体(Me.Font.FontFamily,20)
TextRenderer.DrawText(g、fontParts(0)、bigFont、_
新点(0,fromLine-GetBaseLine(bigFont,g))_
Me.ForeColor,Color.Empty)
如果fontParts.Length>1,则
Dim bigWidth As Integer=TextRenderer.MeasureText(g,fontParts(0),bigFont_
Point.Empty,TextFormatFlags.NopAdd.Width
使用smallFont作为新字体(Me.Font.FontFamily,8)
TextRenderer.DrawText(g,“.”和fontParts(1),smallFont_
新点(bigWidth+3,fromLine-GetBaseLine(smallFont,g))_
Me.ForeColor,Color.Empty)
终端使用
如果结束
终端使用
端接头
私有函数GetBaseLine(fromFont作为字体,g作为图形)作为单个
Dim fontHeight As Single=fromFont.GetHeight(g)
将线间距调整为单个=fromFont.fontfeamily.GetLineSpacing(fromFont.Style)
Dim cellAscent As Single=fromFont.FontFamily.GetCellAscent(fromFont.Style)
返回字体高度*单元格上升/行距
端函数
末级
该代码基本上是测量字体从一行到另一行的高度。在我的示例中,我使用标签的客户机空间的底部25%表示,从以下行开始绘制:Me.ClientSize.Height*0.75
对于使用的每种字体,必须测量该字体的基线,然后从绘图线中减去基线,以便偏移文本的绘图位置
由于锯齿和轮廓悬垂,测量单个角色的尺寸并不容易。我在大文本和小文本之间添加了一个小填充:bigWidth+3
,试图让它看起来很好。如果大数字以7结尾,则距离看起来有点偏离,因为7的杆是成角度的
结果:
使用devexpress
LabelControl。将whtmlstring
属性设置为true,并在LabelControl
的文本属性中使用受支持的
标记,如HTML文本格式文档中所述。您可以在windows窗体中使用用户控件WPF。要做到这一点,请执行此步骤。
1.将用户控件添加到windows窗体
2.从usercontrol名称网格(如t1)的xml
3.将此函数添加到usercontrol.wpf.cs
public void Actor(string text)
{
StringBuilder sb = new StringBuilder();
sb.Append(@"<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> ");
sb.Append(text);
sb.Append(@"</TextBlock>");
TextBlock myButton = (TextBlock)XamlReader.Parse(sb.ToString());
this.t1.Children.Clear();
t1.Children.Add(myButton);
}
public void Actor(字符串文本)
{
StringBuilder sb=新的StringBuilder();
某人加上(@“);
附加(正文);
某人加上(@“);
TextBlock myButton=(TextBlock)XamlReader.Parse(sb.ToString());
this.t1.Children.Clear();
t1.Children.Add(myButton);
}
四,。之后,从form1.css在您想要的每个地方添加这个函数
userControl11.Actor("<Run Text='Hi ' FontWeight='Bold'/><Run Text='Hitler ' FontWeight='Bold'/>");
userControl11.Actor(" < Run FontWeight = 'Bold' FontSize = '14' Text = 'This is WPF TextBlock Example. ' />");
userControl11.Actor(“”);
userControl11.Actor(“ ”;
您可以使用xml wpf来管理Actor函数的“编写代码”。字体和画笔是
IDisposable
的,应该使用(…){}块放入中。