|

Win32/ FPC - Chapter 5 - by Delax
Windows is event driven. We had that line before, but I will cover it again. Windows is sending messages to every running application to inform them what's happening. "A key is pressed", "The screen saver is starting", ... I said that your application needs to process these messages somehow. Now we get there.
The event handler of your application is written by the programmer (most likely you ;), but don't worry! You won't have to handle every single messageo your application receives. Your Gfx Demo for example is not interested in incoming mails, is it? In real life you only need to look after a couple of messages and ignore the rest.
Let's look back at your Window Class. You defined some kind of event handler back there, right? With WindowClass.lpfnWndProc := WndProc(@WindowProc); you actually defined a pointer to the event handling procedure we want to write now. Your window will use the given WinProc as
the default event handler.
Windows does not send any messages directly to your program by the way. It is storing the messages in some kind of buffer and they stay there
until they get processed.
To be crystal clear on this issue: Windows encounters an event. Windows sends message. Message is put in buffer. Your application looks in buffer. If the message is "interesting", process it. If not, let Windows deal with it.
Let's look at a simple WinProc.
function WindowProc(
HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam )
The usual suspects:
hwnd: This is the window handle. Normaly, it's only interesting if we open more than one window with one Window Class. hwnd then looks which window sent the message.
msg: This is the orginal message. Well...at least an ID of a message ;)
Wparam/ Lparam: Message parameters that are given by the system.
LResult Callback is important. Don't forget it. Anyway, the whole thing is going like this: We get the message, look if the msg ID is interesting and if that we process it further. Now for a small selection of IDs.
WM_ACTIVATE - The window is activated.
WM_CLOSE - The window is closed.
WM_CREATE - The window is created.
WM_DESTROY - The windows is destroyed.
WM_MOUSEMOVE - The mouse is moving.
Of course these are only the tip of the iceberg. Take a look at the SDK Reference to get them all. Remember - there is a message for every possible event that can happen in a Windows system. And then some.
To make things as easy as possible we are only interested in only one message: WM_DESTROY. This message is sent if the user has closed our window. Remember: if the user closes the window our program is NOT terminated automatically! We have to catch the mesasage and terminate our program ourself! Here is an example WinProc:
function WindowProc(Window: HWnd; AMessage, WParam,
LParam: Longint): Longint; stdcall; export;
begin
WindowProc := 0;
case AMessage of
wm_Destroy:
begin
PostQuitMessage(0);
Exit;
end;
end;
WindowProc := DefWindowProc(Window, AMessage, WParam, LParam);
end;
In the "WinHello.pp", the WinProc is looking a bit different. Here we need to draw the "Hello World" in our Window, but we don't need that for now ;)
And that's it? Hmm, yes this is a simple WinProc. But did you notice that we are not getting our messages from anywhere? We have to do that in the WinMain. The WinMain is our main loop. Remember our first program? It consisted only of a main loop.
while GetMessage(@AMessage, 0, 0, 0) do begin
TranslateMessage(AMessage);
DispatchMessage(AMessage);
end;
So what are we doing there? GetMessage gets the message from the buffer and stores it in AMessage. It will then be translated and processed. Looks simple, eh?
And that's it. Finally! We have all the basics to start hacking a little application together. Relax, read the chapters again and then on with the show.
Delax/ Sundancer Inc.
[delax@sundancerinc.de]
Back to previous page
|