Jump to content
jkonrad

Running a Different Function on Different Windows

Recommended Posts

jkonrad

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

Share this post


Link to post
Share on other sites
JLogan3o13

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

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
jkonrad

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

 

Share this post


Link to post
Share on other sites
topten

#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

Share this post


Link to post
Share on other sites
jkonrad

 

 

#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

Share this post


Link to post
Share on other sites
JLogan3o13

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


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
topten

Question: May be your gui is Always ACTIVE? if so, then you should try some ini records, which will be indicating that the window is active or not?

Share this post


Link to post
Share on other sites
topten

"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 :)

Share this post


Link to post
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

×