坐落在WPF定义光标图像热点(Set Custom Cursor Image Hotspot in

2019-07-31 15:06发布

我想在光标使用自定义图像和我用这些代码做了。

public static class cursorHelper
{

    public static Cursor vertical = new Cursor(Application.GetResourceStream(getFromResource("PenCADwpf", "Images/cursors/Vertical.ico")).Stream);
    public static Cursor horizontal = new Cursor(Application.GetResourceStream(getFromResource("PenCADwpf", "Images/cursors/Horizontal.ico")).Stream);
    public static Uri getFromResource(string psAssemblyName, string psResourceName)
    {
        Uri oUri = new Uri("pack://application:,,,/" + psAssemblyName + ";component/" + psResourceName, UriKind.RelativeOrAbsolute);
        return oUri;
    }
    public static ImageSource getImageSourceFromResource(string psAssemblyName, string psResourceName)
    {
        Uri oUri = getFromResource(psAssemblyName, psResourceName);

        return BitmapFrame.Create(oUri);
    }

}

并使用代码

    private void btnVerticalMullion_Click(object sender, RoutedEventArgs e)
    {
        this.Cursor = cursorHelper.vertical;
    }

我的问题是光标的热点是左下点。 我需要将其更改为图像的0,0(左上角)点。 任何机构可以帮助我吗? 提前致谢,

Answer 1:

这是因为你使用.ico文件,而不是.CUR文件的数据的Cursor类。

虽然.ICO和.CUR文件格式都差不多,.ICO格式不包含热点信息。

  • http://en.wikipedia.org/wiki/ICO_(file_format)#cite_note-11

你有两个选择:

  • 转换您的.ico文件来.CUR文件和嵌入这些资源作为代替

    做到这一点通过使用转换工具,它可以在网络上找到,
    要么
    Visual Studio创建一个新的文件名为.CUR,然后复制并从您的.ico文件粘贴数据。

  • 让他们为.ico文件,但黑客的数据,以便传递给Cursor类时遵循CUR格式。

下面是一些示例代码修改ICO流把它变成了CUR格式。

在这个例子中我用含有单个32X32X4bit BMP图像ICO文件测试它,我想光标到具有(15,15)的热点。

此代码只是为了让你开始,如果你沿着这条路走下去......它需要更多的代码来处理错误,并处理包含多个图标图像ICO文件(即,如果多个条目)等的能力

你也可以使用一个BinaryWriter更自然地处理数据,例如写出来,通过使用使用2个字节(即超过255)热点COORDS Write(UInt16)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Resources;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Uri uri = new Uri("pack://application:,,,/test.ico");

            Stream iconstream = GetCURFromICO(uri, 15, 15 );

            Cursor cursor = new Cursor(iconstream);

            this.Cursor = cursor;
        }

        public static Stream GetCursorFromICO(Uri uri, byte hotspotx, byte hotspoty)
        {
            StreamResourceInfo sri = Application.GetResourceStream(uri);

            Stream s = sri.Stream;

            byte []buffer = new byte[s.Length];

            s.Read(buffer, 0, (int)s.Length);

            MemoryStream ms = new MemoryStream();

            buffer[2] = 2; // change to CUR file type
            buffer[10] = hotspotx;
            buffer[12] = hotspoty;

            ms.Write(buffer, 0, (int)s.Length);

            ms.Position = 0;

            return ms;
        }

        public static Stream GetCURFromICOAlternativeMethod(Uri uri, byte hotspotx, byte hotspoty)
        {
            StreamResourceInfo sri = Application.GetResourceStream(uri);

            Stream s = sri.Stream;

            byte []buffer = new byte[s.Length];

            MemoryStream ms = new MemoryStream();

            ms.WriteByte(0); // always 0
            ms.WriteByte(0);
            ms.WriteByte(2); // change file type to CUR
            ms.WriteByte(0);
            ms.WriteByte(1); // 1 icon in table
            ms.WriteByte(0);

            s.Position = 6; // skip over first 6 bytes in ICO as we just wrote

            s.Read(buffer, 0, 4);
            ms.Write(buffer, 0, 4);

            ms.WriteByte(hotspotx);
            ms.WriteByte(0);

            ms.WriteByte(hotspoty);
            ms.WriteByte(0);

            s.Position += 4; // skip 4 bytes as we just wrote our own

            int remaining = (int)s.Length - 14;

            s.Read(buffer, 0, remaining);
            ms.Write(buffer, 0, remaining);

            ms.Position = 0;

            return ms;
        }
    }
}

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>


文章来源: Set Custom Cursor Image Hotspot in WPF