Results 1 to 7 of 7

Thread: Mouse (x,y)

  1. #1
    DaoK
    Guest

    Mouse (x,y)

    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.

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    Try using this code
    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.

  3. #3
    DaoK
    Guest
    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

  4. #4
    DaoK
    Guest
    Some one in a other site told me than this code is not good 2 can some here help me ?

  5. #5
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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:
    VB Code:
    1. 'assuming you already have the hDC
    2. Dim x As Long, y As Long, blnFound As Boolean
    3. blnFound = False
    4. For x = 0 To (Screen.Width \ Screen.TwipsPerPixelX) - 1
    5.    For y = 0 To (Screen.Height \ Screen.TwipsPerPixelY) - 1
    6.       If GetPixel(hDC, x, y) = vbWhite Then
    7.          SetCursorPos x, y
    8.          blnFound = True
    9.          Exit For
    10.       End If
    11.       'the DoEvents isn't necessary, but it prevents the system from
    12.       'being hung up while it's searching pixels
    13.       DoEvents
    14.    Next y
    15.    If blnFound Then Exit For
    16. 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.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  6. #6
    DaoK
    Guest
    Kaverin this code make an error on that line

    SetCursorPos x, y


    :\

    Nice try but not work

  7. #7
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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?
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

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