views:

158

answers:

2

I'm creating a WinForms control in C# (using VS2008, .net 3.5) which allows text input. I've imported the necessary Win32 API functions from User32.dll for displaying the normal Windows caret and these are all working fine, but it's not displaying exactly how I'd like it.

Text is displayed on the control with a blank border and I use Graphics.SetClip() to leave this margin clear. I want the caret to be clipped to the same region, but since I don't paint it and there's no obvious API function to set a clipping region, I can't see any way of doing this. Have I missed anything obvious?

The caret is clipped inside the control in which it is drawn. I'm therefore aware that one solution could be to place the text in a separate sub-control with no border. However, if there's a simpler way than redesigning this part of the control, I'd like to look for that first.

Thanks in advance for any help!

A: 

So, have I understood correctly that your problem is that the default caret is 'bleeding' into your margin area?

I think your best option is to place the text on a secondary or sub-control as you mentioned. Two other options that you could consider, which might fit your requirements

1- Use CreateCaret to create a smaller cursor the fits into you clipped region. Of course you would still need to make sure that you do not position the caret in the border space.

[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private extern static bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);

// Create a caret which is 2 pixels wide by 8 high
CreateCaret(theControl.Handle, IntPtr.Zero, 2, 8);

2- Another option which is probably less usefull, but it will clip the caret, so I will mention it. Use Control.Region to clip the entire window to the clipping area you require. Of course this will also clip your border area which might or might not be a problem for you. Just depends if the background is of such a nature that it does not intrude on the border, basicall the border will be transparent.

But at the end of the day I think you option of using a sub-control would provide the most control.

Chris Taylor
Thanks for your suggestions. I'm thinking the sub-control will be the neatest option at the moment too.
Bob Sammers
A: 

How about defining the area you currently clip with SetClip() as the windows true client area? (by overriding WM_NCCALCSIZE).

Alex K.
This is an interesting idea. But I can't make out from the documentation I've read exactly what output from this function is used for. For example, if I exclude the part of the control on which the scrollbar is located, will it still be redrawn when necessary?
Bob Sammers
Alex K.