Results 1 to 6 of 6

Thread: [RESOLVED] Using custom cursor

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Posts
    89

    Resolved [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:


    Quote Originally Posted by i00 View Post
    If you want an animated one from your project resources check this out:

    http://www.planet-source-code.com/vb...7808&lngWId=10

    Kris

    Thank you sir.
    Last edited by MattyR; Feb 12th, 2012 at 02:39 PM.

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Using custom cursor

    Properties - Cursor

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Posts
    89

    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

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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:
    1. <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="DestroyIcon")> _
    2.     Public Shared Function DestroyIcon(<System.Runtime.InteropServices.InAttribute()> ByVal hIcon As System.IntPtr) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    3.     End Function
    4.  
    5.     Private ico As Icon
    6.     Private customCursor As Cursor
    7.  
    8.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    9.                 'Create the custom cursor from a bitmap:
    10.         Dim bmp As New Bitmap("D:\pictures\boot64x64.png")
    11.         ico = Icon.FromHandle(bmp.GetHicon)
    12.         customCursor = New Cursor(ico.Handle)
    13.         Me.Cursor = customCursor       
    14.     End Sub
    15.  
    16.     Private Sub Form1_Click(sender As Object, e As System.EventArgs) Handles Me.Click
    17.                'Destroy the icon when the custom cursor is no longer needed:
    18.                Me.Cursor = Cursors.Default
    19.         DestroyIcon(ico.Handle)              
    20.     End Sub

    Tested in WinXP by another dinosaur, BB

  5. #5
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    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

  6. #6
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    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
  •  



Click Here to Expand Forum to Full Width