Jump to content
Sign in to follow this  
NiceBoy1234

Put .exe window to foreground if

Recommended Posts

NiceBoy1234

I want to check if the cmd.exe + window of it is running, if it is running I want to put the window to the foreground, but somehow this does not work:
 

Local $sCommandlinePath = "C:\Windows\system32\cmd.exe"

        RunCm()

        Func RunCm()

            If FileExists($sCommandlinePath) Then
                If ProcessExists("cmd.exe") Then
                    If WinExists("[CLASS:ConsoleWindowClass]") And WinActive("[CLASS:ConsoleWindowClass]") Then
                        Local $hCmdWindow = WinGetHandle("[CLASS:ConsoleWindowClass]")
                        WinSetOnTop($hCmdWindow, "", $WINDOWS_ONTOP)
                    EndIf
                Else
                    Local $iCmMax = Run($sCommandlinePath, "", @SW_SHOWMAXIMIZED)
                EndIf
            EndIf

        EndFunc

I started the command line and put another window above it then I started my script and it did not put the command line window to the foreground.

Edited by NiceBoy1234

Share this post


Link to post
Share on other sites
InunoTaishou
If WinExists("[CLASS:ConsoleWindowClass]") And WinActive("[CLASS:ConsoleWindowClass]") Then

If the window is active then set it on top.

Share this post


Link to post
Share on other sites
NiceBoy1234

I do not understand what you are trying to explaining.

Share this post


Link to post
Share on other sites
InunoTaishou

I do not understand what you are trying to explaining.

You're only calling WinSetOnTop if the window Exists AND the window is already active.

HotKeySet("{F1}", "RunCm")
HotKeySet("{Esc}", "Close")

Local $sCommandlinePath = "C:\Windows\system32\cmd.exe"

While (True)
    Sleep(100)
WEnd

Func RunCm()
    If FileExists($sCommandlinePath) Then
        If ProcessExists("cmd.exe") Then
            If WinExists("[CLASS:ConsoleWindowClass]") and WinActive("[CLASS:ConsoleWindowClass]") Then
                MsgBox("", "", "WinSetOnTop " & (WinSetOnTop("[CLASS:ConsoleWindowClass]", "", 1) = True ? "Succeeded!" : "Failed"))
            Else
                MsgBox("", "", "Cmd.exe does not exist OR Cmd.exe is not already active" & @CRLF & "WinExists: " & WinExists("[CLASS:ConsoleWindowClass]") & @CRLF & "WinActive:" & WinActive("[CLASS:ConsoleWindowClass]"))
            EndIf
        Else
            Local $iCmMax = Run($sCommandlinePath, "", @SW_SHOWMAXIMIZED)
        EndIf
    EndIf

EndFunc   ;==>RunCm

Func Close()
    Exit 0
EndFunc

Sometimes some debugging code helps. I put the return for WinSetOnTop in a message box, if it's True (1) Then it succeeded, otherwise it failed. Also put in an else statement for the If WinExists...WinActive Then to put in a debugging message letting you know why it failed.

Edited by InunoTaishou

Share this post


Link to post
Share on other sites
NiceBoy1234

Ok I changed it to this now:

HotKeySet("{F1}", "RunCm")
HotKeySet("{Esc}", "Close")

Local $sCommandlinePath = "C:\Windows\system32\cmd.exe"

While (True)
    Sleep(100)
WEnd

Func RunCm()
    If FileExists($sCommandlinePath) Then
        If ProcessExists("cmd.exe") Then
            If WinExists("[CLASS:ConsoleWindowClass]") Then
               WinSetOnTop("[CLASS:ConsoleWindowClass]", "", 1)
               MsgBox("", "", "WinSetOnTop " & (WinSetOnTop("[CLASS:ConsoleWindowClass]", "", 1) = True ? "Succeeded!" : "Failed"))
            Else
               MsgBox("", "", "Cmd.exe does not exist OR Cmd.exe is not already active" & @CRLF & "WinExists: " & WinExists("[CLASS:ConsoleWindowClass]") & @CRLF & "WinActive:" & WinActive("[CLASS:ConsoleWindowClass]"))
            EndIf
        Else
            Local $iCmMax = Run($sCommandlinePath, "", @SW_SHOWMAXIMIZED)
        EndIf
    EndIf

