Results 1 to 10 of 10

Thread: Toolbar and picturebox

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Toolbar and picturebox

    I have toolbar,How do I move cursor to the picturebox when I pressed button in the toolbar?

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Toolbar and picturebox

    Have a look at this link!

    It seems to be a very neat way of doing what you ask Only you will put the code in the button's click event of the toolbar

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Toolbar and picturebox

    Would this work for you?
    Code:
    Option Explicit
    
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
    
    Private Sub Command1_Click()
    Dim rc As RECT
        
        GetWindowRect Picture1.hwnd, rc
        SetCursorPos rc.Left, rc.Top
    
    End Sub

  4. #4
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Toolbar and picturebox

    That code works effectively as well

    The only difference now is if you want it to go to the center (the link i posted) of the picture box) or the corner (rhino's post)

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Toolbar and picturebox

    a'a it helpful... but how to make the cursor to the center of the picturebox?

  6. #6
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Toolbar and picturebox


  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Toolbar and picturebox

    Link you give to me is totally lot of code but the RhinoBull post is easier to identify

  8. #8
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Toolbar and picturebox

    Code:
    Private Sub cmdClick_Click()
    Const NUM_MOVES = 3000
    Dim pt As POINTAPI
    Dim cur_x As Single
    Dim cur_y As Single
    Dim dest_x As Single
    Dim dest_y As Single
    Dim dx As Single
    Dim dy As Single
    Dim i As Integer
    
        ScaleMode = vbPixels
        picClicker.ScaleMode = vbPixels
    
        GetCursorPos pt
        cur_x = pt.X * 65535 / ScaleX(Screen.Width, vbTwips, _
            vbPixels)
        cur_y = pt.Y * 65535 / ScaleY(Screen.Height, vbTwips, _
            vbPixels)
    
        pt.X = picClicker.ScaleWidth / 2
        pt.Y = picClicker.ScaleHeight / 2
        ClientToScreen picClicker.hwnd, pt
        dest_x = pt.X * 65535 / ScaleX(Screen.Width, vbTwips, _
            vbPixels)
        dest_y = pt.Y * 65535 / ScaleY(Screen.Height, vbTwips, _
            vbPixels)
    
        dx = (dest_x - cur_x) / NUM_MOVES
        dy = (dest_y - cur_y) / NUM_MOVES
        For i = 1 To NUM_MOVES - 1
            cur_x = cur_x + dx
            cur_y = cur_y + dy
            mouse_event _
                MOUSEEVENTF_ABSOLUTE + _
                MOUSEEVENTF_MOVE, _
                cur_x, cur_y, 0, 0
            DoEvents
        Next i
    
        mouse_event _
            MOUSEEVENTF_ABSOLUTE + _
            MOUSEEVENTF_MOVE + _
            MOUSEEVENTF_LEFTDOWN + _
            MOUSEEVENTF_LEFTUP, _
            dest_x, dest_y, 0, 0
    End Sub
    Does that look better Yea your right though his code is simpler but i am not sure if API might be faster or not.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: Toolbar and picturebox

    I got error at this line. The second line from top

    Dim pt As POINTAPI

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Toolbar and picturebox

    Quote Originally Posted by matrik02
    a'a it helpful... but how to make the cursor to the center of the picturebox?
    You just had to improvise a little.
    Code:
    Option Explicit
    
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
    
    Private Sub Command1_Click()
    Dim rc As RECT
        
        GetWindowRect Picture1.hwnd, rc
        SetCursorPos rc.Left + (rc.Right - rc.Left) / 2, rc.Top + (rc.Bottom - rc.Top) / 2
    
    End Sub

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