mfc >> Dynamic context CMenu...

by Tom » Tue, 25 Nov 2003 18:12:39 GMT

Hi,
As usual I can write:
menu.LoadMenu(IDR_MENU1);
CMenu *pPopup = menu.GetSubMenu( 0 );
pPopup->TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
this );
to show a context menu.

But I do not want that since I plan to use the same menu in different
contexts, showing different menu items. And I do not want to delete existing
menu items. I'd much rather add menu items actively!

So, I'd like to create a context menu dynamically, something like:
void CDynMenuView::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
if (!menu.CreatePopupMenu())
return;

menu.InsertMenu(-1,MF_BYCOMMAND|MF_BYPOSITION,ID_POPUP_TEST);

menu.TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
this );
}

However, this does not work...
I guess I use ID_POPUP_TEST incorrectly. I want to use ID_POPUP_TEST as a
commandid and menutext, hence defined it as:
IDR_MENU1 MENU DISCARDABLE
BEGIN
POPUP ""
BEGIN
MENUITEM "Test...", ID_POPUP_TEST
END
END

What is the correct way of creating this context menu dynamically?

/Tom




mfc >> Dynamic context CMenu...

by Doug Harrison [MVP] » Wed, 26 Nov 2003 10:37:54 GMT






Two things:

1. MF_BYCOMMAND and MF_BYPOSITION are mutually exclusive. Use one or the
other but not both. (Actually, if you grep the Windows headers, you'll find
MF_BYCOMMAND is equal to 0, so you end up getting MF_BYPOSITION above.)

2. You forgot to specify the menu item text, which is the 4th and last
parameter. You should create a string resource and load it into a CString,
which you pass to InsertMenu.


That defines a whole new menu resource. You can certainly do that to provide
a standard set of commands for a menu you later modify, but you can also
build a menu from scratch using InsertMenu and related functions.

--
Doug Harrison
Microsoft MVP - Visual C++



Similar Threads

1. Simple Context Menu using CMenu (in a RichEditCtrl)

This is the context of editing in a RichEditCtrl.

All I want to do is create a simple context menu that pops up on right click.

Let me mention that I already have implemented a context menu that I use in 
conjunction with TrackPopupMenu().

However, I want to implement a second context menu with significantly 
different behavior.

The TrackPopupMenu() implementation performs a command (puts a command ID 
into a message queue) when a menu item is selected, which is the desired 
behavior.
And the menu entries are known at compile time.

For the context menu that I wish to implement:

1) The entries in the menu cannot be known at compile time (they are 
determined after the right click is detected).

I should be able to simpley call AppendMenu() for each entry.

2) When a menu item is selected, I don't want an entry added to a message 
queue. I simply want the menu to "pop down", and then to be able to inquire 
of the CMenu which entry was selected.


I can't see any means of doing this. Does anyone happen to have some sample 
code?

Basically, I want to "pop up" the menu by calling some method, then have the 
menu automatically pop down when an entry is selected, and then to inquire of 
the menu object which entry was selected.

I suppose it might be possible to create a CDialog with only a single list 
control and with the appropriate properties to appear and behave as a context 
menu.

CMenu seems to support "checkable" menu items. However, I don't want to 
determine which item is checked. For the user to have to "put a check" next 
to a menu item seems quite cludgy.


2. CMenu problem with a dynamic submenu

3. WinForm Dynamic Context Menu

I've got one Context Menu named mi_EasterEggs with three (3) menu items:

* mi_FontArial
* mi_FontCourier
* mi_RawData

All menu items have their Visible properties set to False when the form loads.

This context menu is included in one (1) ListView control and one (1) 
TextBox control.

When one of these controls is right-clicked (to bring up the Context Menu), 
I want to make menu items viisble depending on what item was right-clicked.

I tried this code, but the sender is always the mi_EasterEggs Menu Item:

private void mi_EasterEggs_Opening(object sender, CancelEventArgs e) {
  if (sender.Equals(ListView1) == true) {
    mi_RawView.Visible = (0 < ListView1.Items.Count);
  } else {
    mi_FontArial.Visible = sender.Equals(TextBox1);
    mi_FontCourier.Visible = sender.Equals(TextBox1);
    mi_RawView.Visible = false;
  }
}

I searched google and found a suggestion by Plausibly Damp where he 
mentioned usingt the .SourceControl field, so I modified my listing to this:

private void mi_EasterEggs_Opening(object sender, CancelEventArgs e) {
  if (mi_EasterEggs.SourceControl == ListView1) {
    mi_RawView.Visible = (0 < ListView1.Items.Count);
  } else {
    mi_FontArial.Visible = (mi_EasterEggs.SourceControl == TextBox1);
    mi_FontCourier.Visible = (mi_EasterEggs.SourceControl == TextBox1);
    mi_RawView.Visible = false;
  }
}

It still doesn't work, though.

What am I doing wrong?

Also, I'd like to still have the default Context Menu for the Controls, but 
this is a bonus and is not necessary.

First thing is first, right?

4. Context Menu - Microsofts standard file context menu - CSharp/C#

5. Context.Items vs Context.Cache

There doesn't seem to be much doc on Items vs Cache in HttpContext.

Basically I want to instantiate a class at web application level so
that all web pages can have access to it.  The object would have the
life span of the application itself.

Can anyone point me in right direction regarding Items vs Cache?

Richard

6. IIS SDK API for writing cookies, (context.cpp, context.h file need

7. Dynamic validation on dynamic form objects in ASP.NET

Hi All,

I am trying to build a dynamic web form using C# which depending on
certain factors will display various fields. I've got all of this
working fine but now I'd like to add dynamic validation to it using the
validation server objects.

I'm not exactly sure where to look or how to go about this though? I
can make it work at design time, but haven't the foggiest for
configuring it and adding it to controls at run time...

Any pointers would be much appreciated.

Cheers
AndrewF

8. dynamic html vs dynamic formview/detailsView - CSharp/C#