Click here to Skip to main content
Click here to Skip to main content

How to modify a CEdit context menu

By , 16 Sep 2001
Rate this:
Please Sign up or sign in to vote.

Introduction

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.

Updates

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

The Source Files

The Header file

// MenuEdit.h : header file
// Written by PJ Arends
// pja@telus.net
// http://www3.telus.net/pja/

#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
#endif 

class CMenuEdit : public CEdit
{
public:
    CMenuEdit() {};

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

    DECLARE_MESSAGE_MAP()
};

#endif 

The Source file

// MenuEdit.cpp : implementation file
// Written by PJ Arends
// pja@telus.net
// http://www3.telus.net/pja/

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

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

#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

BEGIN_MESSAGE_MAP(CMenuEdit, CEdit)
    ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()

void CMenuEdit::OnContextMenu(CWnd* pWnd, CPoint point)
{
    SetFocus();
    CMenu menu;
    menu.CreatePopupMenu();
    BOOL bReadOnly = GetStyle() & ES_READONLY;
    DWORD flags = CanUndo() && !bReadOnly ? 0 : MF_GRAYED;
    menu.InsertMenu(0, MF_BYPOSITION | flags, EM_UNDO,
        MES_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,
        MES_COPY);

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

    flags = IsClipboardFormatAvailable(CF_TEXT) &&
        !bReadOnly ? 0 : MF_GRAYED;
    menu.InsertMenu(4, MF_BYPOSITION | flags, WM_PASTE,
        MES_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,
        MES_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);
    default:
        return CEdit::OnCommand(wParam, lParam);
    }
}

License

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

About the Author

PJ Arends
President
Canada Canada
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 PinmemberFalkir26-Mar-11 8:42 
GeneralMy vote of 5 PinmemberYangyong Qin1-Mar-11 13:22 
GeneralExample program would be useful Pinmemberl_d_allan1-Jan-04 2:24 
QuestionDoes this work on CDialogBar? PinmemberSteveBob3-Jun-03 14:21 
AnswerRe: Does this work on CDialogBar? PinmemberPJ Arends4-Jun-03 21:13 
GeneralA small question PinmemberDoan Quang Minh31-May-03 17:57 
GeneralRe: A small question PinmemberPJ Arends13-Jul-03 0:57 
GeneralRe: A small question PinmemberDoan Quang Minh14-Jul-03 0:21 
GeneralRe: A small question Pinmemberhoc9629-Jul-03 22:11 
QuestionHow to access data PinmemberJack_pt15-Nov-02 16:55 
AnswerRe: How to access data PinmemberPJ Arends16-Nov-02 9:10 
GeneralRe: How to access data PinmemberJack_pt18-Nov-02 3:08 
GeneralWay to avoid overriding OnCommand PinmemberPaul S. Vickery9-Aug-02 0:59 
GeneralRe: Way to avoid overriding OnCommand PinmemberPJ Arends9-Aug-02 5:55 
GeneralRe: Way to avoid overriding OnCommand PinmemberPaul Tankard28-Jan-03 5:01 
GeneralRe: Way to avoid overriding OnCommand PinmemberPaul S. Vickery29-Jan-03 11:06 
GeneralRe: Way to avoid overriding OnCommand PinmemberPaul Tankard30-Jan-03 0:09 
GeneralRe: Way to avoid overriding OnCommand PinmemberCrnjan22-Oct-04 2:30 
GeneralRe: Way to avoid overriding OnCommand PinmemberPaul S. Vickery22-Oct-04 2:52 
GeneralThere are some bits missing PinmemberPaul S. Vickery9-Aug-02 0:28 
GeneralRe: There are some bits missing PinmemberPJ Arends9-Aug-02 5:47 
GeneralRe: There are some bits missing PinmemberPaul S. Vickery9-Aug-02 6:06 
QuestionQuestion? PinmemberDejan Petrovic27-Sep-01 19:44 
AnswerRe: Question? PinmemberPJ Arends28-Sep-01 6:08 
GeneralRe: Question? Pinmemberzsh21-Oct-08 4:10 
GeneralRe: Question? Pinmemberllothar2-Jul-10 22:53 
QuestionHow to use this if I create dialog with ClassWizard? PinmemberGordy29-May-01 19:47 
AnswerRe: How to use this if I create dialog with ClassWizard? PinmemberJames R. Twine13-Sep-01 20:08 
Generalcouldn't read it PinmemberAnonymous16-Apr-01 21:15 
Generalin different windows basic menu looks different :-( PinmemberYury Goltsman15-Apr-01 4:47 
GeneralRe: in different windows basic menu looks different :-( PinmemberPJ Arends15-Apr-01 7:29 
GeneralRe: in different windows basic menu looks different :-( PinsussAnonymous29-Apr-04 6:44 
GeneralRe: in different windows basic menu looks different :-( PinmemberPJ Arends29-Apr-04 8:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140415.2 | Last Updated 17 Sep 2001
Article Copyright 2001 by PJ Arends
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid