Jump to content

Put .exe window to foreground if


Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

×
×
  • Create New...