15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Inserting menus

Ganesh Vaideeswaran -- gvaidees@intertrust.com
Tuesday, April 01, 1997


Hi,

Environment: VC++ 4.2b, NT 4.0

I have a Pop Up menu called IDR_POPUP_MENU, that contains three items -   
    

      

Add, Delete and Update.

My main menu IDR_MAINFRAME has a menu item called Action. This contains   
    

two sub-items - Save and Refresh.

IDR_POPUP_MENU

 Dummy         <= menu Bar

   Add
   Delete
   Update

IDR_MAINFRAME

 File        Action     About      <= menu bar

   Exit      Save
    Refresh

At run time, I want to insert the items in the popup menu under the
Action item of the main menu.
How do I do this ?

Thanks

Ganesh - gvaidees@intertrust.com







RS.Manavalan@blr.siemens.de
Tuesday, April 01, 1997

[Mini-digest: 2 responses]

>>Hi,

>>Environment: VC++ 4.2b, NT 4.0

>>I have a Pop Up menu called IDR_POPUP_MENU, that contains three items -   
    
>>Add, Delete and Update.

>>My main menu IDR_MAINFRAME has a menu item called Action. This contains   
>>two sub-items - Save and Refresh.

>>IDR_POPUP_MENU

>> Dummy         <= menu Bar

>>   Add
>>   Delete
>>   Update

>>IDR_MAINFRAME

>> File        Action     About      <= menu bar

>>   Exit      Save
    Refresh

>>At run time, I want to insert the items in the popup menu under the
>>Action item of the main menu.
>>How do I do this ?

>>Thanks

>>Ganesh - gvaidees@intertrust.com

Hi,

You can insert menu items dynamically by getting a pointer to the
Action menu item and using CMenu::AppendMenu. Here's the code 
snippet.

// first get the CMenu* to Action menu item, say it's ID
// is ID_ACTION

CMenu* pActionMenu = NULL;
CMenu* pTopMenu = AfxGetMainWnd()->GetMenu();
int iPos;
for (iPos = pTopMenu->GetMenuItemCount()-1; iPos >= 0; iPos--)
{
CMenu* pMenu = pTopMenu->GetSubMenu(iPos);
if (pMenu && pMenu->GetMenuItemID(0) == ID_ACTION)
{
pActionMenu = pMenu; // gotcha!
break;
}
}

// We've got menu pointer to Action item
// Add items to the Action Item.
pActionMenu->AppendMenu() ...

Hope this helps..

-sunder




- - - - - - - - - - - - End of Original Message - - - - - - - - - - - -
-----From: Ben Burnett 

Ganesh Vaideeswaran wrote:
> 
> Hi,
> 
> Environment: VC++ 4.2b, NT 4.0
> 
> I have a Pop Up menu called IDR_POPUP_MENU, that contains three items -
> 
> 
> 
> 
> Add, Delete and Update.
> 
> My main menu IDR_MAINFRAME has a menu item called Action. This contains
> 
> 
> two sub-items - Save and Refresh.
> 
> IDR_POPUP_MENU
> 
>  Dummy         <= menu Bar
> 
>    Add
>    Delete
>    Update
> 
> IDR_MAINFRAME
> 
>  File        Action     About      <= menu bar
> 
>    Exit      Save
>     Refresh
> 
> At run time, I want to insert the items in the popup menu under the
> Action item of the main menu.
> How do I do this ?

have you tried calling -> CMenu::AppendMenu()

Call this after you gather all the info on the popup menu (text,ID)
use: CMenu::GetMenuItemID() and CMenu::GetMenuString() to gather the
required info.

Hope this helps.




-- 
----------------------------------------------
 Reality is for those who have no imagination 

 Ben Burnett   /   Pulse productions
 e-mail: benner@supernet.ab.ca



Satadal Bhattacharjee -- satadalb@mindware.soft.net
Thursday, April 03, 1997

[Mini-digest: 2 responses]



Hi Ganesh,
This is the way u should do it:

CMenu* pMenu = AfxGetMainWnd() -> GetMenu();//AfxGetMain Wnd() is NOT   
//required if u are placing this piece of code in mainframe itself
 ASSERT( pMenu != NULL );
 pMenu = pMenu -> GetSubMenu(1);
 CMenu cmPopup;
 cmPopup.LoadMenu(IDR_POPUP_MENU);
 CMenu* pPopup = cmPopup.GetSubMenu(0);
 int nMenuCount = (int)pPopup -> GetMenuItemCount();
 if(nMenuCount == -1)
 {
  AfxMessageBox("No menu items inserted in Popup");
  return;
 }
 char strMenuString[20];
 for( int i = 0 ; i < nMenuCount ; i++ )
 {
  pPopup -> GetMenuString( i, strMenuString, sizeof(strMenuString),   
MF_BYPOSITION);
  pMenu  -> AppendMenu( MF_STRING, cmPopup.GetMenuItemID(i),   
strMenuString);
 }