EndFunc   ;==>RunCm

Func Close()
    Exit 0
EndFunc

And works.

Edited by NiceBoy1234

Share this post


Link to post
Share on other sites
NiceBoy1234

OK it works now.

Edited by NiceBoy1234

Share this post


Link to post
Share on other sites
InunoTaishou

I apologize, I misunderstood. Looking back at your first post you said you wanted it in the foreground. Maybe you didn't mean WinSetonTop? WinSetOnTop just sets the Window to have the TOPMOST status, so it's always on top.

If you want it to have the topmost status then it worked for me if I had cmd active when I pressed the hotkey. If you just want to bring it to the foreground try

DllCall("user32.dll", "int", "SetForegroundWindow", "hwnd", WinGetHandle("[CLASS:ConsoleWindowClass]"))

Or you could use this to bring it to the foreground and it will activate the window

WinActivate("[CLASS:ConsoleWindowClass]")
Edited by InunoTaishou

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
Sign in to follow this  

  • Similar Content

    • bbProg
      By bbProg
      Hi,
       
      how can I wait activate for windows with information as in the photos attached?
      Class is not being recognized by the script.
       
      thanks in advanced.



    • chriswaldeck
      By chriswaldeck
      I am capturing data from a web page as soon as it opens. The problem is I am running a dual screen setup and the script don't capture the data when the window is opened up in the non default monitor. On the default monitor it works fine.
      Func Monitor() Local $hTimer = TimerInit()             Do             $fDiff = TimerDiff($hTimer)         Until WinActive("ActivityDetail - Internet Explorer") Or $fDiff >= 5000     ; Only sees window on default monitor.         If WinActive("ActivityDetail - Internet Explorer") Then                                 ; Only sees window on default monitor.             Local $hWnd = WinGetHandle("ActivityDetail - Internet Explorer", "")             Local $oIE = _IEAttach($hWnd, "hwnd")             If @error Then                 $oIE.Quit()             Else                 Local $oForm = _IEGetObjByName($oIE, "lblHeaderActRefValue")                 Local $Ref = _IEPropertyGet($oForm, "innertext")                 Local $oForm = _IEGetObjByName($oIE, "lblCompileDate")                 Local $Compile = _IEPropertyGet($oForm, "innertext")                 Local $oForm = _IEGetObjByName($oIE, "Table2")                 Local $Description = _IEPropertyGet($oForm, "innertext")                 $Description = StringTrimLeft($Description, 49)                 Local $iLength = StringLen($Description)                 Local $iPosition = StringInStr($Description, "Network Tag")                 Local $sString = StringTrimRight($Description, ($iLength - ($iPosition - 1)))                 Local $sString1 = StringStripWS($sString, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)                 Local $sString = $sString1                 Local $oForm = _IEGetObjByName($oIE, "lblDisruption")                 Local $Disruption = _IEPropertyGet($oForm, "innertext")                 If $flag <> $Ref And $Ref <> 0 and $Compile <> 0 Then                     $ICount = _GUICtrlListView_GetItemCount($ListView1)                     _GUICtrlListView_AddItem($ListView1, $Compile, $ICount)                     _GUICtrlListView_AddSubItem($ListView1, $ICount, $Ref, 1)                     _GUICtrlListView_AddSubItem($ListView1, $ICount, $Disruption, 2)                     _GUICtrlListView_AddSubItem($ListView1, $ICount, $sString, 3)                     _FileWriteLog(@ScriptDir & "\" & @YEAR & @MON & ".txt", $Ref & " " & $Disruption & " " & $sString & @CRLF)                     $flag = $Ref                 EndIf             EndIf         EndIf EndFunc   ;==>Monitor  
    • rcmaehl
      By rcmaehl
      A UDF with Extended Functions for Window Management
       
      Notes:
      Fixes WinGetClassList's barbaric returning of a @LF separated string instead of an array.
       
      Potential Uses:
      Automating applications that change their controls' handles/classes on each launch (e.g. half of Cisco's programs)
       
      Functions:
      _WinGetClassList
      _WinGetClassNNList
      _WindowGetHandleList
      _WindowGetHandleListFromPos
       
      Download: 
      WindowEx.zip  (v0.4)
       
      Changelog:
      10/04/2016 (v0.4): _WinGetClassNNList Fixed : Not Returning an Index when using $2D_ARRAY _WinGetClassNNList Fixed : Not Properly returning $aArray[x][1] on Classes with instances > 9 when using $2D_ARRAY 10/03/2016 (v0.3): _WinGetClassList Added : Exactly the same as WinGetClassList but returns a more civilized Array _WinGetClassNNList Added : Returns Classes and their instances in either a 1D or 2D array depending on Flags _WindowGetHandleList Renamed: _WinGetHandleList SCRIPT BREAKING! _WindowGetHandleListFromPos Renamed: _WinGetHandleListFromPos SCRIPT BREAKING! 10/01/2016 (v0.2): WindowsExConstants.au3 Added : Flags in _WindowGetHandleListFromPos _WindowGetHandleListFromPos Removed: ConsoleWrite left in during debug _WindowGetHandleListFromPos Added : Flag for if part of a Control is at $X, $Y return it as well. 10/01/2016 (v0.1): _WindowGetHandleList Added : Retrieves the handles of classes from a window. _WindowGetHandleListFromPos Added : Retrieves the handles of classes at a specific position from a window. Known and Reported Bugs:
      None reported To Do:
      To Be Decided. Opinions welcome! Upcoming Changes:
      To Be Decided.
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning everyone

      I was playing a little bit with "Screen Capture" UDF, and I was trying to make a "Window" capture, but, since I made a GUI which through I fire the event "Capture", my GUI is captured as well, and I don't want to
      This is the line of code that makes the capture:
       
      _ScreenCapture_CaptureWnd($strScreenCaptureFileName, $objActiveWindow, 0, 0, -1, -1, False) And these are the lines of code which select the "active" window:
       
      Local $objCurrentWindow = 9999 If _IsPressed("01") Then $objCurrentWindow = WinGetHandle("[ACTIVE]") If $objCurrentWindow <> $objMyGUI Then $objActiveWindow = $objCurrentWindow EndIf EndIf Sorry If I made stupid mistakes
      Thanks in advance.

      Francesco
    • jantograaf
      By jantograaf
      Hi all,
      I'm having some trouble restoring a program which uses a normal AutoIT-GUI and is used as a ToolKit for our customers to open different (external) tools, check their network connections or ping different destinations. Almost all customers are running this script on Win7 x64. 
      What do I want to do?
      When the program is already running and the user tries to start it for a second time, it should exit the second instance after activating/restoring the first instance.
      What did I do to integrate this?
      I used a little piece of script found on the forums to prevent opening a second instance and modified it as follows:
      $g_szVersion = "VSOL ToolKit" If WinExists($g_szVersion) Then Local $gState = WinGetState($g_szVersion) MsgBox(0,"","gState = " & $gState) ;For debugging purposes WinSetState($g_szVersion,"",@SW_SHOW) MsgBox(0,"","gState = " & $gState) ;For debugging purposes WinSetState($g_szVersion,"",@SW_RESTORE) MsgBox(0,"","gState = " & $gState) ;For debugging purposes WinActivate($g_szVersion) Exit EndIf AutoItWinSetTitle($g_szVersion) I have tried it with only the @SW_SHOW, only the @SW_RESTORE, both of them in a different order, same difference, it doesn't work.
      What does happen then?
      It detects the current state (when it's minimized) as '5', which means enabled and hidden. After setting the state to show, the state changes to 7. Restoring doesn't change the state. But what is really odd, is that the window doesn't appear but AutoIt seems to create a néw window with the same title as the existing one. 

      It doesn't matter if the window was originally minimized by clicking the minimize button in the GUI or by a line of code in my script. For example, when users open a software package from within my script, a similar function to this one will be called:
      Func VSOL_FFTOOLS_SHOW_DOWNLOADS() GUISetState(@SW_MINIMIZE,$VSOL_BACKEND) ShellExecute(@UserProfileDir & "\Downloads") EndFunc The only thing that does work, is when the window is running in a non-minimized state (somewhere behind the current window), it gets activated and receives the focus when trying to start the second instance.
      If anyone could help me out with this one, I'd be forever thankful :-)
      Thanks in advance!
      Kind regards,
      Jan Geurts
×