rulerricheditctrl.cpp
来自「管理项目进度工具的原代码」· C++ 代码 · 共 2,215 行 · 第 1/4 页
CPP
2,215 行
SetCurrentFontColor((COLORREF) color, bForeground);
m_rtf.SetFocus();
return 0;
}
void CRulerRichEditCtrl::OnButtonTextColor()
{
SetCurrentFontColor(m_toolbar.GetFontColor(TRUE), TRUE);
}
void CRulerRichEditCtrl::OnButtonBackColor()
{
SetCurrentFontColor(m_toolbar.GetFontColor(FALSE), FALSE);
}
void CRulerRichEditCtrl::DoColor()
/* ============================================================
Function : CRulerRichEditCtrl::DoColor
Description : Externally accessible member to set the
color of the selected text
Access : Public
Return : void
Parameters : none
Usage : Call to set the color of the selected text.
============================================================*/
{
// Get the current color
COLORREF clr(RGB(0, 0, 0));
CharFormat cf;
m_rtf.SendMessage(EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
if (cf.dwMask & CFM_COLOR)
clr = cf.crTextColor;
// Display color selection dialog
CColorDialog dlg(clr);
if (dlg.DoModal() == IDOK)
{
// Apply new color
cf.dwMask = CFM_COLOR;
cf.dwEffects = 0;
cf.crTextColor = dlg.GetColor();
m_rtf.SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
}
m_rtf.SetFocus();
}
void CRulerRichEditCtrl::DoBold()
/* ============================================================
Function : CRulerRichEditCtrl::DoBold
Description : Externally accessible member to set/unset
the selected text to/from bold
Access : Public
Return : void
Parameters : none
Usage : Call to toggle the selected text to/from
bold.
============================================================*/
{
m_toolbar.GetToolBarCtrl().CheckButton(BUTTON_BOLD, !m_toolbar.IsButtonChecked(BUTTON_BOLD));
int effect = 0;
if (m_toolbar.IsButtonChecked(BUTTON_BOLD))
effect = CFE_BOLD;
SetEffect(CFM_BOLD, effect);
}
void CRulerRichEditCtrl::DoStrikethrough()
{
m_toolbar.GetToolBarCtrl().CheckButton(BUTTON_STRIKETHRU, !m_toolbar.IsButtonChecked(BUTTON_STRIKETHRU));
int effect = 0;
if (m_toolbar.IsButtonChecked(BUTTON_STRIKETHRU))
effect = CFE_STRIKEOUT;
SetEffect(CFM_STRIKEOUT, effect);
}
void CRulerRichEditCtrl::DoSuperscript()
{
CharFormat cf;
m_rtf.SendMessage(EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
BOOL bSuperscript = (cf.dwEffects & CFE_SUPERSCRIPT);
SetEffect(CFM_SUPERSCRIPT, bSuperscript ? 0 : CFE_SUPERSCRIPT);
}
void CRulerRichEditCtrl::DoSubscript()
{
CharFormat cf;
m_rtf.SendMessage(EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
BOOL bSubscript = (cf.dwEffects & CFE_SUBSCRIPT);
SetEffect(CFM_SUBSCRIPT, bSubscript ? 0 : CFE_SUBSCRIPT);
}
void CRulerRichEditCtrl::DoItalic()
/* ============================================================
Function : CRulerRichEditCtrl::DoItalic
Description : Externally accessible member to set/unset
the selected text to/from italic
Access : Public
Return : void
Parameters : none
Usage : Call to toggle the selected text to/from
italic.
============================================================*/
{
m_toolbar.GetToolBarCtrl().CheckButton(BUTTON_ITALIC, !m_toolbar.IsButtonChecked(BUTTON_ITALIC));
int effect = 0;
if (m_toolbar.IsButtonChecked(BUTTON_ITALIC))
effect = CFE_ITALIC;
SetEffect(CFM_ITALIC, effect);
}
void CRulerRichEditCtrl::DoUnderline()
/* ============================================================
Function : CRulerRichEditCtrl::DoUnderline
Description : Externally accessible member to set/unset
the selected text to/from underline
Access : Public
Return : void
Parameters : none
Usage : Call to toggle the selected text to/from
underlined.
============================================================*/
{
m_toolbar.CheckButton(BUTTON_UNDERLINE, !m_toolbar.IsButtonChecked(BUTTON_UNDERLINE));
int effect = 0;
if (m_toolbar.IsButtonChecked(BUTTON_UNDERLINE))
effect = CFE_UNDERLINE;
SetEffect(CFM_UNDERLINE, effect);
}
void CRulerRichEditCtrl::DoLeftAlign()
/* ============================================================
Function : CRulerRichEditCtrl::DoLeftAlign
Description : Externally accessible member to set the
selected text to left aligned.
Access : Public
Return : void
Parameters : none
Usage : Call to left-align the selected text
============================================================*/
{
if (!m_toolbar.IsButtonChecked(BUTTON_LEFTALIGN))
SetAlignment(PFA_LEFT);
}
void CRulerRichEditCtrl::DoCenterAlign()
/* ============================================================
Function : CRulerRichEditCtrl::DoCenterAlign
Description : Externally accessible member to set the
selected text to center aligned
Access : Public
Return : void
Parameters : none
Usage : Call to center-align the selected text
============================================================*/
{
if (!m_toolbar.IsButtonChecked(BUTTON_CENTERALIGN))
SetAlignment(PFA_CENTER);
}
void CRulerRichEditCtrl::DoRightAlign()
/* ============================================================
Function : CRulerRichEditCtrl::DoRightAlign
Description : Externally accessible member to set the
selected text to right aligned
Access : Public
Return : void
Parameters : none
Usage : Call to right-align the selected text
============================================================*/
{
if (!m_toolbar.IsButtonChecked(BUTTON_RIGHTALIGN))
SetAlignment(PFA_RIGHT);
}
void CRulerRichEditCtrl::DoIndent()
/* ============================================================
Function : CRulerRichEditCtrl::DoIndent
Description : Externally accessible member to indent the
selected text to the next tab position
Access : Public
Return : void
Parameters : none
Usage : Call to indent the selected text
============================================================*/
{
// Get current indent
ParaFormat para(PFM_STARTINDENT | PFM_TABSTOPS);
m_rtf.GetParaFormat(para);
int newindent = para.dxStartIndent;
// Find next larger tab
for(int t = MAX_TAB_STOPS - 1 ; t >= 0 ; t--)
{
if (para.rgxTabs[ t ] > para.dxStartIndent)
newindent = para.rgxTabs[ t ];
}
if (newindent != para.dxStartIndent)
{
// Set indent to this value
para.dwMask = PFM_STARTINDENT | PFM_OFFSET;
para.dxStartIndent = newindent;
// para.dxOffset = 10;
m_rtf.SetParaFormat(para);
}
m_rtf.SetFocus();
}
void CRulerRichEditCtrl::DoOutdent()
/* ============================================================
Function : CRulerRichEditCtrl::DoOutdent
Description : Externally accessible member to outdent the
selected text to the previous tab position
Access : Public
Return : void
Parameters : none
Usage : Call to outdent the selected text
============================================================*/
{
// Get the current indent, if any
ParaFormat para(PFM_STARTINDENT | PFM_TABSTOPS);
m_rtf.GetParaFormat(para);
int newindent = 0;
// Find closest smaller tab
for(int t = 0 ; t < MAX_TAB_STOPS ; t++)
if (para.rgxTabs[ t ] < para.dxStartIndent)
newindent = para.rgxTabs[ t ];
// Set indent to this value or 0 if none
para.dwMask = PFM_STARTINDENT | PFM_OFFSET;
para.dxStartIndent = newindent;
// para.dxOffset = newindent;
m_rtf.SetParaFormat(para);
m_rtf.SetFocus();
}
void CRulerRichEditCtrl::DoBullet()
/* ============================================================
Function : CRulerRichEditCtrl::DoBullet
Description : Externally accessible member to set the
selected text to bulleted
Access : Public
Return : void
Parameters : none
Usage : Call to set the selected text to bulleted.
============================================================*/
{
m_toolbar.GetToolBarCtrl().CheckButton(BUTTON_BULLET, !m_toolbar.IsButtonChecked(BUTTON_BULLET));
ParaFormat para(PFM_NUMBERING);
if (m_toolbar.IsButtonChecked(BUTTON_BULLET))
para.wNumbering = PFN_BULLET;
else
para.wNumbering = 0;
m_rtf.SetParaFormat(para);
m_rtf.SetFocus();
}
void CRulerRichEditCtrl::ShowToolbar(BOOL show)
/* ============================================================
Function : CRulerRichEditCtrl::ShowToolbar
Description : Shows or hides the toolbar
Access : Public
Return : void
Parameters : BOOL show - "TRUE" to show
Usage : Call to show or hide the toolbar subcontrol
============================================================*/
{
m_showToolbar = show;
if (m_hWnd)
{
if (show)
m_toolbar.GetToolBarCtrl().ShowWindow(SW_SHOW);
else
m_toolbar.GetToolBarCtrl().ShowWindow(SW_HIDE);
CRect rect;
GetClientRect(rect);
LayoutControls(rect.Width(), rect.Height());
}
}
void CRulerRichEditCtrl::SetWordWrap(BOOL bWrap)
{
if (bWrap != m_bWordWrap)
m_rtf.SetTargetDevice(NULL, bWrap ? 0 : 1);
m_bWordWrap = bWrap;
}
void CRulerRichEditCtrl::ShowRuler(BOOL show)
/* ============================================================
Function : CRulerRichEditCtrl::ShowRuler
Description : Shows or hides the ruler
Access : Public
Return : void
Parameters : BOOL show - "TRUE" to show
Usage : Call to show or hide the ruler subcontrol
============================================================*/
{
m_showRuler = show;
if (m_hWnd)
{
if (show)
m_ruler.ShowWindow(SW_SHOW);
else
m_ruler.ShowWindow(SW_HIDE);
CRect rect;
GetClientRect(rect);
LayoutControls(rect.Width(), rect.Height());
}
}
void CRulerRichEditCtrl::LayoutControls(int width, int height)
/* ============================================================
Function : CRulerRichEditCtrl::LayoutControls
Description : Lays out the sub-controls depending on
visibility.
Access : Private
Return : void
Parameters : int width - Width of control
int height - Height of control
Usage : Called internally to lay out the controls
============================================================*/
{
if (!width || !height)
return;
// TRACE("LayoutControls(w = %d, h = %d)\n", width, height);
int toolbarHeight = 0;
if (m_showToolbar)
{
// would prefer to use CToolbar::CalcDynamicLayout but there
// seems to be a 'first-time' bug in it so we use CalcFixedLayout
// and resize the toolbar twice because this seems to fix a
// bug in the calculation of the toolbar size.
toolbarHeight = TOOLBAR_HEIGHT;
m_toolbar.GetToolBarCtrl().MoveWindow(0, 0, width, toolbarHeight);
CSize sizeTB = m_toolbar.CalcFixedLayout(FALSE, TRUE);
toolbarHeight = sizeTB.cy;
m_toolbar.GetToolBarCtrl().MoveWindow(0, 0, width, toolbarHeight);
/*
toolbarHeight = m_toolbar.CalcDynamicLayout(width, LM_HORZ | LM_STRETCH).cy;
m_toolbar.GetToolBarCtrl().MoveWindow(0, 0, width, toolbarHeight);
*/
}
int rulerHeight = 0;
if (m_showRuler)
rulerHeight = RULER_HEIGHT;
m_ruler.MoveWindow(0, toolbarHeight, width, rulerHeight);
int top = toolbarHeight + rulerHeight;
CRect rect(0, top, width, height);
m_rtf.MoveWindow(rect);
}
BOOL CRulerRichEditCtrl::IsToolbarVisible() const
/* ============================================================
Function : CRulerRichEditCtrl::IsToolbarVisible
Description : Returns if the toolbar is visible or not
Access : Public
Return : BOOL - "TRUE" if visible
Parameters : none
Usage : Call to get the visibility of the toolbar
============================================================*/
{
return m_showToolbar;
}
BOOL CRulerRichEditCtrl::IsRulerVisible() const
/* ============================================================
Function : CRulerRichEditCtrl::IsRulerVisible
Description : Returns if the ruler is visible or not
Access : Public
Return : BOOL - "TRUE" if visible
Parameters : none
Usage : Call to get the visibility of the ruler
============================================================*/
{
return m_showRuler;
}
void CRulerRichEditCtrl::SetReadOnly(BOOL readOnly)
/* ============================================================
Function : CRulerRichEditCtrl::SetReadOnly
Description : Sets the control to read only or not.
Access : Public
Return : void
Parameters : BOOL readOnly - New read only state
Usage : Call to set the read only state of the
control
============================================================*/
{
if (m_rtf.m_hWnd)
{
m_rtf.SetReadOnly(readOnly);
m_rtf.SetBackgroundColor(!readOnly, GetSysColor(COLOR_3DFACE));
m_toolbar.EnableWindow(!readOnly);
m_ruler.EnableWindow(!readOnly);
UpdateToolbarButtons();
}
m_readOnly = readOnly;
}
BOOL CRulerRichEditCtrl::GetReadOnly() const
/* ============================================================
Function : CRulerRichEditCtrl::GetReadOnly
Description : Returns if the control is read only or not
Access : Public
Return : BOOL - "TRUE" if read only
Parameters : none
Usage : Call to get the read only-state of the
control
============================================================*/
{
return m_readOnly;
}
void CRulerRichEditCtrl::OnEnable(BOOL bEnable)
{
CWnd::OnEnable(bEnable);
// update children
m_rtf.EnableWindow(bEnable);
m_toolbar.EnableWindow(bEnable && !m_readOnly);
m_ruler.EnableWindow(bEnable && !m_readOnly);
UpdateToolbarButtons();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?