C# winform 透明 纯只读 不可选择 RichTextBox

经常会碰到许多文字要加背景大图,自定义控件RichTextBox可以帮助我们,不过RichTextBox不是最好的选择,RichTextBox占用资源比较多,如果能重写TextBox是最好的。

 

 

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class MyRichTextBox : RichTextBox
    {
        public MyRichTextBox()
        {
            InitializeComponent();
            this.BorderStyle = BorderStyle.None;
        }

        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;

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;
                return cp;
            }
        }

        public override Cursor Cursor
        {
            get
            {
                return Cursors.Arrow;
            }
        }

        

        /// <summary>
        /// 屏蔽控件所有鼠标消息的发送
        /// </summary>
        /// <param name="m">消息</param>
        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);
        }
    }
}

 

 

 

posted @ 2013-10-25 08:18  醉低调  阅读(1344)  评论(0)    收藏  举报