Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Combobox default pop-up menu

17 views
Skip to first unread message

Moritz

unread,
Feb 25, 2005, 4:51:05 PM2/25/05
to
I'm using the ComboBox control in my MFC/ATL aplication. Is there a way to
customize the default pop-up menu that appears when I right mouse click on
edit control area ?
How can I add new item to the menu?

thatsalok

unread,
Feb 25, 2005, 11:43:22 PM2/25/05
to

"Moritz" <Mor...@discussions.microsoft.com> wrote in message

I'm using the ComboBox control in my MFC/ATL aplication. Is there a way to
customize the default pop-up menu that appears when I right mouse click on
edit control area ?

for creating the Menu on Right Click or any click look at these apis;
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/res
ources/menus/menureference/menufunctions/trackpopupmenu.asp

-> How can I add new item to the menu?

AppendMenu(..)

--

With Regards
Alok Gupta
Visit me at http://alok.bizhat.com

"I think this will Help"


Moritz

unread,
Mar 3, 2005, 1:33:04 PM3/3/05
to
I should add item to pop-up menu that that appears when I right mouse click on
edit control area. I don't know how get handler to this menu.

thatsalok

unread,
Mar 4, 2005, 3:15:30 AM3/4/05
to
Yeah Follow this Simple Code
HMENU hMnu=::CreatePopupMenu();
AppendMenu(hMnu,MF_STRING,NULL,"alok1");
AppendMenu(hMnu,MF_STRING,NULL,"alok3");
AppendMenu(hMnu,MF_STRING,NULL,"alok2");

POINT point;
GetCursorPos(&point);

::TrackPopupMenu(hMnu,TPM_LEFTBUTTON |
TPM_RIGHTBUTTON,point.x,point.y,NULL,this->m_hWnd,NULL);

--

With Regards
Alok Gupta
Visit me at http://alok.bizhat.com

"I think this will Help"

"Moritz" <Mor...@discussions.microsoft.com> wrote in message

news:B0ACA13A-F262-44DB...@microsoft.com...

Moritz

unread,
Mar 5, 2005, 2:53:03 PM3/5/05
to
Thanks for your answer ,but It is not what I asked . There are "default
menu" that appears when I right mouse click on edit control . I don't want
create new menu . I need customize the "default menu" : there are some items
as Paste,Delete... And I need add one more.

Jeff Partch [MVP]

unread,
Mar 6, 2005, 4:28:26 PM3/6/05
to
"Moritz" <Mor...@discussions.microsoft.com> wrote in message
news:1B341326-78D7-45B4...@microsoft.com...

> Thanks for your answer ,but It is not what I asked . There are "default
> menu" that appears when I right mouse click on edit control . I don't
want
> create new menu . I need customize the "default menu" : there are some
items
> as Paste,Delete... And I need add one more.

There are three ways I know of to do this, and all of them involve first
subclassing the combo's edit control. In MFC this means using a class
derived from CEdit, instantiating it as a member variable and calling its
SubclassWindow member function using the HWND returned by calling the
combo's GetWindow(GW_CHILD). After that you can handle the WM_CONTEXTMENU
message in the subclass and 1) CreatePopupMenu or LoadMenu/GetSubMenu your
own and add the standard edit menu items yourself along with your custom
item(s). The command ids map as: WM_UNDO("&Undo"),
WM_COPY("&Copy),WM_PASTE("&Paste"), WM_CUT("Cu&t"), WM_CLEAR("&Delete") and
EM_SETSEL("Select &All"); 2) I've not explored this as to practicality, but
you can load the default edit menu from user32.dll (I think it might be
different on the 9x platforms). Its menu resource id is 1 and submenu index
is 0. In theory this approach would obtain the text for the current locale.
This menu does contain several items that I've never seen in the OS versions
I use, so you may have to discern if and when to remove them before adding
your custom item(s). In either case, you then need to enable/disable the
commands as appropriate, calculate the menu position and call TrackPopupMenu
yourself using TPM_RETURNCMD (along with which ever other flags seem
necessary), and then forward all standard commands to the base class
implementation for default processing and handle your additions locally; 3)
I came up with this one last August, and I've only ever tested it on XP so I
can't promise it on any other platform. It starts by adding a BOOL member
variable to your derived CEdit which you initialize to FALSE in the ctor...

CComboEdit::CComboEdit() : m_bMenuInitialized(FALSE)
{
}


...Then add handlers for the WM_ENTERIDLE and WM_MENUSELECT messages...

VOID CComboEdit::OnEnterIdle(UINT nWhy, CWnd* pWho)
{
CEdit::OnEnterIdle(nWhy, pWho);
if (!m_hMenu && pWho && (MSGF_MENU == nWhy))
{
if (InPopupMenuMode(m_hWnd))
{
HMENU hMenu = (HMENU)pWho->SendMessage(MN_GETHMENU);
if (hMenu)
{
// Add custom item to the standard menu
CString strItem((LPCTSTR)IDS_CUSTOM_MENUITEM_1);
if (AppendMenu(hMenu, MF_STRING, RWM_CUSTOM_MENUITEM_1,
strItem))
m_bMenuInitialized = TRUE;
}
}
}
}

VOID CComboEdit::OnMenuSelect(UINT nItemID, UINT nFlags, HMENU hSysMenu)
{
CEdit::OnMenuSelect(nItemID, nFlags, hSysMenu);
if (!hSysMenu && (0x0000FFFF == nFlags))
m_bMenuInitialized = FALSE;
}

...where the InPopupMenuMode helper function goes something like this...

static CONST _inline BOOL _stdcall InPopupMenuMode(
IN CONST HWND hWndMenuOwner)
{
GUITHREADINFO gti = { sizeof(gti) };
return (GetGUIThreadInfo(NULL, &gti) &&
(gti.flags & GUI_POPUPMENUMODE) &&
(gti.hwndMenuOwner == hWndMenuOwner));
}

...and the RWM_CUSTOM_MENUITEM_1 is declared something like so (or amybe use
a GUID string)...

static CONST UINT RWM_CUSTOM_MENUITEM_1 = RegisterWindowMessage(
_T("CComboEdit::RWM_CUSTOM_MENUITEM_1"));


...lastly, you add a handler for your custom item...

BEGIN_MESSAGE_MAP(CComboEdit, CEdit)
// {{AFX_MSG_MAP(CComboEdit)
ON_WM_ENTERIDLE()
ON_WM_MENUSELECT()
// }}AFX_MSG_MAP
ON_REGISTERED_MESSAGE(RWM_CUSTOM_MENUITEM_1, OnMenuCustom1)
END_MESSAGE_MAP()


...and implement it however you want, really, but for the sake of example...

LRESULT CComboEdit::OnMenuCustom1(WPARAM, LPARAM)
{
AfxMessageBox(IDS_CUSTOM_MENU_1_MSG, MB_OK);
return 0;
}

The benefits of this last approach are that the edit control does all of the
menu creation, localizing, tracking, default command enabling/disabling and
handling itself just as it normally would. All you have to worry about is
your own custom items and matching the locale. Presumably this would involve
localized STRINGTABLE entries, but I have no real experience with supporting
international languages and locales, so I can't say much about that part.

Anyway, HTH...
--
Jeff Partch [VC++ MVP]


0 new messages