An Introduction to Windows programming

Extend your programming skills and get ready for up to date graphical user interface (GUI) programming

created by:

Rainer Döbele

for Bristol University, February 1996

Table of contents

  • Background
  • Introduction
  • How to start
  • Paint Counter: A windows program outline
  • Sort it out: A dialogue based Windows program
  • How do I do...

  • 1. About this tutorial

    In the last ten years the way we use computers has changed considerably. The old text terminals have disappeared and replaced by PCs or Workstations running graphical user interfaces (GUIs) like Microsoft Windows or X-Windows. And although most people would not use anything beyond an application written for these environments for everyday work like word processing, when it comes to programming many people still stick to conventional text-based DOS or UNIX programming.

    This tutorial is designed to help those who have already some experience in PASCAL, C or C++ programming under DOS or UNIX to experience the fancy world of GUI programming. If you are to write programs not just for yourself but for other people you won't get around providing a nice and easy to use graphical user interface for your applications anyway, sooner or later. But even if you do not intend to write programs for a living this tutorial may give you a sound understanding of how modern operating systems and applications work.

    Although many of the principles introduced here apply to all GUIs this tutorial will focus on Microsoft Windows since it is the most commonly used GUI. Windows also offers the most sophisticated development tools and there are plenty of books and other sources of information available. I will try not to use too many development terms here straight away but in order to understand what I am talking about you should have worked with Microsoft Windows and be familiar with common Windows terms like icons, scrollbars, dialogue boxes etc.


    1.1 How difficult is it to write a Windows program?

    Let's start with the good news: if you have already programmed in C or C++ before you don't need to learn a new programming language for programming Windows. But as you will see, programming Windows is somewhat different from what is called ANSI-C programming under DOS or UNIX. However different does not mean more difficult, although there are some hurdles to overcome at the beginning. But once you've understood the basics and written your first program many things get easier with Windows and you get opportunities you have never had before.

    Many people think that its easier to do non-Windows programming first and do the "difficult" windows stuff later. This is probably one reason why windows programming is still paid too little attention in education where they "want to teach you the basics not the details". But programming Windows is more than just using a few more functions, its a different philosophy, its event driven rather than sequential and its not just coding but also designing and creating dialogue boxes and other program resources.

    In the same way many also believe that you should understand C first before starting with object orientated programming in C++ say. I don't know where these myths come from and I can't even prove that they are just myths but what would you say if an alien asked you whether its easier to understand capitalism by learning about communism first. I would say no and I also think its pointless to start with "ordinary" programming when you really want to do Windows and its not necessary either to become a master in C first when you are aiming at C++ in the long run.

    However the combination of Windows and C++ bears some difficulties for beginners. Using so called "class libraries" makes understanding of how Windows works more difficult as these class libraries hide a lot of functionality away from you. Defining your own classes is a bit tricky with Windows especially at the beginning due to a very lose link between Windows' objects and your own program objects.

    This and the fact, that people who have experience in C cannot read C++ program whereas vice versa it is generally not a problem is why I will give you all the code samples in C. This tutorial won't make you a master Windows programmer anyway and you are welcome once you've understood the philosophy of windows programming to go the much more promising way of C++ using either Microsoft's Foundation Classes (MFC) or Borland's Object Windows Library (OWL).


    1.2 Software Development in the 90s

    With the improvements in the way we use software, the way we develop it has also experienced considerable changes. Where a few years ago it was a challenge for a beginner to even type in a piece of source code from a book and compile it successfully we now get sophisticated so called Integrated Development Environments (IDE) that make things a lot easier for both the beginner and the expert.

    Check you current way of programming. If you use an all purpose text editor to write your code and then call a compiler from a command line with something like comp -o myprog.c your not quite up to date. Even if you just want to taste what programming is like and especially if you are a beginner you might find features like source code highlighting, integrated source code debugging and full context sensitive help to name a few not just useful but crucial to get where you want to get. Writing a program is challenging enough and there's no need to fight your way though environment variables and makefiles.

    In order to use this tutorial and to do windows programming all you need is a PC or at least access to a PC with Microsoft Windows and a Windows development package installed. The two most popular packages for developing Windows applications are Microsoft's Visual C++ and Borland's C++. Although both packages feature C++ compilers this does not mean you cannot write C programs with them. They both detect by default whether a source code file is a C or C++ file from the file extension which is .C for C and .CPP for C++ and compile them accordingly.

    Which one of these packages you want to use is a matter of personal preference. I prefer Borland C++ because I find it more user friendly but sometimes some of Microsoft's latest development kits won't compile with Borland and I have to use Visual C++. With Borland you can also develop both 16-Bit Windows and 32-Bit Windows applications, whereas with Microsoft's Visual C++ you need Version 1.x (currently 1.52) to develop 16-Bit Apps and Version 2.x (currently 2.1) to create 32-Bit Apps. On the other hand Microsoft seems to now successfully establish its Foundation Classes (MFC) for C++ which make development especially of more advanced features of Windows such as OLE considerably easier.

    Both packages include the following:


    1.3 How to use this tutorial

    In this tutorial I will first give you some background knowledge about windows and then introduce you to some important terms and conventions. Then in section 4 we start with a first simple windows program which you can download to your machine. The files are compressed and put together into a single .ZIP file. To be able to use the files you have to extract them first. For every ZIP file you should create a new directory.

    In order to load and compile the sample programs you need to open the project files from the IDE. For that you will find an open command in the project menu in both Microsoft Visual C++ 1.x and Borland C++. With later versions of Microsoft Visual C++ you must use the open command in the file menu. A project file keeps track of all the files belonging to a project as well as compiler and linker options necessary to build the executable and is basically a modern version of a makefile (don't worry if you do not know what that is). In Borland (since version 4) a project file has the extension .IDE and it is a binary file which cannot be edited with a text editor. Microsoft's project files have the extension .MAK (for makefile) and they are still plain ASC-II text files. However they should not be changed with a text editor (they've got a line at the beginning saying "DO NOT EDIT").

    This is an example of how a project file is displayed in Borland C++. By double clicking on any of the source files it will be opened with the standard editor, which is a built in text editor for .C, .H and .DEF files and the Resource Workshop for .RC files. You can also right-click an item for additional options.

    The tutorial is designed that you don't need any additional books however it is essential that as we go though the code you have the project loaded in your IDE and that you are able to access the online help of the Windows SDK. This is because it is neither possible nor sensible to explain all the functions we are using in detail with all their parameters and in the online help you will find all the information about a particular function or message you need.

    Just in case you don't know: there are two ways accessing the online help from the IDE. First you will find a command called Windows-API in the help menu which takes you to the index page from where you can use the hyperlinks or the search command to find what you are looking for or just to get an overview of what is available. The other possibility is to position the cursor on a particular function name or data structure and press F1.


    Goto next chapter