#include <GUIConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
;===============================================================================
;
; Description: Window Finder
; Author(s): Dmitry Yudin (Lazycat)
; Date: 26.10.2007
; Notes: Used for learning:
; http://www.codeproject.com/dialog/windowfinder.asp
;===============================================================================
; Initializing resources
Global $CURSOR_TARGET = WriteResource( _
"0x000002000100202000000F001000300100001600000028000000200000004000000001000100000000008000" & _
"00000000000000000000020000000200000000000000FFFFFF0000000000000000000000000000000000000000" & _
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & _
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & _
"00000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" & _
"FFFFFFF83FFFFFE6CFFFFFD837FFFFBEFBFFFF783DFFFF7EFDFFFEAC6AFFFEABAAFFFE0280FFFEABAAFFFEAC6A" & _
"FFFF7EFDFFFF783DFFFFBEFBFFFFD837FFFFE6CFFFFFF83FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" & _
"FFFFFFFFFFFFFFFFFFFFFFFF")
Global $ICON_TARGET_FULL = WriteResource( _
"0x0000010001002020080000000000E80200001600000028000000200000004000000001000400000000000002" & _
"000000000000000000001000000010000000000000000000800000800000008080008000000080008000808000" & _
"00C0C0C000808080000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF000000000000000000" & _
"00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFF" & _
"FFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFF00000FFFFFFFFFFFF000FFFFFFFFFF00FF0FF00FFFFFFFFFF000FF" & _
"FFFFFFF0FF00000FF0FFFFFFFFF000FFFFFFFF0FFFFF0FFFFF0FFFFFFFF000FFFFFFF0FFFF00000FFFF0FFFFFF" & _
"F000FFFFFFF0FFFFFF0FFFFFF0FFFFFFF000FFFFFF0F0F0FF000FF0F0F0FFFFFF000FFFFFF0F0F0F0FFF0F0F0F" & _
"0FFFFFF000FFFFFF0000000F0F0000000FFFFFF000FFFFFF0F0F0F0FFF0F0F0F0FFFFFF000FFFFFF0F0F0FF000" & _
"FF0F0F0FFFFFF000FFFFFFF0FFFFFF0FFFFFF0FFFFFFF000FFFFFFF0FFFF00000FFFF0FFFFFFF000FFFFFFFF0F" & _
"FFFF0FFFFF0FFFFFFFF000FFFFFFFFF0FF00000FF0FFFFFFFFF000FFFFFFFFFF00FF0FF00FFFFFFFFFF000FFFF" & _
"FFFFFFFF00000FFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF0" & _
"00FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000000000000000007770CCCCCCCCCCCCCCCCCCCC" & _
"C07770007070CCCCCCCCCCCCCCCCCCCCC07070007770CCCCCCCCCCCCCCCCCCCCC0777000000000000000000000" & _
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & _
"000000000000000000FFFFFFFF8000000080000000800000008000000080000000800000008000000080000000" & _
"800000008000000080000000800000008000000080000000800000008000000080000000800000008000000080" & _
"0000008000000080000000800000008000000080000000800000008000000080000000FFFFFFFFFFFFFFFFFFFF" & _
"FFFF")
Global $ICON_TARGET_EMPTY = WriteResource( _
"0x0000010001002020080000000000E80200001600000028000000200000004000000001000400000000000002" & _
"000000000000000000001000000010000000000000000000800000800000008080008000000080008000808000" & _
"00C0C0C000808080000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF000000000000000000" & _
"00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFF" & _
"FFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FF" & _
"FFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFF" & _
"F000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFF" & _
"FFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFF" & _
"FFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFF" & _
"FFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFF" & _
"FFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFF0" & _
"00FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000000000000000007770CCCCCCCCCCCCCCCCCCCC" & _
"C07770007070CCCCCCCCCCCCCCCCCCCCC07070007770CCCCCCCCCCCCCCCCCCCCC0777000000000000000000000" & _
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & _
"000000000000000000FFFFFFFF8000000080000000800000008000000080000000800000008000000080000000" & _
"800000008000000080000000800000008000000080000000800000008000000080000000800000008000000080" & _
"0000008000000080000000800000008000000080000000800000008000000080000000FFFFFFFFFFFFFFFFFFFF" & _
"FFFF")
; Loading cursor from file
$hTargetCursor = DllCall("User32.dll", "int", "LoadCursorFromFile", "str", $CURSOR_TARGET)
$hTargetCursor = $hTargetCursor[0]
Global $g_StartSearch = False, $gFoundWindow = 0, $gOldCursor
;~ Global $WM_MOUSEMOVE = 0x200
;~ Global $WM_LBUTTONUP = 0x202
#Region ### START Koda GUI section ### Form=
$hGUI = GUICreate("Window Finder", 586, 104, -1, -1)
GUICtrlCreateGroup("Target", 80, 0, 497, 65)
GUICtrlCreateLabel("Title:", 101, 18, 27, 17, $SS_RIGHT)
GUICtrlCreateLabel("Handle:", 87, 42, 41, 17, $SS_RIGHT)
$hLabelTitle = GUICtrlCreateLabel("", 133, 18, 436, 17, BitOR($SS_CENTER,$SS_RIGHT,$SS_GRAYFRAME,$SS_WHITEFRAME,$SS_SIMPLE))
$hLabelWnd = GUICtrlCreateLabel("", 133, 42, 100, 17)
GUICtrlCreateLabel("Class:", 240, 42, 32, 17)
$hLabelClass = GUICtrlCreateLabel("", 272, 42, 300, 17, BitOR($SS_CENTER,$SS_RIGHT,$SS_GRAYFRAME,$SS_WHITEFRAME,$SS_SIMPLE))
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("", 8, 0, 65, 65)
$hTargetPic = GUICtrlCreateIcon($ICON_TARGET_FULL, 0, 24, 20, 32, 32, BitOR($SS_NOTIFY,$WS_GROUP))
GUICtrlCreateGroup("", -99, -99, 1, 1)
$hOK = GUICtrlCreateButton("Close", 504, 72, 75, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUIRegisterMsg ($WM_MOUSEMOVE, "WM_MOUSEMOVE_FUNC")
GUIRegisterMsg ($WM_LBUTTONUP, "WM_LBUTTONUP_FUNC")
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE, $hOK
Exit
Case $hTargetPic
$g_StartSearch = True
DllCall("user32.dll", "hwnd", "SetCapture", "hwnd", $hGUI)
$gOldCursor = DllCall("user32.dll", "int", "SetCursor", "int", $hTargetCursor)
If not @error Then $gOldCursor = $gOldCursor[0]
GUICtrlSetImage($hTargetPic, $ICON_TARGET_EMPTY)
EndSwitch
Wend
Func WM_MOUSEMOVE_FUNC($hWnd, $nMsg, $wParam, $lParam)
If not $g_StartSearch Then Return 1
Local $mPos = MouseGetPos()
$hWndUnder = DllCall("user32.dll", "hwnd", "WindowFromPoint", "long", $mPos[0], "long", $mPos[1])
If not @error Then $hWndUnder = $hWndUnder[0]
If CheckFoundWindow($hWndUnder) Then
GUICtrlSetData($hLabelTitle, WinGetTitle($hWndUnder))
GUICtrlSetData($hLabelWnd, $hWndUnder)
GUICtrlSetData($hLabelClass, GetWindowClass($hWndUnder))
$gFoundWindow = $hWndUnder
EndIf
Return 1
EndFunc
Func WM_LBUTTONUP_FUNC($hWnd, $nMsg, $wParam, $lParam)
If not $g_StartSearch Then Return 1
$g_StartSearch = False
; Release captured cursor
DllCall("user32.dll", "int", "ReleaseCapture")
DllCall("user32.dll", "int", "SetCursor", "int", $gOldCursor)
GUICtrlSetImage($hTargetPic, $ICON_TARGET_FULL)
Return 1
EndFunc
Func CheckFoundWindow($hFoundWnd)
If $hFoundWnd = $hGUI Then Return False
If $hFoundWnd = 0 Then Return False
If $hFoundWnd = $gFoundWindow Then Return False
If not WinExists($hFoundWnd) Then Return False
Local $hTemp = DllCall("user32.dll", "hwnd", "GetParent", "hwnd", $hFoundWnd)
If not @error and $hTemp[0] = $hGUI Then Return False
Return True
EndFunc
Func GetWindowClass($hWnd)
$pClassName = DllStructCreate("char[256]")
DllCall("user32.dll", "int", "GetClassName", "hwnd", $hWnd, "ptr", DllStructGetPtr($pClassName), "int", 255)
Return DllStructGetData($pClassName, 1)
EndFunc
Func WriteResource($sbStringRes)
Local $sTempFile
Do
$sTempFile = @TempDir & "\temp" & Hex(Random(0, 65535), 4)
Until not FileExists($sTempFile)
Local $hFile = FileOpen($sTempFile, 2+16)
FileWrite($hFile, $sbStringRes)
FileClose($hFile)
Return $sTempFile
EndFunc
Func OnAutoitExit()
If IsDeclared("CURSOR_TARGET") Then
FileDelete($ICON_TARGET_FULL)
FileDelete($ICON_TARGET_EMPTY)
FileDelete($CURSOR_TARGET)
EndIf
EndFunc