Spy on your programs

A user emailed me a question:

How can I get the handle often named as "HWnd" of any Visual Foxpro native visual object such as COMMANDBUTTON,SHAPE,EDITBOX,LISTBOX ,etc.?


There is a tool called SPY++ that ships with Visual Studio (on my machine, Start Menu->All Programs->Microsoft Visual Studio .Net 2003-> Visual Studio .Net Tools\Spyxx.exe) that can help.


Start Spy, choose Spy->Find Window from the menu. The dialog above appears. Click and drag the Finder Tool icon over any other application on your screen. Spy will draw a black box around the window found under the mouse.  It also displays the Handle (hWnd) for that window. (Spy works by injecting a windows hook DLL into every process: spyxxhk.dll)


Spy can display the myriad of windows messages that the window receives: mousemoves, clicks, activate events, paint requests, etc.


Try running this code (typing it in, rather than pasting it, shows intellisense)










Now try Spy on the form and button (you may have to close other VFP windows for Spy to see it) to see the hWnd.


Start Microsoft Outlook, choose Tools->Options, use Spy to show that each checkbox, button has an hWnd.

Now start Microsoft Word (or if you have Word configured as your email editor, just start creating a new email message). Now choose Tools->Options, and use Spy. Word uses Owner Drawn controls! Try Spying on Internet Explorer Tools->Options->Advanced to see more Owner Drawn controls. Or Control Panel->Add or Remove Programs.


Any window with an hWnd was actually created by the Microsoft Windows operating system. Applications can create a window on which to display user interface elements, such as dialog controls and pictures. The OS provides many window types, such as buttons, comboboxes, listboxes (see About Window Classes in MSDN library). .


However, Visual Foxpro has historically used its own “owner drawn” controls. This means VFP just draws buttons and listboxes on the form, rather than creating an OS hWnd for the controls.  


Foxpro 2.6 (more than 10 years ago, when a 66 Mhz CPU was considered fast!) was available for 4 widely different platforms: Windows, DOS, Mac, and Unix.  Using Owner Drawn controls made implementing UI across platforms simpler. Owner Draw also makes it possible to create controls that are not implemented in the OS.


Using OS window controls with hWnds has advantages: Active Accessibility allows other applications to use COM to “read” an application, perhaps for visually impaired users. Visual Styles  or themes are automatically applied to controls with hWnds. 


You say: What? VFP has had Active Accessibility and themes for years. How can that be? VFP implements AA and themes by having code that manually responds to AA and theme APIs. (Don’t be fooled by several dialogs in VFP that are actually OS dialogs: like File->Open or changing a font.)


Owner drawn controls also have disadvantages: UI behaviors can be subtly different.


VFP forms have hWnds. There is a property of VFP forms called “hWnd” that returns the hWnd value for the form.


For more fun, try spying on other applications, like web pages.