如何在C#中实现用户定义的游标?

时间:2015-06-26 11:13:20

标签: c# cursor

我想在我的WPF应用程序中将自己的Cursor定义为当前光标,但是我尝试从.cur文件创建一个新的Cursor对象,我得到一个错误。

我的代码是

     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;
    }

当我尝试拖动HTML对象时,我得到的错误是System.NullReferenceException没有在源代码中处理。但我测试了文件是否存在.... 谁能告诉我,我的错误是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

在询问之前我建议searching the web。标题的第一个搜索结果说明了您需要知道的所有内容。

试试这个:

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);
    }
}

或者这个:

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;
        }
    }
}