rulerricheditctrl.cpp

来自「管理项目进度工具的原代码」· C++ 代码 · 共 2,215 行 · 第 1/4 页

CPP
2,215
字号
	Parameters :	none

	Usage :			Called from MFC.

   ============================================================*/
{

	DoItalic();

}

void CRulerRichEditCtrl::OnButtonUnderline() 
/* ============================================================
	Function :		CRulerRichEditCtrl::OnButtonUnderline
	Description :	Button handler for the Underline button
	Access :		Protected
					
	Return :		void
	Parameters :	none

	Usage :			Called from MFC.

   ============================================================*/
{

	DoUnderline();

}

void CRulerRichEditCtrl::OnButtonLeftAlign() 
/* ============================================================
	Function :		CRulerRichEditCtrl::OnButtonLeftAlign
	Description :	Button handler for the Left aligned button
	Access :		Protected
					
	Return :		void
	Parameters :	none

	Usage :			Called from MFC.

   ============================================================*/
{

	DoLeftAlign();

}

void CRulerRichEditCtrl::OnButtonCenterAlign() 
/* ============================================================
	Function :		CRulerRichEditCtrl::OnButtonCenterAlign
	Description :	Button handler for the Center button
	Access :		Protected
					
	Return :		void
	Parameters :	none

	Usage :			Called from MFC.

   ============================================================*/
{

	DoCenterAlign();

}

void CRulerRichEditCtrl::OnButtonRightAlign() 
/* ============================================================
	Function :		CRulerRichEditCtrl::OnButtonRightAlign
	Description :	Button handler for the Right-aligned button
	Access :		Protected
					
	Return :		void
	Parameters :	none

	Usage :			Called from MFC.

   ============================================================*/
{

	DoRightAlign();

}

void CRulerRichEditCtrl::OnButtonIndent() 
/* ============================================================
	Function :		CRulerRichEditCtrl::OnButtonIndent
	Description :	Button handler for the Indent button
	Access :		Protected
					
	Return :		void
	Parameters :	none

	Usage :			Called from MFC.

   ============================================================*/
{

	DoIndent();

}

void CRulerRichEditCtrl::OnButtonOutdent() 
/* ============================================================
	Function :		CRulerRichEditCtrl::OnButtonOutdent
	Description :	Button handler for the outdent button
	Access :		Protected
					
	Return :		void
	Parameters :	none

	Usage :			Called from MFC.

   ============================================================*/
{

	DoOutdent();

}

void CRulerRichEditCtrl::OnButtonBullet() 
/* ============================================================
	Function :		CRulerRichEditCtrl::OnButtonBullet
	Description :	Button handler for the Bullet button
	Access :		Protected
					
	Return :		void
	Parameters :	none

	Usage :			Called from MFC.

   ============================================================*/
{

	DoBullet();

}

/////////////////////////////////////////////////////////////////////////////
// CRulerRichEditCtrl private helpers

void CRulerRichEditCtrl::SetTabStops(LPLONG tabs, int size)
/* ============================================================
	Function :		CRulerRichEditCtrl::SetTabStops
	Description :	Set the tab stops in the internal tab stop 
					list from the RTF-control, converting the 
					twip values to physical pixels.
	Access :		Private
					
	Return :		void
	Parameters :	LPLONG tabs	-	A pointer to an array of 
									"LONG" twip values.
					int size	-	The size of "tabs"
					
	Usage :			Call to set the tab list.

   ============================================================*/
{

	m_tabs.RemoveAll();

	double twip = (double)m_physicalInch / 1440;
	for(int t = 0 ; t < size ; t++)
	{
		// Convert from twips to pixels
		int tabpos = *(tabs + t);
		tabpos = (int) ((double) tabpos * twip +.5);
		m_tabs.Add(tabpos);

	}

	m_ruler.SetTabStops(m_tabs);
}

