1. Home
  2. Computing & Technology
  3. Delphi Programming
Creating an API GUI Windows program with message loop
Page 1: Preparing to create your first Windows API GUI program
 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 2: What makes an API application: the window Class
• 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

Chapter 3: A guide to developing Delphi programs in raw Windows API
Let's see how to create a Windows GUI program with windows and a message loop. Here's what you'll find in this chapter: an intro to Windows messaging (with a discussion on message structure); about the WndMessageProc function, handles, the CreateWindow function, and much more.

The article is written by Wes Turner, and brought to you by Zarko Gajic

On Windows...
There are a lot of functions related to creating a Windows GUI application, "window" creation has to have many options to give the various looks and functions of different types of "windows". Before you start into windows creation, you should look at the MSDN web page for an Introduction to "Windows" at MSDN-Intro to Windows.
Click the link on that page to "About Windows" and read what it says. You might also go to the "Using Windows" and "Window Features" links if you have time.
Or you can use your local "Win32 API help" under the index name of "Windows". The first sentence in this help is "A window in an application written for the Microsoft® Windows® operating system is a rectangular area of the screen where the application displays output and receives input from the user."

Now go to the next page "about windows". Read this and then continue to next pages and keep reading. After several pages you will get to "Window Creation" page, consider what it says. Now, the next page is "Window Attributes", read this page twice, for useful information. Continue to the next page "Window Handles". Now the next page "Main Window Creation" does not apply for Delphi, since Delphi uses different ways than C code. You can keep on reading the many pages after this for some overviews of using Win32. The "Windows Styles" info may be helpful for you.

...so many things
If you are new to windows messaging and window creation, when you look at this page, it may seem like there is a lot to know in order to make a program. Much of this will be unclear until you use it in programs and get a feel for what is going on and what you need to do to get the results you want.
So read through chapter and try and get an overview of window creation and some of the aspects and ways Windows uses messages. You may not need to know many of the functional details at this time.

To many developers the Windows API methods and functioning sometimes seem to be illogical, without consistency, or more complex than necessary to do "simple" things. But please keep in mind that the windows API keeps you from having to program in low level, machine level or assembly language, which would be much, much more difficult and time consuming. Also you might complain if the OS did not offer many options for a great variety of methods and display. With options and diversity come added complexity, parameters and functions. So with some practice, and trial and error, you can learn how to deal with the API.

Preparing to create your first Windows API GUI program
Here are the steps for creating a GUI program, note that you will have to read the explanations provided later in this chapter to find out what this does and what are the options for window creation.

To get a Windows GUI program to go you will need to:

  1. Put the Windows and Messages units in your uses clause.
  2. Place a WndMessageProc "Window Proc" function in your Program, with at least these 2 statements:

      if Msg = WM_DESTROY then PostQuitMessage(0);
        Result := DefWindowProc(hWnd,Msg,wParam,lParam);
    

  3. Set your Windows class parameters and register that class, with at least these 4 params set:

     wClass.hInstance     := hInstance;
     wClass.lpfnWndProc   := @MessageProc;
     wClass.hbrBackground := 16;
     wClass.lpszClassName := 'Name';
    

  4. Create your "Main" window using the class that you registered.
  5. Have a "while GetMessage(Msg,0,0,0) do" loop to keep your program running, with at least this statement

      DispatchMessage(Msg);
    

That said, the next code is the minimum needed to get a windows GUI program running:

program Project1;

uses
  Windows, Messages;
  {the Messages unit contains the windows
  Message constants like WM_COMMAND}

{$R *.RES}

var
  wClass: TWndClass;
  Msg: TMsg;

function WindowProc(hWnd,Msg,wParam,lParam:Integer):Integer; stdcall;
begin
 if Msg = WM_DESTROY then PostQuitMessage(0);
 Result := DefWindowProc(hWnd,Msg,wParam,lParam);
end;

begin
 wClass.lpszClassName:= 'CN';
 wClass.lpfnWndProc :=  @WindowProc;
 {CreateWindow( ) will not work without setting the
 2 wClass parameters above}

 wClass.hInstance := hInstance;
 wClass.hbrBackground:= 1;
 {CreateWindow( ) will still create a window without the
 2 wClass parameters above, but they shoud be included}

// wClass.hIcon := LoadIcon(hInstance,'MAINICON');
// wClass.hCursor := LoadCursor(0,IDC_ARROW);

 RegisterClass(wClass);

 CreateWindow(wClass.lpszClassName,'Title Bar',
              WS_OVERLAPPEDWINDOW or WS_VISIBLE,
              10,10,340,220,0,0,hInstance,nil);

 while GetMessage(Msg,0,0,0) do
   DispatchMessage(Msg);

end. //program end

An empty API window

You could use this code and compile this program, but it may not give you any knowledge about what is happening. Try to see why this code will create a main window (Form) that is visible and functional. You will be able to Maximize, Minimize and Resize this Form, and close it with the caption close (X) button. If you are used to Delphi VCL programming, then you may not have a clue about what this is doing.

Notice that there are only 2 variables used here: wClass: TWndClass and Msg: TMsg. The first is for a "Windows Class" and the next is for a "Message Record". So we'll talk about windows Class and windows Messages next.

Now, we move onto a discussion describing the above code...

Next page > What makes an API application: the window Class > 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.