Winform应用程序如何使用自定义的鼠标图片
#代码知识 发布时间: 2026-01-12
首先,建立图片与鼠标的对应关系。
class MouseStyle
{
[DllImport("user32.dll")]
public static extern IntPtr SetCursor(IntPtr cursorHandle);
static MouseStyle()
{
InitMouseStyle();
}
private static void InitMouseStyle()
{
if (Hand == null)
{
Hand = SetCursor("Image//Hand.png");
}
if (Arrow == null)
{
Arrow = SetCursor("Image//Arrow.png");
}
}
/// <summary>
/// 鼠标手型样式
/// </summary>
public static Cursor Hand = null;
/// <summary>
/// 鼠标指针样式
/// </summary>
public static Cursor Arrow = null;
/// <summary>
/// 设置鼠标样式
/// </summary>
/// <param name="fileName">自定义的鼠标样式文件</param>
/// <returns>鼠标样式</returns>
private static Cursor SetCursor(string fileName)
{
//文件的绝对路径,在debug下
var path = System.IO.Path.GetFullPath(fileName) ;
//画图
Bitmap bit = (Bitmap)Bitmap.FromFile(path, true);
Bitmap myNewCursor = new Bitmap(bit.Width, bit.Height);
Graphics g = Graphics.FromImage(myNewCursor);
g.Clear(Color.FromArgb(0, 0, 0, 0));
//箭头和手型有点不一样
if (System.IO.Path.GetFileName(fileName).Equals("Hand.png"))
{
g.DrawImage(bit, bit.Width / 2 - 15, bit.Height / 2, bit.Width / 2, bit.Height / 2);
}
else
{
g.DrawImage(bit, bit.Width / 2 - 15, bit.Height / 2, bit.Width / 2, bit.Height / 2);
}
Cursor cursor;
//获取图片的句柄
try
{
cursor = new Cursor(myNewCursor.GetHicon());
}
catch
{
cursor = new Cursor(Icon.FromHandle(myNewCursor.GetHicon()).Handle);
}
//释放资源
g.Dispose();
return cursor;
}
}
如果是鼠标文件.cur结尾,可以直接使用。
法1、在每一个窗体中单独修改其中的鼠标外观,这样鼠标离开自己的程序后,也会恢复到系统默认的鼠标样式。
在上述类中,添加代码:
/// <summary>
/// 设置鼠标样式
/// </summary>
/// <param name="col">控件</param>
public static void SetMouseStyle(Control col)
{
InitMouseStyle();
//设置窗体鼠标为箭头
if (col is Form)
{
((Form)col).Cursor = Arrow;
}
//遍历控件,如果控件是箭头或默认,就改为自定义的箭头
//如果是手型就改为自定义的手型
foreach (Control con in col.Controls)
{
if (con.Cursor == Cursors.Hand)
{
con.Cursor = Hand;
}
if (con.Cursor == Cursors.Arrow || con.Cursor == Cursors.Default)
{
con.Cursor = Arrow;
}
//递归遍历
SetMouseStyle((Control)con);
}
}
然后在所有窗体中,均调用SetMouseStyle方法,传入窗体自身
法2、修改系统鼠标,待程序退出时,还原系统鼠标。首先添加代码,调用window的API:
[DllImport("User32.DLL")]
public static extern bool SetSystemCursor(IntPtr hcur, uint id);
public const uint OCR_NORMAL = 32512;
public const uint OCR_HAND = 32649;
public const uint OCR_IBEAM = 32513;
//速查 https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-setsystemcursor?redirectedfrom=MSDN
//OCR_APPSTARTING:标准箭头和小的沙漏;32650
//OCR_NORMAL:标准箭头 32512
//OCR_CROSS:交叉十字线光标: 32515
//OCR_HAND:手的形状(WindowsNT5.0和以后版本) 32649
//OCR_HELP:箭头和向东标记; 32651
//OCR_IBEAM:I形梁; 32513
//OCR_NO:斜的圆 32648
//OCR_SIZEALL:四个方位的箭头分别指向北、南、东、西 32646
//OCR_SIZENESEW:双箭头分别指向东北和西南; 32643
//OCR_SIZENS:双箭头,分别指向北和南 32645
//OCR_SIZENWSE:双箭头分别指向西北和东南; 32642
//OCR_SIZEWE:双箭头分别指向西和东 32644
//OCR_UP:垂直箭头: 32516
//OCR_WAIT:32514 沙漏返回值:如果成功,返回非零值;如果失败,返回值为零。
[DllImport("User32.DLL")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam,
IntPtr pvParam, uint fWinIni);
public const uint SPI_SETCURSORS = 87;
public const uint SPIF_SENDWININICHANGE = 2;
程序启动和退出时分别调用设置方法和恢复方法:
private void button1_Click(object sender, EventArgs e)
{
//设置
SetSystemCursor(Cursors.WaitCursor.CopyHandle(), OCR_NORMAL);
SetSystemCursor(Cursors.WaitCursor.CopyHandle(), OCR_IBEAM);
//..可以根据情况加
}
private void button2_Click(object sender, EventArgs e)
{
//恢复
SystemParametersInfo(SPI_SETCURSORS, 0, IntPtr.Zero, SPIF_SENDWININICHANGE);
}
以上就是Winform应用程序如何使用自定义的鼠标图片的详细内容,更多关于Winform 自定义鼠标图片的资料请关注其它相关文章!
代码知识SEO上一篇 : SpringBoot上传图片的示例
下一篇 : 解决element-ui的下拉框有值却无法选中的情况
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!