使抽绳部分粗体 [英] Make drawstring partially bold

查看:142
本文介绍了使抽绳部分粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好。

我已经创建了一个自定义的treeview控件,它可以在屏幕上绘制一个正常的树形视图,以及其他各种东西。在绘画事件期间,我使用以下方法将node.text字符串绘制到treeview控件。

  node.text =ABCDEFG 
g.DrawString(node.Text,tvFont,Brushes.Black,strPosX + 10,bothPosY)

基本上我想要做的是使node.text斜体的一部分,以便它像这样在屏幕上呈现...

  AB * CDE * FG 

就我个人而言,它有效地绘制三个不同的字符串...两个非斜体和一个斜体,但是这需要额外的定位。



我希望能够有效地concatinate字体格式化的字符串,然后将其作为第一个参数放在drawstring中,这实际上可能吗?如果是这样,我会怎么做,并/或有任何资源可以帮助我。



我使用的是winforms和visualbasic .net framework 3.5(不幸的是,可以改变,因为它是一个工作项目,这是该软件设计的环境)

解决方案


我希望能够有效地合并字体字符串,然后将其作为第一个参数放在drawstring中,这实际上是可能的吗?

不,这是不可能的。 DrawString 函数将使用您指定的格式标记来绘制所有指定的文本。这是一个全有或无关的事情。



如果您想将某些部分设为粗体而其他部分不是,那么您需要对 DrawString 函数。有一些额外的定位涉及到完成,但它不应该太困难。只有x坐标正在改变; y坐标应该保持不变。除非你做了像 font face 这样的幻想,然后你的基线会变得混乱你必须补偿这一点,以防止结果看起来像一个丑陋的不匹配





除此之外,允许我建议您使用 TextRenderer.DrawText 函数而不是 Graphics.DrawString 函数。前者的优点在于它使用GDI绘制文本,这与内置的本地控件绘制文本的方式相同。这种一致性有许多优点,其中最重要的一点是令人愉悦和谐的视觉效果。



如果您使用的是 TextRenderer.DrawText ,您还需要确保将其合并与 TextRenderer.MeasureText ,因为测量结果与返回的结果不同 Graphics.MeasureString ,它使用GDI +进行绘制。



有关 TextRenderer.DrawText Graphics.DrawString 可在这个问题 Graphics.DrawString 应该保留用于打印,并且 TextRenderer.DrawText 应该用于所有屏幕元素。 / p>

Good morning.

I have created a custom treeview control that paints a normal treeview onto the screen, plus various other things. During the paint event I draw the node.text string to the treeview control using the following method.

node.text = "ABCDEFG"
g.DrawString(node.Text, tvFont, Brushes.Black, strPosX + 10, bothPosY)

Basically what I am trying to do is make part of the node.text italic, so that it renders on screen like so...

AB*CDE*FG

Personally I would approach this in such a way that it effectively draws three different strings... two non-italic and one italic, however this would require additional positioning.

I am hoping that it is possible to effectively concatinate font formated strings then place that as the first parameter in drawstring, is this actually possible? If so how would I go about doing it and/or are there any resources that would help me.

I am using winforms, and visualbasic .net framework 3.5 (unfortuantly non of the aforementioned can be changed as it is a work project and this is the environment the software is designed for)

解决方案

I am hoping that it is possible to effectively concatinate font formated strings then place that as the first parameter in drawstring, is this actually possible?

No, this is not possible. The DrawString function is going to draw all of the text you specify using the formatting flags that you specify. It's an all-or-nothing affair.

If you want to make some portions bold and other portions not, then you will need to make multiple calls to the DrawString function. There is some additional positioning involved to accomplish that, but it shouldn't be too difficult. Only the x-coordinate is changing; the y-coordinate should stay the same. Unless you do something fancy like change font faces, then your baselines will get messed up and you will have to compensate for that to prevent the result from looking like an ugly mismatch.


Aside from that, allow me to recommend that you use the TextRenderer.DrawText function instead of the Graphics.DrawString function. The advantage of the former is that it draws text using GDI, which is the same way that the built-in native controls draw their text. There are a number of advantages to this consistency, not the least of which is a pleasing and harmonious visual appearance.

And if you're using TextRenderer.DrawText, you'll also want to make sure that you combine it with TextRenderer.MeasureText, because the measurements will be different from those returned by Graphics.MeasureString, which draws using GDI+.

More information on the difference between TextRenderer.DrawText and Graphics.DrawString is available in the answers to this question. Graphics.DrawString should be reserved for printing, and TextRenderer.DrawText should be used for all on-screen elements.

这篇关于使抽绳部分粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