⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newmtext.cpp

📁 mtext,编辑器,支持潜入对象
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// NewMText.cpp : implementation file
//

#include "stdafx.h"
#include "NewMText.h"
#include "MtextDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <atlbase.h>
/////////////////////////////////////////////////////////////////////////////
// CMTextRichEditCtrl
class CMtextDlg;
static UINT WM_FINDREPLACE = ::RegisterWindowMessage(FINDMSGSTRING);
CLIPFORMAT CMTextRichEditCtrl::m_cfObjectDescriptor=NULL;
CLIPFORMAT CMTextRichEditCtrl::m_cfPrivate = NULL;
CMTextRichEditCtrl::CMTextRichEditCtrl()
{
	m_pFindDialog=NULL;
	m_FindIndex=0;
}

CMTextRichEditCtrl::~CMTextRichEditCtrl()
{
}


BEGIN_MESSAGE_MAP(CMTextRichEditCtrl, CRichEditCtrl)
	//{{AFX_MSG_MAP(CMTextRichEditCtrl)
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
	ON_REGISTERED_MESSAGE(WM_FINDREPLACE, OnFindReplace)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMTextRichEditCtrl message handlers

void CMTextRichEditCtrl::GetFontInfo(LPTSTR pszFaceName, int &nSize,COLORREF&color)
{
	CHARFORMAT cf;
	GetSelectionCharFormat(cf);
	::lstrcpy(pszFaceName,cf.dwMask&CFM_FACE?cf.szFaceName:_T(""));
	nSize=cf.dwMask&CFM_SIZE?cf.yHeight:-1;
	color=cf.dwMask&CFM_COLOR?cf.crTextColor:-1;

}
void CMTextRichEditCtrl::OnCharBold()
{
	CHARFORMAT cf;
	GetSelectionCharFormat(cf);
	if(!(cf.dwMask&CFM_BOLD)||!(cf.dwEffects&CFE_BOLD))
		cf.dwEffects=CFE_BOLD;
	else
		cf.dwEffects=0;
	cf.dwMask=CFM_BOLD;
	SetSelectionCharFormat(cf);
}

void CMTextRichEditCtrl::OnCharItalic()
{
	CHARFORMAT cf;
	GetSelectionCharFormat(cf);
	if(!(cf.dwMask&CFM_ITALIC)||!(cf.dwEffects&CFE_ITALIC))
		cf.dwEffects=CFE_ITALIC;
	else
		cf.dwEffects=0;
	cf.dwMask=CFM_ITALIC;
	SetSelectionCharFormat(cf);
}

void CMTextRichEditCtrl::OnCharUnderline()
{
	CHARFORMAT cf;
	GetSelectionCharFormat(cf);
	if(!(cf.dwMask&CFM_UNDERLINE)||!(cf.dwEffects&CFE_UNDERLINE))
		cf.dwEffects=CFE_UNDERLINE;
	else
		cf.dwEffects=0;
	cf.dwMask=CFM_UNDERLINE;
	SetSelectionCharFormat(cf);
}

void CMTextRichEditCtrl::ChangeFont(LPCTSTR pszFaceName)
{
	CHARFORMAT	cf;
	cf.cbSize=sizeof(CHARFORMAT);
	cf.dwMask=CFM_FACE;
	::lstrcpy(cf.szFaceName,pszFaceName);
	SetSelectionCharFormat(cf);
}

void CMTextRichEditCtrl::ChangeFontSize(int nSize)
{
	CHARFORMAT	cf;
	cf.cbSize=sizeof(CHARFORMAT);
	cf.dwMask=CFM_SIZE;
	cf.yHeight=nSize;
	SetSelectionCharFormat(cf);
}

int CMTextRichEditCtrl::IsBold()
{
	CHARFORMAT cf;	
	cf.cbSize = sizeof(CHARFORMAT);
	cf.dwMask = CFM_BOLD;
	
	DWORD dwSelMask = GetSelectionCharFormat(cf);
	bool bConsistent = false; 
	if( dwSelMask & CFM_BOLD )
    { 
		bConsistent = true; 
    }
	if( !bConsistent )
    { 
		return (-1); 
    }
	if( cf.dwEffects & CFE_BOLD )
    { 
		return(1);   
    }
	return 0; 
}

int CMTextRichEditCtrl::IsUnderlined()
{
	CHARFORMAT cf;	
	cf.cbSize = sizeof(CHARFORMAT);
	cf.dwMask = CFM_UNDERLINE;
	
	DWORD dwSelMask = GetSelectionCharFormat(cf);
	bool bConsistent = false; 
	if( dwSelMask & CFM_UNDERLINE )
    { 
		bConsistent = true; 
    }
	if( !bConsistent )
    { 
		return (-1); 
    }
	if( cf.dwEffects & CFE_UNDERLINE )
    { 
		return(1);   
    }
	return 0;        
}

