RichtextBox去除闪烁光标

 

 

 

 

https://files.cnblogs.com/xe2011/CustomRichTextBox_HideCaret.rar

 

richTextBox能高亮选择,光标仍在,没有光标闪烁

把重RichTextBox

 

 

去除闪烁光标 http://msdn.microsoft.com/en-us/library/windows/desktop/ms648403%28v=vs.85%29.aspx

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public class CustomRichTextBox:  RichTextBox
    {
        [DllImport("user32.dll")]
        static extern bool HideCaret(IntPtr hWnd);

protected override void WndProc(ref Message m) { base.WndProc(ref m); HideCaret(Handle); } } }

完整代码

CustomRichTextBox.CS

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public class CustomRichTextBox : RichTextBox
    {
        [DllImport("user32.dll")]
        static extern bool HideCaret(IntPtr hWnd);

        private bool bReadOnly = false;
        public void SetReadMode()
        {
            ReadOnly = true;
            bReadOnly = true;
        }

        public void SetEditMode()
        {
            ReadOnly = false;
            bReadOnly = false;
            Focus();
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (bReadOnly)
                HideCaret(Handle);
        }
    }  
}
View Code

 

Form1.CS

using System;
using System.Collections.Generic;

using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            CustomRichTextBox1.SetReadMode();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CustomRichTextBox1.SetEditMode();
        }
    }
}
View Code

 

 

 

这种写法更彻底,不能选择

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{

    public class CustomRichTextBox : 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;

        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);
        }
    }   
}
View Code

附件 https://files.cnblogs.com/xe2011/CustomRichTextBox1ReadMode.rar

 

 

 

 

posted @ 2013-11-26 11:20  XE2011  阅读(2953)  评论(0编辑  收藏  举报