gpt4 book ai didi

c# - RichTextBox 选择突出显示

转载 作者:行者123 更新时间:2023-11-30 13:55:40 25 4
gpt4 key购买 nike

是否有可能在用户选择后,每个选择的字母都显示其原始颜色?并不总是默认的白色?

我想实现这样的目标 enter image description here你可以在写字板中看到。

而不是 enter image description here您在 RichTextBox 中看到的。

最佳答案

您可以使用最新版本的RichTextBoxRICHEDIT50W,为此您应该继承标准 RichTextBox 并覆盖 CreateParams 并将 ClassName 设置为 RICHEDIT50W:

代码

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

截图

enter image description here

注意:

  • 多亏了这个古老而有用的 visual studio 工具,我可以使用 Spy++ 看到写字板的 RichTextBox 类。

  • 如果您在操作系统中使用 RICHEDIT50W 有任何问题,您可以打开 Spy++WordPad 然后选择 RichTextBox 并查看类名是什么。

enter image description here

  • 当我搜索有关将 RICHEDIT50W 类应用到我的控件时,我找到了 this @Elmue 的精彩帖子,感谢他。

关于c# - RichTextBox 选择突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32591157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com