就像标题一样:我在网上搜索了答案,但我无法找到隐藏VB.NET中RichTextBox插入符号的方法.
我试图将RichTextBox.Enabled属性设置为False,然后将背景颜色和前景颜色更改为非灰色,但这并不能解决问题.
提前致谢.
Ped*_*o77 13
解:
来自:http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21896403.html
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;
public class ReadOnlyRichTextBox : System.Windows.Forms.RichTextBox
{
[DllImport("user32.dll")]
private static extern int HideCaret (IntPtr hwnd);
public ReadOnlyRichTextBox()
{
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ReadOnlyRichTextBox_Mouse);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ReadOnlyRichTextBox_Mouse);
base.ReadOnly = true;
base.TabStop = false;
HideCaret(this.Handle);
}
protected override void OnGotFocus(EventArgs e)
{
HideCaret(this.Handle);
}
protected override void OnEnter(EventArgs e)
{
HideCaret(this.Handle);
}
[DefaultValue(true)]
public new bool ReadOnly
{
get { return true; }
set { }
}
[DefaultValue(false)]
public new bool TabStop
{
get { return false; }
set { }
}
private void ReadOnlyRichTextBox_Mouse(object sender, System.Windows.Forms.MouseEventArgs e)
{
HideCaret(this.Handle);
}
private void InitializeComponent()
{
//
// ReadOnlyRichTextBox
//
this.Resize += new System.EventHandler(this.ReadOnlyRichTextBox_Resize);
}
private void ReadOnlyRichTextBox_Resize(object sender, System.EventArgs e)
{
HideCaret(this.Handle);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
将 richTextBox 控件放置在窗体上
将表单名称设置为 Form1
将 richTextBox 名称设置为 richTextBox1
如果您不想允许用户复制文本,请将 richTextBox1 ShortcutsEnabled 属性设置为 False
转到 Project->Add Component,输入组件名称 ReadOnlyRichTextBox.cs
然后打开 ReadOnlyRichTextBox.cs 并粘贴以下代码:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace <Replace with your app namespace>
{
public partial class ReadOnlyRichTextBox : RichTextBox
{
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public ReadOnlyRichTextBox()
{
this.ReadOnly = true;
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
HideCaret(this.Handle);
}
}
}
Run Code Online (Sandbox Code Playgroud)
从解决方案资源管理器中打开“Form1.Designer.cs”并在此文件中替换以下行:
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
和
this.richTextBox1 = new ReadOnlyRichTextBox();
和
private System.Windows.Forms.RichTextBox richTextBox1;
和
private ReadOnlyRichTextBox richTextBox1;
您可以使用 HideCaret API 函数,请在 www.pinvoke.net 上查看。诀窍是知道何时调用它。一种非常简单但肮脏的解决方案是在 RTF 的 Enter 事件中启动一次性计时器。按照 nobugs 的建议在 WndProc 中捕获正确的消息会更好,不幸的是捕获的消息是错误的......