改变控件获得焦点时光标的颜色
///
/// 改变光标的颜色
///
///
///
///
///
///
[DllImport("user32.dll ")]
static extern Int32 CreateCaret(Int32 hwnd, Int32 hBitmap, Int32 nWidth, Int32 nHeight);
[DllImport("user32.dll ")]
static extern Int32 ShowCaret(Int32 hwnd);
[DllImport("user32.dll ")]
static extern Int32 DestroyCaret();
public static void ChangeCursorColor(Control control)
{
System.Drawing.Bitmap img = new System.Drawing.Bitmap(1, 12);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.FromArgb(0, 255, 255)), 0, 0, 3, 12);
control.Focus();
DestroyCaret();
CreateCaret(control.Handle.ToInt32(), img.GetHbitmap().ToInt32(), img.Width, img.Height);
ShowCaret(control.Handle.ToInt32());
}
注:extern常用来声明在外部的非托管代码,也可用来引用外部别名。
托管代码与非托管代码:前者指在运行库的控制下执行的代码,后者指在非运行库下控制的代码,如:COM组件、ActiveX接口、Win32 API函数等。
控件获得焦点时改变光标的颜色
最新推荐文章于 2020-12-24 23:16:10 发布
这篇博客介绍了如何在Windows应用程序中,当控件获取焦点时改变光标的颜色。通过使用P/Invoke调用`user32.dll`的API函数`CreateCaret`、`ShowCaret`和`DestroyCaret`,并创建一个自定义的位图来实现光标的颜色改变。代码示例展示了如何在`Control`获得焦点时,绘制一个蓝色的矩形作为新的光标,并设置为当前控件的光标。
1915

被折叠的 条评论
为什么被折叠?



