怎么修改cedit控件的右键菜单

转载 2006年06月21日 00:12:00
 

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);
    }
}
举报

相关文章推荐

为CEdit控件设置右键菜单

FARPROC OldEditProc = NULL; LRESULT CALLBACK NewEditProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM l...

右键菜单“新建”的修改

转自:http://xuhui-8491.blog.163.com/blog/static/80745891201011202250443/ 一、修改“新建”右键菜单 展开 K...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

右键菜单“发送到”的修改

一、修改“发送到”右键菜单 ①打开注册表编辑器,展开HKEY_CLASSES_ROOT\AllfilesystemObjects\Shellex\ContextMenuHandlers\SendTo...
  • can_yi
  • can_yi
  • 2012-03-07 12:14
  • 7214

krpano 教程 - 修改右键菜单

[img src='examples/custom-contextmenu/krpano_logo.png' style='width:90px; height:25px; margin:0px 0p...

如何修改右键菜单

修改右键菜单应该在注册表中,具体的说,是在 HKEY_CLASSES_ROOT根键下(这里可是右键菜单中所有命令的根源哦!)。展开该根键,其下的子键主要有两种:扩展名键(.+扩展名)和类定 义键(如图...

注册表修改右键菜单项

转自:http://blog.csdn.net/zwhfyy/article/details/3732456 一: 开始,运行里面输入:  regsvr32 /u igfxpph.dll  确定...

rcp修改右键菜单(eclipse3.7.2)

效果如下: 一:新建2个文件: (1)MyTab.java package com.rcp.tab; import org.eclipse.core.runtime...
  • QQzyb
  • QQzyb
  • 2013-06-29 08:30
  • 693

如何修改右键菜单

修改右键菜单应该在注册表中,具体的说,是在HKEY_CLASSES_ROOT根键下(这里可是右键菜单中所有命令的根源哦!)。展开该根键,其下的子键主要有两种:扩展名键(.+扩展名)和类定义键(如图1)...

一 右键功能

右键功能 (1)用鼠标点击右键,会出现按钮: (图 1) (2)实现右键菜单的代码: Append Remove 折叠 展开 ...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)