在.Net的RichTextBox中添加带有自定义颜色的重音标记

发布于 2024-09-28 23:00:01 字数 1682 浏览 5 评论 0原文

使用 WinForms 的 RichTextBox 的 Text 属性,您可以显示上面带有重音符号的字母,其中字母是一个字符,重音符号是另一个字符。即,从视觉上看,它看起来像一个角色,但实际上它由两个角色组成。在 Unicode 术语中,字母称为“基本字符”,而重音称为“组合变音标记”。

我试图显示一个普通的黑色字母,上面有不同颜色的重音(比如说红色)。下面是 VB 中的代码:

'Box is the name of a RichTextBox

'Set the Text property to the letter 'a' + a line above it (called a 'macron')

Box.Text = a & ChrW(&H304)

'Select the macron, which is the second character (0 based)

Box.Select(1,1)

'change its color:

Box.SelectionColor = Color.Red

上面的代码不起作用。马克龙的颜色不会改变。 另一方面,当尝试仅选择“a”字母并更改其颜色时,宏的颜色也会发生变化:

Box.Select(0,1)

'change the letter color:

Box.SelectionColor = Color.Red

所以我不能让一个字母具有一种颜色,而其重音却具有另一种颜色。 我要补充一点,我是希伯来语使用者,当使用希伯来语版本的 Microsoft Word 时,可以通过 Word 菜单轻松更改希伯来字母标记(称为“Nikud”)的颜色,即给它们不同的颜色从信件中。

如果现在不知道如何通过纯 .Net 代码实现此行为, 并且可能知道如何通过 win API 调用来获取它,我很欣赏任何解释,因为我不熟悉该领域。

编辑:

实际上,我找到了一种我无法信任的奇怪方法来部分完成该任务。我没有提到它,因为它看起来像是 RichTextBox 控件的一个“积极错误”。由于该控件事先并未打算以这种方式工作,因此我无法根据最终用户系统的稳定性兼容性来预测此类使用的后果。 如下所示:

'give the control a font I have decalred. It doesn't matter which font
'(as long as it supports the characters in use)
Box.Font = Lucida

'Add text
Box.Text = a & ChrW(&H304)

'Select the macron, which is the second character (0 based)
Box.Select(1,1)

'Here is the line that makes the difference:

Box.SelectionFont = Lucida
'*********************************************

'change its color:
Box.SelectionColor = Color.Red

正如您所看到的,我已为所选内容(宏)分配了一种字体 它已经有了,所以这条线应该没有影响。然而,现在 下一行确实改变了宏的颜色,使字母颜色保持黑色。奇怪的是,这适用于多种字体大小(14、16、26 等),而不适用于其他字体(12、18、24 等)。如果有人对此有任何想法,我将很高兴听到。

Using the Text property of a WinForms' RichTextBox, you can show a letter with an accent above it, in a way that the letter is one character, and the accent is another one. I.e., also visually it looks like one character, it actually consists of two. In Unicode terms, the letter is called a 'base character', while the accent is called 'combining diacritical mark'.

I have tried to show a normal black letter with an accent in different color above it (lets say red). Here is the code in VB:

'Box is the name of a RichTextBox

'Set the Text property to the letter 'a' + a line above it (called a 'macron')

Box.Text = a & ChrW(&H304)

'Select the macron, which is the second character (0 based)

Box.Select(1,1)

'change its color:

Box.SelectionColor = Color.Red

The code above doesn't work. The macron's color doesn't changes.
On the other hand, when trying to select only the 'a' letter and changing its color, the macron's color changes as well:

Box.Select(0,1)

'change the letter color:

Box.SelectionColor = Color.Red

So I couldn't have a letter with one color, and its accent with another.
I will add that I'm an Hebrew speaker, and when using the Hebrew version of Microsoft Word, one can easily changes the color of the Hebrew letters' marks (called 'Nikud') through the Word menus, i.e., give them different color from the letters.

If one doesn't now how to achieve this behavior through pure .Net code,
and may know how to get it through win API calls, I appreciate any explanations, since I'm not familiar with that area.

EDIT:

Actually I have found a weird way, which I can't trust, to accomplish that partialy. I didn't mentioned it, because it looks like a 'positive bug' of the RichTextBox control. Since the control wasn't intended in advance to work that way, I can't perdict the consequences of such uses in terms of stability and compatibility at the end user system.
Here it is:

'give the control a font I have decalred. It doesn't matter which font
'(as long as it supports the characters in use)
Box.Font = Lucida

'Add text
Box.Text = a & ChrW(&H304)

'Select the macron, which is the second character (0 based)
Box.Select(1,1)

'Here is the line that makes the difference:

Box.SelectionFont = Lucida
'*********************************************

'change its color:
Box.SelectionColor = Color.Red

As one can see, I have assigned to the selection (the macron) a font
that it already have, so this line should have no affect. However, now the
next line does change the color of the macron, leaving the letter color black. Strangely enough, This works in several font's sizes (14,16,26 and more) and doesn't work on others(12,18,24 and more). If one have any ideas about this I will be glad to hear.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文