Page 1 of 1

Menu events between threads

Posted: Sun Aug 07, 2011 2:13 am
by Olby
Hi, I'm having a hard time figuring out how to send menu events from auxiliary to the main thread for processing.

I use PostMessage_ at the moment:

Code: Select all

PostMessage_(WindowID(#Main),#PB_Event_Menu,-1,GetMenuItemID_(MenuID(#Menu_Main_Dummy),#Menu_Jobs_Dummy))
but the problem is that GetMenuItemID_ doesn't return proper item id and triggers wrong menu item - I guess due to the fact that target item was created in the main thread (and not auxiliary one).

Any ideas on how to get the right item id's? Thanks.

Re: Menu events between threads

Posted: Sun Aug 07, 2011 11:02 am
by c4s
I'm not sure if that is what you need, however I'm using the following code to simulate menu events (on Windows):

Code: Select all

Macro WindowEventMenuSimulate(WindowNr, MenuItemID)
	PostMessage_(WindowID(WindowNr), #WM_COMMAND, MenuItemID | (1 << 16), 0)
EndMacro


; For the sake of completeness, simulating Gadget events:
Macro WindowEventGadgetSimulate(WindowNr, GadgetNr, EventType)
	PostMessage_(WindowID(WindowNr), #WM_COMMAND, GadgetNr | (EventType << 16), GadgetID(GadgetNr))
EndMacro

; There is also an internal PB function for this, but you never know if it will change so I'm always using the previous one:
Import ""
	PB_Gadget_SendGadgetCommand(hWnd, EventType)
EndImport
PB_Gadget_SendGadgetCommand(GadgetID(GadgetNr), EventType)

Re: Menu events between threads

Posted: Sun Aug 07, 2011 2:36 pm
by tinman
c4s wrote:

Code: Select all

; There is also an internal PB function for this, but you never know if it will change so I'm always using the previous one:
Import ""
	PB_Gadget_SendGadgetCommand(hWnd, EventType)
EndImport
djes reported here http://www.purebasic.fr/english/viewtop ... 78#p357378 that PB_Gadget_SendGadgetCommand does not work when using the threadsafe option, so as you say safer to stick with PostMessage_().

Re: Menu events between threads

Posted: Sun Aug 07, 2011 4:14 pm
by c4s
@tinman
Thanks for the information! I just knew it. :wink:

Re: Menu events between threads

Posted: Sun Aug 07, 2011 10:53 pm
by Olby
@c4s
Yeah it did the trick. Thank you guys!