加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#Windows应用程序中使用自定义彩色光标

发布时间:2020-12-16 00:15:04 所属栏目:百科 来源:网络整理
导读:我正在开发一个SDG(单显示器组件)应用程序,为此我需要多个游标(最简单的不同颜色)用于单个窗口.我开始知道使用C#你可以使用黑白游标,但这并不能解决我的问题.所以请帮我解决这个问题. 提前致谢. 解决方法 Cursor类做得很差.出于某种神秘的原因,它使用传统的C
我正在开发一个SDG(单显示器组件)应用程序,为此我需要多个游标(最简单的不同颜色)用于单个窗口.我开始知道使用C#你可以使用黑白游标,但这并不能解决我的问题.所以请帮我解决这个问题.

提前致谢.

解决方法

Cursor类做得很差.出于某种神秘的原因,它使用传统的COM接口(IPicture),该接口不支持彩色和动画光标.可用一些相当丑陋的肘部油脂固定:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;

static class NativeMethods {
    public static Cursor LoadCustomCursor(string path) {
        IntPtr hCurs = LoadCursorFromFile(path);
        if (hCurs == IntPtr.Zero) throw new Win32Exception();
        var curs = new Cursor(hCurs);
        // Note: force the cursor to own the handle so it gets released properly
        var fi = typeof(Cursor).GetField("ownHandle",BindingFlags.NonPublic | BindingFlags.Instance);
        fi.SetValue(curs,true);
        return curs;
    }
    [DllImport("user32.dll",SetLastError = true,CharSet = CharSet.Auto)]
    private static extern IntPtr LoadCursorFromFile(string path);
}

样品用法:

this.Cursor = NativeMethods.LoadCustomCursor(@"c:windowscursorsaero_busy.ani");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读