How do I create a dynamic mouse cursor .NET without using interop?

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

I have an application which I'm interested in eventually porting to mono so I'm trying to avoid using p/invoke's to accomplish this task.

I would like to load a cursor dynamically, as in I have a Bitmap that is generated on the fly in the application. From what I can tell the safest way to do it without using p/invoke's is to create a .cur file which I can then load to a memory stream and use the Cursor(Stream) constructor. However I have no idea how to create a .cur file.

I found this article on the Microsoft Knowledge Base which sort of explains the format, but I'm not sure how it can be used without the interop calls. How To Create an Alpha Blended Cursor or Icon in Windows XP

Does anyone else have a managed solution I can use to accomplish this task?

回答1:

Here's an example in VB.NET of creating a bitmap on the fly and turning it into a cursor. Not sure who this will help 7+ years on, but here goes:

Private Function GetCircleCursor(iDiameter As Integer) As Cursor Dim oBitmap As Bitmap = New Bitmap(Convert.ToInt32(iDiameter), Convert.ToInt32(iDiameter), System.Drawing.Imaging.PixelFormat.Format32bppArgb)      Using g As System.Drawing.Graphics = Graphics.FromImage(oBitmap)         g.Clear(Color.Transparent)          g.DrawEllipse(New System.Drawing.Pen(Color.White, 3), New Rectangle(0, 0, iDiameter, iDiameter))         g.DrawEllipse(New System.Drawing.Pen(Color.Black, 1), New Rectangle(0, 0, iDiameter, iDiameter))     End Using      Return New Cursor(oBitmap.GetHicon) End Function 


回答2:

I was reference the post: How can i replace cursor with bitmap in winform

You can create a Array of static cursor and use Timer to change it

to make dynamic mouse cursor effect!

Create static cursor from bitmap is so simply and without using interop:

public partial class Form1 : Form {     public Form1()     {         InitializeComponent();          Icon icon = this.Icon;         Bitmap bmp = icon.ToBitmap();         Cursor cur = new Cursor(bmp.GetHicon());          this.Cursor = cur;     } } 


回答3:

Simple: YOU CAN NOT - the functionaltiy you ask for is not part of the .NET framework, so you need to go native.

If you application needds porting to mono, isloate this code in one class so you can turn if off like with a compiler switch - not hard.



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