Click here to Skip to main content
12,079,202 members (26,595 online)
Click here to Skip to main content
Add your own
alternative version


49 bookmarked

How to modify a CEdit context menu

, 16 Sep 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple way to let your CEdit derived class have a modifiable context menu


Have you ever wanted to modify the context menu of an edit control? You would start by overriding the OnInitMenuPopup() function only to find that the edit control does not post a WM_INITMENUPOPUP message, so your function is never called. Well, here is a simple CEdit derived class that you can use.

The CMenuEdit class

The CMenuEdit class does its job with just two functions, OnContextMenu() and OnCommand().

The OnContextMenu() function gets called when a user right-clicks on the edit control. In our override, we create a new popup menu that exactly duplicates the default context menu, and we call TrackPopupMenu() on that menu. By doing this, a WM_INITMENUPOPUP message is posted, which can then be handled in a class derived from CMenuEdit.

The OnCommand() function handles commands generated when a user selects an item from the menu. If the command is not generated by our menu, it is passed onto CEdit::OnCommand().

You can either derive your edit class from CMenuEdit or include these two functions in your class.


September 17, 2001 - Now handles read-only edit controls

The Source Files

The Header file

// MenuEdit.h : header file
// Written by PJ Arends

#if !defined(AFX_MENUEDIT_H__8EA53611_FD2B_11D4_B625_D04FA07D2222__INCLUDED_)
#define AFX_MENUEDIT_H__8EA53611_FD2B_11D4_B625_D04FA07D2222__INCLUDED_

#if _MSC_VER > 1000
#pragma once

class CMenuEdit : public CEdit
    CMenuEdit() {};

    virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
    afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);



The Source file

// MenuEdit.cpp : implementation file
// Written by PJ Arends

#include "stdafx.h"
#include "MenuEdit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

#define MES_UNDO        _T("&Undo")
#define MES_CUT         _T("Cu&t")
#define MES_COPY        _T("&Copy")
#define MES_PASTE       _T("&Paste")
#define MES_DELETE      _T("&Delete")
#define MES_SELECTALL   _T("Select &All")
#define ME_SELECTALL    WM_USER + 0x7000