void CRulerRichEditCtrl::UpdateTabStops()
/* ============================================================
	Function :		CRulerRichEditCtrl::UpdateTabStops
	Description :	Sets the tabs in the internal tab stop 
					list, converting the twip physical (pixel) 
					position to twip values.
	Access :		Private
					
	Return :		void
	Parameters :	none

	Usage :			Call to refresh the tab list from the RTF-
					control. Called from the "OnPaint" handler.

   ============================================================*/
{

	ParaFormat para(PFM_TABSTOPS);
	m_rtf.GetParaFormat(para);
	SetTabStops((LPLONG)(para.rgxTabs), MAX_TAB_STOPS);

}

LRESULT CRulerRichEditCtrl::OnUpdateToolbar(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
	if (!m_toolbar.GetDroppedState())
		UpdateToolbarButtons();

	return 0L;
}

void CRulerRichEditCtrl::UpdateToolbarButtons()
/* ============================================================
	Function :		CRulerRichEditCtrl::UpdateToolbarButtons
	Description :	Updates the toolbar button, by getting 
					formatting information from the currently 
					selected text in the embedded RTF-control.
	Access :		Private
					
	Return :		void
	Parameters :	none

	Usage :			Call as the selection changes in the 
					RTF-control

   ============================================================*/
{
	if (m_showToolbar && m_toolbar.m_hWnd)
	{
		CharFormat	cf;
		cf.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_COLOR | CFM_BACKCOLOR | CFM_STRIKEOUT;

		m_rtf.SendMessage(EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);

		ParaFormat para(PFM_ALIGNMENT | PFM_NUMBERING);
		m_rtf.GetParaFormat(para);

		// Style
		m_toolbar.CheckButton(BUTTON_BOLD, (cf.dwEffects & CFE_BOLD));
		m_toolbar.CheckButton(BUTTON_ITALIC, (cf.dwEffects & CFE_ITALIC));
		m_toolbar.CheckButton(BUTTON_UNDERLINE, (cf.dwEffects & CFM_UNDERLINE));
		m_toolbar.CheckButton(BUTTON_STRIKETHRU, (cf.dwEffects & CFM_STRIKEOUT));

		m_toolbar.CheckButton(BUTTON_LEFTALIGN, (para.wAlignment == PFA_LEFT));
		m_toolbar.CheckButton(BUTTON_CENTERALIGN, (para.wAlignment == PFA_CENTER));
		m_toolbar.CheckButton(BUTTON_RIGHTALIGN, (para.wAlignment == PFA_RIGHT));

		m_toolbar.CheckButton(BUTTON_BULLET, para.wNumbering);
		m_toolbar.CheckButton(BUTTON_FONT, 0); // just to refresh state
		m_toolbar.CheckButton(BUTTON_INDENT, 0); // just to refresh state
		m_toolbar.CheckButton(BUTTON_OUTDENT, 0); // just to refresh state
		m_toolbar.CheckButton(BUTTON_TEXTCOLOR, 0); // just to refresh state
		m_toolbar.CheckButton(BUTTON_BACKCOLOR, 0); // just to refresh state

		if (cf.dwMask & CFM_FACE)
			m_toolbar.SetFontName(cf.szFaceName);

		if (cf.dwMask & CFM_SIZE)
			m_toolbar.SetFontSize(cf.yHeight / 20);
	}
}

void CRulerRichEditCtrl::SetEffect(int mask, int effect)
/* ============================================================
	Function :		CRulerRichEditCtrl::SetEffect
	Description :	Sets the effect (bold, italic and/or 
					underline) for the currently selected text 
					in the embedded RTF-control.
	Access :		Private
					
	Return :		void
	Parameters :	int mask	-	What effects are valid. See 
									the documentation for 
									"CHARFORMAT".
					int effect	-	What effects to set. See the 
									documentation for "CHARFORMAT".
					
	Usage :			Called internally from button handlers

   ============================================================*/
{

	CharFormat cf;
	cf.dwMask = mask;
	cf.dwEffects = effect;

	m_rtf.SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
	m_rtf.SetFocus();

}

