Color custom cursor appears to be black when I run the program. The color custom cursor is imported from desktop by using Resources in vb.net. When I run the program the cursor turns out to be black. HOW DO I FIX IT?

Dim ms As New System.IO.MemoryStream(My.Resources.Cursor1) 
Button1.Cursor = New Cursor(ms)
Member Avatar for Unhnd_Exception

You can use a color cursor from a bitmap with GetHicon

Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    
    'Getting the Hicon from a 24x24 image I added as a resource.
    Me.Cursor = New Cursor(My.Resources.Forum24.GetHicon)
End Sub

That will give you a color cursor but you will notice it sets the hot spot at the center of the image.

You will need to make some api calls to change the hot spot.

Here is what you need to add to your class.

Private Structure IconInfo
    Public fIcon As Boolean
    Public xHotspot As Integer
    Public yHotspot As Integer
    Public hbmMask As IntPtr
    Public hbmColor As IntPtr
End Structure

<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function GetIconInfo(ByVal hIcon As IntPtr, ByRef pIconInfo As IconInfo) As Boolean
End Function

<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function CreateIconIndirect(ByRef icon As IconInfo) As IntPtr
End Function

Now to set the hot spot.

Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    'Get the Hicon from the bitmap
    Dim BitmapPtr As IntPtr = My.Resources.Forum24.GetHicon
    Dim IconInfo As IconInfo = New IconInfo()
    Dim CursorPtr As IntPtr

    'This will set the hbmMask and Color fields of the
    'IconInfo structure
    GetIconInfo(BitmapPtr, IconInfo)

    'Now you can set the x and y hotspot
    IconInfo.xHotspot = 0
    IconInfo.yHotspot = 0
    IconInfo.fIcon = False 'True for Icon; False for cursor

    'Get a handle for the cursor
    CursorPtr = CreateIconIndirect(IconInfo)

    'Now create the cursor from the handle
    Me.Cursor = New Cursor(CursorPtr)
End Sub

The hot spot will now be at 0,0.

Do a search on the msdn library for IconInfo, GetIconInfo, and CreateIconIndirect.

commented: Had helped resolve my problem +1

You can use a color cursor from a bitmap with GetHicon

Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    
    'Getting the Hicon from a 24x24 image I added as a resource.
    Me.Cursor = New Cursor(My.Resources.Forum24.GetHicon)
End Sub

That will give you a color cursor but you will notice it sets the hot spot at the center of the image.

You will need to make some api calls to change the hot spot.

Here is what you need to add to your class.

Private Structure IconInfo
    Public fIcon As Boolean
    Public xHotspot As Integer
    Public yHotspot As Integer
    Public hbmMask As IntPtr
    Public hbmColor As IntPtr
End Structure

<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function GetIconInfo(ByVal hIcon As IntPtr, ByRef pIconInfo As IconInfo) As Boolean
End Function

<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function CreateIconIndirect(ByRef icon As IconInfo) As IntPtr
End Function

Now to set the hot spot.

Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    'Get the Hicon from the bitmap
    Dim BitmapPtr As IntPtr = My.Resources.Forum24.GetHicon
    Dim IconInfo As IconInfo = New IconInfo()
    Dim CursorPtr As IntPtr

    'This will set the hbmMask and Color fields of the
    'IconInfo structure
    GetIconInfo(BitmapPtr, IconInfo)

    'Now you can set the x and y hotspot
    IconInfo.xHotspot = 0
    IconInfo.yHotspot = 0
    IconInfo.fIcon = False 'True for Icon; False for cursor

    'Get a handle for the cursor
    CursorPtr = CreateIconIndirect(IconInfo)

    'Now create the cursor from the handle
    Me.Cursor = New Cursor(CursorPtr)
End Sub

The hot spot will now be at 0,0.

Do a search on the msdn library for IconInfo, GetIconInfo, and CreateIconIndirect.

Now I have one more thing that I need to do. The program needs to run when its minimized or runnig in the background. For example: I need this program to run when I have Slide Show in PowerPoint.

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.