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

    • Jibberish
      By Jibberish
      Hi,
      I am maybe an intermediate AutoIt script writer, but have no experience creating GUIs.
      I have a script with two functions. One for Checkboxes and another with radio buttons. Each function creates it's own window.
      I'd like to use one window with both checkboxes and radio buttons.
      I pulled samples from AutoIt Help and other places and worked it into this: (RadioCheck still uses the example Case and MsgBoxes. I will clean this up soon)
      Func CheckOptions() ; Create a GUI with various controls. Local $hGUI = GUICreate("SGX4CP Options", 275, 250) ; Create a checkbox control. Local $iLoopCheckbox = GUICtrlCreateCheckbox("Loop", 10, 10, 185, 25) Local $iFullScreenCheckbox = GUICtrlCreateCheckbox("Fullscreen", 10, 40, 185, 25) Local $iRestartPlaybackCheckbox = GUICtrlCreateCheckbox("Restart Playback from Sleep", 10, 70, 185, 25) GUICtrlSetState($iRestartPlaybackCheckbox, $GUI_CHECKED) Local $iDisableSleepCheckbox = GUICtrlCreateCheckbox("Disable Sleep", 10, 100, 185, 25) Local $iLogCheckbox = GUICtrlCreateCheckbox("Show Log", 10, 130, 185, 25) GUICtrlSetState($iLogCheckbox, $GUI_CHECKED) Local $idClose = GUICtrlCreateButton("Next", 110, 220, 85, 25) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idClose ExitLoop Case $iLoopCheckbox If _IsChecked($iLoopCheckbox) Then $bLoopChecked = True Else $bLoopChecked = False EndIf Case $iFullScreenCheckbox if _IsChecked($iFullScreenCheckbox) Then $bFullScreenChecked = True Else $bFullScreenChecked = False EndIf Case $iRestartPlaybackCheckbox if _IsChecked($iRestartPlaybackCheckbox) Then $bRestartPlaybackChecked = True Else $bRestartPlaybackChecked = False EndIf Case $iDisableSleepCheckbox if _IsChecked($iDisableSleepCheckbox) Then $bDisableSleepChecked = True Else $bDisableSleepChecked = False EndIf Case $iLogCheckbox if _IsChecked($iLogCheckbox) Then $bLogChecked = True Else $bLogChecked = False EndIf EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc Func RadioCheck() GUICreate("Select Test",300,180) ; will create a dialog box that when displayed is centered Local $idRadio1 = GUICtrlCreateRadio("Loop Forever", 10, 10) Local $idRadio2 = GUICtrlCreateRadio("Play each video 3 times", 10, 40) Local $idRadio3 = GUICtrlCreateRadio("Play each video separately", 10, 70) GUICtrlSetState($idRadio1, $GUI_CHECKED) Local $idClose = GUICtrlCreateButton("Start Test", 120,100) GUISetState(@SW_SHOW) Local $idMsg ; Loop until the user exits. While 1 $idMsg = GUIGetMsg() Select Case $idMsg = $GUI_EVENT_CLOSE ExitLoop Case $idMsg = $idRadio1 And BitAND(GUICtrlRead($idRadio1), $GUI_CHECKED) = $GUI_CHECKED MsgBox($MB_SYSTEMMODAL, 'Info:', 'The app will run forever, playing each video once, then looping back to the first video.') $bTestSelectForever = True Case $idMsg = $idRadio2 And BitAND(GUICtrlRead($idRadio2), $GUI_CHECKED) = $GUI_CHECKED MsgBox($MB_SYSTEMMODAL, 'Info:', 'Each video will loop 3 times then move to the next video.') $bTestSelect3Times = True Case $idMsg = $idRadio3 And BitAND(GUICtrlRead($idRadio2), $GUI_CHECKED) = $GUI_CHECKED MsgBox($MB_SYSTEMMODAL, 'Info:', 'Player opens, first video plays, player closes. Player opens, second video plays, player closes, etc.') $bTestSelectSingleVideo = True EndSelect WEnd EndFunc I would like to combine the checkbox "Loop" and the radio button $idRadio2. Radio2 requires Loop to be checked.
      I planned to remove the Loop checkbox and only enable it if Radio2 is selected.
      Can I combine these two functions into one with one window with both Checkboxes and Radio Buttons?
      Thanks
      Jibberish
    • nacerbaaziz
      By nacerbaaziz
      Hi friends, I have a question, please.
      I have created a window containing a edit box
      I need to know how to change the keyboard language,
      e.g :
      if a user open the  script  it will be change the keyBord language   in window automatically into English
      I found more examples but didn't work with me .
      Note:  
       I use the windows 10 os
      Please help me .
      thanks in advance to all
      with my Greetings and my appreciation for all users and admins
    • WoodGrain
      By WoodGrain
      Hi guys,
      I'm trying to move and resize a program call CPUID HWMonitor (http://www.cpuid.com/softwares/hwmonitor.html) but WinMove() doesn't work. Can someone tell me how to get this to work for this program? I've successfully moved other programs using WinMove(), and I've used WinGetTitle ("[ACTIVE]") to confirm the title is correct. WinActivate("CPUID HWMonitor") does work.
      If I run the following code nothing happens, doesn't move or resize, but AutoIt doesn't complain about the code either:
      WinMove("CPUID HWMonitor", 0, 0, 475, 715) The class changes every time the program opens (and I've nevery had a lot of success with classes tbh, bit hit and miss for me).
      Thanks all!
    • KemyKo
      By KemyKo
      I want to create a program that resizes and changes style/exstyles of other windows. 
      The problem is that i don`t know why my script doesn`t work.
      Here is an example:
      #RequireAdmin #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Global $setStyle, $getStyle, $outputString='', $getHandle, $setState $getHandle = WinGetHandle("[CLASS:Notepad]") $outputString &= $getHandle & @LF $setState = GUISetState(@SW_SHOWNORMAL, $getHandle) $outputString &= $setState & @LF $getStyle = GUIGetStyle($getHandle) If IsArray($getStyle) = 1 Then $outputString &= $getStyle[0]& ' ' &$getStyle[1]& @LF Else $outputString &= "NOTHING" & @LF EndIf $setStyle = GUISetStyle($WS_POPUPWINDOW, -1, $getHandle) $outputString &= $setStyle & @LF MsgBox(0,"OUTPUT", $outputString) It doesn`t work... At least not for me 
      I noticed that $getHandle is "0x0021023A" and AutoIt Window Info shows : "0x000000000021023A"
      I don`t know if that`s a problem . If it is, idk how to fix it 
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning
      I would like to know if I can use the Excel UDF to manipulating a .csv file without having Office installed on the PC I'm going to work...
      I read somewhere that it could be done, but I'm here to ask and be sure of what I remember... 
      I'd like to post another question...
      How can I retrieve the handle of a windows from a PID of an .exe?
      I have my script that does a ShellExecute ( which returns the PID of the .exe ), and then, switching a parameter read from a .ini file, adapt the Window on the screen ( Maximize, Minimize, On Top )...
      I tried, but without having success with this:
       
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile_x64=prova.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <MsgBoxConstants.au3> #include <AutoItConstants.au3> #include <WinAPIEx.au3> #include <Array.au3> Local $sFileConfigurazione = @ScriptDir & "\configurazione_exe.ini" If(FileExists($sFileConfigurazione)) Then Local $aSezioniIni = IniReadSection($sFileConfigurazione, "CONFIGURAZIONE_EXE") If @error Then MsgBox($MB_ICONERROR, "Errore!", "Errore durante l'apertura del file: " & @CRLF & $sFileConfigurazione & @CRLF & "Errore: " & @error) Else ; Lancio dell'applicativo indicato nel file di configurazione Local $iPID = ShellExecute($aSezioniIni[1][1]) Local $hWnd If($iPID <> 0) Then Local $aWinList = WinList() For $i = 1 To $aWinList[0][0] If(WinGetProcess($aWinList[$i][1] = $iPID)) Then $hWnd = $aWinList[$i][1] EndIf Next Switch($aSezioniIni[2][1]) Case $aSezioniIni[2][1] = "MIN" WinSetState($hWnd, "", @SW_MINIMIZE) Case $aSezioniIni[2][1] = "MAX" WinSetState($hWnd, "", @SW_MAXIMIZE) Case $aSezioniIni[2][1] = "TOP" WinSetOnTop($hWnd, "", $WINDOWS_ONTOP) EndSwitch EndIf EndIf EndIf It just set on top the .exe I'm launching...
      Thanks