top of page

Getting windows ... and getting information about windows ... using the Windows API

Updated: Sep 23

This post is one of a series providing implementation examples of Windows API Functions, Types, Enums and Consts using VBA. The code in this post can be used as-is, however, if you regularly (or even just occasionally) work with Windows API declarations in VBA, you may want to see the posts Automatically add Windows API declaration(s) and Using 'F1' to view Windows API web pages which explain some of the functionality that can be added to the VBE by VBE_Extras.

This post covers a number of Windows API Functions relating to:


Getting windows ...


  • FindWindow - gets a handle to the top-level window whose class name and window name match the specified strings (either the class name or the window name can be omitted)

  • FindWindowEx - similar to FindWindow but searches child windows

  • GetWindow - gets a handle to a window that has the specified relationship to another window (eg the parent window, the child at the top of the Z-order)

  • GetAncestor - gets a handle to the ancestor of a specific window.

  • GetParent - gets a handle to a specific window's parent or owner

  • GetForegroundWindow - gets a handle to the foreground window (that is, the window that the user is currently working with)

  • GetDesktopWindow - get a handle to the desktop window


... and getting information about windows ...


  • GetClassName - get the name of the class to which the specified window belongs

  • GetWindowText - get the text of the specified window's title bar (if it has one); if the specified window is a control, gets the text of the control

  • GetWindowInfo - gets information about the specified window ... the information is loaded into a WINDOWINFO Type and includes information on the window coordinates, style, status and more

  • GetWindowRect - gets the coordinates of the specified window ... the information is loaded into a RECT Type

  • GetWindowPlacement - gets information about the specified window ... the information is loaded into a WINDOWPLACEMENT Type (which uses RECT and POINTAPI Types) and includes information on the window 'show state' (maximised, minimised or normal) along with its position in each of those states

  • IsIconic - is the specified window minimised (i.e. iconic)

  • IsZoomed - is the specified window maximised (i.e. zoomed)

  • IsHungAppWindow - is the application associated with the specified window not responding (i.e. hung)

  • GetWindowModuleFileName - get the full path and file name of the module (that is, the .EXE or .DLL file) associated with the specified window


This list of Functions is not exhaustive ... there are many more related Functions to work with windows ... but these are Functions I use regular when wanting to get handles to windows ... and information about those windows.


Also, there is some overlap between many of these Functions ... you can pick the one that works best for you dependent on the requirements of your code.


So how to use each of these? The following code includes the VBA declaration for each Windows API Function (and any Types it depends on, and some related Consts) along with VBA code to use the "getting information about windows" Functions and some of the "getting windows" Functions. The core of this is the LogInfoAboutWindow (which does exactly as it says and logs information about the specified window to the Immediate window) and its 'helper' code as follows ...



... and the following code covers the remainder of the "getting windows" Windows API Functions and then calls LogInfoAboutWindow. You can run each of these to demonstrate using each of the Windows API Functions.



If you are using a host app other than Excel, you may also want to see the Get the application window handle in any app (well ... in Excel, Word, Access, PowerPoint or Outlook) post.


If you are struggling with knowing the window class and window text (e.g. for use with FindWindow and FindWindoeEx), the tool I use for this is Spy++ ... despite its dodgy name, it is a Microsoft product that you can download and install on your device. Once you get the hang of it, it hugely simplifies the process of identifying window class names and window text that you can then use in your code. For example, with a couple of clicks, I can find out that the top level:


  • Excel window has a class name of "XLMAIN"

  • Access window has a class name of "OMain"

  • Word window has a class name of "OpusApp"

  • PowerPoint window has a class name of "PPTFrameClass" (not in presentation mode)

  • Outlook window has a class name of "rctrl_renwnd32"

  • VBE window has a class name of "wndclass_desked_gsk"

  • ... any more for any window that is open on my device


... and that each of them has a specific window text (which depends on the name of the currently open/active file) and that each of them has an entire 'cascade' of child windows (for each of which I can see the relationship, class name, text and more).


Last thing ... if you are looking at some of the values produced for the location of maximised windows and seeing what appears to be invalid values (e.g. top/left has negative values and bottom/right has values beyond the size of the monitor), then you should read Raymond Chen's Why are the dimensions of a maximized window larger than the monitor? article and, possibly, you should subscribe to his The Old New Thing blog.


Comments


bottom of page