Regards,
Satadal
satadalb@mindware.soft.net


Hi,

Environment: VC++ 4.2b, NT 4.0

I have a Pop Up menu called IDR_POPUP_MENU, that contains three items -   
    

      


        


Add, Delete and Update.

My main menu IDR_MAINFRAME has a menu item called Action. This contains   
    

      


two sub-items - Save and Refresh.

IDR_POPUP_MENU

 Dummy         <= menu Bar

   Add
   Delete
   Update

IDR_MAINFRAME

 File        Action     About      <= menu bar

   Exit      Save
    Refresh

At run time, I want to insert the items in the popup menu under the
Action item of the main menu.
How do I do this ?

Thanks

Ganesh - gvaidees@intertrust.com





-----From: "Claire Rollet" 

- First, get a ptr on the actual menu :

CMenu* pMenu = ::AfxGetMainWnd()->GetMenu();
// You'll have to pass by the main window because only this window really
possess
// a menu.

- Then you can get the "Action" pop-up menu :

CMenu* pActionMenu = pMenu->GetSubMenu(1); // 1 because the "Action" menu
is the second sub-menu.

- Now, you can do anything you want!!

In particular, look at the following methods :
(Give the new menu items the same ID as the corresponding menu items in
your contextual menu)

CMenu::InsertMenu  

BOOL InsertMenu( UINT nPosition, UINT nFlags, UINT nIDNewItem = 0, LPCSTR
lpszNewItem
 = NULL );

BOOL InsertMenu( UINT nPosition, UINT nFlags, UINT nIDNewItem, 
const CBitmap* pBmp );

  

nPosition    Specifies the menu item before which the new menu item is to
be inserted. The nFlags parameter can be used to interpret nPosition in the
following ways:

nFlags	Interpretation of nPosition
MF_BYCOMMAND	Specifies that the parameter gives the command ID of the
existing menu item. This is the default if neither MF_BYCOMMAND nor
MF_BYPOSITION is set.
MF_BYPOSITION	Specifies that the parameter gives the position of the
existing menu item. The first item is at position 0. If nPosition is -1,
the new menu item is appended to the end of the menu.


nFlags    Specifies how nPosition is interpreted and specifies information
about the state of the new menu item when it is added to the menu. For a
list of the flags that may be set, see the AppendMenu member function. To
specify more than one value, use the bitwise-OR operator to combine them
with the MF_BYCOMMAND or MF_BYPOSITION flag.
nIDNewItem    Specifies either the command ID of the new menu item or, if
nFlags is set to MF_POPUP, the menu handle (HMENU) of the pop-up menu. The
nIDNewItem parameter is ignored (not needed) if nFlags is set to
MF_SEPARATOR.
lpszNewItem    Specifies the content of the new menu item. nFlags can be
used to interpret lpszNewItem in the following ways:

nFlags	Interpretation of lpszNewItem
MF_OWNERDRAW	Contains an application-supplied 32-bit value that the
application can use to maintain additional data associated with the menu
item. This 32-bit value is available to the application in the itemData
member of the structure supplied by the WM_MEASUREITEM and WM_DRAWITEM
messages. These messages are sent when the menu item is initially displayed
or is changed.
MF_STRING	Contains a long pointer to a null-terminated string. This is the
default interpretation.
MF_SEPARATOR	The lpszNewItem parameter is ignored (not needed).


pBmp    Points to a CBitmap object that will be used as the menu item.

Remarks

Inserts a new menu item at the position specified by nPosition and moves
other items down the menu. The application can specify the state of the
menu item by setting values in nFlags
.. 
Whenever a menu that resides in a window is changed (whether or not the
window is displayed), the application should call CWnd::DrawMenuBar. 
When nIDNewItem specifies a pop-up menu, it becomes part of the menu in
which it is inserted. If that menu is destroyed, the inserted menu will
also be destroyed. An inserted menu should be detached from a CMenu object
to avoid conflict.
If the active multiple document interface (MDI) child window is maximized
and an application inserts a pop-up menu into the MDI application's menu by
calling this function and specifying the MF_BYPOSITION flag, the menu is
inserted one position farther left than expected. This happens because the
Control menu of the active MDI child window is inserted into the first
position of the MDI frame window's menu bar. To position the menu properly,
the application must add 1 to the position value that would otherwise be
used. An application can use the WM_MDIGETACTIVE message to determine
whether the currently active child window is maximized.

Return Value

Nonzero if the function is successful; otherwise 0.


------------------------------------------------------
Francis Girard
fgirard@machinasapiens.com




Become an MFC-L member | Вернуться в корень Архива |