void CRulerRichEditCtrl::SetAlignment(int alignment)
/* ============================================================
	Function :		CRulerRichEditCtrl::SetAlignment
	Description :	Sets the alignment for the currently 
					selected text in the embedded RTF-control.
	Access :		Private
					
	Return :		void
	Parameters :	int alignment	-	Alignment to set. See
										documentation for
										"PARAFORMAT"
					
	Usage :			Called internally from button handlers

   ============================================================*/
{

	ParaFormat	para(PFM_ALIGNMENT);
	para.wAlignment = (WORD) alignment;

	m_rtf.SetParaFormat(para);
	UpdateToolbarButtons();
	m_rtf.SetFocus();

}

// Virtual interface
void CRulerRichEditCtrl::DoFont()
/* ============================================================
	Function :		CRulerRichEditCtrl::DoFont
	Description :	Externally accessible member to set the
					font of the control
	Access :		Public
					
	Return :		void
	Parameters :	none
					
	Usage :			Call to set the font of the selected text.

   ============================================================*/
{

	// Get the current font
	LOGFONT	lf;
	ZeroMemory(&lf, sizeof(LOGFONT));
	CharFormat	cf;
	m_rtf.SendMessage(EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
	int height;

	// Creating a LOGFONT from the current font settings

	// Font
	if (cf.dwMask & CFM_FACE)
		lstrcpy(lf.lfFaceName, cf.szFaceName);

	if (cf.dwMask & CFM_SIZE)
	{
		double twip = (double)m_physicalInch / 1440;
		height = cf.yHeight;
		height = -(int) ((double) height * twip +.5);
		lf.lfHeight = height;

	}

	// Effects
	if (cf.dwMask & CFM_BOLD)
	{
		if (cf.dwEffects & CFE_BOLD)
			lf.lfWeight = FW_BOLD;
		else
			lf.lfWeight = FW_NORMAL;
	}

	if ((cf.dwMask & CFM_ITALIC) && (cf.dwEffects & CFE_ITALIC))
		lf.lfItalic = TRUE;

	if ((cf.dwMask & CFM_UNDERLINE) && (cf.dwEffects & CFE_UNDERLINE))
		lf.lfUnderline = TRUE;

	if ((cf.dwMask & CFM_STRIKEOUT) && (cf.dwEffects & CFE_STRIKEOUT))
		lf.lfStrikeOut = TRUE;

	// Show font dialog
	CFontDialog	dlg(&lf);

	// color
	dlg.m_cf.rgbColors = cf.crTextColor;

	if (dlg.DoModal() == IDOK)
	{
		// Apply new font
		cf.yHeight = dlg.GetSize() * 2;
		lstrcpy(cf.szFaceName, dlg.GetFaceName());

		cf.dwMask = CFM_FACE | CFM_SIZE | CFM_COLOR | CFM_BOLD | 
					CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT;
		cf.dwEffects = 0;
		cf.crTextColor = dlg.GetColor();

		if (dlg.IsBold())
			cf.dwEffects |= CFE_BOLD;

		if (dlg.IsItalic())
			cf.dwEffects |= CFE_ITALIC;

		if (dlg.IsUnderline())
			cf.dwEffects |= CFE_UNDERLINE;

		if (dlg.IsStrikeOut())
			cf.dwEffects |= CFE_STRIKEOUT;

		m_rtf.SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
/*
		m_toolbar.SetFontColor(cf.crTextColor, TRUE);
*/
	}

	m_rtf.SetFocus();

}

void CRulerRichEditCtrl::SetCurrentFontName(const CString& font)
/* ============================================================
	Function :		CRulerRichEditCtrl::SetCurrentFontName
	Description :	Changes the font of the selected text in 
					the editor to "font".
	Access :		Public
					
	Return :		void
	Parameters :	const CString& font	-	Font name of font 
											to change to.

	Usage :			Call to set the font of the selected text 
					in the editor.

   ============================================================*/
{
	CharFormat	cf;
	cf.dwMask = CFM_FACE;

	lstrcpy(cf.szFaceName, font);

	m_rtf.SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
}

void CRulerRichEditCtrl::SetCurrentFontSize(int size)
/* ============================================================
	Function :		CRulerRichEditCtrl::SetCurrentFontSize
	Description :	Changes the size of the selected text in 
					the editor to "size" (measured in 
					typographical points).
	Access :		Public
					
	Return :		void
	Parameters :	int size	-	New size in typographical 
									points

	Usage :			Call to change the size of the selected 
					text.

   ============================================================*/
{
	CharFormat	cf;
	cf.dwMask = CFM_SIZE;
	cf.yHeight = size * 20;

	m_rtf.SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
}

void CRulerRichEditCtrl::SetCurrentFontColor(COLORREF color, BOOL bForeground)
/* ============================================================
	Function :		CRulerRichEditCtrl::SetCurrentFontSize
	Description :	Changes the color of the selected text in 
					the editor to "color".
	Access :		Public
					
	Return :		void
	Parameters :	COLORREF color	-	New color

	Usage :			Call to change the color of the selected 
					text.

   ============================================================*/
{
	CharFormat	cf;
	cf.dwMask = bForeground ? CFM_COLOR : CFM_BACKCOLOR;

	if (bForeground)
	{
		if (color == CLR_DEFAULT)
			cf.dwEffects = CFE_AUTOCOLOR;
		else
			cf.crTextColor = color;
	}
	else // background
	{
		if (color == CLR_DEFAULT)
			cf.dwEffects = CFE_AUTOBACKCOLOR;
		else
			cf.crBackColor = color;
	}

	m_rtf.SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);

}

