Results 1 to 7 of 7

Thread: setting a cursor hotspot (icon cursor)

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    7

    setting a cursor hotspot (icon cursor)

    I have a custom cursor that I have as a ico file as cur files are only mono coloured and 32x32.

    The only problem in using a ico file is that I can't set where the hot spot is and at the moment it is set in the middle of the icon which is no help

    I was looking on the msdn website and it said to set the hotspot like this


    Code:
    Dim instance As Cursor
    Dim value As Point
    
    value = instance.HotSpot
    But I don't know how to set the point value to (0,54) which is where I want the hotspot to be located.

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

    Re: setting a cursor hotspot (icon cursor)

    heres an example:

    vb Code:
    1. #Region "CreateIconIndirect"
    2.  
    3.     Private Structure IconInfo
    4.         Public fIcon As Boolean
    5.         Public xHotspot As Int32
    6.         Public yHotspot As Int32
    7.         Public hbmMask As IntPtr
    8.         Public hbmColor As IntPtr
    9.     End Structure
    10.  
    11.     <DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _
    12.     Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr
    13.     End Function
    14.  
    15.     <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    16.     Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
    17.     End Function
    18.  
    19.     <DllImport("gdi32.dll")> _
    20.     Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
    21.     End Function
    22.  
    23.     Public Function CreateCursor(ByVal bmp As Bitmap) As Cursor
    24.  
    25.         'Setup the Cursors IconInfo
    26.         Dim tmp As New IconInfo
    27.         tmp.xHotspot = 0
    28.         tmp.yHotspot = 54
    29.         tmp.fIcon = False
    30.         tmp.hbmMask = bmp.GetHbitmap()
    31.         tmp.hbmColor = bmp.GetHbitmap()
    32.        
    33.         'Create the Pointer for the Cursor Icon
    34.         Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(tmp))
    35.         Marshal.StructureToPtr(tmp, pnt, True)
    36.         Dim curPtr As IntPtr = CreateIconIndirect(pnt)
    37.  
    38.         'Clean Up
    39.         DestroyIcon(pnt)
    40.         DeleteObject(tmp.hbmMask)
    41.         DeleteObject(tmp.hbmColor)
    42.  
    43.         Return New Cursor(curPtr)
    44.     End Function
    45.  
    46. #End Region

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    7

    Re: setting a cursor hotspot (icon cursor)

    I did see that code before for making a bmp into a cursor

    but where about's do I put the file location for the bmp if i made one of the ico file

    also im getting a error at the > of each dll import

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

    Re: setting a cursor hotspot (icon cursor)

    the error - you need to import:

    vb Code:
    1. Imports System.Runtime.InteropServices

    to create a cursor from an icon, add the code above to your form, then to call it:

    vb Code:
    1. me.cursor = CreateCursor(yourIcon.tobitmap)

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    7

    Re: setting a cursor hotspot (icon cursor)

    sorry do you mean like this

    Code:
    me.cursor = CreateCursor(F:\Cursors\curShark.ico.tobitmap)

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

    Re: setting a cursor hotspot (icon cursor)

    vb Code:
    1. dim newIcon as new icon("F:\Cursors\curShark.ico")
    2. me.cursor = CreateCursor(newIcon.tobitmap)

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    7

    Re: setting a cursor hotspot (icon cursor)

    thank you so much would have killed me to not be able to use this icon

Posting Permissions

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



Click Here to Expand Forum to Full Width