NiceBoy1234 Posted January 13, 2016 Posted January 13, 2016 (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 EndFuncI 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 January 13, 2016 by NiceBoy1234
InunoTaishou Posted January 13, 2016 Posted January 13, 2016 If WinExists("[CLASS:ConsoleWindowClass]") And WinActive("[CLASS:ConsoleWindowClass]") ThenIf the window is active then set it on top.
NiceBoy1234 Posted January 13, 2016 Author Posted January 13, 2016 I do not understand what you are trying to explaining.
InunoTaishou Posted January 13, 2016 Posted January 13, 2016 (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 EndFuncSometimes 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 January 13, 2016 by InunoTaishou
NiceBoy1234 Posted January 13, 2016 Author Posted January 13, 2016 (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 EndFuncAnd works. Edited January 13, 2016 by NiceBoy1234
NiceBoy1234 Posted January 13, 2016 Author Posted January 13, 2016 (edited) OK it works now. Edited January 13, 2016 by NiceBoy1234
InunoTaishou Posted January 13, 2016 Posted January 13, 2016 (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 tryDllCall("user32.dll", "int", "SetForegroundWindow", "hwnd", WinGetHandle("[CLASS:ConsoleWindowClass]"))Or you could use this to bring it to the foreground and it will activate the windowWinActivate("[CLASS:ConsoleWindowClass]") Edited January 13, 2016 by InunoTaishou
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now