Jump to content

Running a Different Function on Different Windows


Recommended Posts

Hi all, been lurking here for a little bit.

This is my first post since this community has been so helpful and I haven't had the need to ask anything , but I can't find an answer to this to save my life.  Hoping someone can help.

I'm working with a program that cycles through multiple windows and I have a GUI built in to a function for each of those windows.  For some reason, I can't get this to run on each window, the way it's sitting right now, it only runs on "Window1".

Thanks in advance for your help and I'm more than happy to give more detail if needed.

While 1
    If WinActive("Window1") Then
        DoThis()
    ElseIf WinActive("Window2") Then
        DoThisInstead()
    EndIf
Wend

 

Edited by jkonrad
Formatting
Link to comment
Share on other sites

  • Moderators

I guess you'll have to give more detail. This works fine for me on two, three, or more windows:

 

While 1
    If WinActive("Input.txt", "") Then
        ControlSend("Input.txt", "", "Edit1", "This is Input.txt" & @CRLF)
        Sleep(100)
    ElseIf WinActive("Output.txt", "") Then
        ControlSend("Output.txt", "", "Edit1", "This is Output.txt" & @CRLF)
        Sleep(100)
    EndIf
WEnd

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Thanks for confirming that I'm not losing my mind there.  Here's the detail on the function(s) that are being called.  It's building a GUI on top of the specific window (the code for each is mostly identical, I've included it below).  

Thanks!

 
Func DoThis()
    Global $Win1Width = 35
    Global $Win1Height = 20
    Global $Window1GUI1 = GUICreate("ButtonGUI", $Win1Width, $Win1Height, 0, 0, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
    $Button = GUICtrlCreateButton("VAX", 0, 0, $Win1Width, $Win1Height)
    GUICtrlSetTip(-1, "Click here")
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button
                TakeThisAction()
        EndSwitch
        MoveWin1GUI1()
    WEnd
EndFunc


Func MoveWin1GUI1()
    If WinActive("Window1") Then
        GUISetState(@SW_SHOWNOACTIVATE)
        WinSetOnTop("ButtonGUI", "", 1)
        Local $WindowSize = WinGetPos("Window1")
        If @error = 1 Then
            MsgBox(0, "Msg", "@error=1 - Local $WindowSize = WinGetPos(Window1)")
            Return
        EndIf
    Local $size = ControlGetPos("Window1", "", 61)
    Local $name = WinGetTitle("Window1")
    Local $x = $size[0] + $WindowSize[0] + 0
    Local $y = $size[1] + $WindowSize[1] + 15
    WinMove($KennelGUI1, "", $x, $y)
    ElseIf WinActive("ButtonGUI") = False Then
    GUISetState(@SW_HIDE)
    EndIf
EndFunc

 

Link to comment
Share on other sites

#include <MsgBoxConstants.au3>
while 1()

If WinActive("window1 — Notepad") Then ; Check if Notepad is currently active.
    MsgBox($MB_SYSTEMMODAL, "", "WinActive" & @CRLF & "Notepad 1 is active.")
 EndIf

 If WinActive("window2 — Notepad") Then ; Check if Notepad is currently active.
    MsgBox($MB_SYSTEMMODAL, "", "WinActive" & @CRLF & "Notepad 2 is active.")
EndIf



 wend

Create 2 txt files window1.txt and window2.txt , then run both txt files and run the script- you will see that it is working

Link to comment
Share on other sites

 

 

#include <MsgBoxConstants.au3>
while 1()

If WinActive("window1 — Notepad") Then ; Check if Notepad is currently active.
    MsgBox($MB_SYSTEMMODAL, "", "WinActive" & @CRLF & "Notepad 1 is active.")
 EndIf

 If WinActive("window2 — Notepad") Then ; Check if Notepad is currently active.
    MsgBox($MB_SYSTEMMODAL, "", "WinActive" & @CRLF & "Notepad 2 is active.")
EndIf



 wend

 

 

Create 2 txt files window1.txt and window2.txt , then run both txt files and run the script- you will see that it is working

​I tried that method earlier, it's not working for me either for some reason.  Thank you!

 

EDIT:  I should mention that what you included here works like a charm, it just doesn't work in the program I'm working with for some reason.

Edited by jkonrad
Link to comment
Share on other sites

  • Moderators

Can you please post your full code? What you posted shows a call to a function not included. All we see is you creating a GUI titled "ButtonGUI" but looking for an active window titled "Window1".

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

"I should mention that what you included here works like a charm, it just doesn't work in the program I'm working with for some reason."

Does Window Info tool show you anything?- sometimes different instances of one and the same program are giving different titles :)

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...