Click to See Complete Forum and Search --> : Mouse (x,y)
DaoK
Jul 15th, 2001, 12:04 AM
I want to set the mouse pos (x,y)at a pixel with color .
Module :
Option Explicit
Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Declare Function SetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Type POINTAPI
x As Long
y As Long
End Type
Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Form:
Private Sub GO_Click()
Dim dc, pnt As POINTAPI, str As String, colr
pnt.x = x
pnt.y = y
dc = GetDC(0)
If GetPixel(dc, pnt.x, pnt.y) = RGB(255, 255, 255) Then
SetCursorPos pnt
End If
End Sub
I know that not working because I think I need to do a loop for the x one ranges from 0 to screen.width \ screen.twipsperpixelX and for the the y one ranges from 0 to screen.height \ screen.twipsperpixely.
But I never found how to do it yet. If some one can help me please reply.
amitabh
Jul 15th, 2001, 02:56 AM
Try using this code
y = 0
x = 0
Do
If GetPixel(dc, pnt.x, pnt.y) = RGB(255, 255, 255) Then
SetCursorPos pnt
Exit Do
End If
If y > Screen.Height / Screen.TwipsPerPixelY Then
Exit Do
End If
If x > Screen.Width / Screen.TwipsPerPixelX Then
x = 0
y = y + 1
Else
x = x + 1
End If
Loop While x <= Screen.Width / Screen.TwipsPerPixelX
I have no checked the code, so I am not sure whether it will work.
DaoK
Jul 15th, 2001, 08:49 AM
That code still not work but we are near of my goal. When I press the button nothing appear for moment so I think something not work but when I'm reading the code all still good :\ If you or someone else can work on it ( Im doing the same now ).
---------------------------
Module:
Option Explicit
Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Declare Function SetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Type POINTAPI
x As Long
y As Long
End Type
Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Form:
Dim dc, pnt As POINTAPI
y = 0
x = 0
Do
If GetPixel(dc, pnt.x, pnt.y) = RGB(255, 255, 255) Then
SetCursorPos pnt
Exit Do
End If
If y > Screen.Height / Screen.TwipsPerPixelY Then
Exit Do
End If
If x > Screen.Width / Screen.TwipsPerPixelX Then
x = 0
y = y + 1
Else
x = x + 1
End If
Loop While x <= Screen.Width / Screen.TwipsPerPixelX
End Sub
Thx amitabh we just need that work :)
DaoK
Jul 15th, 2001, 08:13 PM
Some one in a other site told me than this code is not good 2 can some here help me ?
Kaverin
Jul 15th, 2001, 11:07 PM
Are you just trying to set the cursor to the place where it finds the first white pixel? If that's what you're trying to do, you can just do this:
'assuming you already have the hDC
Dim x As Long, y As Long, blnFound As Boolean
blnFound = False
For x = 0 To (Screen.Width \ Screen.TwipsPerPixelX) - 1
For y = 0 To (Screen.Height \ Screen.TwipsPerPixelY) - 1
If GetPixel(hDC, x, y) = vbWhite Then
SetCursorPos x, y
blnFound = True
Exit For
End If
'the DoEvents isn't necessary, but it prevents the system from
'being hung up while it's searching pixels
DoEvents
Next y
If blnFound Then Exit For
Next x
I don't know where you got the declare for SetCursorPos, but I've never seen one that takes a pointer to a POINTAPI struct. That's the GetCursorPos that does. I've only seen SetCursorPos take the literal X and Y position. That might be the problem. You may want to use GetWindowDC as well, to ensure you get the entire window area, and not just the client portion that GetDC returns. If you use GetWindowDC though, be sure to use ReleaseDC on it when you're done.
DaoK
Jul 16th, 2001, 08:07 AM
Kaverin this code make an error on that line
SetCursorPos x, y
:\
Nice try but not work
Kaverin
Jul 17th, 2001, 11:09 AM
Sorry I didn't reply earlier, but I couldn't get into the site yesterday for some reason. The thing I gave you worked fine on my computer. Where did you get your declare of SetCursorPos from? I've never seen one that takes a POINTAPI pointer, only the direct X and Y. Did you change it so that you stuck the X and Y into the struct and then give it to SetCursorPos?
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.