1

How can i change the mouse pointer to an image when clicked on the image in c#?

rainbower
  • 141
  • 4
  • 14
  • 1
    WinForms, WPF, ASP.NET (which would be kinda impossible) what? This question is very vague. – Tejs Sep 06 '11 at 17:38

3 Answers3

4

This MSDN article explains how you would do it with WPF. Just change their drop-down to a OnClick for the image you're interested in.

You could likely also hook in to an OnClick event in WinForms too, but I don't have an example readily available for that. If you're doing WinForms and not WPF, edit your question to specify!

You can create your own custom cursor with:

    yourCursor = new Cursor(someImageStream);

Here's a way I verified to load a custom cursor. This certainly works and combines some of the information from your own comment as well as the blog article I linked below.

  var image = (Bitmap)Bitmap.FromFile(@"c:\cursorImage.bmp");
  IntPtr ptr = image.GetHicon();
  var handle = new SafeFileHandle(ptr, true);
  var yourCursor = System.Windows.Interop.CursorInteropHelper.Create(handle);
  Cursor = yourCursor;

A more detailed explaination of loading images for use in a custom cursor is provided at this blog article which may be more robust, but looks to be more than you need considering the above.

Kevek
  • 2,534
  • 5
  • 18
  • 29
  • Thanks.But it produces Argument Exception and it says that the image format may be invalid. – rainbower Sep 06 '11 at 17:55
  • What kind of image are you loading? Make sure you're matching the type of image you're loading to the one you're attempting to save to the stream. – Kevek Sep 06 '11 at 17:56
  • Thanks.I got the Code check out [link](http://www.pinvoke.net/default.aspx/user32/SetCursor.html) – rainbower Sep 06 '11 at 17:59
  • I saw similar code here: http://stackoverflow.com/questions/6991023/wpf-cursor-casting-problem/12959851#comment73014212_12959851 I created a `Cursor c = new Cursor(ptr);`, then created the `SafeFileHandle` by passing it `c.Handle` and `false`, instead of `ptr` and `true`, which the author admittedly says the documentation said to use `true` but couldn't get it to work. Perhaps it was because of using the new `Cursor` & not sending `ptr` straight in. But I was able to get that link's code to work in case someone tries the code above and can't. But they're basically the same thing. – vapcguy Mar 23 '17 at 00:08
2

The question is vague/unclear but perhaps you want to change the system cursor (not just your application)? Here is a link to the pinvoke info for SetSystemCursor. Be warned this is considered bad form.

rtalbot
  • 1,615
  • 10
  • 13
  • Thanks the Code provided in the Forum Works Fine. – rainbower Sep 06 '11 at 17:57
  • @rainbower, please note what rtalbot said: You really shouldn't be changing the System Cursor. Instead, you should likely change the cursor only within your application's window. – Kevek Sep 06 '11 at 17:59
0

this.Cursor = Cursors.WaitCursor;

Handle click event of control in which you display image. In this event handler change cursor propery of control to desired one - you can of course construct you own Cursor object see Cursor.

MichaelMocko
  • 5,466
  • 1
  • 21
  • 24