HWND Objects

The HWND class represents a native window handle, which is the type used in the Windows APIs to refer to an on-screen window. This is a subtype of the HANDLE object, which represents any sort of system handle value.

Creation

You don't typically create HWND objects directly. You usually get them from system objects (such as mainWindow) or from external DLL functions.

If necessary, though, you can explicitly create an HWND object via new HWND(value), where value is a string, number, or Uint64 value containing a numeric representation of a window handle value, or another HWND or HANDLE object. This allows you to coerce a generic object handle to the window handle type, or to create a window handle object from a value you obtained externally.

Note that new HWND() doesn't actually create an on-screen window, and it doesn't magically convert any arbitrary number you give it into a valid window handle. The number or handle you supply must already be a valid window handle. If you supply a value that isn't a valid window handle, the various window information methods will return nonsensical results.

Methods

HWND is a subtype of HANDLE, so it has all of the prototype methods of the regular HANDLE object. It has the following additional methods that are specific to windows.

hwnd.getWindowPos(): Returns an object with information on the position of the window represented by the handle. The returned object has the following properties:

The rectangle elements are represented as objects, with properties left, top, right, bottom giving the bounding coordinates as integer values. The bottom right corner is expressed as the position of the first pixel just outside of the window to the right and below. This might seem a little awkward, but all Windows APIs use the same convention, so it ensures consistency with any window measurements you obtain from direct calls to Windows APIs or other sources. It also has the useful property that (right - left) yields the rectangle's width and (bottom - top) yields its height.

hwnd.isVisible(): Returns true if the window is currently visible, false if not. Visibility is a system attribute that determines whether or not the window is being drawn in the UI, so a visible window isn't necessarily actually visible to the user: it might be minimized, obscured by a foreground window that's positioned in front of it, or positioned outside of the visible area of the display.