使富文本框不响应鼠标事件 [英] make richtextbox not respond to mouse events

查看:119
本文介绍了使富文本框不响应鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,并在其中一个表单中放置了一个富文本框,其中包含一些用户将输入的文本,我已将富文本框的 ReadOnly 属性设置为 true,将表单的 keypreview 设置为 true,并且我已经处理了表单按键事件将蓝色应用于正确的按键,将红色应用于错误的按键以应用于富文本框中的当前字符.现在我需要限制用户只能输入文本,他们不应该能够使用鼠标 caz 选择富文本框文本,这样他们就会弄乱我的应用程序.

I am developing an application and in one of its forms i have placed a richtextbox that has some text that the user will be typing,I have set richtextbox's ReadOnly property to true, form's keypreview to true and I have handled forms keypress event to apply blue color to the correct keypress and red color to the wrong keypress to current character in the richtextbox. now i need to restrict the users to only typing the text, they should not be able to select richtextbox text using mouse caz that way they can mess with my application.

提前tnx

推荐答案

您需要子类化 RichTextBox 并禁用鼠标事件的处理.

You need to subclass RichTextBox and disable processing of mouse events.

public class DisabledRichTextBox : System.Windows.Forms.RichTextBox
{
    // See: http://wiki.winehq.org/List_Of_Windows_Messages

    private const int WM_SETFOCUS   = 0x07;
    private const int WM_ENABLE     = 0x0A;
    private const int WM_SETCURSOR  = 0x20;

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        if (!(m.Msg == WM_SETFOCUS || m.Msg == WM_ENABLE || m.Msg == WM_SETCURSOR))
            base.WndProc(ref m);
    }
}

它就像一个标签,防止焦点、用户输入、光标改变,而不会被实际禁用.

It will act like a label, preventing focus, user input, cursor change, without being actually disabled.

您还需要保持 ReadOnly = true 以禁用编辑.

You also need to keep ReadOnly = true to disable editing.

这篇关于使富文本框不响应鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