How to modify the right-click menu for CEDIT controls

xiaoxiao2021-04-03  263

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 The Be Handled In a class deived from cmenuedit.

..

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 overce

#ENDIF

Class Cmenuedit: Public Cedit

{

PUBLIC:

CmenueDit () {};

protected:

Virtual Bool Oncommand (WPARAM WPARAM, LPARAM LPARAM);

AFX_MSG Void OnContextMenu (CWND * PWND, CPOINT);

Declare_message_map ()

}

#ENDIF

The Source File

// menuedit.cpp: importation 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.createPopupnupmenu ();

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 || botonly)? 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);

}

}

转载请注明原文地址:https://www.9cbs.com/read-131486.html

New Post(0)