Jump to content

get window handle


manit
 Share

Recommended Posts

  • Replies 57
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

currently my code is checking first four items in list

The problem is : message box just shows "You checked the CheckBox in Row <appropriate-number> which is "

No further description of the row.

My code is

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>
Global $fClick = False
Global $aCheck[4]

Run("getlog.bat")
$Gui = GUICreate("windows information", 1000, 650)  
$listview = GUICtrlCreateListView("select|handle|level|PID|restore,maximize,minimize|active,inactive|enable,disable|visible,hidden|application|TITLE", 10, 10, 900, 600)
$hWndListView = GUICtrlGetHandle($listview)
_GUICtrlListView_SetExtendedListViewStyle($hWndListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))
_GUICtrlListView_SetColumnWidth($hWndListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
$file = FileOpen("log.txt", 0)
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $item1 = GUICtrlCreateListViewItem($line, $listview)
    GUICtrlSetData($item1,"")
Wend
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
;While 1 # we are commenting simple exit action loop
;    $msg = GUIGetMsg()
;      If $msg = $GUI_EVENT_CLOSE Then ExitLoop
;WEnd
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch

    ; If an item was double clicked
    If $fClick Then
        $fClick = False
        For $i = 0 To 3
            $fCheck = _GUICtrlListView_GetItemChecked($hWndListView, $i)
            If $fCheck <> $aCheck[$i] Then
                $aCheck[$i] = $fCheck
                Switch $fCheck
                    Case True
                        ;ConsoleWrite("You checked the CheckBox in Row " & $i & @CRLF)
                        MsgBox ( 0, "notify", "You checked the CheckBox in Row " & $i & " which is " & _GUICtrlListView_GetItemText($listview, $i))
                    Case False
                        ;ConsoleWrite("You unchecked the CheckBox in Row " & $i & @CRLF)
                        MsgBox ( 0, "notify", "You unchecked the CheckBox in Row " & $i & " which is " & _GUICtrlListView_GetItemText($listview, $i))
                EndSwitch
                ExitLoop
            EndIf
        Next
    EndIf

WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)

    #forceref $hWnd, $iMsg, $iwParam

    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK
                    $fClick = True
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc
Link to comment
Share on other sites

Finally I manged to extract window handle from the row content using

$fullrow = _GUICtrlListView_GetItemTextString($listview, $i)
                        $whandle = StringMid($fullrow, 2, 8)
                        MsgBox ( 0, "notify", "You checked the CheckBox in Row " & $i & " whose id is " & $whandle)

I got line count in table using

MsgBox ( 0, "notify", "The number of lines in log is " & _FileCountLines("log.txt"))

Now what remains is

I have to dynamically assign array size depending on number of lines in log.txt .

How can I declare an array without a length then assign it size after log.txt has been created ?

Edited by manit
Link to comment
Share on other sites

i am troubled the window is not getting manipulated

cmdow gives 6 hex digit id while autoit's winset state uses 8 hex digit.

So I did

$whandle = StringReplace($whandle, "0x", "0x00")
WinSetState( $whandle, "", @SW_HIDE)

Still nothing happens ?

Link to comment
Share on other sites

debugging

$returned = WinSetState( $whandle, "", @SW_HIDE )
                        MsgBox ( 0, "was window found", $whandle & " found : " & $returned)

I get 0 means 'Returns 0 if window is not found.'

Is there some datatype input for winsetstate or normal string will be accepted as handle . I doubt it is trying to find window of that title instead of that handle ?

Edited by manit
Link to comment
Share on other sites

hi

BrewManNH

I got the function that returns top level window .

It is WinList

Please help me in my previous post .

Did you even look or run the script I posted on the last page? WinList is the first command in it, and it took you 20 posts to find it yourself? If you want to be helped, you need to look at the help given and slow down on the random thoughts to the forum. All they're showing to me is that you are not paying attention, not trying very hard to code it for yourself, and probably a bit of attention grabbing.

As to dynamically sizing arrays, please look at ReDim and search for examples of how to do it correctly on the forum.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Actually i was reporting the steps I have tried in quick replies.

Much of my code is borrowed from autoIT help & your replies.

Finally I have changed my code to work without external binaries.

