On one of my long and branching tangents to solve a problem I'm having, I wanted a function that would retrieve all windows' handles (HWNDs), with support for filtering, like visible, main window (not popup etc) and taskbar presence.
It all went well until I wanted to implement the visibility check: depending on how I filtered, I kept either getting windows that had no visible presence, or not getting windows that were actually visible.
Clarification: I'm not interested in a window's position: it can be all off screen for all I care, or have dimensions of 0,0. Those are trivial to check if you need it.
I'm also not interested in whether another window clips it, let alone whether you can still see it through the other window (the latter is pretty much impossible to generalize anyway)
I want to know if a window has a visible presence (i.e. has a window control that could be seen at least by moving it around)
here is what I did:
detecthiddenwindows, off is far too impotent to filter out most of the windows that appear hidden to the user for all kinds of reasons, but I kept it in anyway.
I started by checking WS_ styles and WS_EX_ styles, to see if there was a combination of styles there that would render a window invisible to me.
I had a window that had WS_VISIBLE on, position and dimensions like a maximized window with thick borders/shadow, and indeed, its state is maximized, and it has the WS_MAXIMIZE style.
It is not WS_EX_NOACTIVATE either, yet I lived in complete oblivion to its existence.
When I activated it via script, it suddenly appeared.
Here is the data I collected from it:
Microsoft Store:
WS_BORDER = TRUE
WS_CAPTION = TRUE
WS_CHILD = FALSE
WS_CLIPCHILDREN = FALSE
WS_CLIPSIBLINGS = TRUE
WS_DISABLED = FALSE
WS_DLGFRAME = TRUE
WS_GROUP = TRUE
WS_HSCROLL = FALSE
WS_MAXIMIZE = TRUE
WS_MAXIMIZEBOX = TRUE
WS_MINIMIZE = FALSE
WS_MINIMIZEBOX = TRUE
WS_OVERLAPPED = FALSE
WS_POPUP = TRUE
WS_POPUPWINDOW = TRUE
WS_SIZEBOX = TRUE
WS_SYSMENU = TRUE
WS_TABSTOP = TRUE
WS_THICKFRAME = TRUE
WS_TILED = FALSE
WS_VISIBLE = TRUE
WS_VSCROLL = FALSE
WS_EX_ACCEPTFILES = FALSE
WS_EX_APPWINDOW = FALSE
WS_EX_CLIENTEDGE = FALSE
WS_EX_COMPOSITED = FALSE
WS_EX_CONTEXTHELP = FALSE
WS_EX_CONTROLPARENT = FALSE
WS_EX_DLGMODALFRAME = FALSE
WS_EX_LAYERED = FALSE
WS_EX_LAYOUTRTL = FALSE
WS_EX_LEFT = FALSE
WS_EX_LEFTSCROLLBAR = FALSE
WS_EX_LTRREADING = FALSE
WS_EX_MDICHILD = FALSE
WS_EX_NOACTIVATE = FALSE
WS_EX_NOINHERITLAYOUT = FALSE
WS_EX_NOPARENTNOTIFY = FALSE
WS_EX_NOREDIRECTIONBITMAP = TRUE
WS_EX_RIGHT = FALSE
WS_EX_RIGHTSCROLLBAR = FALSE
WS_EX_RTLREADING = FALSE
WS_EX_STATICEDGE = FALSE
WS_EX_TOOLWINDOW = FALSE
WS_EX_TOPMOST = FALSE
WS_EX_TRANSPARENT = FALSE
WS_EX_WINDOWEDGE = TRUE
X: -8, Y: -8, W 1936, H: 1066
Window state: Maximized
I would say the fact that it has WS_EX_NOREDIRECTIONBITMAP enabled has to do with that, but it's definitely not the whole story.
What's more, I get the exact same style set from another window that IS in fact visible.
DllCall("IsWindowVisible") also fails in those cases (by that I mean it reports the invisible ones as visible).
Sure, a window may use which ever means necessary to display what ever it wants, but does that also turn off collision detection?
And even if that were the case, that would be a reeeeally strange practice, to keep up a maximized window even when it's hidden and toggle rendering and hit detection with its desired state.
Does anybody know what is going on here?
I'm a little lost.
I would like to know 2 things:
- What makes this window, for example, check all the boxes for visibility but appear invisible?
(Windows Store is always running in the background when my system boots, as do some other things like Settings) - Is there a good way to figure out whether a window is visible? (and of course, how?)
![Smile :)](./images/smilies/icon_e_smile.gif)
Bonus: Here is the script I'm running (sort of, threw together some parts of my function library and the thing I'm working on)
Code: Select all
#singleINSTANCE FORCE
setTitleMAtchmode,RegEx
; https://docs.microsoft.com/en-us/windows/desktop/winmsg/window-styles
global WINSTYLES := {"WS_BORDER": 0x800000 ;+/-Border. Creates a window that has a thin-line border.
,"WS_POPUP": 0x80000000 ;Creates a pop-up window. This style cannot be used with the WS_CHILD style.
,"WS_CAPTION": 0xC00000 ;+/-Caption. Creates a window that has a title bar. This style is a numerical combination of WS_BORDER and WS_DLGFRAME.
,"WS_DISABLED": 0x8000000 ;+/-Disabled. Creates a window that is initially disabled.
,"WS_DLGFRAME": 0x400000 ;Creates a window that has a border of a style typically used with dialog boxes.
,"WS_GROUP": 0x20000 ;+/-Group. Indicates that this control is the first one in a group of controls. This style is automatically applied to manage the "only one at a time" behavior of radio buttons. In the rare case where two groups of radio buttons are added consecutively (with no other control types in between them), this style may be applied manually to the first control of the second radio group, which splits it off from the first.
,"WS_HSCROLL": 0x100000 ;Creates a window that has a horizontal scroll bar.
,"WS_MAXIMIZE": 0x1000000 ;Creates a window that is initially maximized.
,"WS_MAXIMIZEBOX": 0x10000 ;+/-MaximizeBox. Creates a window that has a maximize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified.
,"WS_MINIMIZE": 0x20000000 ;Creates a window that is initially minimized.
,"WS_MINIMIZEBOX": 0x20000 ;+/-MinimizeBox. Creates a window that has a minimize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified.
,"WS_OVERLAPPED": 0 ;Creates an overlapped window. An overlapped window has a title bar and a border. Same as the WS_TILED style.
,"WS_OVERLAPPEDWINDOW": 0xCF0000 ;Creates an overlapped window with the WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_THICKFRAME, WS_MINIMIZEBOX, and WS_MAXIMIZEBOX styles. Same as the WS_TILEDWINDOW style.
,"WS_POPUPWINDOW": 0x80880000 ;Creates a pop-up window with WS_BORDER, WS_POPUP, and WS_SYSMENU styles. The WS_CAPTION and WS_POPUPWINDOW styles must be combined to make the window menu visible.
,"WS_SIZEBOX": 0x40000 ;+/-Resize. Creates a window that has a sizing border. Same as the WS_THICKFRAME style.
,"WS_SYSMENU": 0x80000 ;+/-SysMenu. Creates a window that has a window menu on its title bar. The WS_CAPTION style must also be specified.
,"WS_TABSTOP": 0x10000 ;+/-Tabstop. Specifies a control that can receive the keyboard focus when the user presses the Tab key. Pressing the Tab key changes the keyboard focus to the next control with the WS_TABSTOP style.
,"WS_THICKFRAME": 0x40000 ;Creates a window that has a sizing border. Same as the WS_SIZEBOX style.
,"WS_VSCROLL": 0x200000 ;Creates a window that has a vertical scroll bar.
,"WS_VISIBLE": 0x10000000 ;Creates a window that is initially visible.
,"WS_CHILD": 0x40000000 ;Creates a child window. A window with this style cannot have a menu bar. This style cannot be used with the WS_POPUP style.
,"WS_CLIPCHILDREN": 0x02000000 ;Excludes the area occupied by child windows when drawing occurs within the parent window. This style is used when creating the parent window.
,"WS_CLIPSIBLINGS": 0x04000000 ;Clips child windows relative to each other; that is, when a particular child window receives a WM_PAINT message, the WS_CLIPSIBLINGS style clips all other overlapping child windows out of the region of the child window to be updated. If WS_CLIPSIBLINGS is not specified and child windows overlap, it is possible, when drawing within the client area of a child window, to draw within the client area of a neighboring child window.
,"WS_TILED": 0x00000000} ;The window is an overlapped window. An overlapped window has a title bar and a border. Same as the WS_OVERLAPPED style.
; https://docs.microsoft.com/en-us/windows/desktop/winmsg/extended-window-styles
global EXSTYLES := {"WS_EX_ACCEPTFILES": 0x00000010 ;The window accepts drag-drop files.
,"WS_EX_APPWINDOW": 0x00040000 ;Forces a top-level window onto the taskbar when the window is visible.
,"WS_EX_CLIENTEDGE": 0x00000200 ;The window has a border with a sunken edge.
,"WS_EX_COMPOSITED": 0x02000000 ;Paints all descendants of a window in bottom-to-top painting order using double-buffering. For more information, see Remarks. This cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC.
; Windows 2000: This style is not supported.
,"WS_EX_CONTEXTHELP": 0x00000400 ;The title bar of the window includes a question mark. When the user clicks the question mark, the cursor changes to a question mark with a pointer. If the user then clicks a child window, the child receives a WM_HELP message. The child window should pass the message to the parent window procedure, which should call the WinHelp function using the HELP_WM_HELP command. The Help application displays a pop-up window that typically contains help for the child window.
; WS_EX_CONTEXTHELP cannot be used with the WS_MAXIMIZEBOX or WS_MINIMIZEBOX styles.
,"WS_EX_CONTROLPARENT": 0x00010000 ;The window itself contains child windows that should take part in dialog box navigation. If this style is specified, the dialog manager recurses into children of this window when performing navigation operations such as handling the TAB key, an arrow key, or a keyboard mnemonic.
,"WS_EX_DLGMODALFRAME": 0x00000001 ;The window has a double border; the window can, optionally, be created with a title bar by specifying the WS_CAPTION style in the dwStyle parameter.
,"WS_EX_LAYERED": 0x00080000 ;The window is a layered window. This style cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC.
; Windows 8: The WS_EX_LAYERED style is supported for top-level windows and child windows. Previous Windows versions support WS_EX_LAYERED only for top-level windows.
,"WS_EX_LAYOUTRTL": 0x00400000 ;If the shell language is Hebrew, Arabic, or another language that supports reading order alignment, the horizontal origin of the window is on the right edge. Increasing horizontal values advance to the left.
,"WS_EX_LEFT": 0x00000000 ;The window has generic left-aligned properties. This is the default.
,"WS_EX_LEFTSCROLLBAR": 0x00004000 ;If the shell language is Hebrew, Arabic, or another language that supports reading order alignment, the vertical scroll bar (if present) is to the left of the client area. For other languages, the style is ignored.
,"WS_EX_LTRREADING": 0x00000000 ;The window text is displayed using left-to-right reading-order properties. This is the default.
,"WS_EX_MDICHILD": 0x00000040 ;The window is a MDI child window.
,"WS_EX_NOACTIVATE": 0x08000000 ;A top-level window created with this style does not become the foreground window when the user clicks it. The system does not bring this window to the foreground when the user minimizes or closes the foreground window.
; The window should not be activated through programmatic access or via keyboard navigation by accessible technology, such as Narrator.
; To activate the window, use the SetActiveWindow or SetForegroundWindow function.
; The window does not appear on the taskbar by default. To force the window to appear on the taskbar, use the WS_EX_APPWINDOW style.
,"WS_EX_NOINHERITLAYOUT": 0x00100000 ;The window does not pass its window layout to its child windows.
,"WS_EX_NOPARENTNOTIFY": 0x00000004 ;The child window created with this style does not send the WM_PARENTNOTIFY message to its parent window when it is created or destroyed.
,"WS_EX_NOREDIRECTIONBITMAP": 0x00200000 ;The window does not render to a redirection surface. This is for windows that do not have visible content or that use mechanisms other than surfaces to provide their visual.
,"WS_EX_RIGHT": 0x00001000 ;The window has generic "right-aligned" properties. This depends on the window class. This style has an effect only if the shell language is Hebrew, Arabic, or another language that supports reading-order alignment; otherwise, the style is ignored.
; Using the WS_EX_RIGHT style for static or edit controls has the same effect as using the SS_RIGHT or ES_RIGHT style, respectively. Using this style with button controls has the same effect as using BS_RIGHT and BS_RIGHTBUTTON styles.
,"WS_EX_RIGHTSCROLLBAR": 0x00000000 ;The vertical scroll bar (if present) is to the right of the client area. This is the default.
,"WS_EX_RTLREADING": 0x00002000 ;If the shell language is Hebrew, Arabic, or another language that supports reading-order alignment, the window text is displayed using right-to-left reading-order properties. For other languages, the style is ignored.
,"WS_EX_STATICEDGE": 0x00020000 ;The window has a three-dimensional border style intended to be used for items that do not accept user input.
,"WS_EX_TOOLWINDOW": 0x00000080 ;The window is intended to be used as a floating toolbar. A tool window has a title bar that is shorter than a normal title bar, and the window title is drawn using a smaller font. A tool window does not appear in the taskbar or in the dialog that appears when the user presses ALT+TAB. If a tool window has a system menu, its icon is not displayed on the title bar. However, you can display the system menu by right-clicking or by typing ALT+SPACE.
,"WS_EX_TOPMOST": 0x00000008 ;The window should be placed above all non-topmost windows and should stay above them, even when the window is deactivated. To add or remove this style, use the SetWindowPos function.
,"WS_EX_TRANSPARENT": 0x00000020 ;The window should not be painted until siblings beneath the window (that were created by the same thread) have been painted. The window appears transparent because the bits of underlying sibling windows have already been painted.
; To achieve transparency without these restrictions, use the SetWindowRgn function.
,"WS_EX_WINDOWEDGE": 0x00000100} ;The window has a border with a raised edge.
getAllWindows(onlyMain = true, visible = true, taskbar = true){
if(visible || onlyMain)
detectHiddenWindows, off
WinGet, l_array, List
r := []
t := ""
u := ""
Loop, %l_array%{
hwnd := l_array%A_Index%
title := title(hwnd)
WinGet, ws, Style, % "ahk_id " hwnd
WinGet, es, ExStyle, % "ahk_id " hwnd
vis := true
if not DllCall("IsWindowVisible", "UInt", hwnd)
vis := false
If(visible && !(ws & WINSTYLES.WS_VISIBLE && vis))
continue
hastitle := title && title!=""
u := "`n" . title . ":`n"
clipboard .= u
If(onlyMain){
tool := es & EXSTYLES.WS_EX_TOOLWINDOW > 0
pop := ws & WINSTYLES.WS_POPUP > 0
cap := ws & WINSTYLES.WS_CAPTION > 0
app := es & EXSTYLES.WS_EX_APPWINDOW > 0
if(title == "Microsoft Store" || regexMatch(title, "Month View .?- Calendar") || app){
; winActivate, ahk_id %hwnd%
; u .= "test"
for ix,it in WINSTYLES{
if(ws & it)
u .= ix . " = TRUE`n"
else
u .= ix . " = FALSE`n"
}
for ix,it in ExSTYLES{
if(es & it)
u .= ix . " = TRUE`n"
else
u .= ix . " = FALSE`n"
}
}
if(tool || (pop && !cap)){
if(hastitle)
d .= u .= "``ntherefore skipped`n"
continue
}
}
; msgg(new controlObject(hwnd).toString())
wingetpos, x,y,w,h,ahk_id %hwnd%
t .= u . "X: " x ", Y: " y ", W " w ", H: " h "`nWindow state: " . wingetstate("ahk_id " . hwnd, true) . "`n`n"
r.push(hwnd)
}
msgg(t)
msgg(printArray(r))
Return r
}
;;;;;;;;;;;;;;;;;;;;;;;;; Supporting functions ;;;;;;;;;;;;;;;;;;;;;;;;
msgg(str, title=""){
Gui,New, +HwndGuiHwnd +LastFound +Resize
Gui,Font,S10,Consolas
Gui,Add,Edit,x-100 w10
Gui,Add,Edit,ReadOnly xm ym +hwndHED,%str%
Gui,Font,S10,Verdana
Gui,Add,Button,Default gCloseMsg w120 h28 x+-120 y+10 +hwndHOK,OK
Gui,Show, center, %title%
winwait, ahk_id %GuiHwnd%
wingetpos,,,ow,oh, ahk_id %GuiHwnd%
if(ow<156){
winmove, ahk_id %GuiHwnd%,,,,156
guicontrol,move,%HED%,w120
guicontrol,move,%HOK%,x8
}
loop{
if(!winexist("ahk_id " . GuiHwnd))
break
wingetpos,,,w,h, ahk_id %GuiHwnd%
if(w<156)
winmove, ahk_id %GuiHwnd%,,,,156
if(ow!=w){
ws := w - 36
xs := w - 143
guicontrol,move,%HED%,w%ws%
guicontrol,move,%HOK%,x%xs%
}
if(oh!=h){
hs := h - 98
ys := h - 80
guicontrol,move,%HED%,h%hs%
guicontrol,move,%HOK%,y%ys%
}
sleep,50
}
}
CloseMsg(){
winclose
}
PrintList(args*){
vnames := paramWords()
str := ""
for index,item in args{
str .= index . ": " . vnames[index] . " = " . item
if(index < args.length())
str .= "`n"
}
return str
}
PrintArray(args){
str := ""
for index,item in args{
str .= index . ": " . item
if(index < args.length())
str .= "`n"
}
return str
}
paramWords(){
ex := Exception("", -2)
FileReadLine line, % ex.File, % ex.Line
RegExMatch(line, "i)" . ex.What . "\s*\(\K[^\)]*", m)
return split(m,",")
}
title(hwnd){
wingettitle,t,ahk_id %hwnd%
return t
}
wingetstate(title, word = false){
winget,state,minmax,%title%
if(word){
if(state == -1)
return "Minimized"
else if(state == 0)
return "Restored (normal)"
else if(state == 1)
return "Maximized"
else
return "Weird"
}
return state
}
split(string, delim=",", trimvals = true){
parr := StrSplit(string, delim)
if(trimvals) {
for k, v in parr{
parr[k] := trim(v)
}
}
return, parr
}
f6::
getAllWindows(true)
return