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; //调用API,需要引用这个命名空间 namespace WindowsFormsApplication2 { public partial class Form1 : Form { /// <summary> /// 两个API函数,其作用是:指定窗口的指定位置插入插入符. /// </summary> /// <param name="hwnd">窗口句柄</param> /// <param name="hBitmap">插入符的样式,可以为NULL</param> /// <param name="nWidth">插入符的宽度</param> /// <param name="nHeight">插入符的高度</param> /// <returns></returns> [DllImport("user32.dll", EntryPoint = "CreateCaret")] static extern int CreateCaret(IntPtr hwnd, int hBitmap, int nWidth, int nHeight); /// <summary> /// 显示插入符 /// </summary> /// <param name="hWnd">窗口句柄</param> /// <returns></returns> [DllImport("user32.dll", EntryPoint = "ShowCaret")] static extern bool ShowCaret(IntPtr hWnd); public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { CreateCaret(this.Handle, 1, 20, 40); ShowCaret(this.Handle); } } }