可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.