Jump to content

WinGetProcess Question


drizzlx
 Share

Recommended Posts

I was wondering if it were possible to allow the user to click the correct window they wanted the program to target. IE: User opens application, clicks a button and then must click the window of the process they want to bind the program to, rather then just having a static target pre-defined within the code. If so, would this allow let's say.. 2 identical processes to be bound to two of the applications without interference?

Thanks for future replies!

Edited by drizzlx
Link to comment
Share on other sites

You could just monitor _IsPressed() for "01" (left click) and then get the handle of the active window with WinGetHandle("[ACTIVE]").

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

You could just monitor _IsPressed() for "01" (left click) and then get the handle of the active window with WinGetHandle("[ACTIVE]").

:)

That's what I thought, but I didn't want to be cheap. >.<

Like PsaltyDS I assumed you wanted to interact with a window, rather than a process, but this code could easily be modified to get the Process ID a window belongs to.

To make this work with other programs than notepad you may need to change "[Edit1]" in the ControlSend to something else, but it demonstrates the idea nicely.

#include <WinAPI.au3>
#include <Misc.au3>
#include <Array.au3>

Opt("GUIOnEventMode",1)

Global $hGUI, $CtrlInput, $ahWnd[1][2]

$hGUI = GUICreate("Multi window send")
GUISetOnEvent(-3,"_Exit")
GUICtrlCreateButton("Add window",10,5,100)
GUICtrlSetOnEvent(-1,"_AddWindow")
$CtrlInput = GUICtrlCreateInput("String to send",120,5,200)
GUICtrlCreateLabel("This example is made to work with notepad." & @CRLF & "Fire up a few, add them to the list and start sending strings.",10,30,400)
GUISetState()

While 1
    Sleep(100)
WEnd

Func _AddWindow()
    Local $tPos, $hWnd, $hWndParent
    While _IsPressed("01") ;Wait until left mouse is released. (If you left clicked the "Add window" button it would assume you wanted to target this program otherwise.
        Sleep(10)
    WEnd
    While Not _IsPressed("01") ;Wait untill the left mouse button is pressed again.
        Sleep(10)
    WEnd
    $tPos = _WinAPI_GetMousePos() ;Put the coordinates of the mouse in memory and create a pointer.
    $hWnd = _WinAPI_WindowFromPoint($tPos) ;Check what window is at those coordinates.
    While 1 ;Depending on where you clicked you might have gotten a child window. I assume you want to topmost parent.
        $hWndParent = _WinAPI_GetParent($hWnd) ;check if a parent window exists.
        If $hWndParent Then
            $hWnd = $hWndParent ;if a parent handle exists check again with that handle.
        Else
            ExitLoop ;topmost parent is found.
        EndIf
    WEnd

    $ahWnd[0][0] += 1 ;increase the count of windows by 1
    ReDim $ahWnd[$ahWnd[0][0] + 1][2] ;add one row to the array to hold the new handle
    $ahWnd[$ahWnd[0][0]][0] = $hWnd ;add the handle to the array (column 0)
    $ahWnd[$ahWnd[0][0]][1] = GUICtrlCreateButton("Send to text to " & WinGetTitle($hWnd),10,30+(30*$ahWnd[0][0])) ;add the relevant button ID to the same row of the array. (column 1)
    GUICtrlSetOnEvent(-1,"_SendString") ;set a function to the button.

    WinActivate($hGUI)
EndFunc

Func _Exit()
    Exit
EndFunc

;called if you press a button to send the string to a window.
Func _SendString()
    For $i = 0 To $ahWnd[0][0] ;loop through the array.
        If @GUI_CtrlId = $ahWnd[$i][1] Then ;if the button that called the function is found.
            ControlSend($ahWnd[$i][0],"","Edit1",GUICtrlRead($CtrlInput)) ;send this string to "Edit1" in the relevant window.
            Return ;done
        EndIf
    Next
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...