Winform应用程序怎样使用自定义的鼠标图片
一只独行的猿 人气:0首先,建立图片与鼠标的对应关系。
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); }
加载全部内容