Platforms to show: All Mac Windows Linux Cross-Platform

Back to CGWindowMBS module.

CGWindowMBS.CreateWindowList(windowOption as Integer, WindowID as Integer = 0) as UInt32()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method CoreGraphics MBS MacFrameworks Plugin 11.2 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns the list of window IDs associated with the specified windows in the current user session.
Example
dim a(-1) as UInt32 = CGWindowMBS.CreateWindowList(0,0)

MsgBox str(UBound(a)+1) + " windows"

windowOption: The options describing which window IDs to return. Typical options let you obtain IDs for all windows or for windows above or below the window specified in the relativeToWindow parameter.

WindowID: The ID of the window to use as a reference point when determining which other windows to return. For options that do not require a reference window, this parameter can be kCGNullWindowID.

Returns an array of CGWindowID values corresponding to the desired windows. If there are no windows matching the desired criteria, the function returns an empty array. If you call this function from outside of a GUI security session or when no window server is running, this function returns nil.

Available in Mac OS X v10.5 and later.

May fail and cause a consent prompt on MacOS 10.15 to ask user whether he/she allows your app to take the picture.

CGWindowMBS.CreateWindowListCGImage(left as Double, top as Double, width as Double, height as Double, windowOption as Integer, WindowID as Integer = 0, ImageOption as Integer = 0) as Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method CoreGraphics MBS MacFrameworks Plugin 11.2 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Takes a screenshot from a list of windows.

Same as CreateWindowListImage, but returns a CGImageMBS. Declared as Variant to reduce plugin interdependencies.

May fail and cause a consent prompt on MacOS 10.15 to ask user whether he/she allows your app to take the picture.

Some examples using this method:

CGWindowMBS.CreateWindowListImage(left as Double, top as Double, width as Double, height as Double, windowOption as Integer, WindowID as Integer = 0, ImageOption as Integer = 0) as picture

Type Topic Plugin Version macOS Windows Linux iOS Targets
method CoreGraphics MBS MacFrameworks Plugin 11.2 ✅ Yes ❌ No ❌ No ✅ Yes All
Takes a screenshot from a list of windows.
Example
dim p as Picture

// Screenshot of everything:
p = CGWindowMBS.CreateWindowListImage(0, 0, 0, 0, CGWindowMBS.kCGWindowListOptionAll,0, CGWindowMBS.kCGWindowImageDefault)

// Screenshot of everything behind a window:
p = CGWindowMBS.CreateWindowListImage(0, 0, 0, 0, CGWindowMBS.kCGWindowListOptionOnScreenBelowWindow, CGWindowMBS.GetWindowID(window1), CGWindowMBS.kCGWindowImageDefault)

// Screenshot of everything in front of a window (dock and menubar):
p = CGWindowMBS.CreateWindowListImage(0, 0, 0, 0, CGWindowMBS.kCGWindowListOptionOnScreenAboveWindow, CGWindowMBS.GetWindowID(window1), CGWindowMBS.kCGWindowImageDefault)

// screenshot of a window
p = CGWindowMBS.CreateWindowListImage(0, 0, 0, 0, CGWindowMBS.kCGWindowListOptionIncludingWindow, CGWindowMBS.GetWindowID(window1), CGWindowMBS.kCGWindowImageDefault)

// only shadow of a window (will be in the mask)
p = CGWindowMBS.CreateWindowListImage(0, 0, 0, 0, CGWindowMBS.kCGWindowListOptionIncludingWindow, CGWindowMBS.GetWindowID(window1), CGWindowMBS.kCGWindowImageOnlyShadows)

// desktop decoration is white
p = CGWindowMBS.CreateWindowListImage(0, 0, 0, 0, CGWindowMBS.kCGWindowListExcludeDesktopElements, CGWindowMBS.GetWindowID(window1), CGWindowMBS.kCGWindowImageShouldBeOpaque)

Parameters:

leftLeft coordinate rectangle
topTop coordinate rectangle
widthWidth of rectangle
heightHeight of rectangle
windowOptionA combination of kCGWindowListOption* flags
WindowIDThe window ID or 0.
ImageOptionA combination of kCGWindowImage* flags

