Results 1 to 19 of 19

Thread: [RESOLVED] Change cursor with code, by pressing keys, to the respective document.cur

Threaded View

  1. #1

    Thread Starter
    Join Date
    Jan 2016

    Resolved [RESOLVED] Change cursor with code, by pressing keys, to the respective document.cur

    I'm creating a project,
    with a no border [FormBorderStyle=None] transparent [BackColor=White][TransparencyKey=White] screen sized [Size=1366; 768] form (with 10 PictureBoxes):
    Name:  Form.jpg
Views: 432
Size:  7.7 KB
    ...and I need help to make the application work.

    I want it to change the cursor (doesn't matter the location, inside or outside the form, I want always the cursor selected) to one of my 9 cursors stored on my pc (one for each command Alt+1 Alt+2 Alt+3 Alt+4 Alt+5 Alt+6 Alt+7 Alt+8 Alt+9).
    I already have the code for the commands and the code to change the cursor, but it only works at the beggining (when the form is being loaded [Event Form1_Load] and at the end [Event Form1_FormClosed] so that it changes back to the default cursor).
    I tried to put on the if Alt+1to9 code but it just doesn't work.
    I think that I need to disable/unload the cursor, then change it, and enable/load it again but I don't know how to do it (or even destroy it and create a new one).
    I searched everywhere but I can't understand the informations on the Internet because I can't find any example working or it's in C#/C++.

    Here's all the code on the project:
    Imports System.Runtime.InteropServices
    Public Class Form1
        <DllImport("user32.dll")> Public Shared Function CopyIcon(ByVal hIcon As IntPtr) As IntPtr
        End Function
        <DllImport("user32.dll")> Private Shared Function LoadCursorFromFile(ByVal fileName As String) As IntPtr
        End Function
        <DllImport("User32.dll")> Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
        End Function
        <DllImport("user32.dll")> Private Shared Function SetSystemCursor(ByVal hCursor As IntPtr, ByVal id As Integer) As Boolean
        End Function
        <DllImport("User32.dll")> Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
        End Function
        Const OCR_NORMAL As UInteger = 32512
        Dim oldCursor As IntPtr
        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\Rúben\Desktop\Rúben\Texturas\Cursor Grass Block 3D.cur")), OCR_NORMAL)
            Me.PictureBox2.Location = Me.PictureBox1.PointToClient(Me.PointToScreen(Me.PictureBox2.Location))
            Me.PictureBox2.Parent = Me.PictureBox1
            Me.PictureBox3.Location = Me.PictureBox1.PointToClient(Me.PointToScreen(Me.PictureBox3.Location))
            Me.PictureBox3.Parent = Me.PictureBox1
            Me.PictureBox4.Location = Me.PictureBox1.PointToClient(Me.PointToScreen(Me.PictureBox4.Location))
            Me.PictureBox4.Parent = Me.PictureBox1
            Me.PictureBox5.Location = Me.PictureBox1.PointToClient(Me.PointToScreen(Me.PictureBox5.Location))
            Me.PictureBox5.Parent = Me.PictureBox1
            Me.PictureBox6.Location = Me.PictureBox1.PointToClient(Me.PointToScreen(Me.PictureBox6.Location))
            Me.PictureBox6.Parent = Me.PictureBox1
            Me.PictureBox7.Location = Me.PictureBox1.PointToClient(Me.PointToScreen(Me.PictureBox7.Location))
            Me.PictureBox7.Parent = Me.PictureBox1
            Me.PictureBox8.Location = Me.PictureBox1.PointToClient(Me.PointToScreen(Me.PictureBox8.Location))
            Me.PictureBox8.Parent = Me.PictureBox1
            Me.PictureBox9.Location = Me.PictureBox1.PointToClient(Me.PointToScreen(Me.PictureBox9.Location))
            Me.PictureBox9.Parent = Me.PictureBox1
            Me.PictureBox10.Location = Me.PictureBox1.PointToClient(Me.PointToScreen(Me.PictureBox10.Location))
            Me.PictureBox10.Parent = Me.PictureBox1
            RegisterHotKey(Me.Handle, 0, &H1, 0)
            RegisterHotKey(Me.Handle, 100, &H1, Keys.D1)
            RegisterHotKey(Me.Handle, 200, &H1, Keys.D2)
            RegisterHotKey(Me.Handle, 300, &H1, Keys.D3)
            RegisterHotKey(Me.Handle, 400, &H1, Keys.D4)
            RegisterHotKey(Me.Handle, 500, &H1, Keys.D5)
            RegisterHotKey(Me.Handle, 600, &H1, Keys.D6)
            RegisterHotKey(Me.Handle, 700, &H1, Keys.D7)
            RegisterHotKey(Me.Handle, 800, &H1, Keys.D8)
            RegisterHotKey(Me.Handle, 900, &H1, Keys.D9)
        End Sub
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = &H312 Then
                Dim id As IntPtr = m.WParam
                Select Case (id.ToString)
                    Case "0"
                        PictureBox1.Image = My.Resources.Toolbar_0
                        PictureBox1.Visible = True
                    Case "100"
                        PictureBox1.Image = My.Resources.Toolbar_1
                        PictureBox1.Visible = True
                    Case "200"
                        PictureBox1.Image = My.Resources.Toolbar_2
                        PictureBox1.Visible = True
                    Case "300"
                        PictureBox1.Image = My.Resources.Toolbar_3
                        PictureBox1.Visible = True
                    Case "400"
                        PictureBox1.Image = My.Resources.Toolbar_4
                        PictureBox1.Visible = True
                    Case "500"
                        PictureBox1.Image = My.Resources.Toolbar_5
                        PictureBox1.Visible = True
                    Case "600"
                        PictureBox1.Image = My.Resources.Toolbar_6
                        PictureBox1.Visible = True
                    Case "700"
                        PictureBox1.Image = My.Resources.Toolbar_7
                        PictureBox1.Visible = True
                    Case "800"
                        PictureBox1.Image = My.Resources.Toolbar_8
                        PictureBox1.Visible = True
                    Case "900"
                        PictureBox1.Image = My.Resources.Toolbar_9
                        PictureBox1.Visible = True
                End Select
            End If
        End Sub
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            PictureBox1.Visible = False
        End Sub
        Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
            UnregisterHotKey(Me.Handle, 0)
            UnregisterHotKey(Me.Handle, 100)
            UnregisterHotKey(Me.Handle, 200)
            UnregisterHotKey(Me.Handle, 300)
            UnregisterHotKey(Me.Handle, 400)
            UnregisterHotKey(Me.Handle, 500)
            UnregisterHotKey(Me.Handle, 600)
            UnregisterHotKey(Me.Handle, 700)
            UnregisterHotKey(Me.Handle, 800)
            UnregisterHotKey(Me.Handle, 900)
        End Sub
        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
    End Class
    Note:The fact that there's no border on the form makes the 'change the cursor back to the default' not work, making me have to change the cursor every time I run the app. The form is invisible so it's like the cursor it's outside the form. On the code above, the 2 lines that sets the cursor are on the load event and what I'm trying to accomplish is to have it on each case statment.
    My two threads about it:

    If you want the program, inform me where I should display it.
    Any questions about the project, please post and I will try to answer right away...
    Last edited by rdrmdr; Jan 29th, 2016 at 09:34 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width