WPF 和自定义游标
WPF and custom cursors
我想在我的 WPF 应用程序中设置自定义光标。最初我有一个 .png 文件,我将其转换为 .ico,但由于我没有找到任何方法将 .ico 文件设置为 WPF 中的光标,我尝试使用适当的 .cur 文件来做到这一点。
我使用 Visual Studio 2013(新项目 -> 光标文件)创建了这样一个 .cur 文件。光标是一个彩色的 24 位图像,它的构建类型是 "Resource".
我用这个获取资源流:
var myCur = Application.GetResourceStream(new Uri("pack://application:,,,/mycur.cur")).Stream;
此代码能够检索流,因此 myCur
是 NOT null aferwards。
尝试使用
创建游标时
var cursor = new System.Windows.Input.Cursor(myCur);
返回默认光标 Cursors.None
而不是我的自定义光标。所以这似乎有问题。
谁能告诉我为什么 .ctor 对我的光标流有问题?该文件是使用 VS2013 本身创建的,因此我假设 .cur 文件的格式正确。或者:如果有人知道如何在 WPF 中加载 .ico 文件作为游标,我将非常高兴和感激。
编辑:刚刚尝试使用来自 VS2013 (8bpp) 的全新 .cur 文件进行同样的操作,以防添加新调色板破坏了图像格式。同样的结果。 System.Windows.Input.Cursor
的 .ctor 甚至无法从 'fresh' 游标文件创建合适的游标。
基本上你必须使用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);
}
}
这正是我所做的,而且似乎工作正常。我刚刚在 Visual Studio 2013 年将它添加到我的项目下的 "Images" 文件夹中。也许它无法解析您的 URI?
Cursor paintBrush = new Cursor(
Application.GetResourceStream(new Uri("Images/paintbrush.cur", UriKind.Relative)).Stream
);
我想在我的 WPF 应用程序中设置自定义光标。最初我有一个 .png 文件,我将其转换为 .ico,但由于我没有找到任何方法将 .ico 文件设置为 WPF 中的光标,我尝试使用适当的 .cur 文件来做到这一点。
我使用 Visual Studio 2013(新项目 -> 光标文件)创建了这样一个 .cur 文件。光标是一个彩色的 24 位图像,它的构建类型是 "Resource".
我用这个获取资源流:
var myCur = Application.GetResourceStream(new Uri("pack://application:,,,/mycur.cur")).Stream;
此代码能够检索流,因此 myCur
是 NOT null aferwards。
尝试使用
创建游标时var cursor = new System.Windows.Input.Cursor(myCur);
返回默认光标 Cursors.None
而不是我的自定义光标。所以这似乎有问题。
谁能告诉我为什么 .ctor 对我的光标流有问题?该文件是使用 VS2013 本身创建的,因此我假设 .cur 文件的格式正确。或者:如果有人知道如何在 WPF 中加载 .ico 文件作为游标,我将非常高兴和感激。
编辑:刚刚尝试使用来自 VS2013 (8bpp) 的全新 .cur 文件进行同样的操作,以防添加新调色板破坏了图像格式。同样的结果。 System.Windows.Input.Cursor
的 .ctor 甚至无法从 'fresh' 游标文件创建合适的游标。
基本上你必须使用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);
}
}
这正是我所做的,而且似乎工作正常。我刚刚在 Visual Studio 2013 年将它添加到我的项目下的 "Images" 文件夹中。也许它无法解析您的 URI?
Cursor paintBrush = new Cursor(
Application.GetResourceStream(new Uri("Images/paintbrush.cur", UriKind.Relative)).Stream
);