If you pass a rectangle with all values zero, you select the whole screen.
Returns the screenshot as picture or nil on any error.

Window Options:
kCGWindowListOptionAll0List all windows in this user session, including both on and off-screen windows. relativeToWindow should be kCGNullWindowID=0.
kCGWindowListOptionOnScreenOnly1List all on-screen windows in this user session, ordered from front to back. relativeToWindow should be kCGNullWindowID=0.
kCGWindowListOptionOnScreenAboveWindow2List all on-screen windows above the specified window ordered from front to back. relativeToWindow should be the window number.
kCGWindowListOptionOnScreenBelowWindow4List all on-screen windows below the specified window ordered from front to back. relativeToWindow should be the window number.
kCGWindowListOptionIncludingWindow8Include the named window in any list, effectively creating 'at-or-above' or 'at-or-below' lists. relativeToWindow should be the window number.
kCGWindowListExcludeDesktopElements16Exclude any windows from the list that are elements of the desktop, including the background picture and icons on the desktop.

Image Options:
kCGWindowImageDefault0Default behavior: If a rect of CGRectNull is used bounds computation includes the framing effects, such as a shadow.
kCGWindowImageBoundsIgnoreFraming1If a rect of CGRectNull is used, ignore framing effects for bounds computation
kCGWindowImageShouldBeOpaque2The captured image should be opaque. Empty areas are white
kCGWindowImageOnlyShadows4Capture only shadows.

May fail and cause a consent prompt on MacOS 10.15 to ask user whether he/she allows your app to take the picture.

Some examples using this method:

CGWindowMBS.GetWindowID(w as DesktopWindow) as integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method CoreGraphics MBS MacFrameworks Plugin 22.0 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Queries the CoreGraphics Window ID for the given window.

Returns 0 on any error.
This ID can be used for CreateWindowListImage.

See also:

Some examples using this method:

CGWindowMBS.GetWindowID(w as window) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method CoreGraphics MBS MacFrameworks Plugin 11.2 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Queries the CoreGraphics Window ID for the given window.

Returns 0 on any error.
This ID can be used for CreateWindowListImage.

See also:

CGWindowMBS.GetWindowListInfo(windowOption as Integer, WindowID as Integer = 0) as dictionary()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method CoreGraphics MBS MacFrameworks Plugin 11.2 ✅ Yes ❌ No ❌ No ✅ Yes All
Generates and returns information about the selected windows in the current user session.
Example
dim a(-1) as Dictionary = CGWindowMBS.GetWindowListInfo(0,0)

dim u as Integer = UBound(a)
if u > 10 then u = 10 // show only 10 times

dim lines(-1) as string
for i as Integer = 0 to u
dim d as Dictionary = a(i)

lines.Append d.Value(CGWindowMBS.kCGWindowName)+" of "+d.Value(CGWindowMBS.kCGWindowOwnerName)

next

// shows 11 windows with names. Not all windows have names.
MsgBox Join(lines, EndOfLine)

option: The options describing which window dictionaries to return. Typical options let you return dictionaries for all windows or for windows above or below the window specified in the relativeToWindow parameter. For more information, see "Window List Option Constants."

WindowID: The ID of the window to use as a reference point when determining which other window dictionaries to return. For options that do not require a reference window, this parameter can be 0.

Returns an array of CFDictionaryRef types, each of which contains information about one of the windows in the current user session. If there are no windows matching the desired criteria, the function returns an empty array. If you call this function from outside of a GUI security session or when no window server is running, this function returns nil.

You can use this function to get detailed information about the configuration of one or more windows in the current user session. For example, you can use this function to get the bounds of the window, its window ID, and information about how it is managed by the window server. For the list of keys and values that may be present in the dictionary, see kCGWindow* constants.

Generating the dictionaries for system windows is a relatively expensive operation. As always, you should profile your code and adjust your usage of this function appropriately for your needs.

Available in Mac OS X v10.5 and later.

Some examples using this method:

The items on this page are in the following plugins: MBS MacFrameworks Plugin.


The biggest plugin in space...