This article reprinted from really interesting Net (http://www.zu14.cn)
Triangle Cat Deltacat
Abstract: c#/vb.net WinForm Program custom input cursor implementation, we can call the windows provided by a set of input cursor control API to operate ...
Windows provides a set of APIs to control the input cursor, including: Createcaret,setcaretpos,destroycaret,showcaret,hidecaret. These APIs are defined as follows:
[DllImport ("user32.dll")]static extern bool CreateCaret (IntPtr hWnd, IntPtr hbitmap, int nwidth, int nheight); [DllImport ("user32.dll")]static extern bool ShowCaret (IntPtr hWnd); [DllImport ("User32.dll")]static extern bool HideCaret (IntPtr hWnd); [DllImport ("User32.dll")]static extern bool Setcaretpos (int x, int y); [DllImport ("user32.dll")]static extern bool Destroycaret ();
The parameters in the CreateCaret above are
- HWND: Handle to the control to customize the input cursor
- HBITMAP: If you use a picture as the input cursor, it is the handle to the picture, otherwise: 0 indicates a black cursor color, 1 indicates the use of a gray cursor color
- Nwidth: The width of the cursor
- Nheight: The height of the cursor
Let's take an example below, assuming that we have an input box textBox2 that makes the cursor of this input box turn into a small black block
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.text;using system.windows.forms;using system.runtime.interopservices;namespace CustomCaret{//< summary>///Custom input cursor///triangle CAT////http://www.zu14.cn///Website://Reprint Please keep this information///</summary> Public Partial class Form1:form {[DllImport ("User32.dll")] static extern bool CreateCaret (IntPtr hWnd, INTPT R hbitmap, int nwidth, int nheight); [DllImport ("User32.dll")] static extern bool ShowCaret (IntPtr hWnd); [DllImport ("User32.dll")] static extern bool HideCaret (IntPtr hWnd); [DllImport ("User32.dll")] static extern bool Setcaretpos (int x, int y); [DllImport ("User32.dll")] static extern bool Destroycaret (); Public Form1 () {InitializeComponent (); Handling events that bind cursor changes for the input box This.textBox2.GotFocus + = new EventHandler (tExtbox2_gotfocus); This.textBox2.LostFocus + = new EventHandler (textbox2_lostfocus); } void Textbox2_lostfocus (object sender, EventArgs e) {HideCaret (this.textBox2.Handle); Destroycaret (); } void Textbox2_gotfocus (object sender, EventArgs e) {CreateCaret (Textbox2.handle, IntPtr.Zero, Ten, Textbox2.height); ShowCaret (Textbox2.handle); } }}