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

📄 richeditpage.cpp

📁 MFC窗口程序设计源代码。相信大家看得懂。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// RicheditPage.cpp : implementation file
//

#include "stdafx.h"
#include "..\commonctrls.h"
#include "..\CtrlPage\RicheditPage.h"
#include <afxpriv.h> // for WM_SETMESSAGESTRING and WM_IDLEUPDATECMDUI
#include <afxres.h> // for AFX_IDW_STATUS_BAR
#include <afxole.h> // for COleDataObject

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRicheditPage property page

IMPLEMENT_DYNCREATE(CRicheditPage, CPropertyPage)

CRicheditPage::CRicheditPage() : CPropertyPage(CRicheditPage::IDD)
{
	//{{AFX_DATA_INIT(CRicheditPage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CRicheditPage::~CRicheditPage()
{
}

void CRicheditPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRicheditPage)
	DDX_Control(pDX, IDC_RICHEDIT1, m_rtf);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CRicheditPage, CPropertyPage)
	//{{AFX_MSG_MAP(CRicheditPage)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
	ON_COMMAND(ID_BOLD, OnBold)
	ON_COMMAND(ID_ITALIC, OnItalic)
	ON_COMMAND(ID_UNDERLINE, OnUnderline)
	ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
	ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_COMMAND(ID_COLOUR, OnColour)
	ON_WM_CLOSE()
	ON_COMMAND(ID_PARAGRAPH_LEFT, OnParagraphLeft)
	ON_COMMAND(ID_PARAGRAPH_CENTER, OnParagraphCenter)
	ON_COMMAND(ID_PARAGRAPH_RIGHT, OnParagraphRight)
	ON_COMMAND(ID_PARAGRAPH_BULLETED, OnParagraphBulleted)
	ON_COMMAND(ID_FORMAT_FONT, OnFormatFont)
	ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
	ON_COMMAND(ID_EDIT_DELETE, OnEditDelete)
	ON_WM_SIZE()
	ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
	ON_UPDATE_COMMAND_UI(ID_BOLD, OnUpdateBold)
	ON_UPDATE_COMMAND_UI(ID_ITALIC, OnUpdateItalic)
	ON_UPDATE_COMMAND_UI(ID_UNDERLINE, OnUpdateUnderline)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
	ON_WM_LBUTTONDOWN()
	ON_UPDATE_COMMAND_UI(ID_EDIT_DELETE, OnUpdateEditDelete)
	ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
	ON_UPDATE_COMMAND_UI(ID_PARAGRAPH_BULLETED, OnUpdateParagraphBulleted)
	ON_UPDATE_COMMAND_UI(ID_PARAGRAPH_CENTER, OnUpdateParagraphCenter)
	ON_UPDATE_COMMAND_UI(ID_PARAGRAPH_LEFT, OnUpdateParagraphLeft)
	ON_UPDATE_COMMAND_UI(ID_PARAGRAPH_RIGHT, OnUpdateParagraphRight)
	//}}AFX_MSG_MAP
	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
	ON_WM_ENTERIDLE()
	ON_WM_INITMENUPOPUP()
	
	// 6 aug 2003 for updating dialog box controls
	ON_MESSAGE(WM_KICKIDLE,OnKickIdle)
	ON_MESSAGE(WM_IDLEUPDATECMDUI,OnIdleUpdateCmdUI)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRicheditPage message handlers

