-2

I want to define my own Cursor as the Current Cursor in my WPF Application, but wenn I try to create a new Cursor Object from my .cur File, I get an Error.

My Code is

     private void NewFile()
    {  ...

      iEvent_dragdrop = (HTMLDocumentEvents2_Event)doc;
      iEvent_dragdrop.ondragstart += new HTMLDocumentEvents2_ondragstartEventHandler(IEvent_ondragstart);         
    }

     private bool IEvent_ondragstart(IHTMLEventObj pEvtObj)
    {
        x_start = pEvtObj.x;           // Read position of Mouse
        y_start = pEvtObj.y;

        ....

        if (File.Exists("MyCursor.cur"))
        {
            System.Windows.Forms.Cursor myCursor = new System.Windows.Forms.Cursor(GetType(), "MyCursor.cur");
            System.Windows.Forms.Cursor.Current = myCursor;
            //System.Windows.Forms.MessageBox.Show("File exist");
        }
        else System.Windows.Forms.MessageBox.Show("File does not exist");

        return false;
    }

When I try to drag the HTML Object, I get the error System.NullReferenceException wasn´t handled in the source-code. But I tested if the File exists .... Can anyone tell me, what´s my mistake?

Thanks!

3
  • 1
    "I get an Error." - What is it? Commented Jun 26, 2015 at 11:14
  • In System.NullReferenceException an exception occurs from the type "System.Windows.Forms.dll", but this wasn´t handled in the user-code... but I did an if(File.Exist("myCursor.cur") command before creating the new one Commented Jun 26, 2015 at 11:43
  • Please edit your post with the error description, line of occurrence and all of the relevant code. Commented Jun 26, 2015 at 11:46

1 Answer 1

0

I recommend searching the web before asking. First search result for the title explains everything you should need to know.

Try this out:

class Program {
    [System.STAThread]
    static void Main(string[] args) {
        byte[] cursorBytes = new System.Net.WebClient().DownloadData(@"https://github.com/tlorach/nvGraphy/raw/master/cursor1.cur");
        System.IO.Stream cursorStream = new System.IO.MemoryStream(cursorBytes, false);
        System.Windows.Forms.Cursor cursor = new System.Windows.Forms.Cursor(cursorStream);
        System.Windows.Forms.Form mainForm = new System.Windows.Forms.Form();
        mainForm.Cursor = cursor;
        System.Windows.Forms.Application.Run(mainForm);
    }
}

Or this:

class Program {
    [System.STAThread]
    static void Main(string[] args) {
        System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
        openDialog.Filter = "Cursor (*.cur)|*.cur";
        switch(openDialog.ShowDialog()) {
            case System.Windows.Forms.DialogResult.OK:
                System.Windows.Forms.Cursor cursor = new System.Windows.Forms.Cursor(openDialog.FileName);
                System.Windows.Forms.Form mainForm = new System.Windows.Forms.Form();
                mainForm.Cursor = cursor;
                System.Windows.Forms.Application.Run(mainForm);
                break;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I can't raise more flags for this question ;)
Thanks. But is there a way to generate a cursor not from an embedded resource but from a resource on my pc?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.