Carriage trimming in C # program
I am creating a WinForms control in C # (using VS2008, .net 3.5) that allows text input. I imported the required Win32 API functions from User32.dll to display the normal Windows caret and they all work fine but don't display exactly the way I like it.
The text is displayed in the control with an empty border, and I use Graphics.SetClip () to leave this margin transparent. I want the caret to be anchored to the same region, but since I am not drawing it and there is no obvious API function to set the clipping region, I see no way to do this. Am I missing something obvious?
The caret is clipped inside the control in which it is drawn. So I know one solution might be to put the text in a separate sub-control without a border. However, if there is an easier way than redesigning this part of the control, I would like to find this first.
Thanks in advance for your help!
a source to share
So, did I understand correctly that your problem is that the default caret "bleeds out" in your field area?
I think your best option is to place the text on a secondary or sub-control as you mentioned. Two other options you might consider that might suit your requirements
1- Use CreateCaret to create a smaller cursor that fits you in the cropped area. Of course, you still need to make sure that you are not positioning the caret in 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 useful, but it will close the caret, which is why I mentioned that. Use Control.Region to copy the entire window to the desired clipping region. Of course, this will also close your border area, which may or may not be a problem for you. It just depends if the background is of such a nature that it does not invade the border, the base border will be transparent.
But at the end of the day, I think you can use additional controls.
a source to share
How do you define the realm you are currently clicking SetClip()
with as the realm of Windows client names? (by overriding WM_NCCALCSIZE
).
a source to share