BOOL CRicheditPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	// allow tool tips to appear
	EnableToolTips();
	// TODO: Add extra initialization here
	// 创建工具栏	
	// Create toolbar at the top of the dialog window
	if (!m_toolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC)||!m_toolBar.LoadToolBar(IDR_TOOLBAR1))
	{
		TRACE("初始化属性页时,创建工具栏失败");
		return -1;
	}
	// a small gap between buttons	
	m_toolBar.SetButtonInfo(m_toolBar.CommandToIndex(ID_FILE_SAVE)+1,ID_SEPARATOR,TBBS_SEPARATOR,5);
	m_toolBar.SetButtonInfo(m_toolBar.CommandToIndex(ID_EDIT_DELETE)+1,ID_SEPARATOR,TBBS_SEPARATOR,5);
	m_toolBar.SetButtonInfo(m_toolBar.CommandToIndex(ID_EDIT_UNDO)+1,ID_SEPARATOR,TBBS_SEPARATOR,5);
	m_toolBar.SetButtonInfo(m_toolBar.CommandToIndex(ID_COLOUR)+1,ID_SEPARATOR,TBBS_SEPARATOR,5);
	
    // bold,italic and underline toggles between 
	// the pressed and unpressed state
	m_toolBar.SetButtonStyle(m_toolBar.CommandToIndex(ID_BOLD),TBBS_CHECKBOX);
	m_toolBar.SetButtonStyle(m_toolBar.CommandToIndex(ID_UNDERLINE),TBBS_CHECKBOX);
	m_toolBar.SetButtonStyle(m_toolBar.CommandToIndex(ID_ITALIC),TBBS_CHECKBOX);
	// Creates a check button that stays pressed 
	// until another button in the group is pressed.
	m_toolBar.SetButtonStyle(m_toolBar.CommandToIndex(ID_PARAGRAPH_LEFT),TBBS_CHECKGROUP);
	m_toolBar.SetButtonStyle(m_toolBar.CommandToIndex(ID_PARAGRAPH_CENTER),TBBS_CHECKGROUP);
	m_toolBar.SetButtonStyle(m_toolBar.CommandToIndex(ID_PARAGRAPH_RIGHT),TBBS_CHECKGROUP);
	
	
    // paragraph bulleted toggles between 
	// the pressed and unpressed state
	m_toolBar.SetButtonStyle(m_toolBar.CommandToIndex(ID_PARAGRAPH_BULLETED),TBBS_CHECKBOX);
	
	m_toolBar.SetBarStyle(m_toolBar.GetBarStyle() |
		CBRS_TOOLTIPS | CBRS_FLYBY |CBRS_HIDE_INPLACE| CBRS_SIZE_DYNAMIC);
	// code added on 21 september 2001 from VC sample dlgcbr32
	// We need to resize the dialog to make room for control bars.
	// First, figure out how big the control bars are.
	CRect rcClientStart;
	CRect rcClientNow;
	GetClientRect(rcClientStart);
	RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,
		0, reposQuery, rcClientNow);
	
	// Now move all the controls so they are in the same relative
	// position within the remaining client area as they would be
	// with no control bars.
	CPoint ptOffset(rcClientNow.left - rcClientStart.left,
		rcClientNow.top - rcClientStart.top);
	
	CRect  rcChild;
	CWnd* pwndChild = GetWindow(GW_CHILD);
	while (pwndChild)
	{
		pwndChild->GetWindowRect(rcChild);
		ScreenToClient(rcChild);
		rcChild.OffsetRect(ptOffset);
		pwndChild->MoveWindow(rcChild, FALSE);
		pwndChild = pwndChild->GetNextWindow();
	}
	
	// Adjust the dialog window dimensions
	CRect rcWindow;
	GetWindowRect(rcWindow);
	rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
	rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
	MoveWindow(rcWindow, FALSE);
	
	// And position the control bars
	RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
	// end sample dlgcbr32
	// line added on Tuesday 9 Octomber 2001	
	// set background colour 
	m_rtf.SetBackgroundColor(FALSE, RGB(0,128, 128));	
	m_bFisNou=m_bFisOpen=FALSE;
	m_bBeginWriting=TRUE;
	m_nBeginWriting=m_nFileNew=0;
	// Note that LoadAccelerator does not require DestroyAcceleratorTable	
	m_rtf.LimitText(100000000); 
	// center the dialog app
	CenterWindow();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// function added with classwizard in 19 september 2001
void CRicheditPage::OnFileNew() 
{	
	m_bFisNou=TRUE;
	m_bBeginWriting=FALSE;
	m_bFisOpen=FALSE;
	m_nFileNew=0;

	m_nLinesNew=m_rtf.GetTextLength (); 
	if ( m_nLinesNew!=m_nLinesOpen)
	{
			// delete all of the text
		m_rtf.SetSel(0, -1);
		m_rtf.Clear();	
		m_rtf.SetModify();
	}
	else
	{
		// delete all of the text
	m_rtf.SetSel(0, -1);
	m_rtf.Clear();	
	m_rtf.SetModify(FALSE); 
	}
  CString msg;	
  msg.Format("MyRtf");
  SetWindowText ( msg ) ;

}

 
void CRicheditPage::OnFileOpen() 
{
	// TODO: Add your command handler code here
	CFileDialog fileDlg(TRUE, "rtf", NULL, OFN_FILEMUSTEXIST,
                       "rtf files(*.rtf)|*.rtf|");
  //显示打开文件对话框
  // if I press the Cancel button
  if (fileDlg.DoModal () == IDCANCEL)
  {
	  m_bFisOpen=FALSE;
	  m_bBeginWriting=TRUE;
	  return;
  }
	m_bFisOpen=TRUE;
  	m_bBeginWriting=FALSE;
	m_bFisNou=FALSE;
  //获取文件名
  CString filename = fileDlg.GetPathName ();
  m_strFilename=fileDlg.GetFileName(); 
  CFile fis;
  fis.Open(filename, CFile::modeRead | CFile::shareExclusive   );
  m_rtf.SetRtf(&fis);
  CString msg;	
  msg.Format("MyRtf - %s" , filename );
  SetWindowText ( msg ) ;

  fis.Close();		
  m_rtf.SetModify(FALSE); 
  m_nLinesOpen=m_rtf.GetTextLength () ;

}

