Sign in to follow this  
Followers 0
NiceBoy1234

Put .exe window to foreground if

7 posts in this topic

#1 ·  Posted (edited)

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

I do not understand what you are trying to explaining.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

#5 ·  Posted (edited)

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

#6 ·  Posted (edited)

OK it works now.

Edited by NiceBoy1234

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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  
Followers 0

  • Similar Content

    • ur
      By ur
      I am trying to identify the window based on the window title and text.
      The title will be the "erwin DM - filename"

      It is working till date, but some operating systems our application is displaying window as "erwin DM - [filename]"
       
      I tried  "erwin DM - *filename*" But this regular expression is not working.
      Any suggestion?
       
      $sModelFile = "C:\Users\Administrator\Documents\My Models\eMovies.erwin" $wdModel = _WinWaitActivate1("erwin DM - "&FileNameOnly($sModelFile),"") Func _WinWaitActivate1($title,$text,$timeout=0);Will Return the window Handler Logging("Waiting for "&$title&":"&$text) $dHandle = WinWait($title,$text,$timeout) if not ($dHandle = 0) then If Not WinActive($title,$text) Then WinActivate($title,$text) return WinWaitActive($title,$text,$timeout) Else Logging("Timeout occured while waiting for the window...") Exit EndIf EndFunc Func FileNameOnly($sFilePath) Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = "" Local $aPathSplit = _PathSplit($sFilePath, $sDrive, $sDir, $sFileName, $sExtension) ;_ArrayDisplay($aPathSplit, "_PathSplit of " & @ScriptFullPath) return $sFileName EndFunc  
    • Miliardsto
      By Miliardsto
      Hello . How to do that
      $regexp = starts from "abcdef" and after this could be anything in name
      WinActivate($regexp)
    • ur
      By ur
      When we run our exe from any service or through any background process, it won't interact with the applications running in the foreground, I mean launched when the user is logged in or in GUI mode.
       
      Let's say, the onedrive sync app can't be detected from the exe we created and running it in background.

      I tried both options below.

       
      Any Idea how to interact with foreground processes using AuotIT code.

      Other than telling to launch the exe also in foreground.
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Friends i am new to autoit and i am stuck in middle of automation of flashing tool..
      my requirement is i have a window and i have to get the value present in that window (below is the picture)

      in the above window i need to get the value of the highlighted field.the value of this filed keeps on changing...ii need to get the current value present in the field..below are the details of the window and field..

       
      CAN it be done?
       
      thanks

    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Guys i am working on automating a flashing tool..When flashing is started if there is any error in connection it pops's up a error window...(as shown below)

       whenever this popup appears i need a msgbox to appear saying "error occured" how can i do this?
      Thanks