views:

494

answers:

1

I am rewriting a windows forms application (updated framework, rewrote the logic, etc.) and there is one thing that I just can't figure out how they did. Textboxes in the original application had a thick blinking cursor like so: http://screencast.com/t/8QYUcjuh3n

For the life of me I can't figure how to do this. Please help?

+1  A: 

You can do it with pinvoke CreateCaret

The example uses a Winform with a button (button1) and textbox (textBox1).

Add this using directive:

using System.Runtime.InteropServices;

Add these declarations:

[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);

Add this code to the button click event:

  // Thickness is set where I have 10.
  CreateCaret(textBox1.Handle, IntPtr.Zero, 10, textBox1.Height);
  ShowCaret(textBox1.Handle);

When you click the button you'll get a thicker cursor.

There's also a discussion of this here.

Jay Riggs
awesome thanks...now that I know what to search for I see how they invoked this. Actually...this is a dailyWTF winner for sure
George Mauer
No problem. Unfortunately for me my WTFs are more than daily!
Jay Riggs
Actually, I do have a problem with this, it seems like it causes the control handle and the form to be created on different threads. form.Show() therefore causes a cross threading exception any ideas?
George Mauer
Hmm, I'm not sure why that would be. I didn't see the problem when I tried the code but the example I constructed was very simple.I wonder if you'd have more luck with the code linked below. As you'll see, the class presented extends the TextBox by adding the same API calls but in a 'complete' solution. (I admit though I haven't tried the code in a real program). Can't create links in comments, so here it is separate:http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/d916db72-7744-44b7-ba66-7610927426ea?ppud=4Good luck!
Jay Riggs