WPF 和自定义游标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27734005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:49:04  来源:igfitidea点击:

WPF and custom cursors

c#wpfcursor

提问by PuerNoctis

I want to set a custom cursor within my WPF application. Originally I had a .png file, which i converted to .ico, but since I didn't find any way to set an .ico file as a cursor in WPF, I tried to do this with a proper .cur file.

我想在我的 WPF 应用程序中设置一个自定义光标。最初我有一个 .png 文件,我将其转换为 .ico,但由于我没有找到任何方法将 .ico 文件设置为 WPF 中的光标,我尝试使用适当的 .cur 文件来执行此操作。

I have created such an .cur file using Visual Studio 2013 (New Item -> Cursor File). The cursor is a coloured 24-bit image and its build-type is "Resource".

我使用 Visual Studio 2013(新项目 -> 光标文件)创建了这样一个 .cur 文件。光标是一个彩色的 24 位图像,它的构建类型是“资源”。

I accquire the resource stream with this:

我通过以下方式获取资源流:

var myCur = Application.GetResourceStream(new Uri("pack://application:,,,/mycur.cur")).Stream;

This code is able to retrieve the stream, so myCuris NOTnull aferwards.

这个代码能够检索流,所以myCur不是空aferwards。

When trying to create the cursor with

尝试创建游标时

var cursor = new System.Windows.Input.Cursor(myCur);

the default cursor Cursors.Noneis returned and not my custom cursor. So there seems to be a problem with that.

Cursors.None返回默认光标,而不是我的自定义光标。所以这似乎有问题。

Can anybody tell me why the .ctor has problems with my cursor stream? The file was created using VS2013 itself, so I'd assume that the .cur file is correctly formatted. Alternatively: If someone knows how to load a .ico file as a cursor in WPF, I'd be very happy and very grateful.

谁能告诉我为什么 .ctor 的光标流有问题?该文件是使用 VS2013 本身创建的,因此我假设 .cur 文件的格式正确。或者:如果有人知道如何在 WPF 中加载 .ico 文件作为光标,我会非常高兴和非常感激。

EDIT: Just tried the same with a fresh new .cur file from VS2013 (8bpp) in case adding a new palette has screwed the image format. Same result. The .ctor of System.Windows.Input.Cursorcan't even create a proper cursor from the 'fresh' cursor file.

编辑:刚刚尝试使用来自 VS2013 (8bpp) 的全新 .cur 文件,以防添加新调色板破坏了图像格式。结果一样。.ctorSystem.Windows.Input.Cursor甚至无法从“新”游标文件中创建正确的游标。

采纳答案by jt000

Note:Possible dupe of Custom cursor in WPF?

注意:WPF 中自定义光标可能被欺骗了

Essentially you have to use the win32 method CreateIconIndirect

基本上你必须使用 win32 方法 CreateIconIndirect

// FROM THE ABOVE LINK
public class CursorHelper
{
    private struct IconInfo
    {
        public bool fIcon;
        public int xHotspot;
        public int yHotspot;
        public IntPtr hbmMask;
        public IntPtr hbmColor;
    }

    [DllImport("user32.dll")]
    private static extern IntPtr CreateIconIndirect(ref IconInfo icon);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);


    public static Cursor CreateCursor(System.Drawing.Bitmap bmp, int xHotSpot, int yHotSpot)
    {
        IconInfo tmp = new IconInfo();
        GetIconInfo(bmp.GetHicon(), ref tmp);
        tmp.xHotspot = xHotSpot;
        tmp.yHotspot = yHotSpot;
        tmp.fIcon = false;

        IntPtr ptr = CreateIconIndirect(ref tmp);
        SafeFileHandle handle = new SafeFileHandle(ptr, true);
        return CursorInteropHelper.Create(handle);
    }
}

回答by sciencectn

This is exactly what I did and it seems to be working fine. I just added this to an "Images" folder under my project in Visual Studio 2013. Maybe it can't resolve your URI?

这正是我所做的,它似乎工作正常。我刚刚将它添加到 Visual Studio 2013 项目下的“Images”文件夹中。也许它无法解析您的 URI?

    Cursor paintBrush = new Cursor(
        Application.GetResourceStream(new Uri("Images/paintbrush.cur", UriKind.Relative)).Stream
        );

Sample cursor (worked for me): http://www.rw-designer.com/cursor-detail/67894

示例光标(对我有用):http: //www.rw-designer.com/cursor-detail/67894