C#/vb.net WinForm Program Custom input cursor

Source: Internet
Author: User

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); }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.