带有菜单的EDIT控件实现

翻译|其它|编辑:郝浩|2004-12-28 09:50:00.000|阅读 1813 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


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


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP