omatai
omatai

Reputation: 3728

Handling multiple WM_USER messages (or achieving the same result)

I have a WidgetController class that controls multiple widgets (motors as it turns out, but it could be practically anything). It has a companion UI class WidgetControllerUI to do UI things representing the multiple widgets.

I would like to send messages such as WM_USER + 1000 + N from the WidgetController instance to the WidgetControllerUI instance to indicate that something has occurred with widget N. That part is easy:

// Somewhere inside WidgetController:
pWidgetUI->PostMessage( WM_USER + 1000 + N );

But how can I direct all these (contiguous) messages into a single message handler in my WidgetControllerUI class? On the surface, ON_COMMAND_RANGE, ON_UPDATE_COMMAND_UI_RANGE and ON_CONTROL_RANGE do not appear suited. Are they? Is there a more elegant mechanism, perhaps not using Windows messages at all?

Upvotes: 1

Views: 491

Answers (1)

Martin James
Martin James

Reputation: 24857

Yes, as @Ross suggested, you can pass you 'N' into PostMessage() as one of the [lParam, wParam] arguments. They are pointer-size on both 32 and 64 bit OS.

Note that your shenanigans with N are often unnecessary. With dynamically-allocated objects, (like GUI elements usually are since they must outlive the function that created them), it's common to pass in the object instance pointer as one of the parameters. The other can be used, (eg. as as an enum), to command the message-handler function, (eg with a switch/case), to perform some specific action with the instance passed.

You will need a bit of casting either way, but I've never had any problem with such mechanisms.

The only possible issue is that the instance may not exist by the time the message is received, eg. because the user has deleted it by 'normal' human interaction with the GUI. How likely this is, and how you might guard against it, is app-specific.

Upvotes: 1

Related Questions