PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Change cursor image-VBForums
Results 1 to 15 of 15

Thread: Change cursor image

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Change cursor image

    How do you change the cursor image to an image from the project resources?

    The methods I have found either don't work or only change the image within the form, and reverts back to default when the mouse leaves the window.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Change cursor image

    you can use the SetSystemCursor api:

    http://social.msdn.microsoft.com/For...a-ad65de753b86

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Change cursor image

    Quote Originally Posted by .paul. View Post
    you can use the SetSystemCursor api:

    http://social.msdn.microsoft.com/For...a-ad65de753b86
    How exactly do I use that? Can you give an example of changing the cursor to curs.ico and then back to the default again?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Change cursor image

    ok. upload curs.ico + i'll write an example

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Change cursor image

    i couldn't use your .ico file.
    i've attached the file i used at the end of this post.
    be careful using this code as it's difficult to restore you standard arrow cursor:

    vb Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.  
    5.     <DllImport("user32.dll")> _
    6.     Friend Shared Function SetSystemCursor(ByVal hCursor As IntPtr, ByVal id As UInteger) As Boolean
    7.     End Function
    8.  
    9.     <DllImport("user32.dll")> _
    10.     Friend Shared Function LoadCursor(ByVal hInstance As IntPtr, ByVal id As UInteger) As IntPtr
    11.     End Function
    12.  
    13.     Public Const OCR_CROSS As UInteger = 32515
    14.     Public Const OCR_WAIT As UInteger = 32514
    15.     Public Const IDC_ARROW As UInteger = 32512
    16.  
    17.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    18.         Dim b As New Bitmap("C:\Users\Paul\Desktop\picker.png")
    19.         b.MakeTransparent(Color.White)
    20.         Dim c As Cursor = create.CreateCursor(b, 0, b.Height)
    21.         SetSystemCursor(c.Handle, IDC_ARROW)
    22.     End Sub
    23.  
    24.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    25.         Dim currentDefaultCursor As New Cursor("C:\Windows\Cursors\arrow_i.cur")
    26.         SetSystemCursor(currentDefaultCursor.Handle, IDC_ARROW)
    27.     End Sub
    28.  
    29. End Class

    vb Code:
    1. Imports System.Windows.Forms
    2. Imports System.Runtime.InteropServices
    3. Imports System.drawing
    4. ''' <summary>
    5. ''' contains API + local functions for creating
    6. ''' a cursor from a bitmap on the fly
    7. ''' </summary>
    8. ''' <remarks></remarks>
    9. Public Class create
    10.  
    11. #Region "   CreateIconIndirect"
    12.  
    13.     Private Structure IconInfo
    14.         Public fIcon As Boolean
    15.         Public xHotspot As Int32
    16.         Public yHotspot As Int32
    17.         Public hbmMask As IntPtr
    18.         Public hbmColor As IntPtr
    19.     End Structure
    20.  
    21.     <DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _
    22.     Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr
    23.     End Function
    24.  
    25.     <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    26.     Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
    27.     End Function
    28.  
    29.     <DllImport("gdi32.dll")> _
    30.     Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
    31.     End Function
    32.  
    33.     ''' <summary>
    34.     ''' CreateCursor
    35.     ''' </summary>
    36.     ''' <param name="bmp"></param>
    37.     ''' <returns>custom Cursor</returns>
    38.     ''' <remarks>creates a custom cursor from a bitmap</remarks>
    39.     Public Shared Function CreateCursor(ByVal bmp As Bitmap, ByVal xHotspot As Integer, ByVal yHotspot As Integer) As Cursor
    40.         'Setup the Cursors IconInfo
    41.         Dim tmp As New IconInfo
    42.         tmp.xHotspot = xHotspot
    43.         tmp.yHotspot = yHotspot
    44.         tmp.fIcon = False
    45.         tmp.hbmMask = bmp.GetHbitmap()
    46.         tmp.hbmColor = bmp.GetHbitmap()
    47.  
    48.         'Create the Pointer for the Cursor Icon
    49.         Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(tmp))
    50.         Marshal.StructureToPtr(tmp, pnt, True)
    51.         Dim curPtr As IntPtr = CreateIconIndirect(pnt)
    52.  
    53.         'Clean Up
    54.         DestroyIcon(pnt)
    55.         DeleteObject(tmp.hbmMask)
    56.         DeleteObject(tmp.hbmColor)
    57.  
    58.         Return New Cursor(curPtr)
    59.     End Function
    60.  
    61. #End Region
    62.  
    63. End Class
    Attached Images Attached Images  

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Change cursor image

    How do I change it to an image from the resources, rather than "C:\Users\Paul\Desktop\picker.png"?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Change cursor image

    if that image is the 1 you uploaded you won't be able to use it.

    vb Code:
    1. Dim b As Bitmap = my.resources.[resourceName]

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Change cursor image

    It works, but it doesn't revert back to the default cursor, instead it turns it into one with a negative color.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Change cursor image

    i warned you about that

    as i said i couldn't find the original cursor that was changed + the black cursor is what i could find

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Change cursor image

    to restore your cursor, go to control panel + search for cursor
    vb.net doesn't appear to be able to use aero cursors

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Change cursor image

    Nevermind then... Guess I'll have to do without changing the cursor.

  13. #13
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,252

    Re: Change cursor image

    Quote Originally Posted by .paul. View Post
    i warned you about that

    as i said i couldn't find the original cursor that was changed + the black cursor is what i could find
    This seems to work for me in XP32, got the idea from vb6 code on vbhelper.com.

    Code:
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private old_cursor As IntPtr
    
        Private Declare Function GetCursor Lib "user32.dll" () As IntPtr
    
        <DllImport("user32.dll")> _
        Public Shared Function CopyIcon(ByVal hIcon As IntPtr) As IntPtr
        End Function
    
        <DllImport("user32.dll")> _
        Friend Shared Function SetSystemCursor(ByVal hCursor As IntPtr, ByVal id As UInteger) As Boolean
        End Function
    
        Public Const IDC_ARROW As UInteger = 32512
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            old_cursor = GetCursor()
            old_cursor = CopyIcon(old_cursor)
            '
            Dim b As New Bitmap("R:\picker.png") ' our image
            b.MakeTransparent(Color.White)
            Dim c As Cursor = create.CreateCursor(b, 0, b.Height)
            SetSystemCursor(c.Handle, IDC_ARROW)
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            SetSystemCursor(old_cursor, IDC_ARROW)
        End Sub
    
    End Class
    Last edited by Edgemeal; Dec 29th, 2011 at 12:50 AM. Reason: remove uneeded api

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Change cursor image

    @EdgeMeal. good find. that works in win7 too.

  15. #15
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,252

    Re: Change cursor image

    Quote Originally Posted by .paul. View Post
    @EdgeMeal. good find. that works in win7 too.
    Good to know, thanks.
    Last edited by Edgemeal; Dec 17th, 2011 at 09:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width