Domanda

I am using a custom cursor named hand2.cur in my C#-WPF application. I have added the cursor to a folder named Images which has all the images that I use in my application. However I've realized that I cannot add relative path to use my custom cursor as:

    Cursor newCur = new Cursor("Images\\hand2.cur");
    window.Cursor = newCur;

So I used this:

    string absolute = System.IO.Path.GetFullPath("hand2.cur");
    Cursor newCur = new Cursor(absolute);
    window.Cursor = newCur;

This tries to find the hand2.cur file in the \bin\Release folder. So I added the file there and I got it working.

But the problem is, if I Publish this application and use it on a different computer, it does not work. Now the problem is with the cursor file path, because if I deploy it after commenting those 3 lines, it works correctly. So what do I do to rectify this problem?

I am using other images from the Image folder in my XAML code and they seem to port fine. But then again my knowledge of WPF is limited so if anyone has any ideas, that would help.

EDIT: I have added my Images folder to the project. I have also set the Build Action of the cursor file hand2.cur to Embedded Resource. However when I use the following two lines, I get an XAMLParseException.

    System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(new Uri("pack://application:,,,/Slideshow;component/Images/hand2.cur"));
        window.Cursor = new System.Windows.Input.Cursor(info.Stream); 

The Inner Exception field when I view the details of the error reads: {"Cannot locate resource 'images/hand2.cur'."}

È stato utile?

Soluzione

You could make the cursor a resource in your app/assembly and then use GetResourceStream with the pack Uri to the resources location. Pass the Stream of the StreamResourceInfo to the ctor of the Cursor. e.g.

var info = Application.GetResourceStream(new Uri("pack://application:,,,/Images/hand2.cur"));
var cursor = new Cursor(info.Stream);

Altri suggerimenti

I've got this working after I added the cursor file hand2.cur to my Resource1.resx resource file. Then I used the following statement in my code:

window.Cursor = new Cursor(new System.IO.MemoryStream(MyNameSpace.Resource1.hand2));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top