Jump to content
Sign in to follow this  

How to intercept new window ?

Recommended Posts


Is there some smart way to create a tray-icon program, which will only react whenever a new window is created ?

The program must use no or little CPU while not active.

Have a nice day!


Share this post

Link to post
Share on other sites

You can take this code as basic to hook window creation

#include <GUIConstantsEx.au3>
#include "DllCallBack.au3"

Global Const $WH_MOUSE = 0x7

Global $hMouseHook, $pStub_MouseProc, $hWnd_ui

$dwThread = DllCall("kernel32.dll", "dword", "GetCurrentThreadId")
$dwThread = $dwThread[0]
$pStub_MouseProc = _DllCallBack ("_MouseProc", "int;ptr;ptr")
$hMouseHook = DllCall("user32.dll", "ptr", "SetWindowsHookEx", "int", $WH_MOUSE, "ptr", $pStub_MouseProc, "ptr", 0, "dword", $dwThread)
$hMouseHook = $hMouseHook[0]

$hWnd_ui = GUICreate("")

While 1
    If $GUI_EVENT_CLOSE = GUIGetMsg() Then
        DllCall("user32.dll", "int", "UnhookWindowsHookEx", "ptr", $hMouseHook)
        _DllCallBack_Free ($pStub_MouseProc)

Func _MouseProc($nCode, $wParam, $lParam)
    If $nCode < 0 Then Return _CallNextHookEx($hMouseHook, $nCode, $wParam, $lParam)
    $vMHS = DllStructCreate("long;long;hwnd;uint;dword;dword", $lParam)
    WinSetTitle($hWnd_ui, "", "X: " & DllStructGetData($vMHS, 1) & " Y: " & DllStructGetData($vMHS, 2) & " HitTest: " & DllStructGetData($vMHS, 4))
    Return 0
EndFunc  ;==>_MouseProc

Func _CallNextHookEx($hhk, $nCode, $wParam, $lParam)
    Local $aTmp = DllCall("user32.dll", "long", "CallNextHookEx", "ptr", $hhk, "int", $nCode, "ptr", $wParam, "ptr", $lParam)
    Return $aTmp[0]
EndFunc  ;==>_CallNextHookEx

However you need $WH_CBT = 5 instead of $WH_MOUSE. And your hook procedure have to have the filter for HCBT_CREATEWND = 3 code.

The point of world view

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...