Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27329

VS 2010 Sending a click to exact coordinates in an inactive program

$
0
0
So I'm trying to send a click of exact coordinates to an inactive program. I've used this code send clicks before and it's worked fine but the coordinates don't seem to be correct because when I enter them in the textboxes for the ovalshape the messagebox won't popup. So my question is, how can I grab the coordinates that work with the click and set them to label1.text?

Here's the form:


And here's the complete code:
Code:

Imports System.Runtime.InteropServices

Public Class Form1

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
    End Function

    <DllImport("user32.dll")> _
    Shared Function WindowFromPoint(ByVal pnt As Point) As IntPtr
    End Function

    Private point As New Point
    Private lButtonUp As UInteger = &H202
    Private lButtonDown As UInteger = &H201

    Private Function returnlParam(ByVal Lo As Short, ByVal Hi As Short) As IntPtr
        Return CInt(Hi) << 16 Or Lo
    End Function

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = (Cursor.Position.X - Me.Location.X) & ", " & (Cursor.Position.Y - Me.Location.Y)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        point = New Point(textbox1.text, textbox2.text)
        Timer2.Enabled = True
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim hWnd As IntPtr
        Dim wParam As IntPtr
        Dim lParam As IntPtr

        hWnd = WindowFromPoint(point)
        wParam = CUInt(Keys.LButton)
        lParam = returnlParam(point.X, point.Y)

        PostMessage(hWnd, lButtonDown, wParam, lParam)
        PostMessage(hWnd, lButtonUp, wParam, lParam)

        MsgBox("clicked")

        Timer2.Enabled = False
    End Sub

    Private Sub OvalShape1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OvalShape1.Click
        MsgBox("clicked me!")
    End Sub
End Class

This is the code I'm using to get the coordinates currently:
Code:

Label1.Text = (Cursor.Position.X - Me.Location.X) & ", " & (Cursor.Position.Y - Me.Location.Y)

Viewing all articles
Browse latest Browse all 27329

Trending Articles