C#WinForms RichTextBox鼠标中键滚动:阻止箭头光标

时间:2018-06-02 08:32:17

标签: c# winforms richtextbox qtextcursor

在C#WinForms中,我有一个自定义的RichTextBox,我自己处理鼠标中键滚动。完成后,我想显示自己的光标。当按下中间按钮时,我在MouseDown事件中切换到此光标。

    void richText_MouseDown(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Middle) {
            Cursor.Current = MainForm.cursors["hand_NS"];
        }
    }

然而,文本框然后立即切换到Windows“箭头”光标。这似乎是RichTextBox autom的一部分。行为,在MouseDown或MouseMove中。我可以通过不断地在MouseMove中显示我的光标来覆盖它,但它看起来很闪烁,因为两个游标互相争斗。我可以以某种方式阻止这个自动切换到“箭头”光标吗?

编辑:尝试设置Cursor属性:

 void richText_MouseDown(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Middle) {
            richText.Cursor = MainForm.cursors["hand_NS"];
            //Cursor.Current = MainForm.cursors["hand_NS"];
        }
    }

恢复工字梁光标:

void richText_MouseUp(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Middle) {
        richText.Cursor = Cursors.IBeam;
        //Cursor.Current = Cursors.IBeam;
    }

}

1 个答案:

答案 0 :(得分:0)

最后通过投掷我能找到的所有火炮,让它正常工作(几乎没有闪烁)。在MouseMove(下面)中完成的操作也在MouseDown中完成。

    public const uint LVM_SETHOTCURSOR = 4158;
    [DllImport("user32.dll")]
    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);


    void richText_MouseMove(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Middle) {
                this.TopLevelControl.Cursor = Cursors.PanNorth;
                richText.Cursor = Cursors.PanNorth;
                SendMessage(richText.Handle, LVM_SETHOTCURSOR, IntPtr.Zero, Cursors.PanNorth.Handle);
                Cursor.Current = Cursors.PanNorth;
         }
    }

覆盖RTB控件中的SETCURSOR消息:

    [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) {
            SetCursor(Cursors.PanNorth.Handle);
            m.Result = new IntPtr(1);
            return;
        }
        base.WndProc(ref m);
    }

来源:

ListView Cursor changing & flickering

Mouse cursor flickers over selected text - how to prevent this?