Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I assing the Caret to a Control in WPF

Need to make a new WPF control and give the Caret to it. In WINFORMS or previos Windows UI it was easy using the WIN API Caret functions, but now in WPF we don't have hwnd for each control so... is there a way to do it?

like image 790
jmayor Avatar asked Oct 15 '22 14:10

jmayor


2 Answers

In WPF, the caret is actually CaretElement, an internal FrameworkElement used for rendering selection blocks and the caret.

If you do not inherit from some sort of control which already offers caret support and try to manipulate it, sadly, you could be facing the need to implement it from scratch.

EDIT

The CaretElement is internal to the framework and yes, not documented.

It is mentioned here, for example.

A closed source custom implementation exists in a commercial package.

like image 127
Kenan E. K. Avatar answered Oct 20 '22 00:10

Kenan E. K.


Caret in WPF is an just another animation, no special API for that. Draw a line, and change its opacity with DoubleAnimation.

If you have closer look at WPF TextBox, the caret doesn't even do 'pixel inversion', it's just gray line drawn over the letter.

Potentially you can do pixel inversion in WPF, by implementing a pixel shader effect. Not worth it probably. VS2010 beta doesn't do inversion for text caret.

Apart from TextBox/RichTextBox there are 3 other editing components you can find:

  • Expression Blend code editor
  • Visual Studio 2010 code editor
  • SharpDevelop 4 code editor
like image 29
Oleg Mihailik Avatar answered Oct 20 '22 00:10

Oleg Mihailik