一部分取自网上源程序,一部分为自己加的功能
既不完全转载,也不完全原创,就当做翻译吧
CMyRichEditCtrl类:
头文件
#pragma once #define ID_RICH_UNDO 101 #define ID_RICH_CUT 102 #define ID_RICH_COPY 103 #define ID_RICH_PASTE 104 #define ID_RICH_CLEAR 105 #define ID_RICH_SELECTALL 106 #define ID_RICH_SETFONT 107 #define ID_RICH_BACKGROUND 108 // CMyrichEditView 视图 class CMyrichEditView : public CRichEditView { DECLARE_DYNCREATE(CMyrichEditView) protected: CMyrichEditView(); // 动态创建所使用的受保护的构造函数 virtual ~CMyrichEditView(); public: #ifdef _DEBUG virtual void AssertValid() const; #ifndef _WIN32_WCE virtual void Dump(CDumpContext& dc) const; #endif #endif protected: DECLARE_MESSAGE_MAP() virtual BOOL PreCreateWindow(CREATESTRUCT& cs); public: afx_msg void OnRButtonDown(UINT nFlags, CPoint point); afx_msg void OnCopy(){GetRichEditCtrl().Copy();} afx_msg void OnCut(){GetRichEditCtrl().Cut();} afx_msg void OnPaste(){GetRichEditCtrl().Paste();} afx_msg void OnSelectall(){GetRichEditCtrl().SetSel(0,-1);} afx_msg void OnUndo(){GetRichEditCtrl().Undo();} afx_msg void OnClear(){GetRichEditCtrl().Clear();} afx_msg void OnSelectFont(); afx_msg void OnBackGroundColor(); };
CPP文件
// MyrichEditView.cpp : 实现文件 // #include "stdafx.h" #include "Test.h" // 改为自己工程的头文件名 #include "MyrichEditView.h" static const TCHAR szClassRE[] = TEXT("RICHEDIT50W"); // CMyrichEditView IMPLEMENT_DYNCREATE(CMyrichEditView, CRichEditView) CMyrichEditView::CMyrichEditView() { } CMyrichEditView::~CMyrichEditView() { } BEGIN_MESSAGE_MAP(CMyrichEditView, CRichEditView) ON_WM_RBUTTONDOWN() ON_COMMAND(ID_RICH_COPY,OnCopy) ON_COMMAND(ID_RICH_CUT,OnCut) ON_COMMAND(ID_RICH_PASTE,OnPaste) ON_COMMAND(ID_RICH_SELECTALL,OnSelectall) ON_COMMAND(ID_RICH_UNDO,OnUndo) ON_COMMAND(ID_RICH_CLEAR,OnClear) ON_COMMAND(ID_RICH_SETFONT,OnSelectFont) ON_COMMAND(ID_RICH_BACKGROUND,OnBackGroundColor) END_MESSAGE_MAP() // CMyrichEditView 诊断 #ifdef _DEBUG void CMyrichEditView::AssertValid() const { CRichEditView::AssertValid(); } #ifndef _WIN32_WCE void CMyrichEditView::Dump(CDumpContext& dc) const { CRichEditView::Dump(dc); } #endif #endif //_DEBUG // CMyrichEditView 消息处理程序 BOOL CMyrichEditView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: 在此添加专用代码和/或调用基类 BOOL bRes = CRichEditView::PreCreateWindow(cs); cs.style |= ES_VERTICAL; cs.lpszClass = TEXT("RICHEDIT50W"); return bRes; } void CMyrichEditView::OnRButtonDown(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 CRichEditView::OnRButtonDown(nFlags, point); SetFocus(); CMenu popmenu; popmenu.CreatePopupMenu(); // popmenu.AppendMenu(MF_STRING,ID_RICH_UNDO,L"撤销操作"); // popmenu.AppendMenu(0,MF_SEPARATOR); popmenu.AppendMenu(MF_STRING,ID_RICH_COPY,L"复制"); popmenu.AppendMenu(MF_STRING,ID_RICH_CUT,L"剪切"); popmenu.AppendMenu(MF_STRING,ID_RICH_PASTE,L"粘贴"); popmenu.AppendMenu(MF_STRING,ID_RICH_CLEAR,L"清除"); popmenu.AppendMenu(0,MF_SEPARATOR); popmenu.AppendMenu(MF_STRING,ID_RICH_SELECTALL,L"全部"); popmenu.AppendMenu(0,MF_SEPARATOR); popmenu.AppendMenu(MF_STRING,ID_RICH_SETFONT, L"字体"); popmenu.AppendMenu(MF_STRING,ID_RICH_BACKGROUND,L"背景色"); // UINT nUndo=(CanUndo()?0:MF_GRAYED); // popmenu.EnableMenuItem(ID_RICH_UNDO,MF_BYCOMMAND|nUndo); UINT nSel=((GetRichEditCtrl().GetSelectionType()!=SEL_EMPTY)?0:MF_GRAYED); popmenu.EnableMenuItem(ID_RICH_CUT,MF_BYCOMMAND|nSel); popmenu.EnableMenuItem(ID_RICH_COPY,MF_BYCOMMAND|nSel); popmenu.EnableMenuItem(ID_RICH_CLEAR,MF_BYCOMMAND|nSel); UINT nPaste=(CanPaste()? 0:MF_GRAYED); popmenu.EnableMenuItem(ID_RICH_PASTE,MF_BYCOMMAND|nPaste); popmenu.EnableMenuItem(ID_RICH_BACKGROUND,MF_BYCOMMAND|0); CPoint pt; GetCursorPos(&pt); popmenu.TrackPopupMenu(TPM_RIGHTBUTTON,pt.x,pt.y,this); popmenu.DestroyMenu(); } void CMyrichEditView::OnSelectFont() { CHARFORMAT2 cf; LOGFONT lf; memset(&cf,0,sizeof(CHARFORMAT2)); memset(&lf,0,sizeof(LOGFONT)); BOOL bSelect=(GetRichEditCtrl().GetSelectionType()!=SEL_EMPTY)?TRUE:FALSE; if (bSelect) { GetRichEditCtrl().GetSelectionCharFormat(cf); }else{ GetRichEditCtrl().GetDefaultCharFormat(cf); } /* BOOL bIsBold=cf.dwEffects&CFE_BOLD; BOOL bIsItalic=cf.dwEffects&CFE_ITALIC; BOOL bIsUnderLine=cf.dwEffects&CFE_UNDERLINE; BOOL bIsStrickOut=cf.dwEffects&CFE_STRIKEOUT; lf.lfCharSet=cf.bCharSet; lf.lfHeight=cf.yHeight/15; lf.lfPitchAndFamily=cf.bPitchAndFamily; lf.lfItalic=bIsItalic; lf.lfWeight=(bIsBold?FW_BOLD:FW_NORMAL); lf.lfUnderline=bIsUnderLine; lf.lfStrikeOut=bIsStrickOut; _stprintf(lf.lfFaceName,cf.szFaceName); */ CFontDialog dlg(cf); dlg.m_cf.rgbColors=cf.crTextColor; if (dlg.DoModal()==IDOK) { dlg.GetCharFormat(cf); if (bSelect) { GetRichEditCtrl().SetSelectionCharFormat(cf); }else{ GetRichEditCtrl().SetDefaultCharFormat(cf); } } } void CMyrichEditView::OnBackGroundColor() { CColorDialog dlg(RGB(255,255,255),0,this); if (dlg.DoModal()==IDCANCEL) { return; } GetRichEditCtrl().SetBackgroundColor(FALSE,dlg.GetColor()); }
CMyRichEditView类:
头文件
#pragma once #define ID_RICH_UNDO 101 #define ID_RICH_CUT 102 #define ID_RICH_COPY 103 #define ID_RICH_PASTE 104 #define ID_RICH_CLEAR 105 #define ID_RICH_SELECTALL 106 #define ID_RICH_SETFONT 107 #define ID_RICH_BACKGROUND 108 #define ID_RICH_HORIZON 109 #define ID_RICH_VERTICAL 110 // CMyRichEditView 视图 extern UINT g_style; extern UINT g_HasCreate; class CMyRichEditView : public CRichEditView { DECLARE_DYNCREATE(CMyRichEditView) protected: CMyRichEditView(); // 动态创建所使用的受保护的构造函数 virtual ~CMyRichEditView(); public: #ifdef _DEBUG virtual void AssertValid() const; #ifndef _WIN32_WCE virtual void Dump(CDumpContext& dc) const; #endif #endif protected: DECLARE_MESSAGE_MAP() public: afx_msg int OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message); afx_msg void OnCopy(){GetRichEditCtrl().Copy();} afx_msg void OnCut(){GetRichEditCtrl().Cut();} afx_msg void OnPaste(){GetRichEditCtrl().Paste();} afx_msg void OnSelectall(){GetRichEditCtrl().SetSel(0,-1);} afx_msg void OnUndo(){GetRichEditCtrl().Undo();} afx_msg void OnClear(){GetRichEditCtrl().Clear();} afx_msg void OnSelectFont(); afx_msg void OnBackGroundColor(); afx_msg void OnHorizon(); afx_msg void OnVertical(); public: afx_msg void OnRButtonDown(UINT nFlags, CPoint point); protected: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); public: virtual void OnInitialUpdate(); };
CPP 文件
本来想把CPP 也发上来, 但是一发CPP CSDN就死掉,不知道为什么?
难道是有行数限制吗?
// MyRichEditView.cpp : 实现文件
//
#include "stdafx.h"
#include "MagazineEdit.h" //根据自己的需要替换
#include "MyRichEditView.h" //根据自己的需要进行替换
#include "ImageView.h"
#include "MainFrm.h"
UINT g_style=0;
UINT g_HasCreate=1;
static CString temCString;
IMPLEMENT_DYNCREATE(CMyRichEditView, CRichEditView)
CMyRichEditView::CMyRichEditView()
{
}
CMyRichEditView::~CMyRichEditView()
{
}
BEGIN_MESSAGE_MAP(CMyRichEditView, CRichEditView)
ON_WM_MOUSEACTIVATE()
ON_COMMAND(ID_RICH_COPY,OnCopy)
ON_COMMAND(ID_RICH_CUT,OnCut)
ON_COMMAND(ID_RICH_PASTE,OnPaste)
ON_COMMAND(ID_RICH_SELECTALL,OnSelectall)
ON_COMMAND(ID_RICH_UNDO,OnUndo)
ON_COMMAND(ID_RICH_CLEAR,OnClear)
ON_COMMAND(ID_RICH_SETFONT,OnSelectFont)
ON_COMMAND(ID_RICH_BACKGROUND,OnBackGroundColor)
ON_COMMAND(ID_RICH_HORIZON,OnHorizon)
ON_COMMAND(ID_RICH_VERTICAL,OnVertical)
ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()
// CMyRichEditView 诊断
#ifdef _DEBUG
void CMyRichEditView::AssertValid() const
{
CRichEditView::AssertValid();
}
#ifndef _WIN32_WCE
void CMyRichEditView::Dump(CDumpContext& dc) const
{
CRichEditView::Dump(dc);
}
#endif
#endif //_DEBUG
// CMyRichEditView 消息处理程序
int CMyRichEditView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//return CRichEditView::OnMouseActivate(pDesktopWnd, nHitTest, message);
return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);
}
void CMyRichEditView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CRichEditView::OnRButtonDown(nFlags, point);
CRichEditView::OnRButtonDown(nFlags, point);
SetFocus();
CMenu popmenu;
popmenu.CreatePopupMenu();
// popmenu.AppendMenu(MF_STRING,ID_RICH_UNDO,L"撤销操作");
// popmenu.AppendMenu(0,MF_SEPARATOR);
popmenu.AppendMenu(MF_STRING,ID_RICH_HORIZON,L"水平排列");
popmenu.AppendMenu(MF_STRING,ID_RICH_VERTICAL,L"垂直排列");
popmenu.AppendMenu(0,MF_SEPARATOR);
popmenu.AppendMenu(MF_STRING,ID_RICH_COPY,L"复制");
popmenu.AppendMenu(MF_STRING,ID_RICH_CUT,L"剪切");
popmenu.AppendMenu(MF_STRING,ID_RICH_PASTE,L"粘贴");
popmenu.AppendMenu(MF_STRING,ID_RICH_CLEAR,L"清除");
popmenu.AppendMenu(0,MF_SEPARATOR);
popmenu.AppendMenu(MF_STRING,ID_RICH_SELECTALL,L"全部");
popmenu.AppendMenu(0,MF_SEPARATOR);
popmenu.AppendMenu(MF_STRING,ID_RICH_SETFONT, L"字体");
popmenu.AppendMenu(MF_STRING,ID_RICH_BACKGROUND,L"背景色");
// UINT nUndo=(CanUndo()?0:MF_GRAYED);
// popmenu.EnableMenuItem(ID_RICH_UNDO,MF_BYCOMMAND|nUndo);
popmenu.EnableMenuItem(ID_RICH_HORIZON,MF_BYCOMMAND);
popmenu.EnableMenuItem(ID_RICH_VERTICAL,MF_BYCOMMAND);
UINT nSel=((GetRichEditCtrl().GetSelectionType()!=SEL_EMPTY)?0:MF_GRAYED);
popmenu.EnableMenuItem(ID_RICH_CUT,MF_BYCOMMAND|nSel);
popmenu.EnableMenuItem(ID_RICH_COPY,MF_BYCOMMAND|nSel);
popmenu.EnableMenuItem(ID_RICH_CLEAR,MF_BYCOMMAND|nSel);
UINT nPaste=(CanPaste()? 0:MF_GRAYED);
popmenu.EnableMenuItem(ID_RICH_PASTE,MF_BYCOMMAND|nPaste);
popmenu.EnableMenuItem(ID_RICH_BACKGROUND,MF_BYCOMMAND|0);
CPoint pt;
GetCursorPos(&pt);
popmenu.TrackPopupMenu(TPM_RIGHTBUTTON,pt.x,pt.y,this);
popmenu.DestroyMenu();
}
void CMyRichEditView::OnSelectFont()
{
CHARFORMAT2 cf;
LOGFONT lf;
memset(&cf,0,sizeof(CHARFORMAT2));
memset(&lf,0,sizeof(LOGFONT));
BOOL bSelect=(GetRichEditCtrl().GetSelectionType()!=SEL_EMPTY)?TRUE:FALSE;
if (bSelect)
{
GetRichEditCtrl().GetSelectionCharFormat(cf);
}else{
GetRichEditCtrl().GetDefaultCharFormat(cf);
}
/*
BOOL bIsBold=cf.dwEffects&CFE_BOLD;
BOOL bIsItalic=cf.dwEffects&CFE_ITALIC;
BOOL bIsUnderLine=cf.dwEffects&CFE_UNDERLINE;
BOOL bIsStrickOut=cf.dwEffects&CFE_STRIKEOUT;
lf.lfCharSet=cf.bCharSet;
lf.lfHeight=cf.yHeight/15;
lf.lfPitchAndFamily=cf.bPitchAndFamily;
lf.lfItalic=bIsItalic;
lf.lfWeight=(bIsBold?FW_BOLD:FW_NORMAL);
lf.lfUnderline=bIsUnderLine;
lf.lfStrikeOut=bIsStrickOut;
_stprintf(lf.lfFaceName,cf.szFaceName);
*/
CFontDialog dlg(cf);
dlg.m_cf.rgbColors=cf.crTextColor;
if (dlg.DoModal()==IDOK)
{
dlg.GetCharFormat(cf);
if (bSelect)
{
GetRichEditCtrl().SetSelectionCharFormat(cf);
}else{
GetRichEditCtrl().SetDefaultCharFormat(cf);
}
}
}
void CMyRichEditView::OnBackGroundColor()
{
CColorDialog dlg(RGB(255,255,255),0,this);
if (dlg.DoModal()==IDCANCEL)
{
return;
}
GetRichEditCtrl().SetBackgroundColor(FALSE,dlg.GetColor());
}
void CMyRichEditView::OnHorizon()
{
g_HasCreate=1;
g_style=0;
CHARFORMAT2 f2;
COLORREF color;
CImageView * pView=(CImageView* )(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,1));
CRect rc;
pView->modelDlg.GetWindowRect(rc);
pView->modelDlg.m_pRichEditView->GetWindowText(temCString);
pView->modelDlg.m_pRichEditView->GetRichEditCtrl().GetDefaultCharFormat(f2);
color=pView->modelDlg.m_pRichEditView->GetRichEditCtrl().SetBackgroundColor(TRUE,RGB(0,0,0));
pView->modelDlg.DestroyWindow();
pView->modelDlg.m_pRichEditView=NULL;
pView->modelDlg.Create( MAKEINTRESOURCE(IDD_DIALOG_MODEL),pView);
pView->modelDlg.m_pRichEditView->SetWindowText(temCString);
pView->modelDlg.MoveWindow(rc.left,rc.top,rc.Width(),rc.Height(),TRUE);
pView->modelDlg.m_pRichEditView->GetRichEditCtrl().SetDefaultCharFormat(f2);
pView->modelDlg.m_pRichEditView->GetRichEditCtrl().SetBackgroundColor(FALSE,color);
pView->modelDlg.ShowWindow(SW_SHOW);
pView->modelDlg.m_pRichEditView->GetRichEditCtrl().SetSel(temCString.GetLength(),temCString.GetLength());
}
void CMyRichEditView::OnVertical()
{
g_HasCreate=1;
g_style=1;
CHARFORMAT2 f2;
COLORREF color;
CImageView * pView=(CImageView* )(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,1));
CRect rc;
pView->modelDlg.GetWindowRect(rc);
pView->modelDlg.m_pRichEditView->GetWindowText(temCString);
pView->modelDlg.m_pRichEditView->GetRichEditCtrl().GetDefaultCharFormat(f2);
color=pView->modelDlg.m_pRichEditView->GetRichEditCtrl().SetBackgroundColor(TRUE,RGB(0,0,0));
pView->modelDlg.DestroyWindow();
pView->modelDlg.m_pRichEditView=NULL;
pView->modelDlg.Create(MAKEINTRESOURCE(IDD_DIALOG_MODEL),pView);
pView->modelDlg.m_pRichEditView->SetWindowText(temCString);
pView->modelDlg.MoveWindow(rc.left,rc.top,rc.Width(),rc.Height(),TRUE);
pView->modelDlg.m_pRichEditView->GetRichEditCtrl().SetDefaultCharFormat(f2);
pView->modelDlg.m_pRichEditView->GetRichEditCtrl().SetBackgroundColor(FALSE,color);
pView->modelDlg.ShowWindow(SW_SHOW);
pView->modelDlg.m_pRichEditView->GetRichEditCtrl().SetSel(temCString.GetLength(),temCString.GetLength());
}
BOOL CMyRichEditView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此添加专用代码和/或调用基类
BOOL bRre=CRichEditView::PreCreateWindow(cs);
cs.style|=ES_WANTRETURN;
if (g_style==1)
{
cs.style|=ES_VERTICAL;
}
cs.style&=~WS_BORDER;
cs.lpszClass=_T("RICHEDIT50W");
return bRre;
}
void CMyRichEditView::OnInitialUpdate()
{
CRichEditView::OnInitialUpdate();
// TODO: 在此添加专用代码和/或调用基类
}
BOOL CMyRichEditView::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if (pMsg->message==WM_KEYDOWN)
{
switch (pMsg->wParam)
{
case VK_DOWN:
case VK_LEFT:
case VK_RIGHT:
{
CImageView * pView=(CImageView* )(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,1));
pView->SendMessage(WM_KEYDOWN,pMsg->wParam,pMsg->lParam);
}
return TRUE; //此处是关键
default:
break;
}
}
return CRichEditView::PreTranslateMessage(pMsg);
}