Introduction
Have you ever wanted to modify ' context menu of ' an ' edit control? The would start by overriding the Oninitmenupopup () function is only "find", "Edit control" does not post a wm_initmen Upopup message, so your function is never called. It's a simple CEdit derived class. 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 which exactly duplicates the default context menu, and we call TrackPopupMenu () On that menu. By doing this, the a WM_INITMENUPOPUP message was posted, which can then be handled to a class derived from Cmenuedit.
The OnCommand () function handles commands generated when a user selects an item from the menu. If the command isn't generated by we menu, it is passed onto Cedit::oncommand ().
You can either derive your edit class from Cmenuedit or include this two functions in your class. Updates
September, 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!d efined (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 "St
Dafx.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 The Mes_copy _t ("&co PY ") #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_MESS
Age_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 (LO
WORD (WParam));
Case Me_selectall:return SendMessage (Em_setsel, 0,-1);
Default:return Cedit::oncommand (WParam, LParam);
}
}