Enumerate All Windows

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Enumerate All Windows

Post by JRL » Wed Aug 03, 2016 6:34 pm

Code: Select all

/*
Enumerate All Windows.scp
James Richard Lockey - September 28, 2014
Similar to "View System Windows" tool.
Initially displays relative level, window handle, window name,
window class and window process.  Double clicking on a line additionally
displays the window size, window location and window process id
in a message dialog.  Click on a text item to make the selection happen quickly.
Double click on the message dialog also puts
the line information to the clipboard in the order:
Level, Handle, Name, Class, Xpos,Ypos, Width,Height, Process Id, Process
*/

Dialog>Dialog1
object Dialog1: TForm
  Left = 243
  Top = 108
  Width = 1500
  Height = 813
  HelpContext = 5000
  AutoScroll = True
  BorderIcons = [biSystemMenu]
  Caption = 'All Windows'
  Color = 16777200
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Position = poScreenCenter
  ShowHint = True
  OnTaskBar = False
  DesignSize = (
    1425
    775)
  PixelsPerInch = 96
  TextHeight = 13
  object MSStringGrid1: tMSStringGrid
    Left = 10
    Top = 10
    Width = 1461
    Height = 757
    Anchors = [akLeft, akTop, akRight, akBottom]
    BiDiMode = bdLeftToRight
    Ctl3D = True
    DefaultRowHeight = 17
    DrawingStyle = gdsGradient
    FixedColor = 8388863
    FixedCols = 0
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    Options = [goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goDrawFocusSelected, goColSizing, goRowSelect, goFixedColClick, goFixedRowClick, goFixedHotTrack]
    ParentBiDiMode = False
    ParentCtl3D = False
    ParentFont = False
    ParentShowHint = False
    ShowHint = True
    TabOrder = 0
    ExplicitWidth = 845
    ExplicitHeight = 740
    ColWidths = (
      27
      150
      350
      350
      550)
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,MSStringGrid1,OnDblClick,GetProcess

Dialog>Dialog2
object Dialog2: TForm
  Left = 436
  Top = 239
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  BorderStyle = bsNone
  Caption = ' '
  ClientHeight = 150
  ClientWidth = 300
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Position = poScreenCenter
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 0
    Top = 0
    Width = 300
    Height = 150
    Align = alClient
    Alignment = taCenter
    BiDiMode = bdLeftToRight
    Caption = 'qqq'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clNavy
    Font.Height = -20
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentBiDiMode = False
    ParentFont = False
    Layout = tlCenter
    ExplicitWidth = 33
    ExplicitHeight = 25
  end
end
EndDialog>Dialog2

SetDialogProperty>Dialog2,Label1,Caption,Building Window List%crlf%Please Wait...

Dialog>Dialog3
object Dialog3: TForm
  Left = 436
  Top = 239
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  BorderStyle = bsToolWindow
  Caption = ' '
  ClientHeight = 211
  ClientWidth = 476
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object LabelHeader: TLabel
    Left = 12
    Top = 12
    Width = 11
    Height = 20
    Caption = '  '
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -16
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label1: TLabel
    Left = 12
    Top = 32
    Width = 59
    Height = 25
    Caption = 'Label1'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlue
    Font.Height = -20
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
  end
    object Label2: TLabel
    Left = 157
    Top = 32
    Width = 59
    Height = 25
    Caption = 'Label1'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlue
    Font.Height = -20
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
  end
  object Label3: TLabel
    Left = 300
    Top = 32
    Width = 59
    Height = 25
    Caption = 'Label1'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlue
    Font.Height = -20
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
  end
  object Label4: TLabel
    Left = 446
    Top = 32
    Width = 59
    Height = 25
    Caption = 'Label1'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlue
    Font.Height = -20
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
  end
  object LabelTop: TLabel
    Left = 0
    Top = 0
    Width = 10
    Height = 25
    Caption = '  '
  end
  object LabelBottom: TLabel
    Left = 0
    Top = 75
    Width = 10
    Height = 25
    Caption = '  '
  end
end
EndDialog>Dialog3

AddDialogHandler>Dialog3,,OnDblClick,PutToClipboard
AddDialogHandler>Dialog3,LabelHeader,OnDblClick,PutToClipboard
AddDialogHandler>Dialog3,Label1,OnDblClick,PutToClipboard
AddDialogHandler>Dialog3,Label2,OnDblClick,PutToClipboard
AddDialogHandler>Dialog3,Label3,OnDblClick,PutToClipboard
AddDialogHandler>Dialog3,Label4,OnDblClick,PutToClipboard

Show>Dialog2

DeleteFile>%temp_dir%childwindowlist.csv
Writeln>%temp_dir%childwindowlist.csv,wres,"Lev.","Handle","Name","Class","Process"

Let>WindowCount=0
Let>Level=0
Let>WIN_USEHANDLE=1

//Get and write data for top window
LibFunc>user32,GetDesktopWindow,hwnd
GetWindowNames>hwnd,title,class
GoSub>Print,hwnd,title,class

//Initial jump down to first top level child window.
Let>Level=1
LibFunc>user32,GetWindow,GWres,hwnd,5
GetWindowNames>GWres,title,class
GoSub>Print,GWres,title,class
Let>Window_%Level%=GWres
Let>DoNextFlag=0

Label>StepDown
  If>DoNextFlag=0
    GoSub>GetChild,GWres
    If>{(%GWChildRes%<>0)or(%DoNextFlag%=1)}
      GoTo>StepDown
    Else>
      Sub>Level,1
      Let>LastWindow=Window_%Level%
      GoSub>GetNext,LastWindow
      Goto>StepDown
    EndIf
  Else>
    GoSub>GetNext,GWres
    GoTo>StepDown
  EndIf

