How Windows Works - Win95/98/NT only |
Before coding for Windows, it helps to get an understanding of how Windows works. That's why I have put this document together. After reading this you should have at least a basic understanding of the structure of a Windows program.
Differences between DOS and Windows
A program written in DOS has a very straightforward procedural layout. Generally, you enter your main() function, call some initialisation routines, then you would usually enter a main loop which would deal with user input, processing of that input, and then exit the loop when the user indicates that they wish to. Then, you would do your shutdown routines, after which you would be returned to the operating system.
This linear, procedural approach is simple and straightforward. It is easily to read, and easy to code, and also, very easy to design and document.
Windows, however, doesn't work in this manner. Although you can create windows applications that follow this basic procedural scheme, if you wish to create decent windows applications, you need to learn how the Windows Messaging Scheme works.
The Windows Messaging Scheme
Windows takes a different approach to DOS in terms of program structure. The main trick behind windows is that it is a message-based-system. As discussed earlier, DOS does its initialisation, main loop, then shutsdown. WIndows, however, does its initialisations, then, the program sits there and waits to be told what to do next.
These instructions that the program is waiting for are the messages we have been referring to. These messages provide the program with all its instructions, telling it when to look for a keypress, a mouse click, a window resizing etc. These messages are sent to your program via the operating system.
It is the responsibility of your program to deal with these messages as necessary. If your program does not deal with these messages, your program will not interact with the user, if it even runs at all.
For example, you start your windows program and enter your mainline. You do whatever intialisation routines are required. Then you create your window, with a frame, a title bar and a client area (the large open area underneath the title bar). If you were to ignore all the messages passed to the program, you would never see this window. So, to remedy this, we must "capture" the necessary message, which in this case would be the WM_PAINT message. This message tells your program that it is required to draw/redraw whatever is supposed to appear in the client are of your window. So we draw what we need to. Now we can see the window we created. Now we need to allow the user to quit. To do this we look for the WM_DESTROY message. This indicates that the window in your program has been destroyed. Once we capture this message, we tell our windows program to perform the necessary shutdown routines, after which our program will end.
You must create code for each message that you wish to process. This does not mean you must deal with every message that the operating system sends, but that you only need to deal with those that are relevant to your program. All other messages will simply be ignored.
A Word Of Caution
As mentioned before, the WM_PAINT message indicates a need to redraw the body of the window. However, while the processing of this message is being performed, no other messages are being sent to the application. Therefore, an endless loop in this situation will effectively "hang" your program. This is because the window is not receiving any new messages, because it still is not finished with the last one. So take care when developing code to handle this message; it will save you a lot of trouble in the long run.
Threading, Multithreading, and Processes
COMING SOON