具有多种字体的矩形内的 DrawString 属性c#
我正在使用 C# WinForms 和 GDI+ 来做一些我希望不会有太大问题的事情,但是...
我基本上试图在一个矩形内绘制一个字符串,该矩形突出显示了字符串中的部分。当在一行上打印时,这一切都很好,但是当我尝试将文本换行到矩形内的下一行时,我遇到了问题。
使用的算法如下: -
将字符串拆分为高亮和不高亮的集合。
Do
If Highlightedtext Then
DrawString(HighLightedText);
Move X position forward to next character space
Else
DrawString(NormalText);
Move X position forward to next character space
End If
Loop
我会把代码放进去,但它又乱又长(我正在维护它)。它会打印出 find 文本是否是一个突出显示的字符串,因为如果文本太长,它会将其包裹在矩形的边界内,而不会出现问题。如果它是多重突出显示并且字符串比矩形大,它会写在它的外面...这是因为“向前移动X位置...”只是移动有问题的矩形!
我想基本上移动原始矩形内打印文本的点,并在需要换行时将其打印在下一行。有人可以帮忙吗?真是痛苦啊!
I'm using C# WinForms and GDI+ to do something I hoped wouldn't be too much problem but...
I'm basically trying to draw a string within a rectangle that has highlighted sections within the string. This all works fine when printing on one line, but I have issues when trying to wrap the text onto the next line within the rectangle.
The algorithm used is as follows: -
Split strings into a collection of highlight and not highlight.
Do
If Highlightedtext Then
DrawString(HighLightedText);
Move X position forward to next character space
Else
DrawString(NormalText);
Move X position forward to next character space
End If
Loop
I would put the code in, but it's messy and long (i'm maintaining it). It'll print out find if the text is one string of either highlighting or not, as it'll wrap it within the bounds of the rectangle without issue if it's too long. If it's multiple highlighting and the string is bigger than the rectangle, it'll write outside of it... this is because the "move X position forward..." just moves the rectangle on which is a problem!
I want to essentially move the point the text is printed within the original rectangle and print it on the next line if wrapping is required. Can anyone assist with this? It's a real pain!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经设法通过让我的函数一次处理一个字符来解决这个问题。
为此,我创建了一个函数来获取布尔值数组(即字符串本身的长度),该数组已将所有突出显示的字符设置为 true。
(FindMatchingTerms() 是一个函数,它将在字符串中查找并将找到的匹配项返回到集合中)。
然后我循环这个数组将其绘制到屏幕上,但跟踪我的矩形边框宽度。当它缩小到相关尺寸时,我将绘制位置重置回起始位置,然后将起始 Y 位置向下移动一点。
希望其他人会发现这很有用:)我在那里有一些 spaceCharacter 和 hypenCharacter 的常量,这些常量应该是不言自明的!有自定义函数来绘制字符串,但它仍然应该有意义,希望它对其他人有帮助。
I've managed to sort this by having to make my function do one character at a time.
To do this, I made a function to get an array (which is the length of the string itself) of boolean values which have set any highlighted characters to true.
(FindMatchingTerms() is a function that will look in the string and return the matches found into a collection).
I then loop this array to draw it out to screen but keeping track of my rectangle border width. When it reduces to the relevant size, I reset the position of drawing back to the start and then move the starting Y position down a bit.
Hopefully someone else will find this useful :) I've got some constants in there for spaceCharacter and hypenCharacter which should be self explanatory! There are custom functions to draw the string, but it should make sense nonetheless, hope it helps anyone else.