Print Page | Close Window

Dynamic Menu Shared Handler?

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Toolkit Pro
Forum Description: Topics Related to Codejock Toolkit Pro
URL: http://forum.codejock.com/forum_posts.asp?TID=7000
Printed Date: 22 May 2025 at 4:45am
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: Dynamic Menu Shared Handler?
Posted By: robosport
Subject: Dynamic Menu Shared Handler?
Date 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.



Replies:
Posted By: Oleg
Date Posted: 27 April 2007 at 6:54am
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


Posted By: robosport
Date Posted: 27 April 2007 at 3:54pm

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!



Posted By: robosport
Date Posted: 03 May 2007 at 3:50am
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?


Posted By: robosport
Date Posted: 03 May 2007 at 4:32am
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.


Posted By: Oleg
Date Posted: 03 May 2007 at 5:22am
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


Posted By: robosport
Date Posted: 03 May 2007 at 5:23am
Much better. Thank you!



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net