win32gui magic

I've been using the amazing win32gui by John Torjo.

I was confused by the magic way he registers his event handlers, so I wrote to him, and he responded. He said he should add his response to his FAQ, but until then, here's the question and the answer:


I love your library. There is one thing I don't quite understand. How do you create the correspondence between events and event handlers? I see that event handlers tell the library what event they have handled once they have done so, but I don't know how the library knows which ones to call in advance - or does it call all of them every time?

Thanks in advance,

Stephen Clarke-Willson

Dear Stephen,

I guess I should include this question in the FAQ ;) It's been asked by most dilligent readers ;)

I will avoid a detailed explanation (it would be pretty large), but in short is this:

When you say something like:

handle_event on_size() {
return event_ex ().HANDLED_BY(&me::on_size);

This invisibily registers your on_size() function for the wm::size event. It does so (the event_ex class, that is), by having a static variable, which makes sure it's initialized. Thus, before main(), the on_size function is registered to handle the wm::size event.

That's it!


Ah, nothing like code that doesn't execute in any obvious order.