I have toolbar,How do I move cursor to the picturebox when I pressed button in the toolbar?
Printable View
I have toolbar,How do I move cursor to the picturebox when I pressed button in the toolbar?
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 ;)
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
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) ;)
a'a it helpful... but how to make the cursor to the center of the picturebox?
Have a look at the link ;)
http://www.vb-helper.com/howto_move_click_mouse.html
Link you give to me is totally lot of code but the RhinoBull post is easier to identify :bigyello:
Does that look better :) Yea your right though his code is simpler but i am not sure if API might be faster or not.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
I got error at this line. The second line from top
Dim pt As POINTAPI
You just had to improvise a little. ;)Quote:
Originally Posted by matrik02
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