I use C++ Builder and I pass messages to window which are handled in WndProc to update user interface. Something like:
struct TWmGuiUpdatedStruct
{
int id1;
int id2;
};
TWmGuiUpdatedStruct gui { 1, 2 };
TMessage msg;
msg.Message = WM_MY_GUI_UPDATED;
msg.WParam = WM_MY_GUI_UPDATED_MESSAGE_LIST;
msg.LParam = reinterpret_cast<NativeInt>(&guiparam);
// send to all forms in a loop
for (int i = 0; i < Screen->FormCount; i++) Screen->Forms[i]->Perform(msg.Message, msg.WParam, msg.LParam);
// in WndProc
if (fMessage.Msg == WM_MY_GUI_UPDATED && msg.WParam == WM_MY_GUI_UPDATED_MESSAGE_LIST)
{
TWmGuiUpdatedStruct* gui = reinterpret_cast<TWmGuiUpdatedStruct*>(fMessage.LParam);
// use gui->id1 and guid->id2 here...
fMessage.Result = 0;
return;
}
TForm::WndProc(fMessage);
I find it over-complicated to initialize all in so many lines.
I need something simpler, like:
TMessage msg { WM_MY_GUI_UPDATED, {WM_MY_GUI_UPDATED_MESSAGE_LIST, reinterpret_cast<NativeInt>(&guiparam), 0} };
But it doesn't compile - it wants System::Word instead.
Is there a simpler way to initialize this to pass such GUI update messages?
If you look at the declaration of
TMessage, it is astructcontaining aunionholding 2structs.Per cppreference, Struct and union initialization explains that when dealing with a "nested initialization", you can only initialize the first member of a
union, but you are trying to initialize the second member.Since C++Builder does not support designated initializers at this time, you will have to do something like this instead:
With that said, I would suggest simply getting rid of the
TMessagealtogether, you don't actually need it:Otherwise, if you must use
TMessage, then I would suggest creating a wrapper function to create a newTMessage, eg:And then call a control's
WindowProc()instead ofPerform(), eg: