早上好。
我创建了一个自定义树 View 控件,可以在屏幕上绘制一个普通的树 View ,以及其他各种东西。在绘制事件期间,我使用以下方法将 node.text 字符串绘制到树 View 控件。
node.text = "ABCDEFG"
g.DrawString(node.Text, tvFont, Brushes.Black, strPosX + 10, bothPosY)
基本上我想要做的是使 node.text 斜体的一部分,以便它像这样在屏幕上呈现......
AB*CDE*FG
就我个人而言,我会以这样一种方式来处理它,即它有效地绘制三个不同的字符串……两个非斜体和一个斜体,但这需要额外的定位。
我希望可以有效地连接字体格式的字符串,然后将其作为拉绳中的第一个参数,这真的可能吗?如果是这样,我将如何去做和/或是否有任何资源可以帮助我。
我正在使用 winforms 和 Visualbasic .net 框架 3.5(不幸的是,上述任何一项都不能更改,因为它是一个工作项目,这是该软件的设计环境)
最佳答案
怎么样:
private void Form1_Paint(object sender, PaintEventArgs e)
{
using (Font normal = new Font("Tahoma", 10, FontStyle.Regular))
using (Font bold = new Font("Tahoma", 10, FontStyle.Bold))
using (StringFormat format =
(StringFormat)StringFormat.GenericTypographic.Clone())
{
format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
Rectangle temp = ClientRectangle;
DrawString(e.Graphics, SystemBrushes.WindowText, ref temp, format, "AB", normal);
DrawString(e.Graphics, SystemBrushes.WindowText, ref temp, format, "CDE ", bold);
DrawString(e.Graphics, SystemBrushes.WindowText, ref temp, format, "FG", normal);
}
}
void DrawString(Graphics g, Brush brush, ref Rectangle rect, StringFormat format, string text, Font font)
{
using (StringFormat copy = (StringFormat)format.Clone())
{
copy.SetMeasurableCharacterRanges(new CharacterRange[] {
new CharacterRange(0, text.Length)});
Region[] regions = g.MeasureCharacterRanges(text, font, rect, copy);
g.DrawString(text, font, brush, rect, format);
int width = (int)(regions[0].GetBounds(g).Width);
rect.X += width;
rect.Width -= width;
}
}
获取自 here .
关于vb.net - 使抽绳部分加粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11664871/