CWL

Basics: Developing Your First Windows Program (with Lazarus)

 

For this guide, I wanted to show you the steps involved in creating your first Windows program. For many, learning to create an application seems out of reach. When I’m trying to understand something, it can be good just to start and explore. This guide will show you the basics of building a Windows program; from downloading the free development tools to coding a basic application, to testing it. There are many tools, languages, and environments from which to develop, but if you just wanted to get started with something simple – this is a good place to start.

1. Download and Install Lazarus

For this column, I’m going to show you the free Object Pascal integrated development environment (IDE) called Lazarus. To start, go to the Lazarus homepage and choose downloads. Pick the version right for your environment. It will probably be the “Windows (32 and 64 Bits) Direct download”. Once that’s downloaded, run it and install the program on your computer [1].

2. Run Lazarus

When running Lazarus for the first time, you’ll be asked to configure the IDE. You can choose the defaults and click “Start IDE“. Then, the full IDE will load and you’ll see the following:

Lazarus FirstRun

While Lazarus is very economical, don’t be fooled by this bare looking screen. There are a bunch of elements already laid out here for you, including a new project started by default. For a more advanced article, I’ll walk you through more of these options. But for now, let’s get to making our first Windows program.

3. Place a Button on Your Form

Since a new project with a form has already been created, simply click on the button object (looks like a button with OK written on it) on the “Standard” tab and then immediately after, click somewhere on form1.

You’ll then see Button1 on Form1. Now you’ve added a button to your new application!

4. Add Code to the Button

Great! Now we want to tell the button to do something. We’re just going to tell the button to make a popup message for us. To start, double-click on Button1. You’ll be taken directly to the “Source Editor” and directly to the place that takes action on that button click:

This is where the magic happens. Just below “begin“, I want you to type (or paste) the following code:

ShowMessage('Hello World!');

Be sure the above code is pasted exactly. The (‘  ‘) is required, as well as the final colon. This is the Pascal programming language.

5. Save the Project

The next step is to save your new project. First, go to the Lazarus menu under File -> Save and give your project a name. For my example, we’ll just keep “Project1“. All of that can be changed later. Next, save your first unit in the same place. I’ll use the name “Unit1“. When you compile your program, that’s where the executable will be.

6. Compile the Project

You’re almost there! Go back to the Lazarus menu under Run -> Compile and click on that. You’ll see a number of messages in the status window (including “linking..”), and then the final message: “Compile Project, Target: project1.exe: Success”.

7. Run your new program!

Now, look for your project file (I called it project1). If you chose all the defaults, it will probably be located in your “My Documents” folder with a blue paw print icon. If there are two of them, look for the one that is of type “Application”. When you find it, double-click on the file. You’ll see the Button1 you placed on the form. Go ahead, click on the button and see what happens when you click it:

That’s it! That Project1.exe file can be taken to any Windows XP (or newer) computer and run without anything else installed to help it. It’s a self-contained Windows program. Naturally, this example isn’t doing anything useful [2], but the possibilities are only limited by your imagination. I’ve written a number of applications with the Lazarus IDE, and it’s quite versatile.

If you’re looking for a more advanced idea of what’s possible, you can find a bunch of sample projects that can be compiled right away (assuming installation point of c:\lazarus):

c:\lazarus\examples\

Go ahead, mess around. Change things. Re-Compile. Get the hang of customizing the look and feel of your program. Learn the hotkeys (CTRL+F9, anyone). The added benefit to Lazarus is that many Delphi projects can also be imported and compiled. This also includes the vast number of code examples and snippets you might find online. Write a Windows program and share it with us.


1. You can safely choose all the defaults here
2. And, you can modify the properties of any of the application elements. You don’t want anything to be called “button1”.
Exit mobile version