问题
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 achieve something like that which you can see in wordpad.
instead of which you see in RichTextBox
.
回答1:
You can use the latest version of RichTextBox that is RICHEDIT50W
, to do so you should inherit from standard RichTextBox
and override CreateParams
and set the ClassName
to RICHEDIT50W
:
Code
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class ExRichText : RichTextBox
{
[DllImport("kernel32.dll", EntryPoint = "LoadLibraryW",
CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr LoadLibraryW(string s_File);
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
{
LoadLibrary("MsftEdit.dll"); // Available since XP SP1
cp.ClassName = "RichEdit50W";
}
catch { /* Windows XP without any Service Pack.*/ }
return cp;
}
}
}
Screenshot
Note:
I could see the class of RichTextBox of wordpad using Spy++ thanks to this ancient useful visual studio tool.
If you had any problem with
RICHEDIT50W
in your os, you can openSpy++
andWordPad
and then select theRichTextBox
of it and see what's the class name.
- When I searched about applying the
RICHEDIT50W
class to my control, I reached to this great post of @Elmue, Thanks to him.
来源:https://stackoverflow.com/questions/32591157/richtextbox-selection-highlight