Simulate Mouse Click

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
9 posts Page 1 of 1
Contributors
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Simulate Mouse Click
M1z23R
Ok, so is it possible to simulate mouse click on minimized window without changing it's window state ? I want to be able to "get" when user double clicks on app, save pointers location, and perform mouse click on that app without maximizing it for example 10secs every ?
User avatar
zulfikar
Member
Member
Posts: 30
Joined: Fri Aug 19, 2011 4:22 am

Re: Simulate Mouse Click
zulfikar
if u open it and have it immediately click at the X,Y cord. then have it minimize might work. Since the code would be performed quickly if thats what you want. Otherwise im sure theres a way, maybe have some sort of code go in form load.
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Simulate Mouse Click
mandai
It would be easier to simulate the target event rather than the click, if the window must remain minimized.
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: Simulate Mouse Click
M1z23R
How would i do that ? - target event ?
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Simulate Mouse Click
mandai
If you are trying to click on a named control, you could use this:
Code: Select all
    <DllImport("user32.dll")> Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll")> Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
    End Function

    Delegate Function del_EnumChildProc(ByVal hwnd As IntPtr, ByVal lParam As IntPtr) As Boolean

    Const WM_LBUTTONDOWN As UInteger = &H201
    Const WM_LBUTTONUP As UInteger = &H202

    Function EnumChildProc(ByVal hwnd As IntPtr, ByVal lParam As IntPtr) As Boolean

        Dim sb As StringBuilder = New StringBuilder(255)
        GetWindowText(hwnd, sb, sb.MaxCapacity)
        Dim text As String = sb.ToString()

        If text = "1" Or text = "3" Then 'simulate a click down and up on the 1 and 3 buttons if they are found, lParam puts the coordinates at X:0, Y:0
            SendMessage(hwnd, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero)
            SendMessage(hwnd, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero)
        End If

        Return True
    End Function

    <DllImport("user32.dll")> Shared Function EnumChildWindows(ByVal hWndParent As IntPtr, ByVal lpEnumFunc As [Delegate], ByVal lParam As IntPtr) As Boolean
    End Function

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click

        Dim hwnd As IntPtr = Process.GetProcessesByName("calc")(0).MainWindowHandle 'windows calculator (example)

        EnumChildWindows(hwnd, New del_EnumChildProc(AddressOf EnumChildProc), IntPtr.Zero)

    End Sub
This will work in any window state.
Last edited by mandai on Thu Mar 22, 2012 4:57 pm, edited 1 time in total.
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: Simulate Mouse Click
M1z23R
Also, is there a way to "detect" clicked buttons name ? to read from api ? i am not that good ( i know 1% :) ) at using APIs
Or to perform click on Item From Coordinates ?
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: Simulate Mouse Click
M1z23R
Ok, so i am messing with the code for an hour or two i think, and i still can't figure out how to double click item in listview on another app, i am figuring out, but nothing works, i even trying changing the X|Y coords of mouse click, but still nothing :/

EDIT : It is a listview ! not listbox
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Simulate Mouse Click
mandai
You could detect a click with GetAsyncKeyState, you can then record information on the active window, cursor/window positions etc.

As of yet, I haven't come across a simple way to double click on a listview item while the window is minimized.
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: Simulate Mouse Click
M1z23R
Ok, if you ever come into something like that, please let me know, i have been bumping my head for 6,7 hours last three days, searching for LVM_GETTEXT and i haven't found even 1 code that works :( Not even codes in C#
9 posts Page 1 of 1
Return to “Tutorial Requests”