Welcome to the new Friends-of-FPC!

Here you can find all kinds of information about the FreePascal Compiler. We have many tutorials and howtos as well as a selection of tools to help you with your programming. We also have some example codes for you. And if you want to contribute some information/ sources/ tools yourself you can do so.
Also we have finally relaunched the FoFPC forum. It's your chance for some Q&A about everything FreePascal.

Friends-of-FPC

Tutorials: Learn how to code with FreePascal.

Source Codes: A collection of examples, miscellaneous source codes and open source stuff.

Tools and Help Files: Intro- duction of some tools that might help you with FPC.

Community

Forum: Ask or answer questions about the FreePascal Compiler, programming or just babble about coding.

Contribute! Contribute your own Tutorial, Source Codes or Tools and send them to us!

Website

About: Information about Friends-of-FPC.org.

Win32/ FPC - Chapter 2 - by Delax

Andre LaMothe once said: "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it." I avoided Windows programming for a long time but as the time came, I spend 3 weeks in hell and now I'm actually liking it. And let me tell you this right from the start: it is different than I thought ;)

If we say "Windows", we mean some kind of Win32 system: Windows 95, 98, ME, NT4, 2000, .. . These are Operating Systems, able to do multitasking and multithreading. Multitasking means that the OS can execute multiple applications at the same time. Oh, and while we are at it, all Windows programs are applications ;) Multithreading means that every application can be composed of several smaller threads, that will also be executed at the same time. Right. Let's get real now..

The core of a Windows system is the so called "Scheduler". The Scheduler just waits until a new application starts. This new application gets a number and is added to the "Process List". The Scheduler is then going through the Process List and gives every application a small time slice to run. This is happening so fast that everything seems to run at the same time. If you are curious: you can take a look at the process list in an NT-based Windows by pressing CTRL, ALT, DEL and choosing "Task Manager" and "Process List".
To be honest: in reality it is a bit more complicated (as we will see later in this tutorial), but for now it is everything you need to know.

Windows is also "event driven". It means that, for Windows, everything that happens is an event which is processed as a message. Let's try it with an example: A user presses a key on the keyboard. Windows is aware that an event (key pressed) is happening and sends a mesage to your application "Hey, a key is pressed". Your application has to deal with this information and you have to make the choice which messages will be processed and which are ignored.

If Windows sends messages you will need some part of your application to handle these events. But more on this subject later on. For now let's take a look at a simple Windows program.

{$APPTYPE GUI}
{$MODE DELPHI}
program WinHello;

uses
  Windows;

begin
    MessageBox(0, 'Hello World', 'Hi there!', mb_Ok);
end.

Download the Source Code here.

This application creates a small Message Box with the message "Hello World" (as usual). Compile it and take a look. Oh, and another thing. To compile it properly you have to tell the compiler that you want to build a Windows program. To do so, just add the parameter "-TWin32" to your call. Your command line should now look something like this: "ppc386.exe source01.pp -TWin32". If you use EditPlus or some other Editor, you only need the "-TWin32" in the argument line. And now: let's take a closer look!

{$APPTYPE GUI} - APPTYPE shows what kind of application we want to create. (GUI shows we want an application on the Windows GUI, not a concole application).

{$MODE DELPHI} - We then change into Delphi Mode.

MessageBox(0, 'Hello World', 'Hi there!', mb_Ok);

For this line we need a little more explaination. MessageBox is a call for a Windows Message Box, followed by the parameters. These parameters define the look and behavior of the box. The Win32 SDK Reference has the following to say about a standard Windows Message Box:

MessageBox(hwnd, lptext, lpcaption, utype);

Let's go through the parameters.

hwnd - In our case set to 0. It's the handle of the Box and defines which application the owner of the box is. By setting it to 0 we choose the top window: the Windows Desktop.

lptext - This is the text to put out in the Message Box.

lpcaption - This is the title of the box. It appears in the title bar.

utype - This is the parameter to have fun with. Here you can define what kind of buttons your Message Box has (OK, Cancel, Retry, ...), what kind of icon is shown (question marks, ...) and many more things to try out. I will not show them here as there are too many. For a complete list, take a look at the Win32 SDK Reference Help (see chapter 1).

Well, that was a simple Windows application. Take a break, compile it, read the SDK Reference and fool around with the parameters.

Delax/ Sundancer Inc.
[delax@sundancerinc.de]

Back to previous page

Useful Links









Link to us