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.
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.
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 open Spy++ and WordPad and then select the RichTextBox of it and see what's the class name.
RICHEDIT50W class to my control, I reached to this great post of @Elmue, Thanks to him.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.