RichTextBox Selection Highlight

后端 未结 2 1272
你的背包
你的背包 2020-12-03 19:52

Is there a possibility that after selection made by user, every selected letter displays it\'s original color? And not always white as it\'s by default?

I want to ac

2条回答
  •  执念已碎
    2020-12-03 20:38

    For anyone having problem with Visual Studio 2019 and exception "Class already exists" as mentioned in Hunar's comment I modified slightly code to load library only once and that solved issue for me.

    using System;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    [DesignerCategory("Code")]
    public class RichTextBox5 : RichTextBox
    {
        [DllImport("kernel32.dll", EntryPoint = "LoadLibraryW", CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern IntPtr LoadLibraryW(string s_File);
    
        private static readonly object libraryLoadLock = new object();
        private static bool libraryLoadFlag;
    
        public static IntPtr LoadLibrary(string s_File)
        {
            var module = LoadLibraryW(s_File);
            if (module != IntPtr.Zero)
            {
                return module;
            }
            var error = Marshal.GetLastWin32Error();
            throw new Win32Exception(error);
        }
    
        protected override CreateParams CreateParams
        {
            get
            {
                var cp = base.CreateParams;
                try
                {
                    lock (libraryLoadLock)
                    {
                        if (!libraryLoadFlag)
                        {
                            LoadLibrary("MsftEdit.dll"); // Available since XP SP1
                            libraryLoadFlag = true;
                        }
                    }
                    cp.ClassName = "RichEdit50W";
                }
                catch { /* Windows XP without any Service Pack.*/ }
                return cp;
            }
        }
    }
    

    Sorry for answering this way, but due to insufficient reputation points I was able only to post an answer.

提交回复
热议问题