![]() |
Dynamic Menu Shared Handler? |
Post Reply ![]() |
Author | |
robosport ![]() Groupie ![]() Joined: 20 June 2005 Location: United States Status: Offline Points: 62 |
![]() ![]() ![]() ![]() ![]() Posted: 27 April 2007 at 4:02am |
Sorry for the newb-like question but I've never needed dynamically generated menus in an MFC/XTP app before.
When adding each new button to a popup menu I am giving each button a unique ID at run time. There are literally thousands of them in a relatively simply and organized menu hierarchy.
The question is: Do I need to enter a member function/procedure for each unique ID's event handler or is there a way to have a single shared handler for these dynamically generated buttons, that can simply accept the unique ID as a parameter and deal with it?
Thanks in advance.
|
|
![]() |
|
Oleg ![]() Admin Group ![]() Joined: 21 May 2003 Location: United States Status: Offline Points: 11234 |
![]() ![]() ![]() ![]() ![]() |
Hi,
You can use ON_COMMAND_RANGE macro:
ON_COMMAND_RANGE(MINRANGE, MAXRANGE, OnDynamicButtton)
void CMainFrame::OnDynamicButtton(UINT nButton)
{ } or specific for XTP Menus: use same ID for all dynamic buttons but assign some parameter/integer data for CXTPControl.
|
|
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS |
|
![]() |
|
robosport ![]() Groupie ![]() Joined: 20 June 2005 Location: United States Status: Offline Points: 62 |
![]() ![]() ![]() ![]() ![]() |
That was more of a newbie question than I thought and I'm suitably embarrassed. I learn something new every day. Thank you again Oleg! |
|
![]() |
|
robosport ![]() Groupie ![]() Joined: 20 June 2005 Location: United States Status: Offline Points: 62 |
![]() ![]() ![]() ![]() ![]() |
Okay, I went with the second option you suggested, namely the XTP specific one.
Another embarrassing question came up. If they use a shared ID (one handler procedure set with ON_COMMAND) and I place a custom value in the CXTPControlButton using SetTag for example when creating the menu items, how do I get a pointer to the CXTPControlButton pressed from the handler function (so I can retreive the custom value)?
The problem is ON_COMMAND handlers take no parameters so how do I get the CXTPControlButton that initated the command handler?
|
|
![]() |
|
robosport ![]() Groupie ![]() Joined: 20 June 2005 Location: United States Status: Offline Points: 62 |
![]() ![]() ![]() ![]() ![]() |
I may have figured this out on my own.
Here is what I did:
- Derived a custom menu item class from CXTPControlButton
- Override OnLButtonUp for my derived menu item.
- When adding the dynamic menu item to the controls list I use the Add function that lets me pass in my derived control instead of the version that lets me pass the control type.
- I don't use ON_COMMAND at all for custom menu item handling.
This way works. Let me know if there is a better way.
Thanks.
|
|
![]() |
|
Oleg ![]() Admin Group ![]() Joined: 21 May 2003 Location: United States Status: Offline Points: 11234 |
![]() ![]() ![]() ![]() ![]() |
Hi,
your way is too difficult :) and user can press Enter instead click mouse.
easy way is use ON_XTP_EXECUTE macro:
ON_XTP_EXECUTE(ID_FAVORITE_LINK, OnFavorite)
void CMainFrame::OnFavorite(NMHDR* pNMHDR, LRESULT* pResult)
{ NMXTPCONTROL* tagNMCONTROL = (NMXTPCONTROL*)pNMHDR; AfxMessageBox(tagNMCONTROL->pControl->GetParameter()); *pResult = 1;
} check Samples\CommandBars\DynamicPopups it show how create dynamic favorites with same way.
|
|
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS |
|
![]() |
|
robosport ![]() Groupie ![]() Joined: 20 June 2005 Location: United States Status: Offline Points: 62 |
![]() ![]() ![]() ![]() ![]() |
Much better. Thank you!
|
|
![]() |
Post Reply ![]() |
|
Tweet
|
Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |