1. Home
  2. Computing & Technology
  3. Delphi Programming
Creating an API GUI Windows program with message loop
Page 2: What makes an API application: the window Class.
 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 3: Intro to Windows Messaging
• Page 4: On message loops and WndMessageProc function
• Page 5: Windows handles and the CreateWindow 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

There are two things that "window" creation must have. A "window" can not be created without a "window Class" and a function to connect it to the OS messages (this function is set in the Class). So we'll start talking about the windows Class first and then begin to explain Windows messaging.

Note: I did not want to start with the RegisterClassEx(wClassEx); and CreateWindowEx; The "Ex" versions are meant for 32-bit programming, but the non-EX work just as well if you do not need the extra Parameters. More will be said about the Ex versions later.

Window Class
The Windows OS has many window creation options which use a "Class" to define properties for that window. Read your local windows API help for "CreateWindow", notice the first parameter is for a Class Name. The Windows "Class" type used in window creation is different than the Delphi "Class" type used for Objects. If you read the WNDCLASS in your local windows API help you will see:

typedef struct _WNDCLASS {  
    UINT    style; 
    WNDPROC lpfnWndProc; 
    int     cbClsExtra; 
    int     cbWndExtra; 
    HANDLE  hInstance; 
    HICON   hIcon; 
    HCURSOR hCursor; 
    HBRUSH  hbrBackground; 
    LPCTSTR lpszMenuName; 
    LPCTSTR lpszClassName; 
} WNDCLASS;

//using Delphi types
type
  PWndClassA = ^TWndClassA;
  PWndClass = PWndClassA;

  tagWNDCLASSA = packed record
    style: Cardinal;
    lpfnWndProc: TFNWndProc;  //Pointer
    cbClsExtra: Integer;
    cbWndExtra: Integer;
    hInstance: THandle;
    hIcon: THandle;
    hCursor: THandle;
    hbrBackground: THandle;
    lpszMenuName: PChar;
    lpszClassName: PChar;
  end;

All windows that are created must use a registered windows Class, because the Class sets the function (Window Proc) that processes the messages to that window.
To register a Class, you first need to set it's parameters. The two parameters that must be set in order to have CreateWindow succeed are lpszClassName and lpfnWndProc. Set the windows Class hInstance to the memory locacation of your apps hInstance, this associates that Class with your programs thread execution. The registered Class will be then unregistered when that hInstance is terminated. The hIcon and hCursor will define the Icon and Cursor used by windows of this Class. The hbrBackground is the brush handle used to paint the background of windows of this class, you can also use a system color reference number here. The lpszMenuName sets a menu for this class. The "style" sets how the window will update after moving it, how to process double-clicks, the way to allocate space for it's device context, and other properties of the window. Look at the Class styles in WNDCLASS API help. We will not be using a class style in this first example. The lpfnWndProc is set to the memory Address of the process (function) where windows messages are sent, often called a "Window Proc".

Later in this chapter, you'll find the code for a Windows GUI application called "First Window GUI Application" where you can see how the class parameters are set up and then registered, look below the Program's BEGIN for wClass.
Since we set lpfnWndProc := @WndMessageProc; we need to have a WndMessageProc function with the (hWnd: HWnd; Msg: UINT; WParam: WPARAM; LParam: LPARAM): UINT; stdcall; parameters to receive the messages. If there is no message function or that message function does not "Handle" (use) any messages, then a working GUI can not be created. The lpszMenuName is for a PChar name of a Resource menu, we will not use this parameter here and set it to an empty string. The cbClsExtra and cbWndExtra params are for "Extra" information, you can place in the Registered class to read or change later. These will not be used here.

There are "Standard" Win32 classes like "Button", "Edit", "Static", "Listbox" and others, so you don't have to create and register those. Look at the Win32 help for "CreateWindow" and review the "Button" class, which will be used in the "First Window GUI Application" program code later.

Before we proceed, let's take a look at Windows messaging...

Next page > Intro to Windows Messaging > Page 1, 2, 3, 4, 5, 6

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

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.