// function added with classwizard in 20 september 2001
// code with file find added on Tuesday 30 oct 2001
void CRicheditPage::OnFileSave() 
{
	m_nBeginWriting++;
	m_nFileNew++;
if ( m_bBeginWriting )
{ 
	if ( m_nBeginWriting==1)
	{

	// I define the file dialog
	CFileDialog fileDlg(FALSE, "rtf", NULL, OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT,
                       "rtf files(*.rtf)|*.rtf|");
	// display the file dialog
	if (fileDlg.DoModal () == IDCANCEL) 
	{
		m_nBeginWriting=0;
		m_rtf.SetModify(); 
		return;
	}

	// I obtain the file name
	CString filename = fileDlg.GetPathName ();
    m_strFilename=filename; 
	CFile fis;
	
	EDITSTREAM es;
	es.dwCookie = (DWORD) &fis;
	es.pfnCallback = MyStreamOutCallback; 

	// I open the file with tne modeWrite and modeCreate mode
	fis.Open(filename, CFile::modeCreate | CFile::modeWrite);

	es.dwCookie = (DWORD) &fis;
	es.pfnCallback = MyStreamOutCallback;
	// I put the content in the file
	m_rtf.StreamOut(SF_RTF, es);
	fis.Close();
	m_rtf.SetModify(FALSE); 
	}// end m_nBeginWriting == 1
	else if ( m_nBeginWriting >1)
	{

	CFile fis;
	
	EDITSTREAM es;
	es.dwCookie = (DWORD) &fis;
	es.pfnCallback = MyStreamOutCallback; 
	// I open the file with tne modeWrite mode
	fis.Open(m_strFilename,  CFile::modeWrite);
	// I put the content in the file
	m_rtf.StreamOut(SF_RTF, es);
	m_rtf.SetModify(FALSE); 
	fis.Close(); 
	}
}//end if begin writing

else if ( m_bFisNou)
{ 
	if ( m_nFileNew == 1)
	{

	// define the dialog window
	CFileDialog fileDlg(FALSE, "rtf", NULL, OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT,
                       "rtf files(*.rtf)|*.rtf|");
	// display the dialog window
	if (fileDlg.DoModal () == IDCANCEL) 
	{
		m_nFileNew=0;
		m_rtf.SetModify(); 
		return;
	}

	// obtain the file name
	CString filename = fileDlg.GetPathName ();
	m_strFilename=filename;
	CFile fis;
	
	EDITSTREAM es;
	es.dwCookie = (DWORD) &fis;
	es.pfnCallback = MyStreamOutCallback; 
	// I open the file with tne modeWrite and modeCreate mode
	fis.Open(filename, CFile::modeCreate | CFile::modeWrite);

	es.dwCookie = (DWORD) &fis;
	es.pfnCallback = MyStreamOutCallback;
	// put the content in the file
	m_rtf.StreamOut(SF_RTF, es);
	fis.Close();
	m_rtf.SetModify(FALSE); 
	}// end m_nFileNew==1
	else if ( m_nFileNew > 1 )
	{

	CFile fis;
	
	EDITSTREAM es;
	es.dwCookie = (DWORD) &fis;
	es.pfnCallback = MyStreamOutCallback; 
	// open the file with the modeWrite mode
	fis.Open(m_strFilename,  CFile::modeWrite);
	// put the content in the file
	m_rtf.StreamOut(SF_RTF, es);
	m_rtf.SetModify(FALSE); 
	fis.Close(); 
	}
}//end if file new
else if (m_bFisOpen)// file open
{
	// if I want to save the file that I have opened and I did not 
	// CREATED and I have changed the content, I don't want to appear 
	// the dialog box which asks me for the name to save the file, but
	// I want the file to be saved with the name the file has already

	CFile fis;
	
	EDITSTREAM es;
	es.dwCookie = (DWORD) &fis;
	es.pfnCallback = MyStreamOutCallback; 
	// open the file with the modeWrite mode
	fis.Open(m_strFilename,  CFile::modeWrite);
	// put the content in the file
	m_rtf.StreamOut(SF_RTF, es);
	m_rtf.SetModify(FALSE); 
	fis.Close(); 
}// end file open
  CString msg;	
  msg.Format("MyRtf - %s" , m_strFilename );
  SetWindowText ( msg ) ;

}

// My callback procedure that reads the rich edit control content
// and put it into a file
// function added on 20 september 2001 from VC help(MSDN)
//----------------------------------------------------------------------------
// Function CMyRichEditCtrl::MyStreamOutCallback
// @func    reads the rich edit control content and put it into a file

⌨️ 快捷键说明

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