C# 如何在 .NET 应用程序中使用自定义鼠标光标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/489791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you use a custom mouse Cursor in a .NET application?
提问by BFree
Well, as the title says, how can I set an image or anything else as the mouse cursor. I know you can use the built in ones that are part of the Cursors class, but I was wondering if there's a way to use your own image (preferably without P/Invoke, but either way is fine.)
好吧,正如标题所说,如何将图像或其他任何内容设置为鼠标光标。我知道您可以使用属于 Cursors 类的内置图像,但我想知道是否有一种方法可以使用您自己的图像(最好没有 P/Invoke,但无论哪种方式都可以。)
EDIT: I guess I didn't explain what I wanted clearly enough. I don't have any .cur files (are they easy to create?), I want to know if there's a way to take an image from disk (or wherever) and use that as the cursor. Ultimatley what I'd like would be to do something like this:
编辑:我想我没有足够清楚地解释我想要什么。我没有任何 .cur 文件(它们容易创建吗?),我想知道是否有办法从磁盘(或任何地方)获取图像并将其用作光标。最终我想做的是做这样的事情:
myForm.Cursor = new Cursor(Image.FromFile("foo.jpg"));
Possible?
可能的?
采纳答案by Marc Gravell
At the simplest, you just use:
最简单的,你只需使用:
form.Cursor = new Cursor(path);
But there are overloads to load from other sources (an unmanaged pointer, a raw stream, or a resx).
但是有从其他源(非托管指针、原始流或 resx)加载的重载。
回答by Jared
First add the custom cursor to your project then it's pretty simple:
首先将自定义光标添加到您的项目中,然后它非常简单:
Cursor myCursor = new Cursor("custom.cur");
Then just assign it to your controls
然后只需将其分配给您的控件
someControl.cursor = myCursor
回答by Stu Mackellar
If you want some more information on how to create your own cursor resources then there is a good tutorial here. You should create your cursor file and embed it as a resource in your executable - easy in Visual Studio. This is tidier and more efficient than loading it from a separate file. You can then load it directly using the Cursor constructor that takes a resouce name.
如果您想了解如何创建自己的光标资源的一些详细信息,然后有一个很好的教程在这里。您应该创建光标文件并将其作为资源嵌入到您的可执行文件中 - 在 Visual Studio 中很容易。这比从单独的文件加载它更整洁、更有效。然后,您可以使用采用资源名称的 Cursor 构造函数直接加载它。
回答by paradisontheitroad
In addition to what mentioned above, you can do this:
除了上面提到的,你还可以这样做:
Mouse.OverrideCursor = Cursors.Arrow;
and Cursors can be : AppStarting Arrow ArrowCD Cross Hand Help Pen UpArrow or others.
和光标可以是: AppStarting Arrow ArrowCD Cross Hand Help Pen UpArrow 或其他。