dcsimg
Results 1 to 15 of 15

Thread: [RESOLVED] Change mouse's cursor outside form.

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2016
    Posts
    51

    Resolved [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.

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

    Re: Change mouse's cursor outside form.

    You can use the SetCursor API method...

    MSDN description:

    https://msdn.microsoft.com/en-us/lib...or=-2147217396

    SetCursor:

    http://www.pinvoke.net/default.aspx/...SetCursor.html

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2016
    Posts
    51

    Re: Change mouse's cursor outside form.

    Quote Originally Posted by .paul. View Post
    Can you please tell me the code, the links are in C++ and I can't figure it out.

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

    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

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,252

    Re: Change mouse's cursor outside form.

    Quote Originally Posted by rdrmdr View Post
    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

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2016
    Posts
    51

    Re: Change mouse's cursor outside form.

    Quote Originally Posted by .paul. View Post
    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?

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2016
    Posts
    51

    Re: Change mouse's cursor outside form.

    Quote Originally Posted by Edgemeal View Post
    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
    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.

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,252

    Re: Change mouse's cursor outside form.

    Quote Originally Posted by rdrmdr View Post
    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.

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2016
    Posts
    51

    Re: Change mouse's cursor outside form.

    Quote Originally Posted by Edgemeal View Post
    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.

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2016
    Posts
    51

    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:\ ...
    Name:  MyCursor.jpg
Views: 854
Size:  7.3 KB
    ...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
    Name:  Code.jpg
Views: 854
Size:  9.0 KB
    ...the result is:
    Name:  Form.png
Views: 876
Size:  8.1 KB
    ...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...
    Last edited by rdrmdr; Jan 28th, 2016 at 04:05 PM.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,460

    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...

  12. #12

    Thread Starter
    Member
    Join Date
    Jan 2016
    Posts
    51

    Re: Change mouse's cursor outside form.

    Quote Originally Posted by .paul. View Post
    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.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,460

    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

  14. #14

    Thread Starter
    Member
    Join Date
    Jan 2016
    Posts
    51

    Re: Change mouse's cursor outside form.

    Quote Originally Posted by .paul. View Post
    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)

  15. #15
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,252

    Re: Change mouse's cursor outside form.

    Quote Originally Posted by rdrmdr View Post
    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
  •  



Featured


Click Here to Expand Forum to Full Width