rulerricheditctrl.cpp
来自「管理项目进度工具的原代码」· C++ 代码 · 共 2,215 行 · 第 1/4 页
CPP
2,215 行
void CRulerRichEditCtrl::OnSetFocus(CWnd* pOldWnd)
/* ============================================================
Function : CRulerRichEditCtrl::OnSetFocus
Description : We handle over the focus to the embedded
RTF-control.
Access : Protected
Return : void
Parameters : CWnd* pOldWnd - Not used
Usage : Called from MFC.
============================================================*/
{
CWnd::OnSetFocus(pOldWnd);
m_rtf.SetFocus();
}
LRESULT CRulerRichEditCtrl::OnGetScrollPos(WPARAM, LPARAM)
/* ============================================================
Function : CRulerRichEditCtrl::OnGetScrollPos
Description : The function handles the registered message
"urm_GETSCROLLPOS", that is sent from the
ruler to get the current scroll position
of the embedded RTF-control.
Access : Protected
Return : LRESULT - Current scroll pos
Parameters : WPARAM mode - Not used
LPARAM pt - Not used
Usage : Called from MFC
============================================================*/
{
return m_rtf.GetScrollPos(SB_HORZ);
}
LRESULT CRulerRichEditCtrl::OnTrackRuler(WPARAM mode, LPARAM pt)
/* ============================================================
Function : CRulerRichEditCtrl::OnTrackRuler
Description : The function handles the registered message
"urm_RULERACTION", that is sent from the
mouse handling mappings in the ruler control.
The function handles dragging of tabulator
points in the ruler.
Access : Protected
Return : LRESULT - Not used
Parameters : WPARAM mode - The type of mouse operation,
"DOWN", "MOVE" or "UP"
LPARAM pt - Cursor point for the cursor.
Usage : Called from MFC.
============================================================*/
{
CPoint* point = (CPoint*) pt;
int toolbarHeight = 0;
if (m_showToolbar)
toolbarHeight = TOOLBAR_HEIGHT;
switch(mode)
{
case DOWN:
// The left mouse button is clicked
{
// Check if we clicked on a tab-marker.
int pos = m_rtf.GetScrollPos(SB_HORZ);
m_movingtab = -1;
CRect hitRect;
int y = RULER_HEIGHT - 9;
for(int t = 0 ; t < MAX_TAB_STOPS ; t++)
{
int x = m_tabs[ t ] + m_margin - pos;
hitRect.SetRect(x - 2, y - 1, x + 3, y + 3);
if (hitRect.PtInRect(*point))
{
// Yes, we did.
m_movingtab = t;
// Calc offset, as the hit area is wider than
// the 1 pixel tab line
m_offset = point->x - (m_tabs[ t ] + m_margin - pos);
}
}
if (m_movingtab != -1)
{
// We did click in a tab marker.
// Start dragging
// Find initial ruler position
m_rulerPosition = point->x - m_offset;
CRect rect;
GetClientRect(rect);
// Draw a new ruler line
CClientDC dc(this);
dc.SelectObject(&m_pen);
dc.SetROP2(R2_XORPEN);
dc.MoveTo(m_rulerPosition, toolbarHeight + 3);
dc.LineTo(m_rulerPosition, rect.Height());
dc.SelectStockObject(BLACK_PEN);
}
}
break;
case MOVE:
// The mouse is moved
{
if (m_movingtab != -1)
{
CRect rect;
GetClientRect(rect);
CClientDC dc(this);
// Erase previous line
dc.SelectObject(&m_pen);
dc.SetROP2(R2_XORPEN);
dc.MoveTo(m_rulerPosition, toolbarHeight + 3);
dc.LineTo(m_rulerPosition, rect.Height());
// Set up new line
// Calc min and max. We can not place this marker
// before the previous or after the next. Neither
// can we move the marker outside the ruler.
int pos = m_rtf.GetScrollPos(SB_HORZ);
int min = m_margin + m_offset;
if (m_movingtab > 0)
min = (m_tabs[ m_movingtab - 1 ] + m_margin - pos) + 3 + m_offset;
int max = rect.Width() - 5 + m_offset;
if (m_movingtab < m_tabs.GetUpperBound())
max = (m_tabs[ m_movingtab + 1 ] + m_margin - pos) - 3 + m_offset;
max = min(max, rect.Width() - 5 + m_offset);
// Set new positions
m_rulerPosition = max(min, point->x - m_offset);
m_rulerPosition = min(m_rulerPosition, max);
// Draw the new line
dc.MoveTo(m_rulerPosition, toolbarHeight + 3);
dc.LineTo(m_rulerPosition, rect.Height());
dc.SelectStockObject(BLACK_PEN);
}
}
break;
case UP:
// The mouse button is released
{
if (m_movingtab != -1)
{
// Set new value for tab position
int pos = m_rtf.GetScrollPos(SB_HORZ);
m_tabs[ m_movingtab ] = m_rulerPosition - m_margin + pos - m_offset;
// Get the current tabstops, as we
// must set all tabs in one operation
ParaFormat para(PFM_TABSTOPS);
para.cTabCount = MAX_TAB_STOPS;
m_rtf.GetParaFormat(para);
// Convert current position to twips
double twip = (double)m_physicalInch / 1440;
int tabpos = m_tabs[ m_movingtab ];
tabpos = (int) ((double) tabpos / twip +.5);
para.rgxTabs[ m_movingtab ] = tabpos;
// Set tabs to control
m_rtf.SetParaFormat(para);
// Erase the ruler
Invalidate();
RedrawWindow();
m_movingtab = -1;
m_rtf.SetFocus();
}
}
break;
}
return 0;
}
LRESULT CRulerRichEditCtrl::OnSetText(WPARAM wParam, LPARAM lParam)
/* ============================================================
Function : CRulerRichEditCtrl::OnSetText
Description : The function handles the "WM_SETTEXT"
message. The handler sets the text in the
RTF-control
Access : Protected
Return : LRESULT - From the control
Parameters : WPARAM wParam - Passed on
LPARAM lParam - Passed on
Usage : Called from MFC.
============================================================*/
{
return m_rtf.SendMessage(WM_SETTEXT, wParam, lParam);
}
LRESULT CRulerRichEditCtrl::OnGetText(WPARAM wParam, LPARAM lParam)
/* ============================================================
Function : CRulerRichEditCtrl::OnGetText
Description : The function handles the "WM_GETTEXT"
message. The handler gets the text from the
RTF-control
Access : Protected
Return : LRESULT - From the control
Parameters : WPARAM wParam - Passed on
LPARAM lParam - Passed on
Usage : Called from MFC.
============================================================*/
{
return m_rtf.SendMessage(WM_GETTEXT, wParam, lParam);
}
LRESULT CRulerRichEditCtrl::OnGetTextLength(WPARAM /*wParam*/, LPARAM /*lParam*/)
/* ============================================================
Function : CRulerRichEditCtrl::OnGetTextLength
Description : The function handles the "WM_GETTEXTLENGTH"
message. The handler gets the length of
the text in the RTF-control
Access : Protected
Return : LRESULT - From the control
Parameters : WPARAM wParam - Passed on
LPARAM lParam - Passed on
Usage : Called from MFC.
============================================================*/
{
return m_rtf.GetTextLength();
}
/////////////////////////////////////////////////////////////////////////////
// CRulerRichEditCtrl public implementation
/* ============================================================
Function : CRulerRichEditCtrl::GetRTF
Description : Returns the contents of the control as RTF.
Access : Public
Return : CString - The RTF-contents of the control.
Parameters : none
Usage : Call this function to get a char buffer
with the contents of the embedded RTF-
control.
============================================================*/
CString CRulerRichEditCtrl::GetRTF()
{
// stream to mem file in big chunks
CMemFile file(100 * 1024);
EDITSTREAM es = { (DWORD)&file, 0, StreamOut };
m_rtf.StreamOut(SF_RTF, es);
// then copy to string
CString sRTF;
int nLen = (int)file.GetLength();
LPTSTR szRTF = sRTF.GetBuffer(nLen);
file.SeekToBegin();
file.Read((void*)szRTF, nLen);
sRTF.ReleaseBuffer(nLen);
return sRTF;
}
int CRulerRichEditCtrl::GetRTFLength()
{
int nLen = 0;
EDITSTREAM es;
es.dwCookie = (DWORD)&nLen;
es.pfnCallback = StreamOutLen;
m_rtf.StreamOut(SF_RTF, es);
return nLen;
}
/* ============================================================
Function : CRulerRichEditCtrl::SetRTF
Description : Set the contents of the embedded RTF-
control from rtf.
Access : Public
Return : void
Parameters : const CString& rtf - The rtf-contents to
set.
Usage : Call this function to set the RTF-contents
of the control.
============================================================*/
void CRulerRichEditCtrl::SetRTF(const CString& rtf)
{
BOOL bEmptyRtf = rtf.IsEmpty();
STREAMINCOOKIE cookie(rtf);
EDITSTREAM es = { (DWORD)&cookie, 0, StreamIn };
m_rtf.StreamIn(SF_RTF, es);
// reset the formatting if the new content is empty else
// the previous formatting hangs around
if (bEmptyRtf)
{
m_rtf.SetRedraw(FALSE);
m_rtf.SetSel(0, -1);
m_rtf.SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &m_cfDefault);
m_rtf.SetSel(-1, 0);
m_rtf.SetRedraw(TRUE);
}
UpdateEditRect();
}
BOOL CRulerRichEditCtrl::Save(CString& filename)
/* ============================================================
Function : CRulerRichEditCtrl::Save
Description : Saves the contents to the file filename.
If filename is empty, a file dialog will
be displayed and the selected name will be
returned in the "CString".
Access : Public
Return : BOOL - "TRUE" if the file
was saved.
Parameters : CString& filename - The file name to save
to. Can be empty.
Usage : Call to save the contents of the embedded
RTF-control do a file.
============================================================*/
{
BOOL result = TRUE;
CString* str = new CString;
EDITSTREAM es;
es.dwCookie = (DWORD) str;
es.pfnCallback = StreamOut;
m_rtf.StreamOut(SF_RTF, es);
CTextFile f(_T("rtf"));
result = f.WriteTextFile(filename, *str);
delete str;
return result;
}
BOOL CRulerRichEditCtrl::Load(CString& filename)
/* ============================================================
Function : CRulerRichEditCtrl::Load
Description : Loads the embedded RTF-control with the
contents from the file filename.
If filename is empty, a file dialog will
be displayed and the selected name will be
returned in the "CString".
Access : Public
Return : BOOL - "TRUE" if the file
was loaded.
Parameters : CString& filename - File name to load
from. Can be empty.
Usage : Call to load an RTF-file to the control.
============================================================*/
{
BOOL result = TRUE;
CString* str = new CString;
CTextFile f(_T("rtf"));
result = f.ReadTextFile(filename, *str);
if (result)
{
EDITSTREAM es;
es.dwCookie = (DWORD) str;
es.pfnCallback = StreamIn;
m_rtf.StreamIn(SF_RTF, es);
}
delete str;
return result;
}
void CRulerRichEditCtrl::SetMode(int mode)
/* ============================================================
Function : CRulerRichEditCtrl::SetMode
Description : Sets the internal mode, that is, if the
ruler should display inches or centimeters.
Access : Public
Return : void
Parameters : int mode - Mode to use, "MODE_INCH" or
"MODE_METRIC" (default)
Usage : Call to change the mode.
============================================================*/
{
m_ruler.SetMode(mode);
}
int CRulerRichEditCtrl::GetMode() const
/* ============================================================
Function : CRulerRichEditCtrl::GetMode
Description : Gets the mode, that is, either "MODE_INCH" or
"MODE_METRIC", that is used to draw the ruler.
Access : Public
Return : int - The mode, either "MODE_INCH" or
"MODE_METRIC"
Parameters : none
Usage : Call to get the current mode.
============================================================*/
{
return m_ruler.GetMode();
}
CRulerRichEdit& CRulerRichEditCtrl::GetRichEditCtrl()
/* ============================================================
Function : CRulerRichEditCtrl::GetRichEditCtrl
Description : Returns an alias to the embedded RTF-control.
Access : Public
Return : CRichEditCtrl& - An alias to the rtf-control
Parameters : none
Usage : Call to access the RTF-control directly.
============================================================*/
{
return m_rtf;
}
/////////////////////////////////////////////////////////////////////////////
// CRulerRichEditCtrl toolbar button handlers
void CRulerRichEditCtrl::OnButtonFont()
/* ============================================================
Function : CRulerRichEditCtrl::OnButtonFont
Description : Button handler for the Font button
Access : Protected
Return : void
Parameters : none
Usage : Called from MFC
============================================================*/
{
DoFont();
}
void CRulerRichEditCtrl::OnButtonColor()
/* ============================================================
Function : CRulerRichEditCtrl::OnButtonColor
Description : Button handler for the Color button
Access : Protected
Return : void
Parameters : none
Usage : Called from MFC.
============================================================*/
{
DoColor();
}
void CRulerRichEditCtrl::OnButtonBold()
/* ============================================================
Function : CRulerRichEditCtrl::OnButtonBold
Description : Button handler for the Bold button
Access : Protected
Return : void
Parameters : none
Usage : Called from MFC.
============================================================*/
{
DoBold();
}
void CRulerRichEditCtrl::OnButtonStrikethrough()
{
DoStrikethrough();
}
void CRulerRichEditCtrl::OnButtonItalic()
/* ============================================================
Function : CRulerRichEditCtrl::OnButtonItalic
Description : Button handler for the Italic button
Access : Protected
Return : void
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?