如何在未聚焦的 WinForms TextBox/RichTextBox 中显示光标?

发布于 2024-12-11 07:40:56 字数 215 浏览 0 评论 0原文

我需要在 WinForms 应用程序的 RichTextBox 控件中显示光标,即使它没有焦点。我该怎么做?我只找到了 WPF 的方法( 如何保持 WPF TextBox 选择什么时候注意力不集中?

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?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我乃一代侩神 2024-12-18 07:40:56

您可以使用 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);

并随时调用 ShowCaret

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

稚然 2024-12-18 07:40:56

您无法同时将焦点设置到两个或多个 UI,但可以通过设置 HideSelection=false 保留选择。

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

司马昭之心 2024-12-18 07:40:56

我不知道您想要实现什么目标以及它真正有用的程度有多少。但如果只是为了视觉目的,请写一些类似“|”的东西在其中。这是一种糟糕的、奇怪的、尴尬的方式,或者无论你怎么称呼它,出于视觉目的它可能会起作用。

    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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文