简体   繁体   中英

RichTextBox changing Font Color using CFM_COLOR code

I am developing a text editor in Windows Forms, for which I use RichTextBox widget. 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.

To solve this problem I created a new class that manipulates CHARFORMAT structure, and sends messages according to the style I want to change. Now I need to make a function that changes the font color of the user selection. For that, I need the CFM_COLOR tag, which I have no idea what it is, and I can't seem to find it anywhere.

    [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);
        }
    }

从referencesource.microsoft.com上的RichTextBoxConstants.cs

internal const int CFM_COLOR               = 0x40000000;

I came across a problem where, when selecting different styled text pieces, the selection would return a null value

I cannot reproduce your problem : for example, I select text with differents styles, and I colorize in Orange the selected text with richTextBox1.SelectionColor =>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM