上一篇 | 下一篇

带有菜单的EDIT控件实现

发布: 2008-6-26 11:21 | 作者: admin | 来源: | 查看: 3次

eidt控制是vc中经常使用的文本输入输出交互控制,有的时候,我们为了实现对输入,输出有特殊的操作过程,那么,原来的edit控制的功能也许就不一定能实现我们的要求,或者实现的不是那么灵活,因此,我们也经常在edit控件的基础上继承基类来做一些配生的子类情况下实现我们自己在工程中需要的特殊的要求。本文介绍一个可以带有菜单操作的edit类的实现过程,基础类eidt基类,在继承的子类上添加各个菜单操作功能,其中的菜单操作也是为了实现多edit中的文本进行各种文本的常规操作。

这个类的实现过程如下:

.h文件

#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

.cpp文件

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

字号: | 推荐给好友

21/212>

评分:0

我来说两句