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!
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.
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.