How to trigger a mouse click in vs.net 2005?

Souma

Newcomer
Joined
Sep 3, 2005
Messages
15
Code:
Imports System.Threading
Public Class Form1
    Dim counter As Integer
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If counter = 0 Or counter = Nothing Then
            counter = 1
            Label2.Text = "ON"
            Label2.BackColor = Color.Lime

            Thread.Sleep(Integer.Parse(TextBox1.Text))
            macro()
        Else
            counter = 0
            Label2.Text = "OFF"
            Label2.BackColor = Color.Red
        End If
    End Sub

    Private Sub macro()
        Windows.Forms.Cursor.Position = New System.Drawing.Point(170, 340)
        SendMessage(&H2, 0, 0, 0)
        Thread.Sleep(200)
        SendMessage(&H4, 0, 0, 0)
    End Sub
End Class

Right now i can trigget my mouse to move by itself to the location i want, but i cannot trigger it to click and vs.net pop out an exception : PInvokeStackImbalance

How to fix this guys? :confused:
 
Firstly change all the 'As Long' bits of your declare to 'As Integer' - that should remove the problem of it crashing.

You might find it easier to just use the Button1.PerformClick() method however to simulate a button click rather than relying on SendMessage.
 
Thanks! Wat i'm trying to do is not click any buttons on the form but clicking some other things outside of the application itself, so i need the mouse_event api

Oh yea one more thing... when i use thread.sleep i cannot seems to do anything to the form (e.g. i can't press button to stop it). Is there any idea i can fix this?
 
Last edited:
Yea thanks for the resource! mouse_event works fine anyway, but how can i fix the thread.sleep problem that causes my app to stop responding while the thread is running?
 
Don't sleep on the UI thread is the simple answer, if the UI thread is asleep it cannot respond to any input therefore it stops responding.

Ideally you would implement some other mechanism here - a timer + callback would possibly be the best choice, or you could always perform a loop with a Application.DoEvents() inside it but this is a poor technique as it can burn a lot of CPU time to do nothing at all.

Out of curiosity is there a reason for the Sleep being there?
 
lol threading eats cpu... ur idea also eats cpu... wat's the point :D

yea i making my own macro program that do interval clicking on other applications buttons, so the first thing that comes to my mind is using thread.sleep to so call "pause" for a specific time between the next action.

is there any other way? geez... if u ever played with macro programs(e.g. Macro Express, AutoIt, ACTool) you should know they have delay function to pause the action for a specified time... that's wat i wanted to achieve
 
I miss the old win3.1 macro recorder :(.

Using doevents in a loop would keep the cpu at 100% load so that would be 'less than ideal'. Using timer+callback is a bit harder to code but would be very light on the cpu. with almost 0 overhead.

Another way could be to put the 'macro execution' part of your code in a separate thread. That way if it goes to sleep the UI thread keeps running. Although this might cause problems if you update the UI from the 'macro execution'.
 
Back
Top