在程序中使用自定义鼠标光标的三种方式:

RadioButton senderButton = sender as RadioButton;

方式一:          
            string cursorPath = Directory.GetCurrentDirectory();
            cursorPath += "/myCursor.cur";
            senderButton.Cursor = new Cursor(cursorPath);

注:由于构造函数Cursor (String) 只接受绝对路径,因此需将自定义光标文件myCursor.cur拷贝到exe文件所在目录中。
           

方式二:                       
            StreamResourceInfo sri = Application.GetResourceStream(new Uri("/Resources/Cursors/myCursor.cur", UriKind.Relative));
            senderButton.Cursor = new Cursor(sri.Stream);

 

方式三:

(1)、在资源字典中定义:

<!--带自定义光标的label样式-->
    <Label x:Key="LabelStyle_myCursor" Cursor="/AEConsole;component/Resources/Cursors/myCursor.cur"/>

注:AEConsole是程序解决方案的名字。

(2)程序使用:

senderButton.Cursor = ((Label)this.Resources["LabelStyle_myCursor"]).Cursor;

 

推荐使用方式二,方式一不能采用ClickOnce方式发布(原因见注1),方式三有点绕。
注1:程序采用ClickOnce方式部署时,不会自动拷贝存在于绝对路径下的文件。因此当光标文件使用绝对路径时,程序发布后会找不到该光标文件。

注2:若Cursor类未提供构造函数Cursor (Stream) ,可使用方式三,想法不错可以借鉴。

 

posted on 2017-10-30 13:55  青叶煮酒  阅读(951)  评论(0编辑  收藏  举报