Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

caret position in custom TextBox inhereting from UserControl

22 views
Skip to first unread message

rac...@bluetang.co.uk

unread,
Nov 7, 2007, 5:35:40 PM11/7/07
to
Hi everyone,

I'm writing a custom TextBox that inherits from UserControl. I can't
use a standard TextBox, or inherit from it or TextBoxBase, as it needs
to support proper transparency, meaning both the background needs to
be transparent, and the foreground back colour (I'm finding that if I
set the background to transparent, and the foreground to black, text
is being rendered as black on white, which rather does away with the
transparency)

I need to draw,( in the OnPaint method) a caret at the correct
position, but can't really get it to work. Have tried computing the
position using graphics.MeasureCharacterRanges, but without luck

any help or tips would be appreciated

thanks in advance

rachel

Bob Powell [MVP]

unread,
Nov 10, 2007, 5:11:44 PM11/10/07
to
Win32 has a Caret API that can be used to "correctly" position the caret in
the control.

Here are the DLL imports you can use...

[DllImport("user32.dll")]

public static extern int CreateCaret(IntPtr hwnd, IntPtr hbm, int cx, int
cy);

[DllImport("user32.dll")]

public static extern int DestroyCaret();

[DllImport("user32.dll")]

public static extern int SetCaretPos(int x, int y);

[DllImport("user32.dll")]

public static extern int ShowCaret(IntPtr hwnd);

[DllImport("user32.dll")]

public static extern int HideCaret(IntPtr hwnd);

You still have to determine the text positions which you can do using
MeasureCharacterRanges.


--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


<rac...@bluetang.co.uk> wrote in message
news:1194474940.2...@d55g2000hsg.googlegroups.com...

0 new messages