SendMessage CHARFORMAT2 text effect CFE_SHADOW

0

I am trying to make a application with the following objects. Richtextbox, a few listviews, combo boxes and a textbox.

The richtextbox is used for read-only purposes (it displays messages). I have added a feature already that enabled me to insert a link that can be clicked and a action to favor will be executed (instead of opening a href url in default browser for example).

Now I want to change the text in the richtextbox to text with a shadow so all colors of text are ok to read in the richtextbox. So all the text in the RTB must be changed to text with a shadow style.

So I have been messing around with SendMessage and the CHARFORMAT2 struct. But I don't really understand how to properly get it to work at all.

This is what I have so far, can sombody explain to me what I am doing wrong and what the mask and effect should be? And do I really need to do AllocCoTaskMem and then StructureToPtr? In question id 1268009 example code I don't see any of that, why is that?

    [StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Auto)]
    public struct CHARFORMAT2
    {
        public int cbSize;
        public int dwMask;
        public int dwEffects;
        public int yHeight;
        public int yOffset;
        public int crTextColor;
        public byte bCharSet;
        public byte bPitchAndFamily;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string szFaceName;
        public short wWeight;
        public short sSpacing;
        public int crBackColor;
        public int lcid;
        public int dwReserved;
        public short sStyle;
        public short wKerning;
        public byte bUnderlineType;
        public byte bAnimation;
        public byte bRevAuthor;
        public byte bReserved1;
    }

    const int CFE_LINK = 0x20;
    const int CFM_LINK = 0x20;
    const int CFM_LCID = 0x2000000;
    const int CFM_REVAUTHOR = 0x8000;
    const int EM_SETCHARFORMAT = 0x444;

    private const Int32 SCF_SELECTION2 = 0x0001;
    private const UInt32 WM_USER = 0x0400;
    private const UInt32 EM_GETCHARFORMAT = (WM_USER + 58);
    const UInt32 CFE_BOLD = 0x0001;
    const UInt32 CFM_BOLD = 0x00000001;
    const UInt32 CFM_SHADOW = 0x0400;     

    const int SCF_SELECTION = 0x1;
    const int SCF_WORD = 0x2;

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    //working
    private void clickableItem(string name, string msg)
    {
        int start = msg.Length - 14;
        int selsc = RTBWindow.Text.Length - start;
        RTBWindow.SelectionStart = selsc;
        RTBWindow.SelectionLength = name.Length;

        CHARFORMAT2 myFormat = new CHARFORMAT2();
        myFormat.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(myFormat);
        myFormat.dwEffects = CFE_LINK;
        myFormat.dwMask = CFM_REVAUTHOR + CFM_LCID + CFM_LINK;
        myFormat.bRevAuthor = 3;

        IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(myFormat));
        Marshal.StructureToPtr(myFormat, lParam, false);
        SendMessage(RTBWindow.Handle, (UInt32)EM_SETCHARFORMAT, (IntPtr)(SCF_SELECTION + SCF_WORD), lParam);
    }

    private void SetEffectTest(UInt32 mask, UInt32 effect, bool valid)
    {
        CHARFORMAT2 fmt = new CHARFORMAT2();
        fmt.cbSize = Marshal.SizeOf(fmt);
        fmt.dwMask = (int)mask;
        fmt.dwEffects = valid ? (int)effect : 0;
        IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmt));
        Marshal.StructureToPtr(fmt, lParam, false);
        SendMessage(RTBWindow.Handle, EM_SETCHARFORMAT, (IntPtr)SCF_SELECTION2, lParam);
    }

    //not working, tried to make a working example from the link below and msdn;
    //https://stackoverflow.com/questions/1268009/reset-rtf-in-richtextbox
    private void btnShadowTest_Click(object sender, EventArgs e)
    {
        CHARFORMAT2 _charFormat = new CHARFORMAT2();
        _charFormat.cbSize = Marshal.SizeOf(_charFormat);
        IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(_charFormat));
        Marshal.StructureToPtr(_charFormat, lParam, false);
        SendMessage(RTBWindow.Handle, EM_GETCHARFORMAT, (IntPtr)SCF_SELECTION, lParam);
        SetEffectTest(CFM_SHADOW, CFM_SHADOW, true); //<- this is wrong for sure.
    }
c#
c#-4.0
asked on Stack Overflow Jul 12, 2018 by Norman L. Selby • edited Jul 12, 2018 by Norman L. Selby

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0