How to hide the caret in a RichTextBox?

后端 未结 9 1505
[愿得一人]
[愿得一人] 2020-12-31 15:10

Just like the title: I\'ve searched the web for an answer, but i was not able to find a way to hide the caret of a RichTextBox in VB.NET.

I\'ve tried to set the Rich

相关标签:
9条回答
  • 2020-12-31 15:56
    /// <summary>
    /// Transparent RichTextBox
    /// To change BackColor add a Panel control as holder of RichTextLabel
    /// </summary>
    public class RichTextLabel : RichTextBox
    {
        public RichTextLabel()
        {
            base.Enabled = false;
            base.ReadOnly = true;
            base.ScrollBars = RichTextBoxScrollBars.None;
            base.ForeColor = Color.FromArgb(0, 0, 1);
        }
    
        override protected CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;
                return cp;
            }
        }
    
        override protected void OnPaintBackground(PaintEventArgs e)
        {
        }
    }
    
    0 讨论(0)
  • 2020-12-31 15:56

    Minimal Version (optimized):

    public class ReadOnlyRichTextBox : RichTextBox
    {
        [DllImport("user32.dll")]
        static extern bool HideCaret(IntPtr hWnd);
    
        public ReadOnlyRichTextBox()
        {
            ReadOnly = true;
            SetStyle(ControlStyles.Selectable, false);
        }
    
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            HideCaret(this.Handle);
        }
    
        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            HideCaret(this.Handle);
        }
    
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            HideCaret(this.Handle);
        }
    
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
                HideCaret(this.Handle);
        }
    }
    
    0 讨论(0)
  • 2020-12-31 15:58

    This works for me :

    public class RichTextLabel : RichTextBox
    {
        public RichTextLabel()
        {
            base.ReadOnly = true;
            base.BorderStyle = BorderStyle.None;
            base.TabStop = false;
            base.SetStyle(ControlStyles.Selectable, false);
            base.SetStyle(ControlStyles.UserMouse, true);
            base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    
            base.MouseEnter += delegate(object sender, EventArgs e)
            {
                this.Cursor = Cursors.Default;
            };
        }
    
        protected override void WndProc(ref Message m) {
            if (m.Msg == 0x204) return; // WM_RBUTTONDOWN
            if (m.Msg == 0x205) return; // WM_RBUTTONUP
            base.WndProc(ref m);
        }
    }
    

    I hope it helps

    0 讨论(0)
提交回复
热议问题