整个只读的RichTextBox

RichTextBox的ReadOnly效果太差了,没有什么现成的属性可以让我们使用

想了半天还是得借助消息

 

代码很简单,就是截获Windows的几个常用的消息

WM_SETFOCUS,WM_LBUTTONDOWN,WM_LBUTTONUP,WM_LBUTTONDBLCLK,WM_RBUTTONDOWN,WM_RBUTTONUP,WM_RBUTTONDBLCLK,WM_KEYDOWN,WM_KEYUP

关于消息我就不多介绍了,这几个通过名字就应该看的出来啥意思,代码如下:

public class TestRichTextBox : System.Windows.Forms.RichTextBox { private const int WM_SETFOCUS = 0x7; private const int WM_LBUTTONDOWN = 0x201; private const int WM_LBUTTONUP = 0x202; private const int WM_LBUTTONDBLCLK = 0x203; private const int WM_RBUTTONDOWN = 0x204; private const int WM_RBUTTONUP = 0x205; private const int WM_RBUTTONDBLCLK = 0x206; private const int WM_KEYDOWN = 0x0100; private const int WM_KEYUP = 0x0101; public TestRichTextBox() { this.Cursor = Cursors.Arrow;//设置鼠标样式 } protected override void WndProc(ref Message m) { if (m.Msg == WM_SETFOCUS || m.Msg == WM_KEYDOWN || m.Msg == WM_KEYUP || m.Msg == WM_LBUTTONDOWN || m.Msg == WM_LBUTTONUP || m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_RBUTTONDOWN || m.Msg == WM_RBUTTONUP || m.Msg == WM_RBUTTONDBLCLK) { return; } base.WndProc(ref m); } }

 

 

简单解释一下:

首先继承一下RichTextBox

然后重写WndProc方法

在这个方法里面截获相应的消息

 

欢迎转载,请注明出处~~

 

 

你可能感兴趣的:(windows,Class)