• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 627
  • Last Modified:

How to set a specific Bitmap as cursor ?

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?
1 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 = My.Resources.cd
PictureBox1.Cursor = New Cursor(b.GetHicon)

Open in new window

Mike TomlinsonMiddle School Assistant TeacherCommented:
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)
jr_barros_jrAuthor Commented:
Thank you so much.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now