Like the title asked,
what should i do to change the cursor into custom image when the form is loaded,
i've tried so many style by google searching, but still can' work. :/

Recommended Answers

All 4 Replies

Have you tried the example in the documentation?
Click Here

ya i tried this code before, and it works but color into black color.
however, my image is a donut cursor download from somewhere and also is a GIF file.

You are out of luck with a color cursor using the built-in .Net functions.

From the documentation:

The Cursor class does not support animated cursors (.ani files) or cursors with colors other than black and white.

But why limit yourself to what is built-in?

Here is a class that you can use to create a cursor from a .Net Image.

Imports System.Runtime.InteropServices

Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      'make cursor with the hotspot (click point) the top-right corner
      Dim mc As New MakeCursor( _
         MakeCursor.ScaleImage(PictureBox1.Image, _
                               MakeCursor.SystemCursorSize.Width, MakeCursor.SystemCursorSize.Height), _
                               MakeCursor.SystemCursorSize.Width, 0)
      Me.Cursor = mc.Cursor
   End Sub

End Class

''' <summary>
''' Allows creating as cursor from an image
''' </summary>
''' <remarks>True Cursor Size is stored in the "Tag" Property</remarks>
Public Class MakeCursor
   Implements IDisposable
   Private cursorptr As IntPtr

   ''' <summary>
   ''' creates cursor from an Image class
   ''' </summary>
   ''' <param name="img">Source Image for cursor</param>
   ''' <param name="HotSpot_x">the x coordinate of the click pt</param>
   ''' <param name="hotSpot_y">the y coordinate of the click pt</param>
   ''' <remarks></remarks>
   Public Sub New(ByVal img As Image, ByVal HotSpot_x As Int32, ByVal hotSpot_y As Int32)
      Dim cusorrinfo As New IconInfo
      Dim bm As Bitmap = CType(img, Bitmap)
      With cusorrinfo
         .fIcon = False 'cursor
         .xHotspot = HotSpot_x
         .yHotspot = hotSpot_y
         .hbmMask = bm.GetHbitmap
         .hbmColor = bm.GetHbitmap
      End With
      Dim cursorinfoptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cusorrinfo))
      Marshal.StructureToPtr(cusorrinfo, cursorinfoptr, True)
      cursorptr = CreateIconIndirect(cursorinfoptr)
      Dim icon As Icon = icon.FromHandle(cursorptr)
      Cursor = New Cursor(cursorptr)
      Cursor.Tag = bm.Size
      Me.Size = bm.Size
      Marshal.FreeHGlobal(cursorinfoptr) 'release allocated memory
      DeleteObject(cusorrinfo.hbmMask) 'relealse gdi object
      DeleteObject(cusorrinfo.hbmColor) 'relealse gdi object
   End Sub

   Dim _Cursor As Cursor
   Public Property Cursor() As Cursor
      Get
         Return _Cursor
      End Get
      Private Set(ByVal value As Cursor)
         _Cursor = value
      End Set
   End Property

   Private _size As Size
   ''' <summary>
   ''' Returns true size of cursor created with this class.  The Forms.Cursor class returns the specified system cursor size, not the true size.
   ''' </summary>
   Public Property Size() As Size
      Get
         Return _size
      End Get
      Private Set(ByVal value As Size)
         _size = value
      End Set
   End Property

   ''' <summary>
   ''' Provides current system cursor size
   ''' </summary>
   Public Shared ReadOnly Property SystemCursorSize() As Size
      Get
         Return SystemInformation.CursorSize
      End Get
   End Property

   ''' <summary>
   ''' utility routine to scale an image to the specified width and height
   ''' </summary>
   Public Shared Function ScaleImage(ByVal img As Image, ByVal width As Int32, ByVal height As Int32) As Image
      Dim Scaledbm As Bitmap
      Scaledbm = New Bitmap(width, height, Imaging.PixelFormat.Format32bppArgb)
      Scaledbm.SetResolution(img.HorizontalResolution, img.VerticalResolution)

      Using g As Graphics = Graphics.FromImage(Scaledbm)
         g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
         g.DrawImage(img, _
                  New Rectangle(0, 0, width, height), _
                  New Rectangle(0, 0, img.Width, img.Height), _
                  GraphicsUnit.Pixel)
      End Using
      Return Scaledbm
   End Function 'ScaleImage

   Private Structure IconInfo
      ''' <summary>Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies an icon; FALSE specifies a cursor.</summary>
      Public fIcon As Boolean
      ''' <summary>The x-coordinate of a cursor's hot spot.</summary>
      Public xHotspot As Int32
      ''' <summary>The y-coordinate of the cursor's hot spot.</summary>
      Public yHotspot As Int32
      ''' <summary>The icon bitmask bitmap. If this structure defines a black and white icon, this bitmask is formatted so that the upper half is the icon AND bitmask and the lower half is the icon XOR bitmask. Under this condition, the height should be an even multiple of two. If this structure defines a color icon, this mask only defines the AND bitmask of the icon.</summary>
      Public hbmMask As IntPtr
      ''' <summary>A handle to the icon color bitmap. This member can be optional if this structure defines a black and white icon. The AND bitmask of hbmMask is applied with the SRCAND flag to the destination; subsequently, the color bitmap is applied (using XOR) to the destination by using the SRCINVERT flag.</summary>
      Public hbmColor As IntPtr
   End Structure

   <DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _
   Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr
   End Function

   <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
   Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
   End Function

   <DllImport("gdi32.dll")> _
   Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
   End Function

   Private disposedValue As Boolean = False      ' To detect redundant calls

   ' IDisposable
   Protected Overridable Sub Dispose(ByVal disposing As Boolean)
      If Not Me.disposedValue Then
         If disposing Then
            ' TODO: free other state (managed objects).
         End If

         ' TODO: free your own state (unmanaged objects).
         ' TODO: set large fields to null.
         If cursorptr <> IntPtr.Zero Then
            DestroyIcon(cursorptr)
         End If
      End If
      Me.disposedValue = True
   End Sub 'Dispose

#Region " IDisposable Support "
   ' This code added by Visual Basic to correctly implement the disposable pattern.
   Public Sub Dispose() Implements IDisposable.Dispose
      ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
      Dispose(True)
      GC.SuppressFinalize(Me)
   End Sub 'Dispose
#End Region

End Class

UNBELIEVABLE I've been searching the internet for ages but useless, finally found what I want!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.