-
Jan 10th, 2012, 02:25 PM
#1
Thread Starter
Lively Member
[RESOLVED] Using custom cursor
I've done a ton of research and haven't found the right code yet, I want to be able to change the cursor once it enters the form, any idea guys?
The answer:
Originally Posted by i00
Thank you sir.
Last edited by MattyR; Feb 12th, 2012 at 02:39 PM.
-
Jan 10th, 2012, 02:46 PM
#2
-
Jan 10th, 2012, 03:42 PM
#3
Thread Starter
Lively Member
Re: Using custom cursor
lol if only it was as easy as that, i'm sorry i forgot to mention custom cursors in the thread and not just the title
-
Jan 10th, 2012, 06:04 PM
#4
Re: Using custom cursor
If you don't happen to have a .CUR file containing your custom cursor, you can very easily create a custom cursor from a bitmap (bmp) like this:
Code:
Dim customCursor As Cursor = New Cursor(bmp.GetHIcon)
That will work for 32-bit images of almost any size, with transparency and alpha-blending.
There is a snag however: the methods leaks memory each time you define a new cursor. The reason is that bmp.GetHicon instantiates an icon which contains no less than 3 copies of the original bitmap, and these aren't released when the icon goes out of scope or even when you Dispose of the icon.
So if you need to define new cursors repeatedly, you should get rid of the icon using the DestroyIcon API once you no longer need the cursor concerned. Here is an example:
vb Code:
<System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="DestroyIcon")> _ Public Shared Function DestroyIcon(<System.Runtime.InteropServices.InAttribute()> ByVal hIcon As System.IntPtr) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean End Function Private ico As Icon Private customCursor As Cursor Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'Create the custom cursor from a bitmap: Dim bmp As New Bitmap("D:\pictures\boot64x64.png") ico = Icon.FromHandle(bmp.GetHicon) customCursor = New Cursor(ico.Handle) Me.Cursor = customCursor End Sub Private Sub Form1_Click(sender As Object, e As System.EventArgs) Handles Me.Click 'Destroy the icon when the custom cursor is no longer needed: Me.Cursor = Cursors.Default DestroyIcon(ico.Handle) End Sub
Tested in WinXP by another dinosaur, BB
Last edited by boops boops; Jan 10th, 2012 at 07:51 PM.
Reason: remove DestroyIcon call from Load event handler
-
Jan 10th, 2012, 06:35 PM
#5
Re: Using custom cursor
If you want an animated one from your project resources check this out:
http://www.planet-source-code.com/vb...7808&lngWId=10
Kris
-
Jun 1st, 2013, 08:31 AM
#6
Lively Member
Re: Using custom cursor
Thank you so much for this!
Anyone who does not wonder, is either omnipotent or a fool.
Amerigoware <<<My Projects
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|