LRESULT CRulerRichEditCtrl::OnSetCurrentFontName(WPARAM font, LPARAM)
/* ============================================================
	Function :		CRulerRichEditCtrl::OnSetCurrentFontName
	Description :	Handler for the registered message 
					"urm_SETCURRENTFONTNAME", called when the 
					font name is changed from the toolbar.
	Access :		Protected

	Return :		LRESULT		-	Not used
	Parameters :	WPARAM font	-	Pointer to the new font name
					LPARAM		-	Not used

	Usage :			Called from MFC

   ============================================================*/
{

	CString fnt((LPCTSTR) font);
	SetCurrentFontName(fnt);

	return 0;
	
}

LRESULT CRulerRichEditCtrl::OnSetCurrentFontSize(WPARAM, LPARAM size)
/* ============================================================
	Function :		CRulerRichEditCtrl::OnSetCurrentFontSize
	Description :	Handler for the registered message 
					"urm_SETCURRENTFONTSIZE", called when the 
					font size is changed from the toolbar.
	Access :		Protected

	Return :		LRESULT		-	Not used
	Parameters :	WPARAM		-	Not used
					LPARAM size	-	New font size in typographical 
									points of the selected text

	Usage :			Called from MFC

   ============================================================*/
{

	SetCurrentFontSize(size);
	return 0;
	
}

LRESULT CRulerRichEditCtrl::OnSetCurrentFontColor(WPARAM bForeground, LPARAM color)
/* ============================================================
	Function :		CRulerRichEditCtrl::OnSetCurrentFontColor
	Description :	Handler for the registered message 
					"urm_SETCURRENTFONTCOLOR", called when the 
					font color is changed from the toolbar.
	Access :		Protected

	Return :		LRESULT		-	Not used
	Parameters :	WPARAM		-	Not used
					LPARAM		-	New color of the selected 
									text

	Usage :			Called from MFC

   ============================================================*/
{

⌨️ 快捷键说明

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