int CMTextRichEditCtrl::IsItalic()
{
	CHARFORMAT cf;	
	cf.cbSize = sizeof(CHARFORMAT);
	cf.dwMask = CFM_ITALIC;
	
	DWORD dwSelMask = GetSelectionCharFormat(cf);
	bool bConsistent = false;

	if( dwSelMask & CFM_ITALIC )
    { 
		bConsistent = true; 
    }
	if( !bConsistent )
    { 
		return (-1); 
    }
	if( cf.dwEffects & CFE_ITALIC )
    { 
		return(1);   
    }
	return 0;        
}

void CMTextRichEditCtrl::InitFindReplaceDlg()
{
	if( NULL == m_pFindDialog )
		{
			m_pFindDialog = new CFindReplaceDialog();  
			
			m_pFindDialog->Create( FALSE,"", "", FR_DOWN, this ); 
			
			m_pFindDialog->m_fr.lStructSize = sizeof(FINDREPLACE);
			m_pFindDialog->m_fr.hwndOwner = this->m_hWnd;
			m_FindIndex=0;
		}
	
}

LONG CMTextRichEditCtrl::OnFindReplace(WPARAM wParam, LPARAM lParam)
{
	
	ASSERT(m_pFindDialog != NULL);
	
   
    if (m_pFindDialog->IsTerminating())
    {
       	m_pFindDialog = NULL;
        return 0;
    }
   
   	ASSERT_VALID(this);
	CFindReplaceDialog* pDialog= CFindReplaceDialog::GetNotifier(lParam);
	ASSERT(pDialog != NULL);
	
	if (pDialog->FindNext())
	{
		OnFindNext(pDialog->GetFindString(), pDialog->SearchDown(),
			pDialog->MatchCase(), pDialog->MatchWholeWord());
	}
	else if (pDialog->ReplaceCurrent())
	{
		
		OnReplaceSel(pDialog->GetFindString(),
			pDialog->SearchDown(), pDialog->MatchCase(), pDialog->MatchWholeWord(),
			pDialog->GetReplaceString());
	}
	else if (pDialog->ReplaceAll())
	{
		
		OnReplaceAll(pDialog->GetFindString(), pDialog->GetReplaceString(),
			pDialog->MatchCase(), pDialog->MatchWholeWord());
	}
	ASSERT_VALID(this);
	return 0;
	
}

void CMTextRichEditCtrl::OnFindNext(LPCTSTR lpszFind, BOOL bNext, BOOL bCase, BOOL bWord)
{
	
	ASSERT_VALID(this);
	CHARRANGE cr;
	GetSel( cr );
	m_FindIndex=cr.cpMax;
	FINDTEXTEX ft;
	ft.chrg.cpMin = m_FindIndex;
	ft.chrg.cpMax = -1;
	ft.lpstrText = (LPSTR)lpszFind;
	DWORD dwFlags = NULL;
 	if (bNext)
		dwFlags |= FR_DOWN;
	if (bCase)
		dwFlags |= FR_MATCHCASE;
	if (bWord)
		dwFlags |= FR_WHOLEWORD;
   
	m_FindIndex = FindText(dwFlags, &ft);	
	if (m_FindIndex != -1)
	{
		SetFocus();
		SetSel(ft.chrgText);
	
	}
	else
	{
		//SetSel(0,0);
		CString strFinish;
		strFinish.Format(IDS_FIND_END,lpszFind);
		AfxMessageBox(strFinish);
	}
	
   	ASSERT_VALID(this);
}
void CMTextRichEditCtrl::OnReplaceSel(LPCTSTR lpszFind, BOOL bNext, BOOL bCase,
								 BOOL bWord, LPCTSTR lpszReplace)
{
	ASSERT_VALID(this);
	
	CString strCurrent;
	strCurrent=GetSelText();
	if((bCase&&strCurrent.Compare(lpszFind)==0)||
		(!bCase&&strCurrent.CompareNoCase(lpszFind)==0))
	{
		ReplaceSel(lpszReplace,TRUE);
		OnFindNext(lpszFind,bNext, bCase,bWord);
	}
	else
	{	
		OnFindNext(lpszFind,bNext, bCase,bWord);
	/*
		CString strFinish;
			strFinish.Format(IDS_FIND_END,lpszFind);
			AfxMessageBox(strFinish);	*/
	
	}
	ASSERT_VALID(this);
}

void CMTextRichEditCtrl::OnReplaceAll(LPCTSTR lpszFind, LPCTSTR lpszReplace, BOOL bCase, BOOL bWord)
{
	ASSERT_VALID(this);
	m_FindAndReplaceCount=0;
	FINDTEXTEX ft;
	ft.chrg.cpMin = 0;
	ft.chrg.cpMax = -1;
	ft.lpstrText = (LPSTR)lpszFind;
	DWORD dwFlags = NULL;
	dwFlags |= FR_DOWN;
	if (bCase)
		dwFlags |= FR_MATCHCASE;
	if (bWord)
		dwFlags |= FR_WHOLEWORD;
	m_FindIndex = FindText(dwFlags, &ft);

	CHARRANGE cr;
	HideSelection(TRUE, FALSE);
	while(m_FindIndex!=-1)
	{
		m_FindAndReplaceCount++;
		SetSel(ft.chrgText);
		ReplaceSel(lpszReplace,TRUE);
		GetSel( cr );
		ft.chrg.cpMin=cr.cpMax;
		m_FindIndex = FindText(dwFlags, &ft);
	}
	HideSelection(FALSE, FALSE);
	CString strReplace;
	strReplace.Format(IDS_Replace_End,m_FindAndReplaceCount);
	AfxMessageBox(strReplace);
	ASSERT_VALID(this);
}

