2011-05-03 97 views
4

我有一组游标(.cur文件),我希望与我的WPF/VB.net应用程序一起使用,而无需更改系统范围的游标。我假设我会以某种方式使用每个WPF对象的“游标”属性,但我不知道如何使用我自己的游标。在WPF应用程序中显示自定义光标

我该怎么做才能做到这一点?

回答

0

您是否尝试过使用文件路径重载来创建游标?

Cursor cursor = new Cursor("<path>"); 

或者这个问题的流?

一旦你有一个游标对象,你可以将它分配给它应该显示的控件。 (FrameworkElement.Cursor


如果使用游标作为资源,例如在项目中的光标文件夹

screen

,你可以在你的XAML,例如在任何地方引用它

<Window Cursor="Cursors/wait_il.cur">... 
+0

嗯,我能怎么看代码可能工作,但我需要一些上下文。我在哪里放置每个片段?我已经尝试了几个景点,但我不断收到例外。 – CodeMouse92 2011-05-04 17:51:49

+0

@ JasonMc92:查看我更新的答案,了解最简单的方法。 – 2011-05-04 18:26:00

0

假设光标在/资源/文件夹,并生成操作设置为资源:

声明:

<TextBlock x:Key="MyCursor" Cursor="/Resources/grab.cur" /> 

然后敷在初始化主窗口:

this.Cursor = (FindResource("MyCursor") as TextBlock).Cursor; 
+2

这种无意义的黑客究竟是什么?为什么不把它分配给窗口本身呢?系统范围内的 – 2011-05-04 00:06:11

+0

我认为他的意思是应用程序范围?新的游标()对我来说是个例外,因为它需要一个绝对路径。为了避免背后的代码,我让TextBlock创建游标,因为TextBlock很小。这是哈克是的,但稳定。但是,是的,他可以编写代码来获取当前程序路径,读取光标文件或任何他想要的内容。 – 2011-05-04 00:16:53

0

好吧,因为HB在我这里是一个类:p

public class CustomCursor 
{ 
    private System.Windows.Input.Cursor _cursor = null; 
    public System.Windows.Input.Cursor Cursor 
    { 
     get 
     { 
      if (_cursor == null) 
       _cursor = GetCursor(); 
      return _cursor; 
     } 
    } 

    public string RelativePath { get; set; } 

    public CustomCursor() 
    { 
    } 

    public CustomCursor(string relativePath) 
    { 
     RelativePath = relativePath; 
    } 

    public System.Windows.Input.Cursor GetCursor() 
    { 
     if (RelativePath == null) 
      throw new ArgumentNullException("You must set RelativePath first"); 

     string directory = Directory.GetCurrentDirectory(); 
     string absPath = directory + '\\' + RelativePath; 

     if (!File.Exists(absPath)) 
      throw new FileNotFoundException(); 

     return new System.Windows.Input.Cursor(absPath); 
    } 
} 

在代码中使用的背后是这样的:

this.Cursor = new CustomCursor("grab.cur").Cursor; 

或者宣布在XAML:

<local:CustomCursor x:Key="MyCursor" RelativePath="grab.cur"/> 

和参考这样的:

this.Cursor = (FindResource("MyCursor") as CustomCursor).Cursor;