Jump to content

How to get the title of new window?


zhao
 Share

Recommended Posts

I want to be noticed if any new window was created,just like Notify and Name of any New Process Started

But my code bellow doesn't work as I wished.

Dim $list = WinList()

AdlibEnable("chk_Process")

While 1
    Sleep(20)
WEnd

Func chk_Process()
    $list2 = WinList()
    If $list2[0][0] > $list[0][0] Then
        if $list2[$list2[0][0]][0] <> "Default IME" then MsgBox(262208,"New Window", $list2[$list2[0][0]][0] & "  ")
        $list[0][0] = $list2[0][0]
    Else
        $list[0][0] = $list2[0][0]
   ; if you close processes - this resets the list
    EndIf
EndFunc
Link to comment
Share on other sites

This is a quick and dirty, probably can be improved upon.

#include <Date.au3>

HotKeySet("{ESC}", "_Exit")

Global $list
Global $winlist

_createWinlist($list, $winlist, 1)

AdlibEnable("chk_Process")

While 1
    Sleep(20)
WEnd

Func chk_Process()

    Local $list2
    Local $winlist2, $z = 0
    _createWinlist($list2, $winlist2)
    For $x = 1 To $winlist2[0][0]
        $found = 0
        For $y = 1 To $winlist[0][0]
            If $winlist2[$x][0] = $winlist[$y][0] And $winlist2[$x][1] = $winlist[$y][1] Then
                $found = 1
                $z = 0
                ExitLoop
            EndIf
            $z = $x
        Next
        If $found = 0 Then ConsoleWrite("New Window (# windows = " & UBound($winlist2) - 1 & "): " & $winlist2[$z][0] & @LF)
    Next
    ReDim $winlist[UBound($winlist2) + 1][2]
    $winlist[0][0] = $winlist2[0][0]
    For $x = 1 To $winlist2[0][0]
        $winlist[$x][0] = $winlist2[$x][0]
        $winlist[$x][1] = $winlist2[$x][1]
    Next
EndFunc   ;==>chk_Process

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _createWinlist(ByRef $a_list, ByRef $a_winlist, $show = 0)
    $a_list = WinList()
    For $x = 1 To $a_list[0][0]
        If $a_list[$x][0] <> "" And $a_list[$x][0] <> "Default IME" Then
            If IsArray($a_winlist) Then
                ReDim $a_winlist[UBound($a_winlist) + 1][2]
            Else
                Dim $a_winlist[2][2]
            EndIf
            $a_winlist[0][0] = UBound($a_winlist) - 1
            $a_winlist[UBound($a_winlist) - 1][0] = $a_list[$x][0]
            $a_winlist[UBound($a_winlist) - 1][1] = $a_list[$x][1]
            If $show = 1 Then ConsoleWrite(UBound($a_winlist) - 1 & ": " & $a_list[$x][0] & @LF)
        EndIf
    Next
EndFunc   ;==>_createWinlist

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

If you are a wee brave.. search "mousehook" in examples and you may find a hook that I coded that you can use to set a "CBT" hook... which will inform you when a window is created through GUIRegisterMsg.

Lar.

I have used your "mousehook" ,but I got too much information,what I want is only the handle of new window.gafrost's code help me a lot,but using GUIRegisterMsg can save me a lot of time, could you help me?

my code:

Const $WH_CBT = 5

Const $HCBT_SETFOCUS = 0x1400 + 0x1A30
Const $HCBT_ACTIVATE = 0x1400 + 0x1A31
Const $HCBT_CREATEWND = 0x1400 + 0x1A32
Const $HCBT_DESTROYWND = 0x1400 + 0x1A33
Const $HCBT_MINMAX = 0x1400 + 0x1A34

Global $n,$msg,$buffer = ""

HotKeySet("{ESC}","GoAway")

$gui = GUICreate("test")

Global $DLLinst = DLLCall("kernel32.dll","hwnd","LoadLibrary","str",".\hook.dll")
Global $cbtHOOKproc = DLLCall("kernel32.dll","hwnd","GetProcAddress","hwnd",$DLLInst[0],"str","CBTProc")
Global $hhCBT = DLLCall("user32.dll","hwnd","SetWindowsHookEx","int",$WH_CBT, _
        "hwnd",$cbtHOOKproc[0],"hwnd",$DLLinst[0],"int",0)
DLLCall(".\hook.dll","int","SetValuesCBT","hwnd",$gui,"hwnd",$hhCBT[0])

GUIRegisterMsg($HCBT_CREATEWND,"myCBTfunc")

While 1
    $msg = GUIGetMsg()
    If $msg = -3 Then ExitLoop
WEnd

Func MyCBTFunc($hWndGUI, $MsgID, $WParam, $LParam)
    $n += 1
    If $n > 25 Then
        $n = 25
        $buffer = StringTrimLeft($buffer,StringInStr($buffer,@LF))
    EndIf
    $buffer &= "CBT: " & $MsgID & "," &  WinGetTitle($WParam) & "," &  $LParam & @LF
    ToolTip($buffer)
EndFunc

Func GoAway()
    Exit
EndFunc

Func OnAutoItExit()
    ;DLLCall("user32.dll","int","UnhookWindowsHookEx","hwnd",$hhMouse[0])
    ;DLLCall("user32.dll","int","UnhookWindowsHookEx","hwnd",$hhKey[0])
    DLLCall("user32.dll","int","UnhookWindowsHookEx","hwnd",$hhCBT[0])
    DLLCall("kernel32.dll","int","FreeLibrary","hwnd",$DLLinst[0])
