Drawing a blinking caret in Winforms

2019-02-20 05:23发布

问题:

I'm developing a custom control, which most resembles a text area. I am drawing text, which works correctly, and accepting input which also works.. But I (the user) am left guessing where the caret is while I type, since I'm doing everything manually. How do I draw a blinking caret to show where I am currently typing? Is there a standard way to do this?

回答1:

I think that, annoyingly, there is no managed API for Carets. You must, therefore, either PInvoke to the Win32 functions for carets, or, implement that functionality yourself (i.e. painting and hiding a blinking caret, when and only when your control has the input focus).

See Custom Caret for WinForms TextBox for example.