开发者

Using Custom Cursor WinForms

Is there a way to use a custom cursor in winforms?

There seems开发者_如何学C to be no option. But when I try to manually add a cursor as a resource, then call it from code, it says that it cannot convert from type byte[] to Cursor.


Adding custom icon to cursor in C# :

Add Icon file to Project resources (ex : Processing.ico)

And in properties window of image switch "Build Action" to "Embedded"

Cursor cur = new Cursor(Properties.Resources.**Imagename**.Handle);
this.Cursor = cur;

Ex:

Cursor cur = new Cursor(Properties.Resources.Processing.Handle);
this.Cursor = cur;


From the MSDN documentation on the Cursor class (with minor corrections):

// The following generates a cursor from an embedded resource.
// To add a custom cursor, create or use an existing 16x16 bitmap
//        1. Add a new cursor file to your project: 
//                File->Add New Item->Local Project Items->Cursor File
//        2. Select 16x16 image type:
//                Image->Current Icon Image Types->16x16
// --- To make the custom cursor an embedded resource  ---
// In Visual Studio:
//        1. Select the cursor file in the Solution Explorer
//        2. Choose View->Properties.
//        3. In the properties window switch "Build Action" to "Embedded"
// On the command line:
//        Add the following flag:
//            /res:CursorFileName.Cur,Namespace.CursorFileName.Cur
//        
//        Where "Namespace" is the namespace in which you want to use
//        the cursor and   "CursorFileName.Cur" is the cursor filename.
// The following line uses the namespace from the passed-in type
// and looks for CustomCursor.MyCursor.Cur in the assemblies manifest.
// NOTE: The cursor name is case sensitive.

this.Cursor = new Cursor(GetType(), "MyCursor.Cur");


I've used the LoadCursorFromFile() method from User32.dll. There are plenty of samples for this on the web.

OR

The ctor for the Cursor type also has a IO.Stream overload. Load your byte[] into a MemoryStream and feed that to the new Cursor.


Convert your cursor from any format to ico using convertico.com(It is the best way of doing this), copy your cursor to your project's debug folder using file explorer and write this code(C#):

this.Cursor = new Cursor("default.ico");


After adding the file to the resources, in the properties window of the image: switch Build Action to Embedded Resource and write in your code:

"name of control".Cursor = new System.Windows.Forms.Cursor(Properties.Resources."name of image".Handle);


I had the same problem for a while. So, as far as I understood, these are 2 ways to solving that issue:

  1. Putting cursor .ico file into the Resources
  2. Getting cursor from .ico file without putting it the resources

First case: After putting to the resources you can just add .Handle after the name of resource while getting it. For instance:

this.Cursor = new Cursor(Properties.Resources.YourResource.Handle);

Second case: This one looks a bit easier, but before that you should add your .ico file to project and in properties of that file you should set Always copy for file to be copied automatically to the execution folder. After that you can easyly use this:

this.Cursor = new Cursor("YourIcon.ico");

To my mind, using the resources for permanent files like cursor icons is the best practice, so the used the first one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