如何获取RichTextBox中字符的char Rectangle(char bounds)? [英] How to get char Rectangle (char bounds) of a character in RichTextBox?

查看:117
本文介绍了如何获取RichTextBox中字符的char Rectangle(char bounds)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GetPositionFromCharIndex(int)方法只返回RichTextBox中指定索引处指定char的位置,在某些情况下,它还不够,我想得到指定char的bound或Rectangle(在RictTextBox中的指定索引)。 (从GetPositionFromCharIndex()获得的位置正好是我想要的矩形的位置)。这意味着如果我们知道该Rectangle的大小,就可以解决问题。



我发现很难找到那个尺寸结构的高度。 (宽度还可以)。请注意,高度不是角色的字体高度。如果你有一个字母A,其字体大小为14,字母B就在A之后,但字体大小为30.这两个字母的高度应该相同(但宽度可以不同,这意味着两者的大小)也是不同的。



在同一行搜索最大字母高度是一种方法,但我认为它真的很难而且效率不高。我正在寻找更好的解决方案。



请帮我解决这个问题。这有点紧急。谢谢!



VipHaLong

The method GetPositionFromCharIndex(int) returns only the position of a specified char at a specified index in RichTextBox, in some case, it''s not enough, I want to get the bound or Rectangle of a specified char (at a specified index) in RictTextBox. (the position obtained from GetPositionFromCharIndex() is exactly the Location of the Rectangle I want). That means the problem can be solved if we know the Size of that Rectangle.

I find difficult to find the Height of that Size structure. (the width is OK). Please notice that that Height is not the font height of the character. If you have a letter A with fontsize being 14 and a letter B right follows A but has a fontsize of 30. The Height here for both these letters should be the same (however the Width can be different, that means the Sizes of the both are also different).

Searching for the maximum Height of letters in the same line is an approach but I think it is really hard and not efficient. I''m finding another solution better.

Please help me solve this. It''s a little urgent. Thanks!

VipHaLong

推荐答案



你必须要为富文本控件创建图形,解析要解析的文本并获取该文本的大小。

Hi,
you will have to create the graphics for the rich text control, parse the text you wanted to parse and get the size of that text.
var font = new System.Drawing.Font(this.Font, FontStyle.Regular);
Graphics graphics = richTextBox1.CreateGraphics();
SizeF textSize = graphics.MeasureString("A", font);
float width = textSize.Width;





这将是你正在解析的文本的宽度,如果你解析一个单词,它将是单词的宽度,依此类推。



你必须记住这个单个Font参数考虑在内,如果文本是Font参数的混合,那么它应该添加在一个字体循环和相应的文本中。



问候

Jegan



This will be the width of the text you are parsing, if you parse a word, it would be the width of the word and so on.

You must bear in mind that this single Font parameters into account, if the text is mixture of Font parameters then it should be added in a loop of fonts and corresponding texts.

Regards
Jegan


这篇关于如何获取RichTextBox中字符的char Rectangle(char bounds)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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