Everything is going well except 'window not found'

Please point out mistake in this

#Include <File.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>
Global $fClick = False
Global $aCheck[4]
$var = WinList()
;MsgBox( 0, "number of top level windows", "number of top level windows : " & $var[0][0])
$Gui = GUICreate("windows information", 1000, 650)  
$listview = GUICtrlCreateListView("check|handle|title|serial number", 10, 10, 900, 600)
$hWndListView = GUICtrlGetHandle($listview)
_GUICtrlListView_SetExtendedListViewStyle($hWndListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))
_GUICtrlListView_SetColumnWidth($hWndListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
For $i = 1 to $var[0][0]
 ;   MsgBox(0, "Details", "Title=" & $var[$i][0] & @LF & "Handle=" & $var[$i][1])
    $everyitem = GUICtrlCreateListViewItem("nullemptyvoid|" & $var[$i][1] & "|" & $var[$i][0] & "|" & $i, $listview)
    GUICtrlSetData($everyitem,"")
Next
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
ReDim $aCheck[$var[0][0]]
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch

    ; If an item was double clicked
    If $fClick Then
        $fClick = False
        For $i = 0 To UBound($aCheck) - 1
            $fCheck = _GUICtrlListView_GetItemChecked($hWndListView, $i)
            If $fCheck <> $aCheck[$i] Then
                $aCheck[$i] = $fCheck
                Switch $fCheck
                    Case True
                        ;ConsoleWrite("You checked the CheckBox in Row " & $i & @CRLF)
                        $fullrow = _GUICtrlListView_GetItemTextString($listview, $i)
                        $whandle = StringMid($fullrow, 2, 10)
                        ;$whandle = StringReplace($whandle, "0x", "0x00")
                        MsgBox ( 0, "you did", "You checked the CheckBox in Row " & $i & " which is " & $whandle)
                        $returned = WinSetState( $whandle, "", @SW_SHOW )
                        MsgBox ( 0, "was window found" , $whandle & " found : " & $returned)
                    Case False
                        ;ConsoleWrite("You unchecked the CheckBox in Row " & $i & @CRLF)
                        $fullrow = _GUICtrlListView_GetItemTextString($listview, $i)
                        $whandle = StringMid($fullrow, 2, 10)
                        ;$whandle = StringReplace($whandle, "0x", "0x00")
                        MsgBox ( 0, "you did", "You unchecked the CheckBox in Row " & $i & " which is " & $whandle)
                        $returned = WinSetState( $whandle, "", @SW_HIDE )
                        MsgBox ( 0, "was window found", $whandle & " found : " & $returned)
                EndSwitch
                ExitLoop
            EndIf
        Next
    EndIf

WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)

    #forceref $hWnd, $iMsg, $iwParam

    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK
                    $fClick = True
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc
Edited by manit
Link to comment
Share on other sites

Various functions such as WinGetHandle, WinList and GUICreate return these handles. It is important to note that a window handle is not classed as a number or string - it is its own special type.

Turns out , I was right that whandle I am using is string.

I tried

Opt("WinTitleMatchMode", 4)

Still , no success.

Edited by manit
Link to comment
Share on other sites

This time I won

I did

MsgBox ( 0, "you did", "You unchecked the CheckBox in Row " & $i & " which is " & $var[$i][1])
                        $returned = WinSetState( $var[$i][1], "", @SW_HIDE )
                        MsgBox ( 0, "was window found", $var[$i][1] & " found : " & $returned)

That is , I used the array returned by winlist .

There should be a way to convert string to datatype appropriate for window handle ?

Link to comment
Share on other sites

I used $whandle = $var[$i][1] and it worked for me. You were trying to convert the value of the handle to a string and using that, rather than using the handle itself. I'm not sure what the difference in datatype a handle is from the string it displays, but it doesn't work with a string that is equivalent to the handle.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

(1)The code in which I am using winlist function works well because the function returns an array of handle & title string for each window.

(2)I also managed to provide sort functionality for title , handle & serial number ( not desirable , as I want this column to remain fixed counting from 1 to end , independent of associated title & handle) column.

(3)I also removed windows from list with empty titles using

