Jump to content

Activate window if instance already running


Go to solution Solved by HurleyShanabarger,

Recommended Posts

Posted

Hello,

I want to check if a the same script is already running and if so I would like to restore the application window and focus on the main or better said currently active window. 

Using _Singleton or _SingletonPID will help me to get the PID of the running process. When trying to get the handle of the window using a combination of WinList and WinGetProcess I am getting a list of windows for a process and I can filter to get the enabled, existing and visible windows only. Up to here everything works fine, but if the application window is minimized, I am not able to detect the active window. For know I would solve this using a hidden label within the script, and register $WM_SETTEXT using GUIRegisterMsg20 and chaning the text of the label from the second instance. A change of the label text will then trigger the restore/focus event on the first instance on the script.

But my gut tells me, there must be an easier way...

  • Solution
Posted

Found the solution, which is working for me!

 

Func _WinRestoreByPid(Const $iPid)
        Local $arWndw = _ProcessGetWinList($iPid, 1)
        If @error Then Return SetError(0x01)
        For $iWndw = 1 To UBound($arWndw) - 1
            WinSetState($arWndw[$iWndw][1], "", @SW_RESTORE)
            WinActivate($arWndw[$iWndw][1])
        Next
    EndFunc   ;==>_WinRestoreByPid

 

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
×
×
  • Create New...