标签:des winform http 使用 width os
Windows 提供了一套对输入光标进行控制的API, 包括:CreateCaret,SetCaretPos,DestroyCaret,ShowCaret,HideCaret。这些API的定义如下:
[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();
上面的 CreateCaret 中的参数以此为
我们下面举个例子,假设:我们有个输入框textBox2,让这个输入的框的光标变成黑色的小块
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>
/// 自定义输入光标的演示
/// 作者: 三角猫
/// 网址: http://www.zu14.cn/
/// 转载请保留此信息
/// </summary>
public partial class Form1 : Form
{
[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();
public Form1()
{
InitializeComponent();
//为输入框绑定光标变化的处理事件
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, 10, textBox2.Height);
ShowCaret(textBox2.Handle);
}
}
}
C#/VB.NET Winform程序自定义输入光标,布布扣,bubuko.com
标签:des winform http 使用 width os
原文地址:http://www.cnblogs.com/qingtianhua/p/3818090.html