鼠标光标在选定的文本上闪烁-如何防止这种情况?
在将鼠标移到RichTextBox(C#、. NET 4.0,WinForms)中的选定文本上时,我遇到奇怪的行为:当我移动鼠标光标时,它会在Cursors.Arrow和Cursors.IBeam之间闪烁.
I'm encountering strange behaviour while moving the mouse over selected text in a RichTextBox (C#, .NET 4.0, WinForms): as I move the mouse cursor, it flickers between Cursors.Arrow and Cursors.IBeam.
我发现了禁用闪烁的代码:
I found code that disables the flickering:
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_SETCURSOR) //WM_SETCURSOR is set to 0x20
return;
}
但是然后鼠标光标停留在Cursors.Arrow上,即使我手动将其设置为其他内容,例如:
but then the mouse cursor is stuck as Cursors.Arrow, even when I manually set it to something else, ex:
void RTFBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor = Cursors.IBeam;
}
(我在MouseMove函数中也有逻辑,可以根据鼠标悬停的位置将 Cursor
设置为其他类型的非 Arrow
游标.)
(I also had logic in the MouseMove function to set Cursor
to other types of non-Arrow
cursors, depending on what the mouse was over.)
我也尝试过:
public override Cursor Cursor
{
get
{
//(I have other logic here to determine the desired cursor type I want; in all cases it was a non-Arrow cursor)
return Cursors.Cross; //'Cross' instead of 'IBeam' just to prove whether this works
}
set
{
return;
}
}
成功使光标变成了十字形(但仅当我注释掉 WndProc
代码时才出现),但是当我将鼠标悬停在选定的文本上时(鼠标光标在Arrow和Cross之间切换),闪烁仍然存在
which successfully made the cursor a cross (but only when I commented out the WndProc
code), but the flickering remained when I moused over selected text (with the mouse cursor changing between Arrow and Cross).
在尝试找到解决方案时,我遇到了这篇文章,但打电话给了 SendMessage(Handle,LVM_SETHOTCURSOR,IntPtr.Zero,Cursors.IBeam.Handle);
从RichTextBox继承的类中获取数据并不能解决闪烁问题.
In trying to find a solution, I came across this post, but callingSendMessage(Handle, LVM_SETHOTCURSOR, IntPtr.Zero, Cursors.IBeam.Handle);
from a class inheriting from RichTextBox did not fix the flickering problem.
My problem seems identical to the one desribed in this post, but the problem was described to exist on .NET 3.0 and fixed in .NET 3.5.
当我创建一个新项目并将RichTextBox插入表单时,闪烁仍然存在.
When I created a new project and inserted a RichTextBox into the form, the flickering is still there.
因此,我的问题是:如何防止这种闪烁?还是有人知道在更高版本的.NET/visual studio中是否可以解决此问题?
Thus, my question is: How do I prevent this flickering? Or does anyone know if this problem is resolved in later versions of .NET/visual studio?
[更新:我下载了Visual Studio 2013,但仍然存在闪烁"效果.我下载了.Net 4.5.1安装程序,并要求它进行修复,但闪烁"仍然存在.在属性">参考"下,它表示"System.Windows.Forms"的版本为4.0.0.0;"System.Windows.Forms"的版本为4.0.0.0.我想这意味着没有必要更新4.0以后的版本?]
[Update: I downloaded Visual Studio 2013, but the "flicker" effect is still present. I downloaded .Net 4.5.1 installer and told it to repair, but the "flickering" remained. Under "Properties" > "References", it says that "System.Windows.Forms" is version 4.0.0.0; I suppose this means that updating past 4.0 was unnecessary?]
我在Chess123mate的答案中添加了答案,以使其在垂直滚动条上显示箭头光标:
I added onto chess123mate's answer to get it to show the arrow cursor over the vertical scrollbar:
[DllImport("user32.dll")]
public static extern int SetCursor(IntPtr cursor);
private const int WM_SETCURSOR = 0x20;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_SETCURSOR)
{
var scrollbarWidth = System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
var x = PointToClient(Control.MousePosition).X;
var inScrollbar = x > this.Width - scrollbarWidth;
var cursor = inScrollbar ? Cursors.Arrow : Cursors.IBeam;
SetCursor(cursor.Handle);
m.Result = new IntPtr(1); //Signify that we dealt with the message. We should be returning "true", but I can't figure out how to do that.
return;
}
base.WndProc(ref m);
}