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
/// <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)
{
}
}
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);
}
}
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