none
gif file as mouse pointer (cursor)

    Question

  • Hi,

    how could I set the mouse cursor to an animated gif on a simply way? I will not use api-s.

    Can you advise me?

    Thanks.

    Wednesday, July 24, 2013 6:01 PM

Answers

  • Hi,

     As Khanna has said you are going to need to use API`s if you want to switch the system cursor. However, the code that is at the link he has given is for loading and using an Animated cursor file not an Animated Gif image. It is also for VB6 and will need to be converted to the correct VB.NET signatures such as the code below. You can convert your animated gif image into an animated cursor using a freeware program such as AniTuner located HERE. This example code i posted below will load an animated cursor file and switch the system cursor to use it when the (Button_SetNewCursor) button is pressed. Then it will switch it back to the old cursor when you press the (Button_RestoreOldCursor) button is pressed or if you close the program. I put the animated cursor in my applications directory but, you can put yours wherever you want and set (CursorPathName) to the path and name of yours.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")> Private Shared Function SetSystemCursor(ByVal hCursor As IntPtr, ByVal id As Integer) As Boolean
        End Function
        <DllImport("user32.dll")> Private Shared Function LoadCursorFromFile(ByVal lpFileName As String) As IntPtr
        End Function
        <DllImport("user32.dll")> Private Shared Function CopyIcon(ByVal hIcon As IntPtr) As IntPtr
        End Function
        Private Declare Function GetCursor Lib "user32.dll" () As IntPtr
    
        Private Const OCR_NORMAL As Integer = 32512
        Dim OldCursor, NewCursor As IntPtr
        Dim CursorPathName As String = System.IO.Path.Combine(Application.StartupPath, "MyAniCursor.ani")
    
        Private Sub Button_SetNewCursor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_SetNewCursor.Click
            If NewCursor.Equals(IntPtr.Zero) Then
                OldCursor = CopyIcon(GetCursor())
                NewCursor = LoadCursorFromFile(CursorPathName)
                SetSystemCursor(NewCursor, OCR_NORMAL)
            End If
        End Sub
    
        Private Sub Button_RestoreOldCursor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_RestoreOldCursor.Click
            RestoreOldCursor()
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            RestoreOldCursor()
        End Sub
    
        Private Sub RestoreOldCursor()
            If Not NewCursor.Equals(IntPtr.Zero) Then
                SetSystemCursor(OldCursor, OCR_NORMAL)
                NewCursor = IntPtr.Zero
                OldCursor = IntPtr.Zero
            End If
        End Sub
    End Class



    • Edited by IronRazerz Wednesday, July 24, 2013 11:44 PM edit
    • Marked as answer by DerStauner Thursday, July 25, 2013 4:06 PM
    Wednesday, July 24, 2013 8:23 PM

All replies

  • If you want to do it programatically, then unfortunately you can't do that without using API.

    If in case you decide to go with API then following link may help you

    http://www.developer.com/net/vb/article.php/1541491/Using-Animated-Cursors-in-Visual-Basic.htm


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Wednesday, July 24, 2013 6:54 PM
  • Hi,

     As Khanna has said you are going to need to use API`s if you want to switch the system cursor. However, the code that is at the link he has given is for loading and using an Animated cursor file not an Animated Gif image. It is also for VB6 and will need to be converted to the correct VB.NET signatures such as the code below. You can convert your animated gif image into an animated cursor using a freeware program such as AniTuner located HERE. This example code i posted below will load an animated cursor file and switch the system cursor to use it when the (Button_SetNewCursor) button is pressed. Then it will switch it back to the old cursor when you press the (Button_RestoreOldCursor) button is pressed or if you close the program. I put the animated cursor in my applications directory but, you can put yours wherever you want and set (CursorPathName) to the path and name of yours.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")> Private Shared Function SetSystemCursor(ByVal hCursor As IntPtr, ByVal id As Integer) As Boolean
        End Function
        <DllImport("user32.dll")> Private Shared Function LoadCursorFromFile(ByVal lpFileName As String) As IntPtr
        End Function
        <DllImport("user32.dll")> Private Shared Function CopyIcon(ByVal hIcon As IntPtr) As IntPtr
        End Function
        Private Declare Function GetCursor Lib "user32.dll" () As IntPtr
    
        Private Const OCR_NORMAL As Integer = 32512
        Dim OldCursor, NewCursor As IntPtr
        Dim CursorPathName As String = System.IO.Path.Combine(Application.StartupPath, "MyAniCursor.ani")
    
        Private Sub Button_SetNewCursor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_SetNewCursor.Click
            If NewCursor.Equals(IntPtr.Zero) Then
                OldCursor = CopyIcon(GetCursor())
                NewCursor = LoadCursorFromFile(CursorPathName)
                SetSystemCursor(NewCursor, OCR_NORMAL)
            End If
        End Sub
    
        Private Sub Button_RestoreOldCursor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_RestoreOldCursor.Click
            RestoreOldCursor()
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            RestoreOldCursor()
        End Sub
    
        Private Sub RestoreOldCursor()
            If Not NewCursor.Equals(IntPtr.Zero) Then
                SetSystemCursor(OldCursor, OCR_NORMAL)
                NewCursor = IntPtr.Zero
                OldCursor = IntPtr.Zero
            End If
        End Sub
    End Class



    • Edited by IronRazerz Wednesday, July 24, 2013 11:44 PM edit
    • Marked as answer by DerStauner Thursday, July 25, 2013 4:06 PM
    Wednesday, July 24, 2013 8:23 PM