防止从只读RichTextBox中闪烁光标(IBeam)

mrt*_*ndi 10 richtextbox winforms

无论如何,RichRextBox只要文本框得到焦点,就会阻止只读的光标(IBeam)闪烁吗?

我试图阻止WM_SETFOCUS来自该消息,WndProc但它导致表单挂起.

if( m.Msg == 0x0007 ) return;
Run Code Online (Sandbox Code Playgroud)

Ani*_*oel 9

您需要使用Win32 API.这是你在VB中可以做的:

'API declares
Private Declare Function HideCaret Lib "user32" _
(ByVal hwnd As IntPtr) As Integer
Private Declare Function ShowCaret Lib "user32" _
(ByVal hwnd As IntPtr) As Integer
'hide the caret in myTextBox
Call HideCaret(myTextBox.Handle)
'show the caret back..
Call ShowCaret(myTextBox.Handle)
Run Code Online (Sandbox Code Playgroud)

并在C#

 [DllImport("user32.dll", EntryPoint = "ShowCaret")]
 public static extern long ShowCaret(IntPtr hwnd);
 [DllImport("user32.dll", EntryPoint = "HideCaret")]
 public static extern long HideCaret(IntPtr hwnd);
Run Code Online (Sandbox Code Playgroud)

然后拨打电话

   HideCaret(richtextbox.Handle)
Run Code Online (Sandbox Code Playgroud)

什么时候你想隐藏它.


Ped*_*o77 6

只是说Anirudh Goel的答案不起作用(至少在C#中).克拉仍然闪烁着:/

我在以下网址找到了解决方案:http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21896403.html

他的班级总是隐藏插入符号,这里是一个改进的插入符号,所以你可以选择隐藏或不隐藏插入符号.

如果你想隐藏不要忘记将MustHideCaret设置为true

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

namespace Lm
{
    public class RichTextBoxEx : RichTextBox
    {
        private readonly object mustHideCaretLocker = new object();

        private bool mustHideCaret;

        [DefaultValue(false)]
        public bool MustHideCaret
        {
            get
            {
                lock (this.mustHideCaretLocker)
                    return this.mustHideCaret;
            }
            set
            {
                TabStop = false;
                if (value)
                    SetHideCaret();
                else
                    SetShowCaret();
            }
        }

        [DllImport("user32.dll")]
        private static extern int HideCaret(IntPtr hwnd);
        [DllImport("user32.dll", EntryPoint = "ShowCaret")]
        public static extern long ShowCaret(IntPtr hwnd);

        public RichTextBoxEx()
        {
        }

        private void SetHideCaret()
        {
            MouseDown += new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
            MouseUp += new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
            Resize += new EventHandler(ReadOnlyRichTextBox_Resize);
            HideCaret(Handle);
            lock (this.mustHideCaretLocker)
                this.mustHideCaret = true;
        }

        private void SetShowCaret()
        {
            try
            {
                MouseDown -= new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
                MouseUp -= new MouseEventHandler(ReadOnlyRichTextBox_Mouse);
                Resize -= new EventHandler(ReadOnlyRichTextBox_Resize);
            }
            catch
            {
            }
            ShowCaret(Handle);
            lock (this.mustHideCaretLocker)
                this.mustHideCaret = false;
        }

        protected override void OnGotFocus(EventArgs e)
        {
            if (MustHideCaret)
                HideCaret(Handle);
        }

        protected override void OnEnter(EventArgs e)
        {
            if (MustHideCaret)
                HideCaret(Handle);
        }

        private void ReadOnlyRichTextBox_Mouse(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            HideCaret(Handle);
        }

        private void ReadOnlyRichTextBox_Resize(object sender, System.EventArgs e)
        {
            HideCaret(Handle);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


ChR*_*NoN 5

更简单的方法:将此事件附加到RichTextBox的Enter事件:

  private void Control_Enter(object sender, EventArgs e) {
    ActiveControl = null;
  }
Run Code Online (Sandbox Code Playgroud)