Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#Windows应用程序中使用自定义彩色光标_C#_Custom Controls_Cursors - Fatal编程技术网

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

在C#Windows应用程序中使用自定义彩色光标,c#,custom-controls,cursors,C#,Custom Controls,Cursors,我正在开发一个SDG(单显示群件)应用程序,为此,我需要为单个窗口使用多个游标(使用不同颜色中最简单的一种)。我了解到,使用C#可以只使用黑白光标,但这并不能解决我的问题。您可以从文件动态加载光标,如下所示: var myCursor = new Cursor("myCursor.cur"); myControl.Cursor = myCursor; 加载后,您可以设置任何控件的光标,如下所示: var myCursor = new Cursor("myCursor.cur"); myCo

我正在开发一个SDG(单显示群件)应用程序,为此,我需要为单个窗口使用多个游标(使用不同颜色中最简单的一种)。我了解到,使用C#可以只使用黑白光标,但这并不能解决我的问题。

您可以从文件动态加载光标,如下所示:

var myCursor = new Cursor("myCursor.cur");
myControl.Cursor = myCursor;
加载后,您可以设置任何控件的光标,如下所示:

var myCursor = new Cursor("myCursor.cur");
myControl.Cursor = myCursor;
游标还接受流作为构造函数参数。这意味着您可以从应用程序中嵌入的资源加载,而不是从文件系统加载

Windows不允许有多个光标,但可以在控件上绘制多个光标。可以使用光标对象的
绘制
方法,如下所示:

myCursor.Draw(g, new Rectangle(...));
如果您使用TCP/IP在客户端之间发送光标数据,那么这就足够了

但是,也有一些应用程序在一台PC上支持多个输入(例如,),因此,您看到的是.NET framework不支持的内容


您可能需要查看PInvoking的一些USB呼叫。(我在这里没有太多的经验,所以我无法理解。)

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:\windows\cursors\aero_busy.ani");

我还尝试了一些不同的方法,似乎可以使用不同颜色的光标,但这段代码的唯一问题是鼠标光标的热点坐标不精确,即光标稍微向右移动。但这可以通过考虑代码中的偏移量来解决

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace MID
{    
    public partial class CustomCursor : Form
    {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr LoadCursorFromFile(string filename);

        public CustomCursor()
        {
            InitializeComponent();

            Bitmap bmp = (Bitmap)Bitmap.FromFile("Path of the cursor file saved as .bmp");
            bmp.MakeTransparent(Color.Black);
            IntPtr ptr1 = blue.GetHicon();

            Cursor cur = new Cursor(ptr1);
            this.Cursor = cur;

        }
    }
}

这个帖子很老了,但它是谷歌最早的热门话题之一,下面是VS2019的答案:

someControl.Cursor = new Cursor(Properties.Resources.somePNG.GetHicon());
您应该添加“somePNG.png”作为一个项目资源,它具有您喜欢的任何透明度


希望它能在2020年帮助某人。

唯一的问题是热点将位于文件的中心。因此,要么:

将文件设置为两倍宽和两倍高,并将图标放置在右下象限

使用此复杂代码调整热点:

彩色光标工作正常。你是怎么发现你只能使用黑白光标的?Windows会允许你有一个以上的光标吗?我曾经需要动态创建光标。这导致了一些奇怪的问题,特别是因为半透明度会与黑色混合,使光标太暗。最后,我在SO社区的帮助下解决了这个问题,整个解决方案显示在这里:@Tim-我在一个C#论坛上读到,使用默认游标类,只能使用黑白游标(甚至不能使用灰度游标)。这是否回答了你的问题?谢谢John,但我确实尝试过这个方法,但对我来说不起作用,对于我的windows应用程序上的多个鼠标控件,基本上我使用的是一个名为SDGToolkit的C#API,它处理所有低级的东西。