void CMTextRichEditCtrl::SetTextColor(COLORREF color)
{
	CHARFORMAT cf;
	//CHARRANGE cr;
	cf.dwMask =CFM_COLOR;
	cf.dwEffects=0;
	cf.crTextColor=color;
	//GetSel(cr);
	//SetSel(0,-1);
	SetSelectionCharFormat(cf);
	//SetSel(cr);
}


void CMTextRichEditCtrl::SetLeft()
{
	HideSelection(TRUE, FALSE);
	CHARRANGE cr;
	GetSel(cr);
	SetSel(0,-1);
	PARAFORMAT pf;    
	pf.cbSize     = sizeof(PARAFORMAT);
	pf.dwMask     = PFM_ALIGNMENT;    
	pf.wAlignment = PFA_LEFT;
	SetParaFormat(pf);	// Set the paragraph.
	HideSelection(FALSE, FALSE);
	SetSel(cr);
}
void CMTextRichEditCtrl::SetCenter()
{
	HideSelection(TRUE, FALSE);
	CHARRANGE cr;
	GetSel(cr);
	SetSel(0,-1);
	PARAFORMAT pf;    
	pf.cbSize     = sizeof(PARAFORMAT);
	pf.dwMask     = PFM_ALIGNMENT;    
	pf.wAlignment = PFA_CENTER;
	SetParaFormat(pf);	// Set the paragraph.
	HideSelection(FALSE, FALSE);
	SetSel(cr);
}
void CMTextRichEditCtrl::SetRight()
{	
	HideSelection(TRUE, FALSE);
	CHARRANGE cr;
	GetSel(cr);
	SetSel(0,-1);
	PARAFORMAT pf;    
	pf.cbSize     = sizeof(PARAFORMAT);
	pf.dwMask     = PFM_ALIGNMENT;    
	pf.wAlignment = PFA_RIGHT;
	SetParaFormat(pf);	// Set the paragraph.
	HideSelection(FALSE, FALSE);
	SetSel(cr);

}

void CMTextRichEditCtrl::GetAlignment(int &nAlignment)
{
	PARAFORMAT pf;
	GetParaFormat(pf);
	if (pf.dwMask & PFM_ALIGNMENT)
	{
		if (pf.wAlignment & PFA_LEFT && pf.wAlignment & PFA_RIGHT)
			nAlignment = 2;
		else
			nAlignment = (pf.wAlignment & PFA_LEFT) ? 0 : 1;
	}
	else
		nAlignment = -1;
}
void CMTextRichEditCtrl::MakeSelectUpperCase(BOOL isUpperCase)
{
	/*
		CString strText;
				strText=_T("");
				if(isUpperCase)
				{
					GetRTF(strText);
					strText.MakeUpper();
					SetRTF(strText);
				}
				else
				{
					GetRTF(strText);
					strText.MakeLower();
					SetRTF(strText);
				}*/
	HideSelection(TRUE,FALSE);
	SetOptions(ECOOP_XOR, ECO_AUTOVSCROLL | ECO_AUTOHSCROLL|ECO_NOHIDESEL);
	
	if (GetSelectionType() & (SEL_TEXT | SEL_MULTICHAR))
	{
		CHARRANGE	 crSelect;
		LONG		 lCurPos;
		CHARFORMAT   cf;
		CString		 strSelect;
		strSelect=GetSelText();
		GetSel( crSelect);
		lCurPos=crSelect.cpMin;
	
		if(strSelect.GetLength()>0)
		{
			
			CString		strCurrent;
			CHARRANGE   crCurrent;
			TCHAR		chr;
			LONG		cpMax;
			
			cpMax=crSelect.cpMax==-1?GetTextLength():crSelect.cpMax;
			while(lCurPos<cpMax)
			{
				crCurrent.cpMin=lCurPos;
				crCurrent.cpMax=lCurPos+1;
				SetSel(crCurrent);
				strCurrent=GetSelText();
				if(strCurrent.GetLength()>1)
				{
					lCurPos++;
					continue;
				}
				if(strCurrent.GetLength()==0)
				{
					lCurPos++;
					continue;
				}
				chr=strCurrent.GetAt(0);
				if(!((chr>='A'&&chr<='Z')||(chr>='a'&&chr<='z')))
				{
					lCurPos++;
					continue;
				}
				
				if(chr>='A'&&chr<='Z'&&isUpperCase)
				{
					lCurPos++;
					continue;
				}
				if(chr>='a'&&chr<='z'&&!isUpperCase)
				{
					lCurPos++;
					continue;
				}
				GetSelectionCharFormat( cf );
				if(isUpperCase)
					strCurrent.MakeUpper();
				else
					strCurrent.MakeLower();
				ReplaceSel(strCurrent,FALSE);
				//SetSel(crSelect);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -