Partager via


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)

PUBLIC x

x=NEWOBJECT("form")

x.Show()

x.AddObject("btn","commandbutton")

x.btn.Visible=1

x.Left=300

?x.HWnd

?"Hex:",TRANSFORM(x.HWnd,"@0x")

 

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.

 

35219

Comments

  • Anonymous
    September 14, 2004
    Thank you very much, Calvin Hsia!
    It's very very helpful!
    ButI think Visual Foxpro should try to change its own “owner drawn” controls to Window Classes in order to give developers further controls of native visual objects by handle,anyone else has the same idea?

  • Anonymous
    July 27, 2005
    I am sure that u know VFP has its own COM interface which other application/COM's can use to access its PEM.

    What would be more interesting, if Microsoft makes VFP apps cross platform than what you have written could be more meaningful.

    Nevertheless what you have written is a sure eye opener for me which i will always keep in mind.


  • Anonymous
    August 18, 2006
    You can use an array of PictureBox controls to display multiple photographs or other images. The sample...

  • Anonymous
    January 12, 2007
    I wanted to create a document, so I went to Outlook, which I always have running, and type Ctrl-N to

  • Anonymous
    February 26, 2007
    Let’s log all the calls that Excel makes to open or create a file. Start Visual Studio (any version),

  • Anonymous
    September 27, 2007
    If you start Task Manager (Ctrl-Shift-Esc) and display the Applications tab, you will see a list of running

  • Anonymous
    November 28, 2008
    Suppose you have some old code lying around that creates a Win32 window with an HWnd (perhaps ATL , MFC

  • Anonymous
    June 08, 2009
    PingBack from http://cellulitecreamsite.info/story.php?id=9181

  • Anonymous
    June 09, 2009
    PingBack from http://quickdietsite.info/story.php?id=9565