1. Computing
Creating an API GUI Windows program with message loop
Page 5: Windows handles and the CreateWindow function
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Page 1: Preparing to create your first Windows API GUI program
• Page 2: What makes an API application: the window Class
• Page 3: Intro to Windows Messaging
• Page 4: On message loops and WndMessageProc function
• Page 6: Your first Windows API GUI Delphi application
 Join the Discussion
Post your views and comments to this chapter of the free "raw API programming" Delphi Course
Discuss!
 Related Resources
• A Guide to raw API programming.TOC

The window "Handle"
Whenever you call the CreateWindow function, it returns the Windows "handle" for that window. This handle is a unique number (Cardinal) to identify the system information structure (record) for the properties of this window. Handles are used as a reference identifier for a Windows object, a Window, Font, File, Brush, Device Context, and many others. A program uses this handle in other API functions to tell the OS which system object to use. A window handle has the HWND (THandle or Cardinal) data type.

Most of the API Create Functions (CreateWindowEx, CreateFont, CreateBrush, CreatePen, CreateFile, CreateDialog and others) return a handle to the object that is created. An Object is a windows structure that represents a system resource, such as a window, font, file, a thread, or a graphic image. A program cannot directly access the internal structure of a window, object or the system resource that an object represents. Unlike Delphi VCL where you use Form1.Top to get or set the Top position of Form1. Instead, the program must use an object handle in functions to examine or modify the system resource. Windows uses objects and handles to limit access to system resources, the use of system objects ensures that developers are not writing code to low-level, internal or device driver structures. This enables Microsoft to add or change functionality of the operating system, as long as the original calling functions are maintained.

When newer versions of the Windows are released, programs will gain this new functionality with little or no additional development. Also security can be used for some objects in an access-control list (ACL).

The value 0, zero, is not a window handle, but you can use it in some functions to specify that no window is affected or to reference the base or desktop screen window. For example, specifying 0 for the CreateWindowEx function's hwndParent parameter creates a window that has no parent or owner. Functions may return 0 instead of a handle, indicating that the given action doesn't apply to a specific window. The IsWindow function determines whether a window handle identifies a valid, existing window.

For most objects, there are functions that create the object returning an object handle, close the object handle, and destroy the object. These functions may be combined or unnecessary, depending on the type of object and it's use. Handles and objects consume memory. So to increase system performance, a program should close handles and delete objects as soon as they are not needed. Any time you use a function that returns a "Handle", like "CreateWindow" or "GetDC", you should make sure to check and see if you need to use a "Destroy", "Delete", "Close" or "Release" function to remove it and release its memory in the Windows.

The CreateWindow function
As you saw in the Windows Class parameters, there are parameters (styles) that set some general charateristics for the windows of that wClass. But registering a wClass does not create any windows, you have to use CreateWindow or CreateWindowEX to actually create a window.
CreateWindowEX has more parameters and will be used later. When you use CreateWindow you can give much more information about the properties you want the window to have in the parameters of this function. Here's the definition for it:

function CreateWindow(lpClassName: PChar; lpWindowName: PChar;
  dwStyle: DWORD; X, Y, nWidth, nHeight: Integer; hWndParent: HWND;
  hMenu: HMENU; hInstance: HINST; lpParam: Pointer): HWND;

CreateWindow(
 lpClassName: PChar;   // PChar to registered class name
 lpWindowName: PChar;  // PChar to window name or text of the window
 dwStyle: Cardinal;    // window style bits
 X: Integer,           // left horizontal position of window 
 Y: Integer,           // top vertical position of window 
 nWidth: Integer,      // window width
 nHeight: Integer;     // window height 
 hWndParent: THandle;  // handle to parent or owner window
 hMenu: THandle;       // handle to menu or child-window identifier 
 hInstance: THandle;   // handle to application instance 
 lpParam: Pointer;     // pointer to window-creation data
): THandle;

lpClassName is set to the window's Class you want this window to be derived from. There are standard windows control classes like "Button" and "Edit" that you would use for windows controls. For our main window (Form) in we will use the class name we registered with wClass.

lpWindowName is usually the displayed text for that window, if the window does not display text or you don't want any text (an empty Edit) then you set this to an empty PChar '' or nil. Although it is called lpWindowName it is NOT like the Delphi VCL "Name" property, and is NOT used to identify or reference the window with the OS, the window handle is used for that.

dwStyle. Look at style for CreateWindow (like WS_BORDER) in the Win32 API Help. The desriptions there are helpful, but you will have to use a style bit flag (and not use a style bit) to see how it affects the look and function of different types of windows. For example, a WS_CAPTION on a Button control will make a strange button with a caption.

X, Y, nWidth, nHeight are integers for position, X (Left), Y (Top) and size.

hWndParent is the parent of the window if WS_CHILD is in the style or sets the owner window of the window being created. This should be 0, zero, for top level windows.

hMenu identifies a menu, or specifies a child-window identifier depending on the window style. For an overlapped or pop-up window, hMenu identifies the menu to be used with the window; it can be zero if the class menu is to be used. For a child window, hMenu specifies the child-window identifier, an integer value used to notify its parent about events.

hInstance is set to the process hInstance.

lpParam is a Pointer to a TCreateStruct Record used by the lParam parameter of the WM_CREATE message. I don't use the lParam here, but you can pass info to the WM_CREATE message with it. This is helpful for MDI window creation, but is generally set to nil otherwise. And finally, look at the CreateWindow functions in the example code on the next page to get some examples on how to use this function...

Next page > Your first Windows API GUI Delphi application > Page 1, 2, 3, 4, 5, 6

A guide to developing Delphi programs in raw Windows API: Next Chapter >>
>> The TOC

©2013 About.com. All rights reserved.