Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to make pop up menu for a right click on text box.
I would like to add own cut ,copy items and short cut for cut and copy also.
Posted
Updated 28-Dec-11 1:36am
v3
Comments
RaviRanjanKr 28-Dec-11 7:33am    
[Edited]pre tag is only for Codes. not for normal texts[/Edited]

Override CWnd::OnContextMenu by adding a WM_CONTEXTMENU handler to your CEdit derived class. From within this handler, you must load your own popup menu, call TrackPopupMenu and call the menu functions according to the returned ID.

Example:
C++
void CMyEdit::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
{
    CMenu menu;
    VERIFY(menu.LoadMenu(IDR_MYEDIT_POPUP_MENU));
    CMenu *pSub = menu.GetSubMenu(0);
    // When right clicking on a control, the focus is sometimes not moved to it
    //  before calling this function.
    if (GetFocus() != this)
        SetFocus();

    // if opened by keyboard (not mouse)
    if (point.x < 0 && point.y < 0)					
    {
        CRect rect;
        GetWindowRect(rect);
        point.x = rect.left;
        point.y = rect.bottom;
    }
    int nCommand = pSub->TrackPopupMenuEx(
        TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_VERPOSANIMATION | TPM_RETURNCMD | TPM_NONOTIFY, 
        point.x, point.y, 
        AfxGetMainWnd(), NULL); 
    switch (nCommand)
    {
    case ID_EDIT_COPY : OnCopy(); break;
    case ID_EDIT_CUT : OnCut(); break;
    }
}
 
Share this answer
 
v2
For this you have to intercept messages that sent to the edit box you need to
replace its window procedure with your own. This technique called
subclassing.
You can read about subclassing here : Windows subclassing and hooking with C++ classes[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900