vb.net - 使抽绳部分加粗

标签 vb.net graphics treeviewitem drawstring

早上好。

我创建了一个自定义树 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/

相关文章:

javascript - HTML5 jQuery .animate()({scrollTop : }); not working as intended

mysql - VB.Net "mask"组合框文本作为另一个文本

java - 高端 2D Java (SE) 图形库

c# - 每次选中一项时如何触发Selected事件

.net - 将 Windows 服务类添加到类库

mysql - 使用 "IN"命令将数组作为参数传递给 SQL 查询

iphone - iPhone 应用程序中 2D 图形元素的编辑器

java - 如何在 JavaFX 中最有效地移动多个矩形

c# - TreeView 与 HierarchicalDataTemplate,有没有办法访问 TreeViewItem 事件,如 MouseDoubleClick 和 ItemSelected?

wpf - 扩展非选定项目的 TreeView 问题