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.
Printable View
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.
you can use the SetSystemCursor api:
http://social.msdn.microsoft.com/For...a-ad65de753b86
ok. upload curs.ico + i'll write an example
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:
Imports System.Runtime.InteropServices Public Class Form1 <DllImport("user32.dll")> _ Friend Shared Function SetSystemCursor(ByVal hCursor As IntPtr, ByVal id As UInteger) As Boolean End Function <DllImport("user32.dll")> _ Friend Shared Function LoadCursor(ByVal hInstance As IntPtr, ByVal id As UInteger) As IntPtr End Function Public Const OCR_CROSS As UInteger = 32515 Public Const OCR_WAIT As UInteger = 32514 Public Const IDC_ARROW As UInteger = 32512 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim b As New Bitmap("C:\Users\Paul\Desktop\picker.png") 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 Dim currentDefaultCursor As New Cursor("C:\Windows\Cursors\arrow_i.cur") SetSystemCursor(currentDefaultCursor.Handle, IDC_ARROW) End Sub End Class
vb Code:
Imports System.Windows.Forms Imports System.Runtime.InteropServices Imports System.drawing ''' <summary> ''' contains API + local functions for creating ''' a cursor from a bitmap on the fly ''' </summary> ''' <remarks></remarks> Public Class create #Region " CreateIconIndirect" Private Structure IconInfo Public fIcon As Boolean Public xHotspot As Int32 Public yHotspot As Int32 Public hbmMask As IntPtr Public hbmColor As IntPtr End Structure <DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _ Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr End Function <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean End Function <DllImport("gdi32.dll")> _ Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean End Function ''' <summary> ''' CreateCursor ''' </summary> ''' <param name="bmp"></param> ''' <returns>custom Cursor</returns> ''' <remarks>creates a custom cursor from a bitmap</remarks> Public Shared Function CreateCursor(ByVal bmp As Bitmap, ByVal xHotspot As Integer, ByVal yHotspot As Integer) As Cursor 'Setup the Cursors IconInfo Dim tmp As New IconInfo tmp.xHotspot = xHotspot tmp.yHotspot = yHotspot tmp.fIcon = False tmp.hbmMask = bmp.GetHbitmap() tmp.hbmColor = bmp.GetHbitmap() 'Create the Pointer for the Cursor Icon Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(tmp)) Marshal.StructureToPtr(tmp, pnt, True) Dim curPtr As IntPtr = CreateIconIndirect(pnt) 'Clean Up DestroyIcon(pnt) DeleteObject(tmp.hbmMask) DeleteObject(tmp.hbmColor) Return New Cursor(curPtr) End Function #End Region End Class
How do I change it to an image from the resources, rather than "C:\Users\Paul\Desktop\picker.png"?
if that image is the 1 you uploaded you won't be able to use it.
vb Code:
Dim b As Bitmap = my.resources.[resourceName]
It works, but it doesn't revert back to the default cursor, instead it turns it into one with a negative color.
i warned you about that:D
as i said i couldn't find the original cursor that was changed + the black cursor is what i could find
to restore your cursor, go to control panel + search for cursor
vb.net doesn't appear to be able to use aero cursors
Nevermind then... Guess I'll have to do without changing the cursor.
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
@EdgeMeal. good find. that works in win7 too.