[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.
Re: Change mouse's cursor outside form.
Re: Change mouse's cursor outside form.
Quote:
Originally Posted by
.paul.
Can you please tell me the code, the links are in C++ and I can't figure it out.
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
Re: Change mouse's cursor outside form.
Quote:
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
Re: Change mouse's cursor outside form.
Quote:
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?
Re: Change mouse's cursor outside form.
Quote:
Originally Posted by
Edgemeal
Can you explain it? I can't understand most of the information on the links. :confused:
I have searched a lot but there's never an example that actually works first try.
Re: Change mouse's cursor outside form.
Quote:
Originally Posted by
rdrmdr
Can you explain it? I can't understand most of the information on the links. :confused:
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.
Re: Change mouse's cursor outside form.
Quote:
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.
3 Attachment(s)
Re: Change mouse's cursor outside form.
I will try to explain my problem a little better...
I have a document MyCursor.cur on the location C:\ ...
Attachment 134749
...and I have a New Project (Windows Forms Application) with the
Code:
Public Class Form1
<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:\MyCursor.cur"))
End Sub
End Class
Attachment 134751
...the result is:
Attachment 134753
...but what I want is to set the cursor for inside and outside of the form (anywhere).
Only while the program is running (of course), when I stop the program I want the default cursor back... ;)
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...
Re: Change mouse's cursor outside form.
Quote:
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? :blush:
Please... *Sad Dog's Face* :cry:
Someone once said:
Quote:
A real reply always comes with an example! -Unknown (it was me :rolleyes:)
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
Re: Change mouse's cursor outside form.
Quote:
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 :bigyello:
(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)
Re: Change mouse's cursor outside form.
Quote:
Originally Posted by
rdrmdr
YES, it works! Thanks :bigyello:
(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