[英]RichTextBox cursor keeps changing to IBeam
I have a readonly RichTextBox
, with its cursor set to Arrow
.我有一个只读
RichTextBox
,其光标设置为Arrow
。 Even so, when I hover it, the cursor flickers, and switches very quickly between Arrow
and IBeam
.即便如此,当我将其悬停时,光标会闪烁,并在
Arrow
和IBeam
之间快速切换。 How can I make it stay on Arrow
and not flicker?我怎样才能让它保持在
Arrow
而不闪烁?
I'm assuming this is the WinForms' RichTextBox, because the WPF one doesn't have this problem .我假设这是 WinForms 的 RichTextBox,因为 WPF 没有这个问题。
The RichTextBox handles WM_SETCURSOR
messages, to change the Cursor to Cursors.Hand
if the Mouse Pointer ends up on a Link. RichTextBox 处理
WM_SETCURSOR
消息,如果鼠标指针位于链接上,则将 Cursor 更改为Cursors.Hand
。 A note from the developers:来自开发人员的说明:
RichTextBox uses the
WM_SETCURSOR
message over links to allow us to change the cursor to a hand.RichTextBox 通过链接使用
WM_SETCURSOR
消息,允许我们将光标更改为手形。 It does this through a synchronous notification message.它通过同步通知消息完成此操作。 So we have to pass the message to the DefWndProc first, and then, if we receive a notification message in the meantime (indicated by changing "LinkCursor", we set it to a hand. Otherwise, we call the
WM_SETCURSOR
implementation on Control to set it to the user's selection for the RichTextBox's cursor.所以我们必须先将消息传递给DefWndProc,然后,如果我们在此期间收到通知消息(通过改变“LinkCursor”表示,我们将其设置为手。否则,我们调用Control上的
WM_SETCURSOR
实现来设置将它添加到用户对 RichTextBox 光标的选择。
You could set the Capture when the Mouse enters the Control's bounds and then release it when the Mouse Pointer leaves the area.您可以在鼠标进入控件边界时设置捕获,然后在鼠标指针离开该区域时释放它。 The capture needs to be released otherwise, when you first click on another control, the cursor will be set to RichTextBox instead:
否则需要释放捕获,当您第一次单击另一个控件时,光标将被设置为 RichTextBox:
private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
if (!richTextBox1.ClientRectangle.Contains(e.Location)) {
richTextBox1.Capture = false;
}
else if (!richTextBox1.Capture) {
richTextBox1.Capture = true;
}
}
Jimi's answer works well to stop flickering, but I don't have a good feeling about capturing mouse on mouse move. Jimi 的回答可以很好地停止闪烁,但我对在鼠标移动时捕获鼠标没有很好的感觉。 For example one issue that I see in that solution, is if you set the capture on mouse move, then keyboard shortcuts like Alt + F4 or Alt + Space will stop working.
例如,我在该解决方案中看到的一个问题是,如果您在鼠标移动时设置捕获,则Alt + F4或Alt + Space等键盘快捷键将停止工作。
I'd prefer to handle WndProc
and set the cursor when received WM_SETCURSOR
:我更喜欢处理
WndProc
并在收到WM_SETCURSOR
时设置光标:
using System.Windows.Forms;
public class ExRichTextBox : RichTextBox
{
const int WM_SETCURSOR = 0x0020;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SETCURSOR)
Cursor.Current = this.Cursor;
else
base.WndProc(ref m);
}
}
It stops flickering.它停止闪烁。 Not a perfect solution, but at least those important shortcuts will continue working.
不是一个完美的解决方案,但至少那些重要的捷径会继续工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.