1 Replies - 4832 Views - Last Post: 07 January 2009 - 06:14 AM Rate Topic: -----

#1 josh.net  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-December 08

Cursor Changer

Posted 06 January 2009 - 10:52 PM

Hi. I am currently creating a program that will change the cursor's image and let it stay like that until the user clicks use default image button. I do not mean like
Me.Cursor = New Cursor("C:\cursor.ico")
I mean as in even if the user's cursor leaves the form, it will still display the custom cursor only.

I have gotten only this much done so far, and the code is not 100% working either.

Private Sub ChangeCursorButton_Click()
	 System.Windows.Forms.Cursor.Hide() 'only hides the cursor when your mouse is on the form, need to fix that
	 Me.Cursor = New Cursor("cursor.ico") 'had no choice but to use this for now, even if when your cursor is out of the form it will revert to the original.
End Sub

Private Sub DefaultCursorButton_Click()
End Sub

The code only works if your cursor is on the form. If its not, it doesn't work as intended.
I need the code to work that the cursor will hide the original cursor and let the custom cursor I declared shown.
Please help.

Is This A Good Question/Topic? 0
  • +

Replies To: Cursor Changer

#2 dklingman  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: Cursor Changer

Posted 07 January 2009 - 06:14 AM

here's where i got most of my info from on how to do this and here's the code.


Public Class Form1
	Private Declare Function SetSystemCursor Lib "user32.dll" (ByVal hCursor As IntPtr, ByVal id As Integer) As Boolean
	Private Declare Function LoadCursorFromFile Lib "user32.dll" Alias "LoadCursorFromFileA" (ByVal lpFileName As String) As IntPtr

	'This is the handle for the default pointer
	Private Const OCR_ARROW As UInt32 = 32512

	Dim DefaultCursor As Icon

	Private Sub ChangeCursorButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeCursorButton.Click
		DefaultCursor = Icon.FromHandle(Cursors.Arrow.CopyHandle)

		'change arrow cursor to yours
		Dim YourCursor As IntPtr = LoadCursorFromFile("YOUR CURSOR")

		'make sure your cursor's handled was loaded.
		If YourCursor = IntPtr.Zero Then
			MessageBox.Show("Error loading cursor from file.")
		End If

		'set and test to see if the cursor was indeed set
		'note what this is doing is the following. it's using 
		'the handle of the new cursor, (your cursor) and changing
		'the system pointer, (arrow) cursor.
		If SetSystemCursor(YourCursor, OCR_ARROW) = 0 Then
			MessageBox.Show("Error setting system cursor.")
		End If

		ChangeCursorButton.Enabled = False
	End Sub

	Private Sub DefaultCursorButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DefaultCursorButton.Click
		Dim DefaultCursorHandle As IntPtr = DefaultCursor.Handle

		SetSystemCursor(DefaultCursorHandle, OCR_ARROW)
	End Sub
End Class

This post has been edited by dklingman: 07 January 2009 - 06:15 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1