CMyRichEditCtrlとCMyRichEditView右クリックポップアップメニュー機能を追加


一部はネット上のソースプログラムから取り、一部は自分で追加した機能です.
完全転載でもオリジナルでもないので、通訳にしましょう.
 
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){//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_PASTTE,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()->>AfxGetMainWnd()->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>m_wndSplitter.GetPane(0,1);pView->SendMessage(WM_KEYDOWN,pMsg->wParam,pMsg->lParam);    }    return TRUE;//ここがポイント
  default:    break;   }  }
 return CRichEditView::PreTranslateMessage(pMsg); }