Danixu86

Detect new windows efficiently

2 posts in this topic

Hi, first of all I'm sorry for my english.

I've a computer with four monitors and i want to move launched programs to the window where were launched. For example, if i click on explorer icon on my 4th monitor, open the explorer on that monitor instead the last position.

 

For now i've this code:

#include <MsgBoxConstants.au3>

;Sleep time in ms
$SleepTime = 300 

;Times to check if a window is oppened: Times * SleepTime = Time in seconds. For example: 15 * 300 = 4500ms = 4.5s
$TimesToCheck = 15 

Local $OldList = WinList()
Local $CheckedTimes = 0

While (true)
    local $ActualList = WinList()
    if $ActualList[0][0] <> $OldList[0][0] Then
        For $n = $ActualList[0][0] To 1 Step -1
            If ($ActualList[$n][0] <> "")
            
                Local $Nueva = True
                
                For $o = $OldList[0][0] To 1 Step -1
                    If ($OldList[$o][0] <> "") Then
                        if $ActualList[$n][1] = $OldList[$o][1] Then
                            $Nueva = False
                        EndIf
                    EndIf
                Next

                If $Nueva == True Then
                    If $CheckedTimes >= $TimesToCheck Then
                        ;MsgBox($MB_SYSTEMMODAL, "Tiempo consumido", "Ya no se esperará a que se abra la ventana")
                        $OldList = $ActualList
                        $CheckedTimes = 0
                    Else
                        $CheckedTimes = $CheckedTimes + 1
                    EndIf
                    
                    If BitAND(WinGetState($ActualList[$n][1]), 2) Then
                        MsgBox($MB_SYSTEMMODAL, "Nueva", "Nueva ventana abierta: " & $ActualList[$n][0])
                        $OldList = $ActualList
                    EndIf
                EndIf
            EndIf
        Next
    EndIf

    sleep(300)
WEnd

but it has a high CPU usage (about 10-20%), and that is not good...

Anyone knows how i can detect when a new window is oppened without use a lot of CPU?

 

Thanks!!

Share this post


Link to post
Share on other sites



For now I've found better ways to do it and now the CPU consumption is of about 0-2% and is fine, but the program still detecting when you minimize a window like a window change and then move that window to 1st monitor (strange, because the pointer is not in 1st monitor).

 

While (true)
    Local $ActualMon = _GetMonitorFromPoint()
    Local $ActualList = WinList($TitleRegExp)
    
    if $ActualList[0][0] <> $OldList[0][0] Then
        For $n = $ActualList[0][0] To 1 Step -1         
            Local $Nueva = True
            
            For $o = $OldList[0][0] To 1 Step -1
                if $ActualList[$n][1] = $OldList[$o][1] Then
                    $Nueva = False
                    ExitLoop
                EndIf
            Next

            If $Nueva == True Then
                If BitAND(WinGetState($ActualList[$n][1]), 2) And Not BitAND(WinGetState($ActualList[$n][1]), 16) Then
                    Local $ProgramWindow = _GetMonitorFromPoint(_GetWindowCenter($ActualList[$n][1]))
                    If $ProgramWindow <> $ActualMon Then
                        ;MsgBox($MB_SYSTEMMODAL, "Otro Monitor", "Se ha abierto la ventana en otro monitor")
                        Local $AbsolutePosition = WinGetPos($ActualList[$n][1])
                        
                        if $AbsolutePosition <> 0 Then
                            ;MsgBox($MB_SYSTEMMODAL, $AbsolutePosition[1], $AbsolutePosition[2])
                            Local $RelativePosition[2]
                
                            $RelativePosition[0] = $AbsolutePosition[0] - $__MonitorList[$ProgramWindow][1]
                            $RelativePosition[1] = $AbsolutePosition[1] - $__MonitorList[$ProgramWindow][2]
                            
                            WinMove($ActualList[$n][1], $ActualList[$n][0], $__MonitorList[$ActualMon][1]+$RelativePosition[0], $__MonitorList[$ActualMon][2]+$RelativePosition[1])
                        EndIf
                    EndIf
                    ;MsgBox($MB_SYSTEMMODAL, "Nueva", "Nueva ventana abierta: " & $ActualList[$n][0])
                    $OldList = $ActualList
                EndIf
            EndIf
        Next
    EndIf
    sleep($SleepTime)
WEnd

Greetings!

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