|
-
Jan 28th, 2016, 08:55 AM
#1
Thread Starter
Member
[RESOLVED] Change mouse's cursor outside form.
I found out how to change the cursor of the mouse inside the form...
Code:
Public Class Form1
<System.Runtime.InteropServices.DllImport("user32.dll")> Private Shared Function LoadCursorFromFile(ByVal fileName As String) As IntPtr
End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Cursor = New Windows.Forms.Cursor(LoadCursorFromFile("C:\Users\-----\Desktop\-----\Textures\----- ----- --.cur"))
End Sub
End Class
...but what I need is the custom cursor to be always on, inside and outside the form.
I use Microsoft Visual Basic 2010 Express.
-
Jan 28th, 2016, 12:47 PM
#2
Re: Change mouse's cursor outside form.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 28th, 2016, 01:34 PM
#3
Thread Starter
Member
Re: Change mouse's cursor outside form.
 Originally Posted by .paul.
Can you please tell me the code, the links are in C++ and I can't figure it out.
-
Jan 28th, 2016, 01:46 PM
#4
Re: Change mouse's cursor outside form.
ok...
Code:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Public Shared Function SetCursor(ByVal hCursor As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> Private Shared Function LoadCursorFromFile(ByVal fileName As String) As IntPtr
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetCursor(LoadCursorFromFile("C:\Users\-----\Desktop\-----\Textures\----- ----- --.cur"))
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 28th, 2016, 01:50 PM
#5
Re: Change mouse's cursor outside form.
 Originally Posted by rdrmdr
Can you please tell me the code, the links are in C++ and I can't figure it out.
Do you want to change the current cursor for Windows? If yes then I think you need to use SetSystemCursor API,
Attached VB10 project based on code from ,...
http://www.vb-helper.com/howto_set_system_cursor.html
.paul. , http://www.vbforums.com/showthread.php?t=667713
-
Jan 28th, 2016, 01:56 PM
#6
Thread Starter
Member
Re: Change mouse's cursor outside form.
 Originally Posted by .paul.
ok...
Code:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Public Shared Function SetCursor(ByVal hCursor As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> Private Shared Function LoadCursorFromFile(ByVal fileName As String) As IntPtr
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetCursor(LoadCursorFromFile("C:\Users\-----\Desktop\-----\Textures\----- ----- --.cur"))
End Sub
End Class
I tested it and it works only for a fraction of a second, on the form, when it's being load...
Do I need some kind of timer or anything?
-
Jan 28th, 2016, 02:04 PM
#7
Thread Starter
Member
Re: Change mouse's cursor outside form.
 Originally Posted by Edgemeal
Can you explain it? I can't understand most of the information on the links. 
I have searched a lot but there's never an example that actually works first try.
-
Jan 28th, 2016, 02:06 PM
#8
Re: Change mouse's cursor outside form.
 Originally Posted by rdrmdr
Can you explain it? I can't understand most of the information on the links. 
I have searched a lot but there's never an example that actually works first try.
Not really, just that demo I posted is old and I forget all that stuff, would have to go to MSDN and readup on those APIs, have fun.
-
Jan 28th, 2016, 02:09 PM
#9
Thread Starter
Member
Re: Change mouse's cursor outside form.
 Originally Posted by Edgemeal
Not really, just that demo I posted is old and I forget all that stuff, would have to go to MSDN and readup on those APIs, have fun.
Okay... 
Thanks anyway, I guess.
-
Jan 28th, 2016, 03:54 PM
#10
Thread Starter
Member
Re: Change mouse's cursor outside form.
Last edited by rdrmdr; Jan 28th, 2016 at 04:05 PM.
-
Jan 28th, 2016, 04:06 PM
#11
Re: Change mouse's cursor outside form.
Ok so you save the current windows cursor in a form level variable, use the code i showed you but as edgemeal said it's setsystemcursor which was what i meant to show you, to change the system cursor. It's important to save your system cursor first, or you won't be able to restore it in your form_formclosed event with the same code you use in form_load where you set your custom cursor...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 28th, 2016, 04:12 PM
#12
Thread Starter
Member
Re: Change mouse's cursor outside form.
 Originally Posted by .paul.
Ok so you save the current windows cursor in a form level variable, use the code i showed you but as edgemeal said it's setsystemcursor which was what i meant to show you, to change the system cursor. It's important to save your system cursor first, or you won't be able to restore it in your form_formclosed event with the same code you use in form_load where you set your custom cursor...
I know you probably don't wanna have the work to writte all the code like I'm not an expert VB programmer but can you do it? 
Please... *Sad Dog's Face* 
Someone once said:
A real reply always comes with an example! -Unknown (it was me  )
Last edited by rdrmdr; Jan 28th, 2016 at 04:16 PM.
-
Jan 28th, 2016, 04:32 PM
#13
Re: Change mouse's cursor outside form.
try this:
Code:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Private Shared Function SetSystemCursor(ByVal hCursor As IntPtr, ByVal id As Integer) As Boolean
End Function
<DllImport("user32.dll")> Private Shared Function LoadCursorFromFile(ByVal fileName As String) As IntPtr
End Function
<DllImport("user32.dll")> _
Public Shared Function CopyIcon(ByVal hIcon As IntPtr) As IntPtr
End Function
Const OCR_NORMAL As UInteger = 32512
Dim oldCursor As IntPtr
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
SetSystemCursor(CopyIcon(oldCursor), OCR_NORMAL)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
oldCursor = CopyIcon(Cursors.Default.CopyHandle)
SetSystemCursor(CopyIcon(LoadCursorFromFile("C:\Users\-----\Desktop\-----\Textures\----- ----- --.cur")), OCR_NORMAL)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 28th, 2016, 04:40 PM
#14
Thread Starter
Member
Re: Change mouse's cursor outside form.
 Originally Posted by .paul.
try this:
Code:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Private Shared Function SetSystemCursor(ByVal hCursor As IntPtr, ByVal id As Integer) As Boolean
End Function
<DllImport("user32.dll")> Private Shared Function LoadCursorFromFile(ByVal fileName As String) As IntPtr
End Function
<DllImport("user32.dll")> _
Public Shared Function CopyIcon(ByVal hIcon As IntPtr) As IntPtr
End Function
Const OCR_NORMAL As UInteger = 32512
Dim oldCursor As IntPtr
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
SetSystemCursor(CopyIcon(oldCursor), OCR_NORMAL)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
oldCursor = CopyIcon(Cursors.Default.CopyHandle)
SetSystemCursor(CopyIcon(LoadCursorFromFile("C:\Users\-----\Desktop\-----\Textures\----- ----- --.cur")), OCR_NORMAL)
End Sub
End Class
YES, it works! Thanks 
(the only time it shows the default for a second is if I try to resize but that's not a problem because I'm gonna use this on an transparent form with the size of my screen)
-
Jan 29th, 2016, 11:35 AM
#15
Re: Change mouse's cursor outside form.
 Originally Posted by rdrmdr
YES, it works! Thanks 
(the only time it shows the default for a second is if I try to resize but that's not a problem because I'm gonna use this on an transparent form with the size of my screen)
That's because the code is only changing the Standard arrow (OCR_NORMAL) cursor, when you move the mouse over say Notepads edit area the cursor will change to a caret (OCR_IBEAM) cursor unless you changed that too!
See, SetSystemCursor function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|