我有一组光标(.cur文件),想在我的WPF/VB.net应用程序中使用,而不会改变整个系统的光标。我假设我需要以某种方式使用每个WPF对象的“cursor”属性,但我不确定如何使用自己的光标。
我该怎么做才能实现这个目标呢?
我有一组光标(.cur文件),想在我的WPF/VB.net应用程序中使用,而不会改变整个系统的光标。我假设我需要以某种方式使用每个WPF对象的“cursor”属性,但我不确定如何使用自己的光标。
我该怎么做才能实现这个目标呢?
Cursor cursor = new Cursor("<path>");
或者说流?
一旦您拥有了光标对象,您可以将其分配给应该显示它的控件。(FrameworkElement.Cursor
)
你可以在XAML的任何地方引用它,例如:
<Window Cursor="Cursors/wait_il.cur">...
假设光标在/Resources/文件夹中并且构建操作设置为Resource:
声明:
<TextBlock x:Key="MyCursor" Cursor="/Resources/grab.cur" />
然后在初始化器中将其应用于主窗口:
this.Cursor = (FindResource("MyCursor") as TextBlock).Cursor;
好的,既然H.B在这里向我抱怨,那么这里有一个类 :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;