我正在 Visual C# 中创建 WinForms 应用程序,并且希望将光标更改为我创建的自定义图像 (.cur)。我希望光标图像仅在表单窗口上更改为此。
之前回答的所有问题都建议将 this.cursor
设置为使用光标路径创建的新 cursor
对象,语句如下:
this.Cursor = new Cursor(@"C:\green.cur"); // this
this.Cursor = new Cursor(GetType(), @"C:\green.cur"); // or this
但这两个语句都会导致相同的错误:
Image format is not valid. The image file may be corrupted.
按照建议导入 user32.dll
后,我还尝试使用 LoadImage
导入光标 here但这导致了错误:
Win32 handle passed to Cursor is not valid or is the wrong type.
我可以使用以下语句将光标设置为标准等待光标:
this.Cursor = Cursors.WaitCursor;
当用于在光标嵌入资源的 C++ 程序中设置光标时,同一个 .cur 文件可以完美地工作。
最佳答案
您需要使用LoadCursorFromFile
:
[DllImport("user32.dll")]
static extern IntPtr LoadCursorFromFile(string lpFileName);
当你想改变光标时:
IntPtr cursor = LoadCursorFromFile(@"C:\green.cur");
this.Cursor = new Cursor(cursor);
关于c# - 在 C# 中的 Windows 窗体上设置自定义光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24158760/