Dal's Programming Course - Lesson 05

Processing Mouse Input

As you know by now, your Windows program is basically a message processor. Windows sends messages to your main loop (in MainApp.cpp), and these messages are then distributed to other parts of your program. Your job, as a Windows programmer, is to process the messages you are interested it.

There are hundreds and hundreds of message types. You will never be interested in processing most of them. Instead, your program just needs to filter out the ones you are interested in, and return the rest of the messages back to windows. To see how this is done in your program, take a look at your mcBasicWindow::WindowProc() function; notice the DefWindows() call a the end of the function. That is where all the unprocessed messages are being returned to windows for processing.

In this lesson, you will "override" the base class WindowProc() with a new WindowProc in mcTopLevelWindow. Then if the new WindowProc doesn't want to process the message, it hands it down to the old WindowProc() in mcBasicWindow.

Materials for this Lesson: Download.
Homework

Item

Time

Description

1 10 Mins Begin by coping and pasting the WindowProc() from mcBasicWindow into mcTopLevelWindow, and delete all the guts. You will have to re-declare WindowProc() to be a virtual function in mcBasicWindow. You will also have to declare WindowProc() in mcTopLevelWindow(). Rewrite the version in Top level window so that it passes all messages to the version in mcBasicWindow. Hint: use "::" to specify which WindowProc() is to be called from your new WindowProc().
2 10 Mins Rebuild and run your program with a breakpoint in your overridden WindowProc(). Verify that all messages going to your window are first passing through your new WindowProc(), before they are handed off to lower level code.
3 10 Min Finally, read about the window message "WM_LBUTTONDOWN". In your mcTopLevelWindow::WindowProc() you want to process this message. When you receive this message, build a text string of the mouse position, and call SetText() with this string. Compile and build your program.
4 5 Min Test your program. What happens at start up? What happens when you click in your window? Make sure you understand exactly how everything works up to this point, and that you can explain all the runtime behavior that you observe. If your program writes the mouse position in the window -- congratulations!