Delphi - Getting Window Handles

I wanted to develop my own print screen function - but instead of capturing the image to the clipboard, I wanted to write the window image directly to a jpeg or png file.

To do that, I needed a way to obtain the handle of the application window.

Windows handles | Windows commands | Using a mouse


Windows handles

Windows uses handles to identify many objects. These include application windows and the controls (buttons, menus, and so forth) displayed on the screen.

In windows.pas, handles are defined as

where a LongWord is an unsigned 32-bit value. In winnt.h, these are defined as pointers to void - which amounts to approximately the same thing .. in a 32-bit operating system.

C++ is a case sensitive language. In it, all the native variable types are lower case and all the Microsoft defined types are uppercase. In Delphi (a case insensitive language), defined types begin with an uppercase-T (by convention).

Note that windows.pas contains data from many c++ header files and is used to define the Windows API as described in the Windows SDK. Specifically, it includes data types, constants, and function calls.


Windows commands

From the Windows SDK help (Window Handles) Delphi 5 ships with a tool - WinSight - that will show you all the current windows, their children, and their handles. Double click an entry for additional information. If you select Messages / Selected windows you will be able to see all the windows messages sent to the selected window. Unfortunately, clicking Stop! crashes the application and it will not restart until you reboot the system. I have tried using both Process Explorer and Alt-ctrl-del to terminate the application, but it still won't restart. (You've been warned!!)

These are some of the available Windows functions


Using a mouse

It is also possible to get a window handle by using the mouse. When the mouse button is released, OnMouseUp gets x/y values relative to the origin of the control where mouse button was pressed. To get the position relative to the form, the control's position must be added. Then ClientToScreen will provide the appropriate coordinates. Even though GetWindowRect requires a pointer, the syntax above works.

In ClientToScreen I hardcoded the name of the button attached to the method. Generally, that is considered bad form. A more generic technique is

However, the help files were far from clear on how that works. Once I read the source files it was clear. The first example above called ClientToScreen for the form, not the control, and I had to add the position of the button to make it work.


GetCursorPos

The Windows API function GetCursorPos provides another way to get the mouse position.

The Delphi 5 help on this is a bit confusing.


Mouse Cursor

Once the button is pressed, the mouse cursor should change to indicate its new function. In the code below, I have simply selected the hand cursor. It was important to only change the cursor when the left mouse button was pressed - without that test, the cursor would change when any button was pressed, but would not change back when either the middle or the right button was used. Apparently, windows does not send a button up message when the cursor is no longer in the application's window. screen.Cursor keeps the cursor image from changing when selecting a window outside the application's border.


Author: Robert Clemenzi
URL: http:// mc-computing.com / Languages / Delphi / WindowHandle.html