How to show cursor in unfocused WinForms TextBox/R

2019-02-18 21:16发布

问题:

I need to show cursor in RichTextBox control in WinForms application even when it's not in focus. How can I do this? I found only the way for WPF ( How to keep WPF TextBox selection when not focused?)

回答1:

You can use WinAPI ..

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

and call ShowCaret whenever you want



回答2:

You can't set focus to the two or more UI at same time however you can preserve the selection by setting HideSelection=false.



回答3:

I don't know what you are trying to achieve and how much is it really useful. But if it is just for visual purpose, write some thing like '|' in it. Its a bad, weird, awkward way or what ever you call it, for visual purpose it may work.

    public void blink()
    {
        while (true)
        {
            textBox1.Text = "|";
            Thread.Sleep(200);
            textBox1.Text = "";
            Thread.Sleep(200);
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Thread t1 = new Thread(new ThreadStart(blink));
        t1.Start();
    }

I am not sure if I am giving is what you are asking, but to get accurate answer, you have to expose your need of this requirement.

Hope it helps.