在WPF应用程序中显示自定义光标
我有一组游标(.cur文件),我希望与我的WPF/VB.net应用程序一起使用,而无需更改系统范围的游标。我假设我会以某种方式使用每个WPF对象的“游标”属性,但我不知道如何使用我自己的游标。在WPF应用程序中显示自定义光标
我该怎么做才能做到这一点?
您是否尝试过使用文件路径重载来创建游标?
Cursor cursor = new Cursor("<path>");
或者这个问题的流?
一旦你有一个游标对象,你可以将它分配给它应该显示的控件。 (FrameworkElement.Cursor
)
如果使用游标作为资源,例如在项目中的光标文件夹
,你可以在你的XAML,例如在任何地方引用它
<Window Cursor="Cursors/wait_il.cur">...
假设光标在/资源/文件夹,并生成操作设置为资源:
声明:
<TextBlock x:Key="MyCursor" Cursor="/Resources/grab.cur" />
然后敷在初始化主窗口:
this.Cursor = (FindResource("MyCursor") as TextBlock).Cursor;
这种无意义的黑客究竟是什么?为什么不把它分配给窗口本身呢?系统范围内的 – 2011-05-04 00:06:11
我认为他的意思是应用程序范围?新的游标(
好吧,因为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;
嗯,我能怎么看代码可能工作,但我需要一些上下文。我在哪里放置每个片段?我已经尝试了几个景点,但我不断收到例外。 – CodeMouse92 2011-05-04 17:51:49
@ JasonMc92:查看我更新的答案,了解最简单的方法。 – 2011-05-04 18:26:00