How to show cursor in unfocused WinForms TextBox/RichTextBox?

折月煮酒 提交于 2019-12-01 05:40:26

问题


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.



来源:https://stackoverflow.com/questions/7873563/how-to-show-cursor-in-unfocused-winforms-textbox-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!