在WPF自定义光标?自定义、光标、WPF

由网友(Rnsh-逆神)分享简介:我想使用的图像或图标在WPF应用程序自定义光标。什么是做到这一点的最好方法是什么?I want to use an image or icon as a custom cursor in WPF app. What's the best way to do it?推荐答案您有两种基本的选择:当鼠标光标在你的控制,...

我想使用的图像或图标在WPF应用程序自定义光标。什么是做到这一点的最好方法是什么?

I want to use an image or icon as a custom cursor in WPF app. What's the best way to do it?

推荐答案

您有两种基本的选择:

当鼠标光标在你的控制,隐藏系统光标通过设置 this.Cursor = Cursors.None; 和使用任何技术绘制自己的光标你喜欢。然后,通过响应鼠标事件更新光标的位置和外观。这里有两个例子:

When the mouse cursor is over your control, hide the system cursor by setting this.Cursor = Cursors.None; and draw your own cursor using whatever technique you like. Then, update the position and appearance of your cursor by responding to mouse events. Here are two examples: http://www.xamlog.com/2006/07/17/creating-a-custom-cursor/ http://www.hanselman.com/blog/DeveloperDesigner.aspx http://www.xamlog.com/2006/07/17/creating-a-custom-cursor/ http://www.hanselman.com/blog/DeveloperDesigner.aspx

通过从的.cur或者.ani的文件加载图像创建一个新的游标对象。您可以创建并在Visual Studio中编辑这些类型的文件。也有一些免费的UTILITES左右浮动处理这些问题。基本上他们是图像(或动画图片)的指定一个热点,指示点图像中的光标定位在。

Create a new Cursor object by loading an image from a .cur or .ani file. You can create and edit these kinds of files in Visual Studio. There are also some free utilites floating around for dealing with them. Basically they're images (or animated images) which specify a "hot spot" indicating what point in the image the cursor is positioned at.

的构造。一瘸一拐,的的相对路径或包URI将无法正常工作。的如果你需要从一个相对路径或挤满了您的程序集资源加载光标,你将需要从文件流,在将它传递给了光标(流cursorStream)的构造。恼人的,但却是事实。

If you choose to load from a file, note that you need an absolute file-system path to use the Cursor(string fileName) constructor. Lamely, a relative path or Pack URI will not work. If you need to load the cursor from a relative path or from a resource packed with your assembly, you will need to get a stream from the file and pass it in to the Cursor(Stream cursorStream) constructor. Annoying but true.

在另一方面,指定一个游标的相对路径加载它使用XAML属性的什么时候的工作时,你可以用它来让你的鼠标装上隐藏的控件,然后复制一个事实引用另一个控件上使用。我还没有尝试过,但它应该工作。

On the other hand, specifying a cursor as a relative path when loading it using a XAML attribute does work, a fact you could use to get your cursor loaded onto a hidden control and then copy the reference to use on another control. I haven't tried it, but it should work.

阅读全文

相关推荐

最新文章