void CMenuEdit::OnContextMenu(CWnd* pWnd, CPoint point)
    CMenu menu;
    BOOL bReadOnly = GetStyle() & ES_READONLY;
    DWORD flags = CanUndo() && !bReadOnly ? 0 : MF_GRAYED;
    menu.InsertMenu(0, MF_BYPOSITION | flags, EM_UNDO,

    menu.InsertMenu(1, MF_BYPOSITION | MF_SEPARATOR);

    DWORD sel = GetSel();
    flags = LOWORD(sel) == HIWORD(sel) ? MF_GRAYED : 0;
    menu.InsertMenu(2, MF_BYPOSITION | flags, WM_COPY,

    flags = (flags == MF_GRAYED || bReadOnly) ? MF_GRAYED : 0;
    menu.InsertMenu(2, MF_BYPOSITION | flags, WM_CUT,
    menu.InsertMenu(4, MF_BYPOSITION | flags, WM_CLEAR,

    flags = IsClipboardFormatAvailable(CF_TEXT) &&
        !bReadOnly ? 0 : MF_GRAYED;
    menu.InsertMenu(4, MF_BYPOSITION | flags, WM_PASTE,

    menu.InsertMenu(6, MF_BYPOSITION | MF_SEPARATOR);

    int len = GetWindowTextLength();
    flags = (!len || (LOWORD(sel) == 0 && HIWORD(sel) ==
        len)) ? MF_GRAYED : 0;
    menu.InsertMenu(7, MF_BYPOSITION | flags, ME_SELECTALL,

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

BOOL CMenuEdit::OnCommand(WPARAM wParam, LPARAM lParam)
    switch (LOWORD(wParam))
    case EM_UNDO:
    case WM_CUT:
    case WM_COPY:
    case WM_CLEAR:
    case WM_PASTE:
        return SendMessage(LOWORD(wParam));
    case ME_SELECTALL:
        return SendMessage (EM_SETSEL, 0, -1);
        return CEdit::OnCommand(wParam, lParam);


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


About the Author

PJ Arends
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionLanguage independant version Pin
Andreas Schniertshauer7-Sep-15 2:27
memberAndreas Schniertshauer7-Sep-15 2:27 
AnswerRe: Language independant version Pin
PJ Arends7-Sep-15 19:13
professionalPJ Arends7-Sep-15 19:13 
GeneralMy vote of 4 Pin
Falkir26-Mar-11 9:42
memberFalkir26-Mar-11 9:42 
GeneralMy vote of 5 Pin
Yangyong Qin1-Mar-11 14:22
memberYangyong Qin1-Mar-11 14:22 
GeneralExample program would be useful Pin
l_d_allan1-Jan-04 3:24
memberl_d_allan1-Jan-04 3:24 
QuestionDoes this work on CDialogBar? Pin
SteveBob3-Jun-03 15:21
memberSteveBob3-Jun-03 15:21 
AnswerRe: Does this work on CDialogBar? Pin
PJ Arends4-Jun-03 22:13
memberPJ Arends4-Jun-03 22:13 
This class is a simple subclassed CEdit control, so there is no reason why it should not work. But I am not familiar with CDialogBars as I have never had a need to use them so I do not know if there is a gotcha to look out for.

Sonork 100.11743 Chicken Little

"You're obviously a superstar." - Christian Graus about me - 12 Feb '03

Within you lies the power for good - Use it!
GeneralA small question Pin
Doan Quang Minh31-May-03 18:57
memberDoan Quang Minh31-May-03 18:57 
GeneralRe: A small question Pin
PJ Arends13-Jul-03 1:57
memberPJ Arends13-Jul-03 1:57 
GeneralRe: A small question Pin
Doan Quang Minh14-Jul-03 1:21
memberDoan Quang Minh14-Jul-03 1:21 
GeneralRe: A small question Pin
hoc9629-Jul-03 23:11
memberhoc9629-Jul-03 23:11 
QuestionHow to access data Pin
Jack_pt15-Nov-02 17:55
memberJack_pt15-Nov-02 17:55 
AnswerRe: How to access data Pin
PJ Arends16-Nov-02 10:10
memberPJ Arends16-Nov-02 10:10 
GeneralRe: How to access data Pin
Jack_pt18-Nov-02 4:08
memberJack_pt18-Nov-02 4:08 
GeneralWay to avoid overriding OnCommand Pin
Paul S. Vickery9-Aug-02 1:59
memberPaul S. Vickery9-Aug-02 1:59 
GeneralRe: Way to avoid overriding OnCommand Pin
PJ Arends9-Aug-02 6:55
memberPJ Arends9-Aug-02 6:55 
GeneralRe: Way to avoid overriding OnCommand Pin
Paul Tankard28-Jan-03 6:01
memberPaul Tankard28-Jan-03 6:01 
GeneralRe: Way to avoid overriding OnCommand Pin
Paul S. Vickery29-Jan-03 12:06
memberPaul S. Vickery29-Jan-03 12:06 
GeneralRe: Way to avoid overriding OnCommand Pin
Paul Tankard30-Jan-03 1:09
memberPaul Tankard30-Jan-03 1:09 
GeneralRe: Way to avoid overriding OnCommand Pin
Crnjan22-Oct-04 3:30
memberCrnjan22-Oct-04 3:30 
GeneralRe: Way to avoid overriding OnCommand Pin
Paul S. Vickery22-Oct-04 3:52
memberPaul S. Vickery22-Oct-04 3:52 
GeneralThere are some bits missing Pin
Paul S. Vickery9-Aug-02 1:28
memberPaul S. Vickery9-Aug-02 1:28 
GeneralRe: There are some bits missing Pin
PJ Arends9-Aug-02 6:47
memberPJ Arends9-Aug-02 6:47 
GeneralRe: There are some bits missing Pin
Paul S. Vickery9-Aug-02 7:06
memberPaul S. Vickery9-Aug-02 7:06 
QuestionQuestion? Pin
Dejan Petrovic27-Sep-01 20:44
memberDejan Petrovic27-Sep-01 20:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.160212.1 | Last Updated 17 Sep 2001
Article Copyright 2001 by PJ Arends
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid