Showing posts with label gui. Show all posts
Showing posts with label gui. Show all posts

endless WM_COMMAND/BN_CLICKED

When the user selects a radio button, the button sends a WM_COMMAND/BN_CLICKED notification message to the button's parent. This lets the program which owns the parent take action based on the new selection.

This works fine as long as nothing could possibly go wrong during said action, but of course that's almost never the case. Suppose the action involves allocating a lot of memory and the allocation fails; what then? A good program will know which radio button was set before the user clicked and restore that button (before or after informing the user of the failure).

Now the fun begins.

If the program restores the old radio button while processing the message, Windows* decides to send an identical message for that very same button as if the button had been clicked again. This doesn't happen right away; it's deferred in some tricky way, and believe me it's not worth trying to second-guess the deferral. A straightforward program is likely to find itself in an endless loop not of its own making. The user clicks the radio button, the program tries to allocate a bunch of memory, fails, restores the old radio button, Windows sends another message as if the user clicked the radio button again, the program tries to allocate a bunch of memory... and hilarity ensues.

The way out of this madness is to observe that the radio button is not set the second time the system informs the program that it was clicked. When the radio button is set, act; when it's not, don't. It's not intuitive that one must make this test, but one must.

* My testing was on Vista only.

mouse "moves" when it doesn't move

MSDN claims you'll receive WM_MOUSEMOVE when the mouse moves, and that's true as far as it goes. However, you'll also receive WM_MOUSEMOVE at a number of other times, notably when your window activates or another window moving reveals part of yours. So the real meaning of WM_MOUSEMOVE is "please act as if the mouse moved." That's fine most of the time, even though it means you might do some redundant work. But if you really want to know whether the mouse moved, you need to cache the last known mouse location, call GetMessagePos, and compare the result to your cached value. If the mouse really did move, then update your cache in addition to the other work you do.

Update 7 Dec 2006: St. Chen has already covered this topic. Big surprise.

tooltip message handling nightmare abated

I recently spent a boggling amount of time — a couple of weeks, on and off — second-guessing Microsoft's tooltips message handling code. Not only does it have at least one nasty bug, but it seems designed to make easy cases easy, which is a diplomatic way of saying it tends to make anything beyond that very difficult. Do try it, and see if it works for you, but if you spend more than a day scratching your head and trying to overcome limitations, my recommendation is to skip TTM_RELAYEVENT, turn off all tool flags other than TTF_TRACK and TTF_ABSOLUTE, and get real cozy with TTM_TRACKACTIVATE and TTM_TRACKPOSITION. Oh, and write your own equivalent of TTF_SUBCLASS. Once I realized I ought to give this approach a try, I was out of the woods in just a couple days, and I've written some C++ classes which will relieve me from ever having to deal with this madness again.