How to set a specific Bitmap as cursor ?

Posted on 2009-12-25
Last Modified: 2013-11-25
I am needing to set a specific bitmap as cursor during run-time. this bitmap is not static, I mean, this bitmap could be any image depending how the user will interact with my application.
i only need to use this custom cursor over my PictureBox.

Ho can I do that?
Question by:jr_barros_jr
    LVL 29

    Accepted Solution

    Example using a bitmap that was added as an existing resource file. Set Build Action on the resource to Embedded Resource.
    Dim b As Bitmap =
    PictureBox1.Cursor = New Cursor(b.GetHicon)

    Open in new window

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Be sure to store the IntPtr returned by GetHicon() and release it with the DestroyIcon() API before you go using GetHicon() again.  Otherwise you'll end up with an eventual GDI leak:

        <DllImport("user32", CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function DestroyIcon(ByVal hIcon As IntPtr) As Boolean
        End Function

            Static hIcon As IntPtr
            If Not hIcon.Equals(IntPtr.Zero) Then
            ENd If
            Dim b As Bitmap = ~some dynamic bitmap~
            hIcon = b.GetHicon()
            PictureBox1.Cursor = New Cursor(hIcon)

    Author Closing Comment

    Thank you so much.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now