SRT>GetChild
  LibFunc>user32,GetWindow,GWChildRes,GetChild_var_1,5
  If>GWChildRes<>0
    Add>Level,1
    Let>Window_%Level%=GWChildRes
    GetWindowNames>GWChildRes,title,class
    GoSub>Print,GWChildRes,title,class
    Let>GWRes=GWChildRes
  Else
      GoSub>GetNext,GetChild_var_1
  EndIf
END>GetChild

SRT>GetNext
  LibFunc>user32,GetWindow,GWRes,GetNext_var_1,2
  If>GWres<>0
    Let>Window_%Level%=GWRes
    Let>GWChildRes=1
    GetWindowNames>GWRes,title,class
    GoSub>Print,GWRes,title,class
    Let>DoNextFlag=0
  Else
    Sub>Level,1
    Let>GWRes=Window_%Level%
    Let>DoNextFlag=1
  EndIf
  If>Level=0
    ReadFile>%temp_dir%childwindowlist.csv,vData
    SetDialogProperty>Dialog1,MSStringGrid1,LoadFromCSV,vData
    SetDialogProperty>Dialog1,,Caption,All %WindowCount% Windows
    CloseDialog>Dialog2
    Wait>0.2
    Show>Dialog1,
    Exit>0
  EndIf
END>GetNext

SRT>Print
  If>GWres=0
    Goto>SkipWrite
  EndIf
    StringReplace>Print_var_2,crlf,space,Print_var_2
    StringReplace>Print_var_2,cr,space,Print_var_2
    StringReplace>Print_var_2,lf,space,Print_var_2
    If>%Print_var_1%<>%hwnd%
      GetWindowProcess>Print_var_1,ProcID,ProcName
    Else
      Let>ProcName=
    EndIf
    Let>Spaces=
    If>Level>0
      Let>SPkk=0
      Repeat>SPkk
        Add>SPkk,1
        ConCat>Spaces,%space%%space%%space%%space%
      Until>SPkk=level
    EndIf
    Add>WindowCount,1
    Writeln>%temp_dir%childwindowlist.csv,wres,"%Level%","%Spaces%%Print_var_1%","%Print_var_2%","%Print_var_3%","%ProcName%"
  Label>SkipWrite
END>Print

SRT>GetProcess
  GetTextInit
  GetCursorPos>CurX,CurY

  Let>GTaP=0
  Repeat>GTaP
  Wait>0.1
    Add>GTaP,1
    GetTextAtPoint>CurX,CurY,WindowData,vCharPos
    If>vCharPos>0
      Let>GTaP=100
    EndIf
  Until>GTaP=100

  Separate>WindowData,space,vItem

  Let>vI=1
  Repeat>vI
    Add>vI,1
    Let>value=vItem_%vI%
    If>value=
    Else
      Let>vI=vItem_Count
      Let>WinHandle=value
    EndIf
  Until>vI=vItem_Count

  Trim>WinHandle,WinHandle
  Let>WIN_USEHANDLE=1
  GetWindowProcess>WinHandle,ProcID,ProcName
  GetWindowPos>WinHandle,vWinX,vWinY
  GetWindowSize>WinHandle,vWinW,vWinH
  SetDialogProperty>Dialog3,,Caption,vItem_3
  SetDialogProperty>Dialog3,LabelHeader,Caption,Window Handle%Tab%XPos,YPos%Tab%Width,Height%Tab%Process ID%space%%space%
  SetDialogProperty>Dialog3,Label1,Caption,%WinHandle%
  SetDialogProperty>Dialog3,Label2,Caption,%vWinX%,%vWinY%
  SetDialogProperty>Dialog3,Label3,Caption,%vWinW%,%vWinH%
  SetDialogProperty>Dialog3,Label4,Caption,%ProcID%
  SetDialogProperty>Dialog3,Label1,AutoSize,True
  SetDialogProperty>Dialog3,,AutoSize,False
  SetDialogProperty>Dialog3,Label1,AutoSize,True
  SetDialogProperty>Dialog3,,AutoSize,True
  Let>WIN_USEHANDLE=1
    ResizeWindow>Dialog3.handle,1000,100
  Let>WIN_USEHANDLE=0
  SetDialogProperty>Dialog3,,Position,poScreenCenter
  Show>Dialog3,
END>GetProcess

SRT>PutToClipboard
  ReadFile>%temp_dir%childwindowlist.csv,vHandleData
  Separate>vHandleData,WinHandle,vHndData
  Separate>vHndData_2,crlf,vHndLine
  Separate>vHndLine_1,"%comma%",vHndItem
  Separate>vHndData_1,crlf,vHndLine
  Let>vLastLine=vHndLine_%vHndLine_Count%
  StringReplace>vLastLine,"%comma%",,vLastLine
  StringReplace>vLastLine,",,CBVar
  StringReplace>vHndItem_4,",,vHndItem_4
  Concat>CBVar,%comma%%Space%%WinHandle%
  Concat>CBVar,%comma%%Space%%vHndItem_2%
  Concat>CBVar,%comma%%Space%%vHndItem_3%
  Concat>CBVar,%comma%%Space%%vWinX%%comma%%vWinY%
  Concat>CBVar,%comma%%Space%%vWinW%%comma%%vWinH%
  Concat>CBVar,%comma%%Space%%ProcID%
  Concat>CBVar,%comma%%Space%%vHndItem_4%
  PutClipBoard>CBVar
  SetDialogProperty>Dialog2,Label1,Caption,Data placed in Clipboard
  Show>Dialog2
  Wait>0.5
  CloseDialog>Dialog2
END>PutToClipboard

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts