Jump to content
Sign in to follow this  
anandchakru

Windows Change event

Recommended Posts

anandchakru

Hi everyone, I'm a NooB, can you help me figure out how to find when a window is switched. Say I have paint and notepad window opened, paint being active now. I need to take a screen shot before and after the window is switched from paint to notepad !!

Thanks,

`A


[font="Book Antiqua"]Thanks`A[/font]

Share this post


Link to post
Share on other sites
martin

Hi everyone, I'm a NooB, can you help me figure out how to find when a window is switched. Say I have paint and notepad window opened, paint being active now. I need to take a screen shot before and after the window is switched from paint to notepad !!

Thanks,

`A

You can get the active window in a number of different ways. One way is

$llastactive = ''
 While 1
     $activeWindow = WinGetTitle("")
     If $activeWindow <> $lastactive Then
         If $activeWindow = $PaintTitle Or $lastactive = $NotePAdTitle Then
             TakeScreenSHot();whatever you use here
         EndIf
         $lastactive = $activeWindow
     EndIf
 
     Sleep(100)
 WEnd

which would take a screenshot whenever one of the 2 programs becomes active.

EDIT: Welcome to the AutoIt forums anandchakru :)

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
sandin

taken from siao's example the following script will hook your previous and your current window:

#include <GuiConstantsEx.au3>
#include <Misc.au3>
#include <WindowsConstants.au3>
#include <ListboxConstants.au3>
#Include <SendMessage.au3>

Global Const $HSHELL_WINDOWACTIVATED = 4;
Global $bHook = 1
Global $previous_hwnd = "", $previous_title = ""
Global $current_hwnd = ""
Global $tooltip_display1 = ""
Global $tooltip_display2 = ""

Global $iGuiW = 400, $iGuiH = 50, $sTitle = "Shell Hooker", $aBtnText[2] = ["START", "STOP"]
$hGui = GUICreate($sTitle, $iGuiW, $iGuiH, -1, 0, $WS_POPUP+$WS_BORDER, $WS_EX_TOPMOST)

GUIRegisterMsg(RegisterWindowMessage("SHELLHOOK"), "HShellWndProc")
ShellHookWindow($hGui, $bHook)

GUISetState(@SW_HIDE)

While 1
    Sleep(1000)
WEnd

Func HShellWndProc($hWnd, $Msg, $wParam, $lParam)
    Switch $wParam
        Case $HSHELL_WINDOWACTIVATED
            Local $win_title = WinGetTitle($lParam)
            Local $win_handle = $lParam
            if $previous_hwnd <> "" Then $tooltip_display1 = "Previous window" & @CRLF & "Title: " & $previous_title & @CRLF & "Handle: " & $previous_hwnd & @CRLF
            $previous_hwnd = $win_handle
            $previous_title = $win_title
            $tooltip_display2 = $tooltip_display1 & @CRLF & "Current window" & @CRLF & "Title: " & $win_title & @CRLF & "Handle: " & $win_handle
            TrayTip("", $tooltip_display2, 30)
    EndSwitch
EndFunc

Func ShellHookWindow($hWnd, $bFlag)
    Local $sFunc = 'DeregisterShellHookWindow'
    If $bFlag Then $sFunc = 'RegisterShellHookWindow'
    Local $aRet = DllCall('user32.dll', 'int', $sFunc, 'hwnd', $hWnd)
    Return $aRet[0]
EndFunc
Func RegisterWindowMessage($sText)
    Local $aRet = DllCall('user32.dll', 'int', 'RegisterWindowMessage', 'str', $sText)
    Return $aRet[0]
EndFunc

and as for capturing window images if they are not visible (currently on top of the screen), use this: LINK

Share this post


Link to post
Share on other sites
anandchakru

Thanks martin, for the warm and quick-reponse-welcome :)

sandin thank you.

Serves my question perfect !!


[font="Book Antiqua"]Thanks`A[/font]

Share this post


Link to post
Share on other sites
notsure

taken from siao's example the following script will hook your previous and your current window:

#include <GuiConstantsEx.au3>
#include <Misc.au3>
#include <WindowsConstants.au3>
#include <ListboxConstants.au3>
#Include <SendMessage.au3>

Global Const $HSHELL_WINDOWACTIVATED = 4;
Global $bHook = 1
Global $previous_hwnd = "", $previous_title = ""
Global $current_hwnd = ""
Global $tooltip_display1 = ""
Global $tooltip_display2 = ""

Global $iGuiW = 400, $iGuiH = 50, $sTitle = "Shell Hooker", $aBtnText[2] = ["START", "STOP"]
$hGui = GUICreate($sTitle, $iGuiW, $iGuiH, -1, 0, $WS_POPUP+$WS_BORDER, $WS_EX_TOPMOST)

GUIRegisterMsg(RegisterWindowMessage("SHELLHOOK"), "HShellWndProc")
ShellHookWindow($hGui, $bHook)

GUISetState(@SW_HIDE)

While 1
    Sleep(1000)
WEnd

Func HShellWndProc($hWnd, $Msg, $wParam, $lParam)
    Switch $wParam
        Case $HSHELL_WINDOWACTIVATED
            Local $win_title = WinGetTitle($lParam)
            Local $win_handle = $lParam
            if $previous_hwnd <> "" Then $tooltip_display1 = "Previous window" & @CRLF & "Title: " & $previous_title & @CRLF & "Handle: " & $previous_hwnd & @CRLF
            $previous_hwnd = $win_handle
            $previous_title = $win_title
            $tooltip_display2 = $tooltip_display1 & @CRLF & "Current window" & @CRLF & "Title: " & $win_title & @CRLF & "Handle: " & $win_handle
            TrayTip("", $tooltip_display2, 30)
    EndSwitch
EndFunc

Func ShellHookWindow($hWnd, $bFlag)
    Local $sFunc = 'DeregisterShellHookWindow'
    If $bFlag Then $sFunc = 'RegisterShellHookWindow'
    Local $aRet = DllCall('user32.dll', 'int', $sFunc, 'hwnd', $hWnd)
    Return $aRet[0]
EndFunc
Func RegisterWindowMessage($sText)
    Local $aRet = DllCall('user32.dll', 'int', 'RegisterWindowMessage', 'str', $sText)
    Return $aRet[0]
EndFunc

and as for capturing window images if they are not visible (currently on top of the screen), use this: LINK

Sandin, is it possible to stop prevent a process from starting before it started, with this code??

Share this post


Link to post
Share on other sites
sandin

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.