如何在 Silverlight TextBlock 中命中测试单个字符

发布于 2024-09-19 08:13:21 字数 914 浏览 7 评论 0原文

这是一个有点抽象的问题,对此我表示歉意。我不一定要寻找特定的经过测试的解决方案,而是寻找一些我可以尝试的建议,以找到最有效的解决方案。欢迎所有建议 - 越多样化、越有创意越好。

问题是这样的:

我有一个 Silverlight TextBlock,其中包含一段换行文本。它可能包含应用了多种格式的多次运行,包括多种字体大小。它可以左对齐、右对齐、居中对齐或两端对齐,并且可能包含使用从右到左文本流的运行。我需要能够检测鼠标指针下的字符。在 WPF 中,我可以使用 GetPositionFromPoint 方法轻松解决此问题,但由于 Silverlight 中不存在此方法,因此我需要一种创造性的解决方法。

我尝试过的一种方法是将文本分解为单独的单词并为每个单词创建一个 TextBlock,并取得了一定的成功。通过在 WrapPanel 中渲染它们,我可以识别每个单词的起始位置,从而缩小命中测试区域。找到实际的目标字符就是逐个字符地构建目标单词,直到其宽度满足鼠标位置。

尽管我的实验技术有效,但它有两个缺点。首先是性能,因为在 WrapPanel 中渲染大量单独的 TextBlock 实例的成本很高。第二个问题是我的换行算法(粗略地基于空格和连字符)可能与 UI 中使用的真实 TextBlock 所使用的算法不完全匹配。刚刚阅读了 Unicode 换行算法,我意识到这本身就是一个完整的主题。我不想冒在某些段落内容条件下破坏设计的风险,

我理想地希望找到一种允许我直接针对主 TextBlock 进行测量的技术,但我欢迎任何创新的建议,不不管它们看起来多么奇怪。

非常感谢, 蒂姆

This is a slightly abstract question, for which I apologize. I am not necessarily looking for a specific tested solution, but rather some suggestions that I can experiment with to find the one that works the best. All suggestions are welcome - the more varied and creative the better.

Here's the problem:

I have a Silverlight TextBlock that contains a paragraph of wrapping text. It may contain multiple runs with a variety of formatting applied, including multiple font sizes. It may be aligned left, right, center or justified and may contain runs that use right-to-left textflow. I need to be able to detect the character under the mouse pointer. In WPF I could solve this problem easily using the GetPositionFromPoint method, but since this method does not exist in Silverlight, I need a creative workaround.

One approach I have tried, with reasonable success, is to break the text into individual words and create a TextBlock for each of them. By rendering them in a WrapPanel I can identify the start position of each word and thus narrow the hit test zone. Finding the actual target character is then a matter of building the target word, character by character, until its width meets the mouse position.

Although my experimental technique works, it has two drawbacks. The first is performance, since it is expensive to render a large number of individual TextBlock instances in a WrapPanel. The second concern is that my line-breaking algorithm (crudely based on spaces and hyphens) may not exactly match that used by the real TextBlock used in the UI. Having just read the Unicode Line Breaking Algorithm, I realize that this is a whole subject in its own right. I do not want to risk a design that breaks under certain paragraph content conditions

I would ideally like to find a technique that allows me to to measure directly against the main TextBlock, but I welcome any innovative suggestions, no matter how bizarre they may appear.

Many thanks,
Tim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹂绝世的画 2024-09-26 08:13:21

请改用 RichTextBox,它具有 GetPositionFromPoint 方法。

Use a RichTextBox instead, that does have a GetPositionFromPoint method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文