You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows Forms .NET中全局修改系统光标图标?

实现Windows Forms .NET替换系统/程序光标

一、仅替换当前应用内的光标(适合新手入门)

这部分无需调用系统API,只用.NET自带的Cursor类即可实现,步骤简单:

  1. 准备光标文件

    • 准备.cur(静态光标)或.ani(动态光标)格式的自定义光标文件。
    • 将文件添加到Visual Studio项目中,右键文件→属性→设置“复制到输出目录”为如果较新则复制,确保程序运行时能读取到文件。
  2. 设置光标代码

    • 在窗体加载事件(Form_Load)中,设置整个窗体的默认光标:
      private void Form1_Load(object sender, EventArgs e)
      {
          // 加载自定义光标文件,路径为项目输出目录下的文件
          this.Cursor = new Cursor("custom.cur");
      }
      
    • 如果需要给特定控件(如按钮、文本框)单独设置光标,直接给控件的Cursor属性赋值:
      button1.Cursor = new Cursor("text_cursor.ani");
      

二、替换系统全局光标(需调用Win32 API)

如果要修改整个Windows系统的默认光标,需要通过P/Invoke调用系统原生API,步骤如下:

  1. 声明Win32 API函数
    在窗体类中添加以下API声明(需引用System.Runtime.InteropServices):

    using System.Runtime.InteropServices;
    
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        private static extern bool SetSystemCursor(IntPtr hcur, uint id);
    
        [DllImport("user32.dll")]
        private static extern IntPtr LoadCursorFromFile(string lpFileName);
    
        [DllImport("user32.dll")]
        private static extern bool DestroyCursor(IntPtr hCursor);
    
        [DllImport("user32.dll")]
        private static extern IntPtr LoadCursor(IntPtr hInstance, IntPtr lpCursorName);
    
        // 常用系统光标ID定义
        private const uint OCR_NORMAL = 32512; // 普通箭头光标
        private const uint OCR_IBEAM = 32513; // 文本输入光标
        private const uint OCR_WAIT = 32514; // 等待沙漏光标
        private const uint OCR_CROSS = 32515; // 十字光标
        // 更多光标ID可参考Win32官方文档
    
  2. 编写替换与恢复光标方法

    // 替换指定类型的系统光标
    private void SetGlobalCursor(string cursorFilePath, uint cursorTypeId)
    {
        // 加载自定义光标文件
        IntPtr customCursorPtr = LoadCursorFromFile(cursorFilePath);
        if (customCursorPtr != IntPtr.Zero)
        {
            // 设置系统光标
            SetSystemCursor(customCursorPtr, cursorTypeId);
            // 释放光标资源
            DestroyCursor(customCursorPtr);
        }
    }
    
    // 恢复指定类型的系统默认光标
    private void RestoreDefaultSystemCursor(uint cursorTypeId)
    {
        // 加载系统默认光标
        IntPtr defaultCursorPtr = LoadCursor(IntPtr.Zero, (IntPtr)cursorTypeId);
        if (defaultCursorPtr != IntPtr.Zero)
        {
            SetSystemCursor(defaultCursorPtr, cursorTypeId);
        }
    }
    
  3. 调用方法(示例)
    可以通过按钮点击事件触发替换或恢复操作:

    // 替换系统普通箭头光标
    private void btnReplaceArrow_Click(object sender, EventArgs e)
    {
        SetGlobalCursor("custom_arrow.cur", OCR_NORMAL);
    }
    
    // 恢复系统普通箭头光标
    private void btnRestoreArrow_Click(object sender, EventArgs e)
    {
        RestoreDefaultSystemCursor(OCR_NORMAL);
    }
    
  4. 注意事项

    • 全局替换系统光标需要管理员权限:右键项目→添加→新建项→选择“应用程序清单文件”,将<requestedExecutionLevel>节点修改为:
      <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      
    • 全局替换会影响整个系统,测试后记得恢复默认光标;若未恢复,重启Windows也会自动还原系统默认设置。

内容的提问来源于stack exchange,提问作者Frindow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 19:48:29