C__winForm自定义鼠标样式的两种方法

本文详细介绍了在C# WinForm中如何自定义鼠标样式,包括使用API和不使用API的方法,并提供了示例代码。通过这些方法,开发者可以实现彩色光标、动画光标和自定义光标的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

以前试过在C# WinForm中自定义鼠标样式,结果显示出来的鼠标变成单色。

    后来百度了下,原来要用API来做。

    首先引入两个命名空间

using System.Runtime.InteropServices; 
using System.Reflection;
    C# winForm自定义鼠标样式方法一

    导入API

[DllImport("user32.dll")] 
ublic static extern IntPtr LoadCursorFromFile(string fileName); 
 
[DllImport("user32.dll")] 
ublic static extern IntPtr SetCursor(IntPtr cursorHandle); 
 
[DllImport("user32.dll")] 
ublic static extern uint DestroyCursor(IntPtr cursorHandle); 
    接下来使用自己的鼠标样式

private void Form1_Load(object sender, EventArgs e) 
  { 
      Cursor myCursor = new Cursor(Cursor.Current.Handle); 
      IntPtr colorCursorHandle = LoadCursorFromFile("my.cur");//鼠标图标路径 
        myCursor.GetType().InvokeMember("handle", BindingFlags.Public | 
      BindingFlags.NonPublic | BindingFlags.Instance | 
      BindingFlags.SetField, null, myCursor, 
     new object[] { colorCursorHandle }); 
     this.Cursor = myCursor; 
  } 
    C# winForm自定义鼠标样式方法之二

    现在介绍另一种不用API方式的,鼠标样式只需要一张背景透明的图片就行了,png或gif格式的

    写个方法

public void SetCursor(Bitmap cursor, Point hotPoint) 
  { 
     int hotX = hotPoint.X; 
     int hotY = hotPoint.Y; 
      Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY); 
      Graphics g = Graphics.FromImage(myNewCursor); 
      g.Clear(Color.FromArgb(0, 0, 0, 0)); 
      g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width,  
      cursor.Height); 
 
     this.Cursor = new Cursor(myNewCursor.GetHicon()); 
     
      g.Dispose(); 
      myNewCursor.Dispose(); 
  } 
    在你想要改变鼠标样式的事件里头使用这个方法就行了

private void Form1_Load(object sender, EventArgs e) 
 { 
     Bitmap a=(Bitmap)Bitmap.FromFile("myCur.png"); 
     SetCursor(a, new Point(0, 0)); 
 }       //this.btnBack.FlatStyle = FlatStyle.Flat;    //set the button no frame 
//this.btnBack.FlatAppearance.BorderSize = 0; 
    C# winForm自定义鼠标样式举例:

    Test.cs

using System;  
using System.Drawing;  
using System.Windows.Forms;  
using System.Runtime.InteropServices;  
using System.Reflection;  
 
namespace ColorCursor  
{  
/// < summary>  
/// 本例子的作用:  
/// 在.NET中实现彩色光标,动画光标和自定义光标。  
/// < /summary>  
public class Form1 : System.Windows.Forms.Form  
{  
[DllImport("user32.dll")]  
public static extern IntPtr LoadCursorFromFile( string fileName );  
 
[DllImport("user32.dll")]  
public static extern IntPtr SetCursor( IntPtr cursorHandle );  
 
[DllImport("user32.dll")]  
public static extern uint DestroyCursor( IntPtr cursorHandle );  
 
 
[STAThread]  
static void Main()  
{  
Application.Run(new Form1());  
}  
 
public Form1()  
{  
this.Text = "欢迎光临【孟宪会之精彩世界】:http://dotnet.aspx.cc/";  
Cursor myCursor = new Cursor(Cursor.Current.Handle);  
//dinosau2.ani为windows自带的光标:  
IntPtr colorCursorHandle = LoadCursorFromFile(@"C:\WINNT\Cursors\dinosau2.ani" );  
myCursor.GetType().InvokeMember("handle",BindingFlags.Public |  
BindingFlags.NonPublic | BindingFlags.Instance |  
BindingFlags.SetField,null,myCursor,  
new object [] { colorCursorHandle } );  
this.Cursor = myCursor;  
}  
}  

access获取相对路径方法:
Environment.CurrentDirectory = Application.StartupPath;  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值