首页 > 解决方案 > RichTextBox 使用 CFM_COLOR 代码更改字体颜色

问题描述

我正在 Windows 窗体中开发一个文本编辑器,为此我使用 RichTextBox 小部件。I came across a problem where, when selecting different styled text pieces, the selection would return a null value, which prevented me from, let's say, changing the size of a text that has parts of Verdana and Arial in it.

为了解决这个问题,我创建了一个新类来操作 CHARFORMAT 结构,并根据我想要更改的样式发送消息。现在我需要创建一个函数来改变用户选择的字体颜色。为此,我需要 CFM_COLOR 标签,我不知道它是什么,而且我似乎无法在任何地方找到它。

    [Browsable(false)] 
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public Color SelectionFontColor
    {
        set
        {
            CHARFORMAT cf = new CHARFORMAT();
            cf.cbSize = Marshal.SizeOf(cf);
            cf.dwMask = CFM_COLOR;
            // There might be some missing code
            ???
            SendMessage(Handle, EM_SETCHARFORMAT, SCF_SELECTION, ref cf);
        }
    }

标签: c#colorsrichtextboxwindows-forms-designer

解决方案


来自referencesource.microsoft.com 上的RichTextBoxConstants.cs

internal const int CFM_COLOR               = 0x40000000;

推荐阅读