For $i = 1 to $var[0][0]
     $empty = StringRegExp ( $var[$i][0] , "\A\s*\z")
    if $empty = 0 Then  
        $count = $count + 1
        $everyitem = GUICtrlCreateListViewItem("nullemptyvoid|" & $count & "|" & $var[$i][1] & "|" & $var[$i][0] & "|" & BitAND( WinGetState($var[$i][1]), 2 ) & "|" & $i , $listview)
        GUICtrlSetData($everyitem,"")
    EndIf
Next

The questions are

(1)The code in which I am using external binary cmdow gives handle as string of 6 hex digit to which I used to prepend 2 additional 0.I prefer handle over title as it is always unique. Currently it seems I can't treat a string as handle.

(2)I am also trying to add refresh button which will recreate the table. ( seems I have to use

_GUICtrlListViewDeleteAllItems(handleORcontrol-id))

)

Edited by manit
Link to comment
Share on other sites

I have given up.

It is too complicated to add button in list view (I saw last post at )

My working script is this

#Include <File.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>
Opt("WinTitleMatchMode", 4)
Global $fClick = False
Global $aCheck[4]
$var = WinList()
$Gui = GUICreate("windows information", 1000, 650)  
$listview = GUICtrlCreateListView("check|count|handle|title|visible|number", 10, 10, 900, 600, $LVS_SORTASCENDING, $LVS_LIST)
$hWndListView = GUICtrlGetHandle($listview)
_GUICtrlListView_SetExtendedListViewStyle($hWndListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))
_GUICtrlListView_SetColumnWidth($hWndListView, 3, $LVSCW_AUTOSIZE_USEHEADER)
$count = 0
For $i = 1 to $var[0][0]
     $empty = StringRegExp ( $var[$i][0] , "\A\s*\z")
    if $empty = 0 Then  
        $count = $count + 1
        $everyitem = GUICtrlCreateListViewItem("nullemptyvoid|" & $count & "|" & $var[$i][1] & "|" & $var[$i][0] & "|" & BitAND( WinGetState($var[$i][1]), 2 ) & "|" & $i , $listview)
        GUICtrlSetData($everyitem,"")
    EndIf
Next
;MsgBox ( 0 , "windows with some title/all level 1  " , $count & "/" & $var[0][0])
ReDim $aCheck[$count]
_GUICtrlListView_RegisterSortCallBack($listview)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()
 
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $listview
            _GUICtrlListView_SortItems($listview, GUICtrlGetState($listview))
    EndSwitch
    ; If an item was double clicked
    If $fClick Then
        $fClick = False
        For $i = 0 To $count - 1
            $fCheck = _GUICtrlListView_GetItemChecked($hWndListView, $i)
            If $fCheck <> $aCheck[$i] Then
                $aCheck[$i] = $fCheck
                Switch $fCheck
                    Case True
                        $fullrow = _GUICtrlListView_GetItemTextString($listview, $i)
                        $individualelements = StringSplit( $fullrow , "|" , 2 )
                        $windownumber = $individualelements [ UBound($individualelements) - 1 ]
                        $returned = WinSetState( $var[$windownumber][1], "", @SW_MINIMIZE )
                        ;MsgBox ( 0, "was window found " , $var[$windownumber][1] & " : " & $returned)
                    Case False
                        $fullrow = _GUICtrlListView_GetItemTextString($listview, $i)
                        $individualelements = StringSplit( $fullrow , "|" , 2 )
                        $windownumber = $individualelements [ UBound($individualelements) - 1 ]
                        $returned = WinSetState( $var[$windownumber][1], "", @SW_HIDE )
                    EndSwitch
                ExitLoop
            EndIf
        Next
    EndIf
WEnd
_GUICtrlListView_UnRegisterSortCallBack($listview)
 
 
 
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
 
    #forceref $hWnd, $iMsg, $iwParam
 
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
 
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
 
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK
                    $fClick = True
                
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

This script will give you list of window with nonempty title .

Visible have value 2 , hidden have 0.

'count' column is serial number while 'number' is window number in array returned by winlist.

You can sort any column.

Selecting shows window & minimizes it while untick hides it.

The only drawback is , it can't be refreshed.

I am immensely thankful to Melba23 , JohnOne & BrewManNH

