Introduction to Windows Programming

Starting programming Windows applications in native C/C++ can be a bit daunting. There are lots of things to learn and very few resources available on how to do it. There are frameworks available to do it, like MFC or QT, but it is possible that you can’t use them for some reason. It is also possible you just would like a deeper understanding of how Windows programming works internally.
For the initial part of this series, I am going to focus on dialog boxes. The reason is simple, while it leaves certain things out, it allows me to focus on key parts of the whole without having to explain too much at a first. Also, the basic layout of these programs are almost identical to the other window type, so it is possible to transfer this information.
Basic Dialog Components
So what is needed in a program which creates a dialog box. First is the dialog box itself. This is usually a resource in your application which you create by using a WYSIWYG resource editor. Then there are two major programming constructs, the message loop (or message pump) and the window procedure (also known as the dialog procedure in this case).
So what is the message loop? Well, in order to understand this you have to know about messages and message queues. I suppose the message queue can be easily explained as a queue of messages. Since this is a queue you would expect new messages to get added to the end of the queue and then the messages get processed in the order that they are received. This is mostly true in a message queue, but there is two exceptions. First, if the message is important it can be sent to the front of the queue. Secondly, there are some very low priority messages which don’t get processed until the queue is empty of all other messages.
The important things to remember about a message queue is that there can be at most one per thread, and it isn’t created automatically, even in a Windows application. Strangely enough a message queue isn’t even tied to a Window, you can actually have a message queue without having a Window. So when is the message queue created? The answer is simply the first time the thread accesses it. For most applications this will be the first call to CreateWindow/CreateWindowEx, but you can also have a message queue just by calling GetMessage or PeekMessage.
If you are someone who has tried to learn before then you may have noticed something strange. That is I mentioned CreateWindow, not the functions which you use to create a dialog box, CreateDialog or DialogBox. Does this mean that you have to do something else to get the message queue to be created? No, the dialog box creation functions call CreateWindow internally.
Next, what are these messages. Well, you can think of them as notes (or maybe if you are more up to date with technology, an SMS message) telling you what is going on in the system. So if you click your mouse then a message will be put in the message queue saying that this has occured. This allows you to keep track of things going on. These messages then give you a chance to do something in response to these things going on. Oh, and if you have used Windows Forms or something like that and think there is a similarity between these messages and events, then that isn’t entirely wrong. Windows Forms are based on standard Windows programming, and deep down it relies on messages too. Events are raised due to these messages in the message queue.
So now you hopefully have at least an idea of what messages and message queues are, now to answer what is the message loop. It is basically a loop inside your program which continually gets the next message to be processed from the message queue and then sends it to be processed. Unless you have something extra that you need to do in your program, you will find that it is only a couple of lines of code too. But wait, I said the message loop sends the message to be processed, but where does it send it to? This is where the last part of our program comes in, the windows procedure.
This is a function which you write for your application, and it allows you to customise what your application does on each message. So this is where you tell your application what to do in response to things which goes on.
Next post will give a sample application with full code walk through, explaining exactly what is going on.
Tags: 

延伸阅读

最新评论

发表评论