Creating our own event handler
Our first program was a rather sterile affair. Admit it. We didn't really write any of the code and it didn't really do anything apart from display a window and smugly sit there until you dismissed it. Our next program is going to be a good deal more powerful. Here's what we are going to do:
All of this means that each time the window is re-sized, the caption will change to tell us the new dimensions as you can see in the animation here.
OK, let's take step 1 in the list above and do it. Start a new project in EasyCode. Call the project "resizeHandler" and the main window "winMain". If you forget how to change these things, click here. We know how to declare a message in assembler. We saw it in the last chapter. What we need to do, then is to put in our WM_SIZE message, together with the name we are going to give our event hander - "OnResize". Press the "View code" button to see ther assembler and add the message and its handler. Your code should look like this:
MESSAGES DD WM_CREATE, OnCreate
DD WM_CLOSE, OnClose
DD WM_SIZE, OnResize
The first two messages were made for you by EasyCode, as you know, and you have now added one of your own. Let's design the handler. Scroll down to the bottom of the code window and add the name of your handler:
OnResize:
; End OnResize
Two things to note here. Firstly, notice the colon at the end of "OnResize"? This is a label and we've been using them already - "MESSAGES" is a label. But you didn't use a colon there, did you? That's because labels really ought to have colons but they are only demanded when the label is in the code area. That is to help the assembler tell the difference from a label and just plain old typing errors. So don't forget the colon in labels within the code area. The second thing is the gray text. Do you see that it is started by the semi-colon? Any text started in this way is regarded as a comment. You can have as many of these in your program and they will not affect the size of the compiled code. Use them! They will make your code so much more readble when you come back to it weeks later. When EasyCode sees a comment, it colours it gray, as you can see. Since this line is a comment, it is not really needed but I put a line like this into my program to show where the code for this handler ends. You don't need to do this if you don't want to but I do recommend it.