none
old version hand cursor RRS feed

  • Question

  • why old version of hand cursor is presented instead new hand cursor on label link?

    its the some kind of the bug?

    i want to use new hand cur

    Sunday, September 22, 2019 8:30 PM

Answers

  • You right! the code is incorrect!

    is disables lable1 some events...

    ---

    finally! done..

    here my code without any problems or flickers, all works..

    Imports System.ComponentModel
    Imports System.Reflection
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Public Delegate Function SUBCLASSPROC(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr, uIdSubclass As IntPtr, dwRefData As UInteger) As Integer
        Public Const WM_SETCURSOR As Integer = &H20
        Public Const IDC_HAND As Integer = 32649
        Public NewSubClassDelegate As SUBCLASSPROC
        Private ReadOnly hCursor As IntPtr = IntPtr.Zero
       
        <DllImport("User32.dll", SetLastError:=True)>
        Public Shared Function SetCursor(hCursor As IntPtr) As IntPtr
        End Function
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function LoadCursor(hInstance As IntPtr, lpCursorName As IntPtr) As IntPtr
        End Function
        <DllImport("Comctl32.dll", SetLastError:=True)>
        Public Shared Function SetWindowSubclass(hWnd As IntPtr, pfnSubclass As SUBCLASSPROC, uIdSubclass As UInteger, dwRefData As UInteger) As Boolean
        End Function
        <DllImport("Comctl32.dll", SetLastError:=True)>
        Public Shared Function DefSubclassProc(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr) As Integer
        End Function
        Private Function NewSubClassProc(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr, uIdSubclass As IntPtr, dwRefData As UInteger) As Integer
            If (uMsg = WM_SETCURSOR) Then
                If (hCursor <> IntPtr.Zero) Then
                End If
                Return 1
            Else
                Return DefSubclassProc(hWnd, uMsg, wParam, lParam)
            End If
        End Function
        Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
            SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
            NewSubClassDelegate = AddressOf NewSubClassProc
            SetWindowSubclass(Label1.Handle, NewSubClassDelegate, 0, 0)
        End Sub
    End Class

    • Edited by YakirFx Sunday, October 6, 2019 5:14 PM
    • Marked as answer by YakirFx Monday, October 7, 2019 1:17 PM
    Sunday, October 6, 2019 8:45 AM

