Previous page Select page Next page

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:

  1. We are going to trap a new event (for us, that is) - the WM_SIZE message. This is broadcast by Windows whenever you resize a window.
  2. We create a handler for this event to ask Windows to tell us the new dimensions of the client area.
  3. We then convert these values into characters and make a text message with them.
  4. Lastly, we will ask Windows to use this text message as the caption for the window.

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.

Do you remember in the last chapter where we were talking about the main event loop? Each time an event occurred, it would be discovered inside this loop and a handler called to deal with it, if one existed. Inside that loop, we are aware of which window the event affected, the type of event and a bunch of other pieces of information. Now that we are inside the handler, we can no longer see this data so we cannot really tell which window was affected, for example. That's a real pain, since we want to check the size of the window, so we need to know where Windows has stored it in memory. One of the great features of goAsm is that we can ask it to retrieve the information that was present in the event loop just prior to calling the event handler. We use this by means of the command:

	UseData winMainProcedure

Note that you have to specify the name of the loop procedure - EasyCode creates this for you, as you saw in the last chapter. Our code, therefore, now looks like this:

	OnResize:
		UseData winMainProcedure
	EndU
	; End OnResize

Note that your UseData must be complemented by an orrsponding EndU as the last thing in your handler. It's also bit of a programming convention to indent the code which lies "inside" a label by one tab space but it is not mandatory. In fact, I use only two spaces normally.

Previous page Select page Next page