Edited by manit
Link to comment
Share on other sites

  • 2 years later...

hi guys ,

I added refresh button

#Include <File.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>
Opt("WinTitleMatchMode", 4)
Global $fClick = False
Global $count = 0
Global $aCheck[4]
$Gui = GUICreate("windows information", 1000, 650)  
$listview = GUICtrlCreateListView("check|count|handle|title|visible|number", 10, 10, 900, 600, $LVS_SORTASCENDING, $LVS_LIST)
$hWndListView = GUICtrlGetHandle($listview)
_GUICtrlListView_SetExtendedListViewStyle($hWndListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))
_GUICtrlListView_SetColumnWidth($hWndListView, 3, $LVSCW_AUTOSIZE_USEHEADER)
Call("createtable")
;MsgBox ( 0 , "windows with some title/all level 1  " , $count & "/" & $var[0][0])
ReDim $aCheck[$count]
_GUICtrlListView_RegisterSortCallBack($listview)
$Button_1 = GUICtrlCreateButton("refresh", 910, 0, 90)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()
 
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $listview
            _GUICtrlListView_SortItems($listview, GUICtrlGetState($listview))
Case $Button_1
Call("refreshtable")
                ;Run('notepad.exe') ; Will Run/Open Notepad
    EndSwitch
    ; If an item was double clicked
    If $fClick Then
        $fClick = False
        For $i = 0 To $count - 1
            $fCheck = _GUICtrlListView_GetItemChecked($hWndListView, $i)
            If $fCheck <> $aCheck[$i] Then
                $aCheck[$i] = $fCheck
                Switch $fCheck
                    Case True
                        $fullrow = _GUICtrlListView_GetItemTextString($listview, $i)
                        $individualelements = StringSplit( $fullrow , "|" , 2 )
                        $windownumber = $individualelements [ UBound($individualelements) - 1 ]
                        $returned = WinSetState( $var[$windownumber][1], "", @SW_MINIMIZE )
                        ;MsgBox ( 0, "was window found " , $var[$windownumber][1] & " : " & $returned)
                    Case False
                        $fullrow = _GUICtrlListView_GetItemTextString($listview, $i)
                        $individualelements = StringSplit( $fullrow , "|" , 2 )
                        $windownumber = $individualelements [ UBound($individualelements) - 1 ]
                        $returned = WinSetState( $var[$windownumber][1], "", @SW_HIDE )
                    EndSwitch
                ExitLoop
            EndIf
        Next
    EndIf
WEnd
_GUICtrlListView_UnRegisterSortCallBack($listview)
 
 Func refreshtable()
;MsgBox(4096, "Test", "This box will time out in 10 seconds", 10)
_GUICtrlListView_DeleteAllItems($listview)
createtable()
EndFunc   ;refresh table
 
Func createtable()
$var = WinList()
$count = 0
For $i = 1 to $var[0][0]
     $empty = StringRegExp ( $var[$i][0] , "\A\s*\z")
    if $empty = 0 Then  
        $count = $count + 1
        $everyitem = GUICtrlCreateListViewItem("nullemptyvoid|" & $count & "|" & $var[$i][1] & "|" & $var[$i][0] & "|" & BitAND( WinGetState($var[$i][1]), 2 ) & "|" & $i , $listview)
        GUICtrlSetData($everyitem,"")
    EndIf
Next
GUISetState()
EndFunc   ;refresh table
 
 
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
 
    #forceref $hWnd, $iMsg, $iwParam
 
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
 
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
 
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK
                    $fClick = True
                
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc
Edited by manit
Link to comment
Share on other sites

The variable $var in your CreateTable function should be declared as a Global variable at the top of your script. The way you have it now, it's a Local variable to that function, and won't be seen in any other part of the script, so this line "$returned = WinSetState($var[$windownumber][1], "", @SW_MINIMIZE)" will cause an error.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

So I added line at the top

'Global $var'

Now , all is well.

thanks BrewManNH

You are the most valuable penguin.

Also , can you suggest any improvement in my program - like unnecessary use of particular variable , function or ways to optimize executable as refreshing takes a little time ?

Edited by manit
Link to comment
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
 Share

  • Recently Browsing   0 members

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