All replies

  • Hi,

    Do you mean the following hand cursors? It is Microsoft standard cursors.

    You can view the relevant documents.

    I hope I can help you.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, September 23, 2019 8:43 AM
    Moderator
  • The "hand" cursor is a part of the OS which is in use. 

    Therefore why do you think this is a bug. It is not related with VB for .Net 


    Success
    Cor

    Monday, September 23, 2019 12:23 PM
  • i mean old hand cursor is display not new hand cursor that come with windows 10

    I mean this

    I want the vb.net to display new hand cursor when i hover with mouse on lable link for example?


    • Edited by YakirFx Tuesday, September 24, 2019 4:07 AM
    Tuesday, September 24, 2019 3:56 AM
  • Hi,

    I'm sorry, the hand cursors you use in VB. net come with the operating system. If you want to use the latest hand cursors, you can customize them.

    Of course, you have to make sure that there are relevant .ini files or .cur files in the system.

    You can try my code as follows.

    Method 1
    1.Introducing two namespaces
    2.Import API
    3.Use your own mouse style

    Imports System.Reflection
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")>
        Public Shared Function LoadCursorFromFile(ByVal fileName As String) As IntPtr
    
        End Function
        <DllImport("user32.dll")>
        Public Shared Function SetCursor(ByVal cursorHandle As IntPtr) As IntPtr
    
        End Function
        <DllImport("user32.dll")>
        Public Shared Function DestroyCursor(ByVal cursorHandle As IntPtr) As UInteger
    
        End Function
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim myCursor As Cursor = New Cursor(Cursor.Current.Handle)
            Dim colorCursorHandle As IntPtr = LoadCursorFromFile("d:\aero_link_xl.cur")
            myCursor.[GetType]().InvokeMember("handle", BindingFlags.[Public] Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.SetField, Nothing, myCursor, New Object() {colorCursorHandle})
            Button1.Cursor = myCursor
        End Sub
    
    End Class

    Method two
    Without API, the mouse style only needs a background transparent picture, just like .PNG or .GIF format.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim a As Bitmap = CType(Bitmap.FromFile("d:\1.png"), Bitmap)
            SetCursor(a, New Point(0, 0))
        End Sub
    
        Public Sub SetCursor(ByVal cursor As Bitmap, ByVal hotPoint As Point)
            Dim hotX As Integer = hotPoint.X
            Dim hotY As Integer = hotPoint.Y
            Dim myNewCursor As Bitmap = New Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY)
            Dim g As Graphics = Graphics.FromImage(myNewCursor)
            g.Clear(Color.FromArgb(0, 0, 0, 0))
            g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width, cursor.Height)
            Button1.Cursor = New Cursor(myNewCursor.GetHicon())
            g.Dispose()
            myNewCursor.Dispose()
        End Sub
    End Class

    Finally, you can find information about cursors in this document.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 24, 2019 6:22 AM
    Moderator
  • i don't use LoadCursorFromFile("d:\aero_link_xl.cur")

    I saw example for vb6 not vb.net

    i want use the WM_SETCURSOR. command instead with API  ,how use it?


    • Edited by YakirFx Thursday, September 26, 2019 3:48 AM
    Wednesday, September 25, 2019 8:54 AM
  • Hi,

    If you want to use the WM_SETCURSOR command with API, you can try mode as follows.

    Please note, &HXY representing a hexadecimal number in VB.NET, can be converted to decimal system: X* 16 + Y. WM_SETCURSOR is equivalent to & H20 when the mouse enters the window and moves on it.

    If the mouse cursor moves in a window and the mouse input is not captured, the window receives the WM_SETCURSOR message. As long as the mouse moves, it automatically sends the WM_SETCURSOR and triggers OnSetCursor.

    Imports System.Runtime.InteropServices
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        End Sub
        Protected Overrides Sub WndProc(ByRef m As Message)
            Dim bitmap As Bitmap = New Bitmap("D:\hand.png")
            If m.Msg = &H20 Then
                Cursor.Current = CursorGenerator.CreateCursor(bitmap, 3, 3)
                m.Result = CType(1, IntPtr)
                Return
                'End If
            End If
            MyBase.WndProc(m)
        End Sub
        Class CursorGenerator
            <DllImport("user32.dll")>
            Public Shared Function GetIconInfo(ByVal hIcon As IntPtr, ByRef pIconInfo As IconInfo) As Boolean
            End Function
            <DllImport("user32.dll")>
            Public Shared Function CreateIconIndirect(ByRef icon As IconInfo) As IntPtr
            End Function
    
            Public Shared Function CreateCursor(ByVal bmp As Bitmap, ByVal xHotSpot As Integer, ByVal yHotSpot As Integer) As Cursor
                Dim ptr As IntPtr = bmp.GetHicon()
                Dim tmp As IconInfo = New IconInfo()
                GetIconInfo(ptr, tmp)
                tmp.xHotspot = xHotSpot
                tmp.yHotspot = yHotSpot
                tmp.fIcon = False
                ptr = CreateIconIndirect(tmp)
                Return New Cursor(ptr)
            End Function
    
            Public Structure IconInfo
                Public fIcon As Boolean
                Public xHotspot As Integer
                Public yHotspot As Integer
                Public hbmMask As IntPtr
                Public hbmColor As IntPtr
            End Structure
        End Class
    End Class

    If the issue still exist, please read this document, or this.I think these documents will be helpful.

    Best Regards,

    Julie



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 26, 2019 4:51 AM
    Moderator
  • Agian ,i don't use 

    Dim bitmap As Bitmap =New Bitmap("D:\hand.png")

    I want use this code at vb.net 

    [DllImport("user32.dll")]
    public static extern int LoadCursor(int hInstance, int lpCursorName);
    
    [DllImport("user32.dll")]
    public static extern int SetCursor(int hCursor);
    
    protected override void WndProc(ref Message m)
    {
        //WM_SETCURSOR == 32
        if (m.Msg == 32)
        {
            //IDC_HAND == 32649
            SetCursor(LoadCursor(0, 32649));
    
            //the message has been handled
            m.Result = IntPtr.Zero;
            return;
        }
    
        base.WndProc(ref m);
    }

    • Edited by YakirFx Friday, September 27, 2019 1:15 PM
    Friday, September 27, 2019 1:14 PM
  • I want use this code at vb.net 

    ' code

    For example :

        Public Const WM_SETCURSOR As Integer = &H20
        Public Const IDC_HAND As Integer = 32649
    
        <DllImport("User32.dll", SetLastError:=True)>
        Public Shared Function SetCursor(hCursor As IntPtr) As IntPtr
        End Function
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function LoadCursor(hInstance As IntPtr, lpCursorName As IntPtr) As IntPtr
        End Function
    
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_SETCURSOR Then
                SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
                m.Result = CType(1, IntPtr)
                Return
            Else
                MyBase.WndProc(m)
            End If
        End Sub

    Friday, September 27, 2019 2:16 PM
  • Wow ,thank you
    Friday, September 27, 2019 7:43 PM
  • Hi,

    Have you solved this problem now?

    I think the above reply can provide you with a solution, have you tried it?

    If so, hope you can close this thread by marking the reply as answer as this will help others looking for the same or similar issues down the road.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 30, 2019 1:08 AM
    Moderator
  • when i try mouse move function i get some flickers of the original arrow mouse

    i used code

    Private Sub Label1_MouseMove(sender As Object, e As MouseEventArgs) Handles Label1.MouseMove
            SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
        End Sub
    how to prevent those flickers

    Monday, September 30, 2019 1:13 PM
  • when i try mouse move function i get some flickers of the original arrow mouse

    i used code

    Private Sub Label1_MouseMove(sender As Object, e As MouseEventArgs) Handles Label1.MouseMove
            SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
        End Sub
    how to prevent those flickers

    But why don't you just use :

    Label1.Cursor = Cursors.Hand

    (otherwise in WM_SETCURSOR, like for the Form)

    Monday, September 30, 2019 2:31 PM
  • Because if i use 

    Label1.Cursor = Cursors.Hand

    Then use the old version of hand cursor ,and i want to use the new version of hand cursor instead old version cursor

    Monday, September 30, 2019 9:05 PM
  • Because if i use 

    Label1.Cursor = Cursors.Hand

    Then use the old version of hand cursor ,and i want to use the new version of hand cursor instead old version cursor

    Most things are easy to handle, but not when you write here statements likewise: "I want to go with my bicycle to the moon likewise I did in past". Not a problem if the "Moon" is just the name of a building two roads away from you but then you have to tell that.

    Again: what is in your perception an old cursor and what is a new one. I wrote already before. The standard new version of the hand cursor is the one which is given by your OS. 

    (In this thread is given all ways to make an own hand cursor, but that is not "the" hand cursor. The format of a user cursor stays a user cursor even if it looks like a hand) 


    Success
    Cor


    • Edited by Cor Ligthert Monday, September 30, 2019 10:51 PM
    Monday, September 30, 2019 10:50 PM
  • The code of castorix31 is fine ,it do the job but it appropriate to window or forms ,not common controls like label..

    Therefore i want customize the code in order to work with lable1 for example

    How to do that without any some flickers?

    Tuesday, October 1, 2019 1:43 PM
  • The code of castorix31 is fine ,it do the job but it appropriate to window or forms ,not common controls like label..

    Therefore i want customize the code in order to work with lable1 for example

    How to do that without any some flickers?

    It is the same code for child controls.

    You can use inherited classes.

    For example :

    Public Class CustomLabel                                                                    
        Inherits Label                                                                          
                                                                                                
        Public Const WM_SETCURSOR As Integer = &H20                                             
        Public Const IDC_HAND As Integer = 32649                                                
                                                                                                
        <DllImport("User32.dll", SetLastError:=True)>                                           
        Public Shared Function SetCursor(hCursor As IntPtr) As IntPtr                           
        End Function                                                                            
                                                                                                
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>                 
        Public Shared Function LoadCursor(hInstance As IntPtr, lpCursorName As IntPtr) As IntPtr
        End Function                                                                            
        Protected Overrides Sub WndProc(ByRef m As Message)                                     
            If m.Msg = WM_SETCURSOR Then                                                        
                SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))                     
                m.Result = CType(1, IntPtr)                                                     
                Return                                                                          
            Else                                                                                
                MyBase.WndProc(m)                                                               
            End If                                                                              
        End Sub                                                                                 
    End Class  

    then :

    Dim customLabel1 As CustomLabel

    customLabel1 = New CustomLabel()
    customLabel1.Location = New System.Drawing.Point(10, 10)
    customLabel1.Name = "Label1"
    customLabel1.Size = New System.Drawing.Size(97, 13)
    customLabel1.Text = "This is a text"
    Controls.Add(customLabel1)

    Tuesday, October 1, 2019 1:52 PM
  • castorix31 , how to use your code ?

    i try to put the code into a class ,but there is no code to do IDC_HAND for lable1

    i mean this

    its works ,but there is some flickers at you see in the picture

    how to avoid them?

    • Edited by YakirFx Wednesday, October 2, 2019 5:39 PM
    Tuesday, October 1, 2019 3:17 PM
  • castorix31 could you help me?
    Thursday, October 3, 2019 9:00 AM
  • I don't see any flicker and I have the old cursor on my environment (VS 2015, Windows 10) (default hand cursor not set to aero_link)


    • Edited by Castorix31 Thursday, October 3, 2019 9:50 AM
    Thursday, October 3, 2019 9:26 AM
  • right! me too about old hand cursor

    but i want to use your code for lable1 

    i want to use new hand cursor WITHOUT flickers between hand to arrow cursors

    Thursday, October 3, 2019 10:06 AM
  • As Julie Xu-MSFT said, LoadCursorFromFile must be used to force the new cursor if it is not set by default,

    from the c:\windows\cursors directory

    Public Class CustomLabel
        Inherits Label
    
        Public Const WM_SETCURSOR As Integer = &H20
        Public Const IDC_HAND As Integer = 32649
    
        <DllImport("User32.dll", SetLastError:=True)>
        Public Shared Function SetCursor(hCursor As IntPtr) As IntPtr
        End Function
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function LoadCursor(hInstance As IntPtr, lpCursorName As IntPtr) As IntPtr
        End Function
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function LoadCursorFromFile(lpFileName As String) As IntPtr
        End Function
    
        Public Sub New()
            Dim sWindowsPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Windows)
            Dim sCursorsPath = sWindowsPath + "\\cursors"
            Dim sAeroLinkpath = sCursorsPath + "\\aero_link.cur"
            hCursor = LoadCursorFromFile(sAeroLinkpath)
        End Sub
    
        Private hCursor As IntPtr = IntPtr.Zero
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_SETCURSOR Then
                SetCursor(hCursor)
                m.Result = CType(1, IntPtr)
                Return
            Else
                MyBase.WndProc(m)
            End If
        End Sub
    End Class


    Dim customLabel1 As CustomLabel
     	customLabel1 = New CustomLabel()
            customLabel1.Location = New System.Drawing.Point(10, 10)
            customLabel1.Name = "Label1"
            customLabel1.Size = New System.Drawing.Size(97, 23)
            customLabel1.Text = "This is a text"
            customLabel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
            Controls.Add(customLabel1)

    It does not flicker for me :





    • Edited by Castorix31 Thursday, October 3, 2019 10:44 AM
    Thursday, October 3, 2019 10:40 AM
  • But i dont use WINDOWS PATH of the cursors 
    Thursday, October 3, 2019 11:24 AM
  • But i dont use WINDOWS PATH of the cursors 

    I don't understand what you mean.

    System cursors are stored in %windir%\cursors (and custom schemes in registry)

    On my Windows 10 OS, the default Hand cursor is the old cursor.

    (that can be changed globally from rundll32 shell32.dll,Control_RunDLL main.cpl @0,1)

    If I want to load the new Hand cursor, I must load it from a cursor file.

    Thursday, October 3, 2019 11:48 AM
  • i explain 

    i want to use the code below 

     Public Const WM_SETCURSOR As Integer = &H20
        Public Const IDC_HAND As Integer = 32649
    
        <DllImport("User32.dll", SetLastError:=True)>
        Public Shared Function SetCursor(hCursor As IntPtr) As IntPtr
        End Function
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function LoadCursor(hInstance As IntPtr, lpCursorName As IntPtr) As IntPtr
        End Function
    
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_SETCURSOR Then
                SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
                m.Result = CType(1, IntPtr)
                Return
            Else
                MyBase.WndProc(m)
            End If
        End Sub

    FOR the lable1 NOT a form WITHOUT flickers

    Thursday, October 3, 2019 2:53 PM
  • i explain 

    i want to use the code below 

     Public Const WM_SETCURSOR As Integer = &H20
        Public Const IDC_HAND As Integer = 32649
    
        <DllImport("User32.dll", SetLastError:=True)>
        Public Shared Function SetCursor(hCursor As IntPtr) As IntPtr
        End Function
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function LoadCursor(hInstance As IntPtr, lpCursorName As IntPtr) As IntPtr
        End Function
    
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_SETCURSOR Then
                SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
                m.Result = CType(1, IntPtr)
                Return
            Else
                MyBase.WndProc(m)
            End If
        End Sub

    FOR the lable1 NOT a form WITHOUT flickers

    It is what I posted with the CustomLabel class,

    with LoadCursorFromFile because it won't work if the new cursor is not set by default (like on my OS) 

    (can also be done with SetWindowSubclass)


    • Edited by Castorix31 Thursday, October 3, 2019 3:06 PM
    Thursday, October 3, 2019 3:02 PM
  • ok how ,give me example 
    Thursday, October 3, 2019 3:17 PM
  • The other method with SetWindowSubclass, on an existing Label1.Handle:

    Public Delegate Function SUBCLASSPROC(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr, uIdSubclass As IntPtr, dwRefData As UInteger) As Integer
    
    <DllImport("Comctl32.dll", SetLastError:=True)>
    Public Shared Function SetWindowSubclass(hWnd As IntPtr, pfnSubclass As SUBCLASSPROC, uIdSubclass As UInteger, dwRefData As UInteger) As Boolean
    End Function
    
    <DllImport("Comctl32.dll", SetLastError:=True)>
    Public Shared Function DefSubclassProc(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr) As Integer
    End Function
    
    Public NewSubClassDelegate As SUBCLASSPROC
    
    Public Function NewSubClassProc(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr, uIdSubclass As IntPtr, dwRefData As UInteger) As Integer
        If (uMsg = WM_SETCURSOR) Then
            If (hCursor <> IntPtr.Zero) Then
                SetCursor(hCursor)
                Return 1
            End If
        Else
            Return DefSubclassProc(hWnd, uMsg, wParam, lParam)
        End If
    End Function
    
    Private hCursor As IntPtr = IntPtr.Zero


    Dim sWindowsPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Windows)
    Dim sCursorsPath = sWindowsPath + "\\cursors"
    Dim sAeroLinkpath = sCursorsPath + "\\aero_link.cur"
    hCursor = LoadCursorFromFile(sAeroLinkpath)
    
    NewSubClassDelegate = AddressOf NewSubClassProc
    SetWindowSubclass(Label1.Handle, NewSubClassDelegate, 0, 0)


    Thursday, October 3, 2019 3:59 PM
  • its works ,but

    if i hover on Lable1 with mouse new hand pointer several times i get System Stack Overflow Exception

    in line:

    Return DefSubclassProc(hWnd, uMsg, wParam, lParam)
    Thursday, October 3, 2019 5:41 PM
  • its works ,but

    if i hover on Lable1 with mouse new hand pointer several times i get System Stack Overflow Exception

    in line:

    Return DefSubclassProc(hWnd, uMsg, wParam, lParam)

    I cannot reproduce this on my OS

    Are you sure you called SetWindowSubclass only once, after the control has been initialized ?

    Thursday, October 3, 2019 7:31 PM
  • I called several times and after that occur this problem 
    Friday, October 4, 2019 3:26 AM
  • I called several times and after that occur this problem 

    Then call SetWindowSubclass once

    Otherwise, RemoveWindowSubclass  should be called
    • Edited by Castorix31 Friday, October 4, 2019 6:51 AM
    Friday, October 4, 2019 6:38 AM
  • when i try mouse move function i get some flickers of the original arrow mouse

    i used code

    Private Sub Label1_MouseMove(sender As Object, e As MouseEventArgs) Handles Label1.MouseMove
            SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
        End Sub
    how to prevent those flickers

    It flickers because you set cursor in mouse move. This means the system is changing it every mouse move event and causes the flicker.

    If you are not going to assign the cursor to the control as castor is showing then you should change cursor in mouseenter and mouse leave events for the control. ie set to the cursor you want for the control on mouse enter and set back to default with mouse leave.

    PS or if you must us mouse move do:

        If cursor <> mycursor then setcursor

    Friday, October 4, 2019 2:49 PM
  • i want to example of it tommytwotrain

    its sounds good idea

    Friday, October 4, 2019 6:16 PM
  • i want to example of it tommytwotrain

    its sounds good idea

    Yeah LOL!

    First off I don't really understand exactly what you have and am not sure that is the problem.

    I think I have been clear? What dont you understand? I think you can do it?

    Try it and then show us a complete example of what does not work for.

    None of this make me an example of what you think I want.

    :)

    Sorry I dont understand what the "old cursor" is.

    I fact I dont really see the blinking you see in your text example above do you see it in that animation can you describe exaclty what you mean.

    So you have to make the example.

    Plus I would not do what you are doing.

    I would make my own curser file and assign it to the control or to the system cursor like castor has mentioned.

    However, just put your code from the mouse move in the mouse enter event.  In the mouse leave its cusror = cursor.default. Then see if it stops.

    Or in mouse move check it as I show.

    So you try that and let us know if it works.

    Again I dont understand what you are doing all I can tell you is dont do that.

    If you want to know hot to make your own mycursor that is a different question.
      
         control.cursor = mycursor

         cursor = mycursor

    Friday, October 4, 2019 6:43 PM
  • ok i did this:

    Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
            SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
        End Sub
    
        Private Sub Label1_MouseLeave(sender As Object, e As EventArgs) Handles Label1.MouseLeave
            Cursor = Cursors.Default
        End Sub

    and nothing happens when i enter mouse ?

    Friday, October 4, 2019 6:57 PM
  • I don't understand why you don't want to use the method 1 from Julie Xu-MSFT, by loading the cursor from %windir%\cursors instead of a custom location, or the method with CustomLabel class (usual way to change the behaviour of a control)

    Both work fine on my Windows 10 OS (with system hand cursor (IDC_HAND) = old hand cursor by default)


    • Edited by Castorix31 Friday, October 4, 2019 7:31 PM
    Friday, October 4, 2019 7:16 PM
  • i want but could not because this picture below

    when I called setWindowsSubclass several times after that occur this problem

    i want to use this method! but how fix the problem

    Friday, October 4, 2019 7:31 PM
  • ok i did this:

    Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
            SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
        End Sub
    
        Private Sub Label1_MouseLeave(sender As Object, e As EventArgs) Handles Label1.MouseLeave
            Cursor = Cursors.Default
        End Sub

    and nothing happens when i enter mouse ?

    Well do it with the system cursor.hand first and see it work correctly. Then figure why your setcursor does not change it.

    And then control.cursor = cursor.hand and see how that works. That works right too?

    Does that setcursor code you have work at all for you? or is it junk?

    All right... I will make an example of how you should do it with your own cursor file. Give me a while...

    BTW what is the .cur file image you show above? is that the one you want do you have the file on your system?

    Stop with the api things you don't understand...  

     :)

    PS wait here try this:

    PictureBox1.Cursor = New Cursor("C:\bitmaps\icons\cursors\curshand.cur")

    With your cursor file.

    No wait we need to make a memory cursor from the file wait...


    Friday, October 4, 2019 7:32 PM
  • when I called setWindowsSubclass several times after that occur this problem

    i want to use this method! but how fix the problem

    But why do you call it in loop in Label1_MouseMove ???

    It must be called once (at the end of Form1_Load for example)


    • Edited by Castorix31 Friday, October 4, 2019 7:41 PM
    Friday, October 4, 2019 7:37 PM
  • Ok but i want to use this for lable not form
    Friday, October 4, 2019 8:06 PM
  • ok i did this:

    Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
            SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
        End Sub
    
        Private Sub Label1_MouseLeave(sender As Object, e As EventArgs) Handles Label1.MouseLeave
            Cursor = Cursors.Default
        End Sub

    and nothing happens when i enter mouse ?

    Well do it with the system cursor.hand first and see it work correctly. Then figure why your setcursor does not change it.

    And then control.cursor = cursor.hand and see how that works. That works right too?

    Does that setcursor code you have work at all for you? or is it junk?

    All right... I will make an example of how you should do it with your own cursor file. Give me a while...

    BTW what is the .cur file image you show above? is that the one you want do you have the file on your system?

    Stop with the api things you don't understand...  

     :)

    PS wait here try this:

    PictureBox1.Cursor = New Cursor("C:\bitmaps\icons\cursors\curshand.cur")

    With your cursor file.

    No wait we need to make a memory cursor from the file wait...


    Yes , i check the system cursor.hand and its work properly

    But 

    SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))

    Not working 

    Friday, October 4, 2019 8:15 PM
  • Yakir,

    Does your setcursor work for anything? Its very complicated.

    All right, here is a complete example of five ways to use a custom cursor from file that work.

    I dont see blinking?

    Anyone trying anything else is asking for headaches. Castor is an expert he can do it. There is no reason as vb.net already gives us the a way if we just find it learn it and do it.

    Note I make a memory file for the examples that are changing (4, 5). I dont make a new cursor each time. If I did then I would dispose the old one as well. Thats why you got out of memory errs.

    The eample makes all the controls. Just cut and paste the code into an empty form. Change the form name as required. Change the cursor file path to your cursor file. It has to be a real cursor file.

    'cursor examples
    Public Class Form10
        Private WithEvents Label1 As New Label With {.Parent = Me,
            .Location = New Point(50, 20), .Text = "Cursor = File"}
        Private WithEvents Label2 As New Label With {.Parent = Me, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(50, 70), .Text = "Cursor = Resource", .AutoSize = True}
        Private WithEvents Label3 As New Label With {.Parent = Me, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(50, 120), .Text = "Cursor = Mycursor", .AutoSize = True}
        Private WithEvents Label4 As New Label With {.Parent = Me, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(50, 170), .Text = "Cursor = MyCursor Mouse Move", .AutoSize = True}
        Private WithEvents Label5 As New Label With {.Parent = Me, .BorderStyle = BorderStyle.FixedSingle,
            .Location = New Point(50, 220), .Text = "Cursor = MyCursor Mouse Enter/Leave", .AutoSize = True}
    
        Private MyCursor As New Cursor("C:\bitmaps\icons\cursors\curshand.cur")
    
        Private Sub Form10_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            BackColor = Color.AliceBlue
    
            'from disc file - This shoule not be used repeatedly unless the new cursors are disposed
            Label1.Cursor = New Cursor("C:\bitmaps\icons\cursors\curshand.cur")
    
            'from resources 
            Label2.Cursor = New Cursor(New System.IO.MemoryStream(My.Resources.CURSHAND))
    
            'from memory file -this can be done repeatedly without dispoing its just a ref to the memory file
            Label3.Cursor = MyCursor
    
        End Sub
    
        Private Sub Label4_MouseMove(sender As Object, e As MouseEventArgs) Handles Label4.MouseMove
    
            If Label4.Cursor IsNot MyCursor Then Label4.Cursor = MyCursor
    
        End Sub
    
        Private Sub Label5_MouseEnter(sender As Object, e As EventArgs) Handles Label5.MouseEnter
    
            Cursor = MyCursor
    
        End Sub
    
        Private Sub Label5_MouseLeave(sender As Object, e As EventArgs) Handles Label5.MouseLeave
    
            Cursor = Cursors.Default
    
        End Sub
    
    End Class

    Edit: Oops I meant to use the app cursor for 5 not control I made the change.

    Friday, October 4, 2019 8:22 PM
  • No you don't understand me.

    I dont load cursor from files 

    I want to use the code 

    SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
    I want to load idc_hand and use it that's it?
    Friday, October 4, 2019 9:51 PM
  • No you don't understand me.

    I dont load cursor from files 

    I want to use the code 

    SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
    I want to load idc_hand and use it that's it?

    No you misunderstand me. Do it how I show you.

    Use MyCursor = setcursor.

    Friday, October 4, 2019 10:29 PM
  • PS

    The example shows ways to change the cursor using the possible events or assign to the control. One must do it at the right way and time or it flickers or out of memory errors. How you make the cursor file is one thing. How to change it without flicker is something else. I have shown you five ways to assign a cursor.

    You need to get a cursor and then show it. The example shows how to show it. Getting the cursor is the other problem. I suggest you capture it and make a .cur file if you cant find one. Resorce or not. Or make one using castors method but instead of using win proc to change it use the events I show.

    Friday, October 4, 2019 11:44 PM
  • OK , i made it...

    here my code after little changes

    Imports System.ComponentModel
    Imports System.Reflection
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Public Delegate Function SUBCLASSPROC(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr, uIdSubclass As IntPtr, dwRefData As UInteger) As Integer
        Public Const WM_SETCURSOR As Integer = &H20
        Public Const IDC_HAND As Integer = 32649
        Public NewSubClassDelegate As SUBCLASSPROC
    
        <DllImport("User32.dll", SetLastError:=True)>
        Public Shared Function SetCursor(hCursor As IntPtr) As IntPtr
        End Function
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function LoadCursor(hInstance As IntPtr, lpCursorName As IntPtr) As IntPtr
        End Function
        <DllImport("Comctl32.dll", SetLastError:=True)>
        Public Shared Function SetWindowSubclass(hWnd As IntPtr, pfnSubclass As SUBCLASSPROC, uIdSubclass As UInteger, dwRefData As UInteger) As Boolean
        End Function
        <DllImport("Comctl32.dll", SetLastError:=True)>
        Public Shared Function DefSubclassProc(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr) As Integer
        End Function
        Public Function NewSubClassProc(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr, uIdSubclass As IntPtr, dwRefData As UInteger) As Integer
            SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
            Return Nothing
        End Function
        Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
            SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
            NewSubClassDelegate = AddressOf NewSubClassProc
            SetWindowSubclass(Label1.Handle, NewSubClassDelegate, 0, 0)
        End Sub
        Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
            Label1.Dispose()
        End Sub
    End Class

    for everybody who want WITHOUT ANY FLICKERS ,WITHOUT ANY SOME ERRORS

    ALL Works !

    Tnk you Castorix31..

    • Edited by YakirFx Saturday, October 5, 2019 3:19 PM
    Saturday, October 5, 2019 1:50 PM
  • It must be called once (at the end of Form1_Load for example)

    what do you mean? 

    give me example please

    Example for what ?

    You just put the code in Form1_Load

    This works for me in Form1_Load for a Form containing a Label1 control =>

    Dim sWindowsPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Windows)
    Dim sCursorsPath As String = sWindowsPath + "\\cursors"
    Dim sAeroLinkpath As String = sCursorsPath + "\\aero_link.cur"
    Dim hCursorHand As IntPtr = LoadCursorFromFile(sAeroLinkpath)
    
    Dim cursorHand As Cursor = New Cursor(hCursorHand)
    Label1.Cursor = cursorHand

    (without LoadCursorFromFile, the resulting cursor is black if I do :

    Dim cursorHand As Cursor = New Cursor(sAeroLinkpath)

    )



    • Edited by Castorix31 Saturday, October 5, 2019 3:29 PM
    Saturday, October 5, 2019 3:26 PM
  • for everybody who want WITHOUT ANY FLICKERS ,WITHOUT ANY SOME ERRORS

    ALL Works !

    This code is not correct :

    • It disables all default events of Label1 control
    • It displays the old hand cursor on my Windows 10 OS, as I explained (Hand value = empty by default in HKEY_CURRENT_USER\Control Panel\Cursor)
    Saturday, October 5, 2019 4:20 PM
  • I see so many replies in this question thread based on 1990 style Win32 code, that I put here the normal way for VB programmers. This to prevent those who find this thread has no chance to see the easy modern way.

    Public Class Form1
        Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
            Cursor = Cursors.Hand
        End Sub
        Private Sub Label1_MouseLeave(sender As Object, e As EventArgs) Handles Label1.MouseLeave
            Cursor = Cursors.Default
        End Sub
    End Class


    Success
    Cor



    Saturday, October 5, 2019 4:31 PM
  • I see so many replies in this question thread based on 1990 style Win32 code, that I put here the normal way for VB programmers. This to prevent those who find this thread has no chance to see the easy modern way.

    Public Class Form1
        Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
            Cursor = Cursors.Hand
        End Sub
        Private Sub Label1_MouseLeave(sender As Object, e As EventArgs) Handles Label1.MouseLeave
            Cursor = Cursors.Default
        End Sub
    End Class


    Success
    Cor



    Hi Cor!

    Where did you go?

    Just for others that may come along...

    You have to read the thread. It has to be the new cursor. Or is it the old? Cant just load the cursor file. It has to be done the way OP shows with the old apis.

    For some reason the five vb.net examples I made above, which do the same thing as the apis castor just showed last, do not work for OP. Everything OP does flickers. Except OPs last api example I guess.

    This is the second or third time this type of situation has come up for OP if you read past threads. For some reason there is always some backwards way that has to be used.

    :)

    PS No offense YaKir! Just my opinion.



    Saturday, October 5, 2019 4:58 PM
  • Hi Tommy, 

    I'm just attending the forums on a lower attitude than in past (I'm not an MVP anymore, and I like that). I really don't care if there is written something stupid and you know that is far different from past. 

    In this thread I asked the OP several times what she did mean with "old" and "new" hand cursor. 

    Not one time she gave an answer. But several persons replying with going on with the old Win32 code she was using, even a Microsoft Moderator. In past I would given direct a reaction on this. Currently at the end I only show the way as normal persons in 2019 handle hoovering and changing the cursor, simply to give not the wrong idea for young people starting with VB. Of course I know more. In past we had on our website even a sample to make a animated cursor including some text. But Ken and I've closed that website. 

    But moreover, you know I like analogies. If somebody ask why the brake does not work anymore and then accuses the maker of the new brakes that what they made contains bugs while it shows up she uses stagecoach brakes on here car,  my strange feelings for right and wrong are still hit.  I'm a little bit stupid ain't it.

     

    :-)



    Success
    Cor



    Saturday, October 5, 2019 7:31 PM
  • Yeah.

    It just seemed like many of the regulars disappeared all at the same time.

    And very slow.

    I recall when I was new to the forum you said I should have seen it a few years ago. Now I know what you meant and it must seem even slower to you?

    Vb and PC in general is slow I guess.

    Sometimes get concerned about online friends all. Get to know folks like with my software, forums etc and one day they just disapear sometimes...

    And on and on...

    Saturday, October 5, 2019 8:09 PM
  • You right! the code is incorrect!

    is disables lable1 some events...

    ---

    finally! done..

    here my code without any problems or flickers, all works..

    Imports System.ComponentModel
    Imports System.Reflection
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Public Delegate Function SUBCLASSPROC(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr, uIdSubclass As IntPtr, dwRefData As UInteger) As Integer
        Public Const WM_SETCURSOR As Integer = &H20
        Public Const IDC_HAND As Integer = 32649
        Public NewSubClassDelegate As SUBCLASSPROC
        Private ReadOnly hCursor As IntPtr = IntPtr.Zero
       
        <DllImport("User32.dll", SetLastError:=True)>
        Public Shared Function SetCursor(hCursor As IntPtr) As IntPtr
        End Function
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function LoadCursor(hInstance As IntPtr, lpCursorName As IntPtr) As IntPtr
        End Function
        <DllImport("Comctl32.dll", SetLastError:=True)>
        Public Shared Function SetWindowSubclass(hWnd As IntPtr, pfnSubclass As SUBCLASSPROC, uIdSubclass As UInteger, dwRefData As UInteger) As Boolean
        End Function
        <DllImport("Comctl32.dll", SetLastError:=True)>
        Public Shared Function DefSubclassProc(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr) As Integer
        End Function
        Private Function NewSubClassProc(hWnd As IntPtr, uMsg As UInteger, wParam As IntPtr, lParam As IntPtr, uIdSubclass As IntPtr, dwRefData As UInteger) As Integer
            If (uMsg = WM_SETCURSOR) Then
                If (hCursor <> IntPtr.Zero) Then
                End If
                Return 1
            Else
                Return DefSubclassProc(hWnd, uMsg, wParam, lParam)
            End If
        End Function
        Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
            SetCursor(LoadCursor(IntPtr.Zero, CType(IDC_HAND, IntPtr)))
            NewSubClassDelegate = AddressOf NewSubClassProc
            SetWindowSubclass(Label1.Handle, NewSubClassDelegate, 0, 0)
        End Sub
    End Class

    • Edited by YakirFx Sunday, October 6, 2019 5:14 PM
    • Marked as answer by YakirFx Monday, October 7, 2019 1:17 PM
    Sunday, October 6, 2019 8:45 AM