问题
Is there anyway to prevent the cursor (IBeam) of a read-only RichRextBox
from blinking whenever the textbox got focus?
I've tried to block the WM_SETFOCUS
message from the WndProc
but it causes the form to hang.
if( m.Msg == 0x0007 ) return;
回答1:
You'll need to use Win32 APIs. Here's what you could do in VB:
'API declares
Private Declare Function HideCaret Lib "user32" _
(ByVal hwnd As IntPtr) As Integer
Private Declare Function ShowCaret Lib "user32" _
(ByVal hwnd As IntPtr) As Integer
'hide the caret in myTextBox
Call HideCaret(myTextBox.Handle)
'show the caret back..
Call ShowCaret(myTextBox.Handle)
and in C#
[DllImport("user32.dll", EntryPoint = "ShowCaret")]
public static extern long ShowCaret(IntPtr hwnd);
[DllImport("user32.dll", EntryPoint = "HideCaret")]
public static extern long HideCaret(IntPtr hwnd);
then make a call to
HideCaret(richtextbox.Handle)
when ever you want to hide it.
回答2:
Just to say that the Anirudh Goel answer does not work (at least in C#). The carat still there blinking :/
I found a solution at: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21896403.html
His class always hide the caret, here is an improved one so you can choose to hide or not the caret.
If you want to hide do not forget to set MustHideCaret to true
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Lm
{
public class RichTextBoxEx : RichTextBox
{
private readonly object mustHideCaretLocker = new object();
private bool mustHideCaret;
[DefaultValue(false)]
public bool MustHideCaret
{
get
{
lock (this.mustHideCaretLocker)
return this.mustHideCaret;
}
set
{
TabStop = false;
if (value)
SetHideCaret();
else
SetShowCaret();
}
}
[DllImport("user32.dll")]
private static extern int HideCaret(IntPtr hwnd);
[DllImport("user32.dll", EntryPoint = "ShowCaret")]
public static extern long ShowCaret(IntPtr hwnd);
public RichTextBoxEx()
{
}
private void SetHideCaret()
{
MouseDown += new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
MouseUp += new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
Resize += new EventHandler(ReadOnlyRichTextBox_Resize);
HideCaret(Handle);
lock (this.mustHideCaretLocker)
this.mustHideCaret = true;
}
private void SetShowCaret()
{
try
{
MouseDown -= new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
MouseUp -= new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
Resize -= new EventHandler(ReadOnlyRichTextBox_Resize);
}
catch
{
}
ShowCaret(Handle);
lock (this.mustHideCaretLocker)
this.mustHideCaret = false;
}
protected override void OnGotFocus(EventArgs e)
{
if (MustHideCaret)
HideCaret(Handle);
}
protected override void OnEnter(EventArgs e)
{
if (MustHideCaret)
HideCaret(Handle);
}
private void ReadOnlyRichTextBox_Mouse(object sender, System.Windows.Forms.MouseEventArgs e)
{
HideCaret(Handle);
}
private void ReadOnlyRichTextBox_Resize(object sender, System.EventArgs e)
{
HideCaret(Handle);
}
}
}
回答3:
Easier method: attach this event to the Enter event of the RichTextBox:
private void Control_Enter(object sender, EventArgs e) {
ActiveControl = null;
}
回答4:
For me solution from Pedro77 didn't work too... I've modified that class to:
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Lm
{
public class RichTextBoxEx : RichTextBox
{
private readonly object mustHideCaretLocker = new object();
private bool mustHideCaret;
[DefaultValue(false)]
public bool MustHideCaret
{
get
{
lock (this.mustHideCaretLocker)
return this.mustHideCaret;
}
set
{
TabStop = false;
if (value)
SetHideCaret();
else
SetShowCaret();
}
}
[DllImport("user32.dll")]
private static extern int HideCaret(IntPtr hwnd);
[DllImport("user32.dll", EntryPoint = "ShowCaret")]
public static extern long ShowCaret(IntPtr hwnd);
public RichTextBoxEx()
{
}
private void SetHideCaret()
{
MouseDown += new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
MouseUp += new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
Resize += new EventHandler(ReadOnlyRichTextBox_Resize);
HideCaret(Handle);
lock (this.mustHideCaretLocker)
this.mustHideCaret = true;
}
private void SetShowCaret()
{
try
{
MouseDown -= new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
MouseUp -= new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
Resize -= new EventHandler(ReadOnlyRichTextBox_Resize);
}
catch
{
}
ShowCaret(Handle);
lock (this.mustHideCaretLocker)
this.mustHideCaret = false;
}
protected override void OnGotFocus(EventArgs e)
{
if (MustHideCaret)
{
HideCaret(Handle);
this.Parent.Focus();//here we select parent control in my case it is panel
}
}
protected override void OnEnter(EventArgs e)
{
if (MustHideCaret)
HideCaret(Handle);
}
private void ReadOnlyRichTextBox_Mouse(object sender, System.Windows.Forms.MouseEventArgs e)
{
HideCaret(Handle);
}
private void ReadOnlyRichTextBox_Resize(object sender, System.EventArgs e)
{
HideCaret(Handle);
}
}
}
then put my RichTextBoxEx into (inside) Panel control... that fixed getting blinking caret on mouse click...
来源:https://stackoverflow.com/questions/734032/prevent-blinking-cursor-ibeam-from-a-read-only-richtextbox