How to change the mouse pointer to an Image when clicked on it in c#

寵の児 提交于 2020-01-05 09:23:07

问题


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


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7323851/how-to-change-the-mouse-pointer-to-an-image-when-clicked-on-it-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!