EndFunc
Edited by zhao
Link to comment
Share on other sites

The code bellow is what I get so far,and as you see,it cost so much CPU usage .If I can use GUIRegisterMsg ,it will be much better.

HotKeySet("{ESC}", "_Exit")

Global $list[22]
$list[0] = ""
$list[1] = "Default IME"
$list[2] = "M"
$list[3] = "SysFader"
$list[4] = "MCI command handling window"
$list[5] = "DirectorExtension"
$list[6] = "CiceroUIWndFrame"
$list[7] = "TF_FloatingLangBar_WndTitle"
$list[8] = "NetDDE Agent"
$list[9] = "TrayIcon"
$list[10] = "CAsyncSocketEx Helper Window"
$list[11] = "Socket Notification Sink"
$list[12] = "Logitech GetMessage Hook"
$list[13] = "LogiTrayMgrWnd"
$list[14] = "Logitech E/M Executive"
$list[15] = "Connections Tray"
$list[16] = "电表"
$list[17] = "Program Manager"
$list[18] = "?汐杵湩圠湩潤wж"
$list[19] = "Acrobat IEHelper"
$list[20] = "MS_WebcheckMonitor"
$list[21] = "「开始」菜单"

Global $b_list = _Createb_list()

While 1
    Sleep(200)
    ;$time1 =TimerInit()
    $back = _CheckNewWindows($b_list,0)
    If $back[0][0]>0 Then
        For $i=1 to $back[0][0]
            ConsoleWrite($back[$i][0]&@CRLF)
        Next
    EndIf
    ;ConsoleWrite("1    "&TimerDiff($time1)&@CRLF)
    ;Exit
WEnd

Func _CheckNewWindows(ByRef $b_list,$flags=0)
    Local $c_list=WinList(),$return[1][2]
    $return[0][0]=0
    If $c_list[0][0] <> $b_list[0][1] Then
        $c_list = _Createb_list()
        ;$time3 =TimerInit()
        If $flags=2 Then; closed windows
            For $x = 1 To $b_list[0][0]
                $found = 0
                For $y = 1 To $c_list[0][0]
                    If $b_list[$x][1] = $c_list[$y][1] Then
                        $found = 1
                        $z = 0
                        ExitLoop
                    EndIf
                    $z = $x
                Next
                If $found = 0 Then 
                    ReDim $return[UBound($return)+1][2]
                    $return[UBound($return)-1][0] = $b_list[$z][0]
                    $return[UBound($return)-1][1] = $b_list[$z][1]      
                EndIf
            Next
        Else
            For $x = 1 To $c_list[0][0]
                $found = 0
                $same_name = 0
                For $y = 1 To $b_list[0][0]
                    If $flags=0 And $c_list[$x][1] = $b_list[$y][1] Then; new windows
                        $found = 1
                        $z = 0
                        ExitLoop
                    ElseIf $flags=1 And $c_list[$x][0] <> $b_list[$y][0] And $c_list[$x][1] = $b_list[$y][1] Then; title changed windows
                        $same_name = 1
                        ExitLoop
                    EndIf
                    $z = $x
                Next
                If $flags=0 And $found = 0 Then 
                    ReDim $return[UBound($return)+1][2]
                    $return[UBound($return)-1][0] = $c_list[$z][0]
                    $return[UBound($return)-1][1] = $c_list[$z][1]
                ElseIf $flags=1 And $same_name = 1 Then
                    ReDim $return[UBound($return)+1][2]
                    $return[UBound($return)-1][0] = $c_list[$z][0]
                    $return[UBound($return)-1][1] = $c_list[$z][1]
                EndIf
            Next
        EndIf
        $return[0][0] = UBound($return)-1
        $b_list = $c_list
        ;ConsoleWrite("3    "&TimerDiff($time3)&@CRLF)
    EndIf
    Return $return
EndFunc   ;==>_CheckNewWindows
    
Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _Createb_list()
    ;$time2 =TimerInit()
    Local $a_list=WinList(),$a_b_list
    $a_list[0][1] = $a_list[0][0]
    For $x = 1 To $a_list[0][0]
        If _IsIn($a_list[$x][0]) =0 And $a_list[$x][0] <> ""Then
            If IsArray($a_b_list) Then
                ReDim $a_b_list[UBound($a_b_list) + 1][2]
            Else
                Dim $a_b_list[2][2]
            EndIf
            $a_b_list[0][0] = UBound($a_b_list) - 1
            $a_b_list[UBound($a_b_list) - 1][0] = $a_list[$x][0]
            $a_b_list[UBound($a_b_list) - 1][1] = $a_list[$x][1]
        EndIf
    Next
    ;ConsoleWrite("2    "&TimerDiff($time2)&@CRLF)
    Return $a_b_list
EndFunc   ;==>_createb_list

Func _IsIn($var)
    ;$time4 =TimerInit()
    $result = 0
    $var2 = StringStripWS($var,3)
    For $x = 0 To UBound($list) - 1
        If $var2 = $list[$x] Then $result = 1
    Next
    ;ConsoleWrite("4    "&TimerDiff($time4)&@CRLF)
    Return $result
EndFunc
Edited by zhao
Link to comment
Share on other sites

  • 1 year later...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...