Start small and work up

Posted September 28th @ 12:20 am by aaron

I re-learned an important lesson tonight: start small and work up from there.

I was working on creating a Form that intercepts the WM_MOUSEACTIVATE message to prevent a click from stealing focus from another form (i.e. a menu, or a drop down list). In my experimentation I had a “grand” idea to create a class that implements IMessageFilter which would keep a map of the Handle of a form and all its controls (and their controls, etc.) and intercept all WM_MOUSEACTIVATE messages for the “registered” controls. (Keep in mind I was experimenting, and the intended use of this is a small form that doesn’t have a huge control tree to track.)

I wrote probably about 100 lines of code (fortunately my lesson was learned on a small scale first!) to handle the registering of controls, the watching of controls added to Controls collections, the cleaning up on disposal, and so on. Finally my masterpiece was complete, I ran it, and … nothing happened. Huh? Debugging showed that the message filter was active and kicking, my controls were registered, but I was never receiving the WM_MOUSEACTIVATE message (as I did when I overrode WndProc). A bit of Google searching turned up the answer (below).

So I guess I learned two lessons:

  1. Start small and work up from there, and
  2. An IMessageFilter only processes windows messages that are posted (PostMessage) and not sent (SendMessage)—and WM_MOUSEACTIVATE is sent. Had I tried to intercept this message via the filter before I created all the supporting code, I could’ve saved myself some time.

Live and learn! (And pass it on!)

No Comments Yet

You can be the first to comment!

Leave a comment

OpenID Login

Standard Login

Options:

Size

Colors