我有一組遊標(.cur文件),我希望與我的WPF/VB.net應用程序一起使用,而無需更改系統範圍的遊標。我假設我會以某種方式使用每個WPF對象的「遊標」屬性,但我不知道如何使用我自己的遊標。在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