📄 richview.cpp
字号:
// RichView.cpp : implementation of the CRichView class
//
#include "stdafx.h"
#include "Rich.h"
#include "RichDoc.h"
#include "CntrItem.h"
#include "RichView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRichView
IMPLEMENT_DYNCREATE(CRichView, CRichEditView)
BEGIN_MESSAGE_MAP(CRichView, CRichEditView)
//{{AFX_MSG_MAP(CRichView)
ON_WM_DESTROY()
ON_COMMAND(ID_FONT_SELECT, OnFontSelect)
ON_COMMAND(ID_CHAR_BOLD, OnCharBold)
ON_UPDATE_COMMAND_UI(ID_CHAR_BOLD, OnUpdateCharBold)
ON_COMMAND(ID_CHAR_ITALIC, OnCharItalic)
ON_UPDATE_COMMAND_UI(ID_CHAR_ITALIC, OnUpdateCharItalic)
ON_COMMAND(ID_CHAR_STRIKEOUT, OnCharStrikeout)
ON_UPDATE_COMMAND_UI(ID_CHAR_STRIKEOUT, OnUpdateCharStrikeout)
ON_COMMAND(ID_CHAR_UNDERLINE, OnCharUnderline)
ON_UPDATE_COMMAND_UI(ID_CHAR_UNDERLINE, OnUpdateCharUnderline)
ON_COMMAND(ID_FONT_COLOR, OnFontColor)
ON_COMMAND(ID_FONT_BACKGROUND, OnFontBackground)
ON_COMMAND(ID_PARA_LEFT, OnParaLeft)
ON_UPDATE_COMMAND_UI(ID_PARA_LEFT, OnUpdateParaLeft)
ON_COMMAND(ID_PARA_RIGHT, OnParaRight)
ON_UPDATE_COMMAND_UI(ID_PARA_RIGHT, OnUpdateParaRight)
ON_COMMAND(ID_PARA_CENTER, OnParaCenter)
ON_UPDATE_COMMAND_UI(ID_PARA_CENTER, OnUpdateParaCenter)
ON_COMMAND(ID_PARA_BULLET, OnParaBullet)
ON_UPDATE_COMMAND_UI(ID_PARA_BULLET, OnUpdateParaBullet)
ON_COMMAND(ID_PARA_INDENTMINUS, OnParaIndentminus)
ON_COMMAND(ID_PARA_INDENTPLUS, OnParaIndentplus)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CRichEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CRichEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRichEditView::OnFilePrintPreview)
ON_CBN_SELCHANGE(IDC_FONTLIST, OnSelchangeFontlist)
ON_CBN_SELCHANGE(IDC_POINTSIZE, OnSelchangePointsize)
ON_NOTIFY_REFLECT(EN_SELCHANGE, OnSelChange)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRichView construction/destruction
CRichView::CRichView()
{
// TODO: add construction code here
}
CRichView::~CRichView()
{
}
BOOL CRichView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.dwExStyle &= ~WS_EX_ACCEPTFILES;
return CRichEditView::PreCreateWindow(cs);
}
void CRichView::OnInitialUpdate()
{
CRichEditView::OnInitialUpdate();
// Set the printing margins (720 twips = 1/2 inch).
SetMargins(CRect(720, 720, 720, 720));
CHARFORMAT2 cf;
memset (&cf, '\0', sizeof (CHARFORMAT2));
cf.cbSize = sizeof (CHARFORMAT2);
GetRichEditCtrl().GetDefaultCharFormat (cf);
cf.dwMask = CFM_FACE | CFM_SIZE | CFM_UNDERLINE | CFM_COLOR | CFM_BOLD;
cf.yHeight = 12 * 20; // 12 points in twips
cf.dwEffects &= ~(CFE_AUTOCOLOR | CFE_BOLD);
cf.crTextColor = RGB(0x00, 0x00, 0x00);
strcpy (cf.szFaceName, "Times New Roman");
GetRichEditCtrl().SetDefaultCharFormat (cf);
long lEventMask = GetRichEditCtrl().GetEventMask();
lEventMask |= ENM_SELCHANGE;
GetRichEditCtrl().SetEventMask(lEventMask);
}
/////////////////////////////////////////////////////////////////////////////
// CRichView printing
BOOL CRichView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CRichView::OnDestroy()
{
// Deactivate the item on destruction; this is important
// when a splitter view is being used.
CRichEditView::OnDestroy();
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
{
pActiveItem->Deactivate();
ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}
}
/////////////////////////////////////////////////////////////////////////////
// CRichView diagnostics
#ifdef _DEBUG
void CRichView::AssertValid() const
{
CRichEditView::AssertValid();
}
void CRichView::Dump(CDumpContext& dc) const
{
CRichEditView::Dump(dc);
}
CRichDoc* CRichView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRichDoc)));
return (CRichDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CRichView message handlers
BOOL CRichView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
BOOL bResult = CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
GetRichEditCtrl().ModifyStyleEx (WS_EX_ACCEPTFILES, 0);
return (bResult);
}
void CRichView::OnFontSelect()
{
LOGFONT lf;
memset (&lf, '\0', sizeof (LOGFONT));
CHARFORMAT cf = GetCharFormatSelection ();
lf.lfItalic = cf.dwEffects & CFE_ITALIC ? TRUE : FALSE;
lf.lfStrikeOut = cf.dwEffects & CFE_STRIKEOUT ? TRUE : FALSE;
lf.lfUnderline = cf.dwEffects & CFE_UNDERLINE ? TRUE : FALSE;
lf.lfWeight = cf.dwEffects & CFE_BOLD ? FW_BOLD : 0;
lf.lfCharSet = cf.bCharSet;
CWindowDC dc(this);
lf.lfHeight = -MulDiv(cf.yHeight / 20, GetDeviceCaps(dc.m_hDC, LOGPIXELSY), 72);
strcpy (lf.lfFaceName, cf.szFaceName);
CFontDialog cd;
cd.m_cf.lpLogFont = &lf;
cd.m_cf.Flags |= CF_INITTOLOGFONTSTRUCT;
cd.m_cf.rgbColors = cf.crTextColor;
if (cd.DoModal () == IDCANCEL)
return;
cf.dwMask = CFM_FACE | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_BOLD | CFM_COLOR | CFM_SIZE | CFM_CHARSET;
cf.dwEffects |= cd.IsItalic() ? CFE_ITALIC : 0;
cf.dwEffects |= cd.IsBold() ? CFE_BOLD : 0;
cf.dwEffects |= cd.IsUnderline() ? CFE_UNDERLINE : 0;
cf.dwEffects |= cd.IsStrikeOut() ? CFE_STRIKEOUT : 0;
cf.crTextColor = cd.GetColor();
cf.yHeight = 20 * cd.GetSize () / 10;
cf.bCharSet = lf.lfCharSet;
strcpy (cf.szFaceName, lf.lfFaceName);
SetCharFormat (cf);
}
void CRichView::OnCharBold()
{
CHARFORMAT cf = GetCharFormatSelection ();
cf.dwMask |= CFM_BOLD;
cf.dwEffects ^= CFE_BOLD;
SetCharFormat (cf);
}
void CRichView::OnUpdateCharBold(CCmdUI* pCmdUI)
{
OnUpdateCharEffect (pCmdUI, CFE_BOLD, CFE_BOLD);
}
void CRichView::OnCharItalic()
{
CHARFORMAT cf = GetCharFormatSelection ();
cf.dwMask |= CFM_ITALIC;
cf.dwEffects ^= CFE_ITALIC;
SetCharFormat (cf);
}
void CRichView::OnUpdateCharItalic(CCmdUI* pCmdUI)
{
OnUpdateCharEffect (pCmdUI, CFE_ITALIC, CFE_ITALIC);
}
void CRichView::OnCharStrikeout()
{
CHARFORMAT cf = GetCharFormatSelection ();
cf.dwMask |= CFM_STRIKEOUT;
cf.dwEffects ^= CFE_STRIKEOUT;
SetCharFormat (cf);
}
void CRichView::OnUpdateCharStrikeout(CCmdUI* pCmdUI)
{
OnUpdateCharEffect (pCmdUI, CFE_STRIKEOUT, CFE_STRIKEOUT);
}
void CRichView::OnCharUnderline()
{
CHARFORMAT cf = GetCharFormatSelection ();
cf.dwMask |= CFM_UNDERLINE;
cf.dwEffects ^= CFE_UNDERLINE;
SetCharFormat (cf);
}
void CRichView::OnUpdateCharUnderline(CCmdUI* pCmdUI)
{
OnUpdateCharEffect (pCmdUI, CFE_UNDERLINE, CFE_UNDERLINE);
}
void CRichView::OnFontColor()
{
static COLORREF clrCustom[16] = {
RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0),
RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0),
RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0),
RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0)
};
CColorDialog cd;
cd.m_cc.lpCustColors = clrCustom;
CHARFORMAT cf;
cf = GetCharFormatSelection ();
cd.m_cc.rgbResult = cf.crTextColor;
cd.m_cc.Flags |= CC_RGBINIT;
if (cd.DoModal () == IDCANCEL)
return;
cf.dwMask = CFM_COLOR;
cf.crTextColor = cd.GetColor ();
SetCharFormat (cf);
}
void CRichView::OnFontBackground()
{
static COLORREF clrCustom[16] = {
RGB(0xFF, 0xFF, 0xFF), RGB(0xFF, 0xFF, 0xFF), RGB(0xFF, 0xFF, 0xFF), RGB(0xFF, 0xFF, 0xFF),
RGB(0xFF, 0xFF, 0xFF), RGB(0xFF, 0xFF, 0xFF), RGB(0xFF, 0xFF, 0xFF), RGB(0xFF, 0xFF, 0xFF),
RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0),
RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0), RGB(0, 0, 0)
};
CHARFORMAT2 cf;
// cf.dwMask = CFM_BACKCOLOR;
CWindowDC dc (&GetRichEditCtrl());
GetRichEditCtrl().GetSelectionCharFormat (cf);
CColorDialog cd;
cd.m_cc.lpCustColors = clrCustom;
cd.m_cc.rgbResult = dc.GetBkColor ();// cf.crBackColor;
cd.m_cc.Flags |= CC_RGBINIT; // | CC_ANYCOLOR | CC_SOLIDCOLOR;
if (cd.DoModal () == IDCANCEL)
return;
dc.SetBkColor (cd.GetColor ());
// cf.dwMask = CFM_BACKCOLOR | 0x7fffffff;
// cf.crBackColor = cd.GetColor ();
// SetCharFormat (cf);
// ::SendMessage (GetRichEditCtrl().m_hWnd, EM_SETCHARFORMAT, (WPARAM)(UINT)SCF_SELECTION, (LPARAM) &cf);
// GetRichEditCtrl().SetBackgroundColor (FALSE, cd.GetColor());
}
void CRichView::OnParaLeft()
{
OnParaAlign (PFA_LEFT);
}
void CRichView::OnUpdateParaLeft(CCmdUI* pCmdUI)
{
OnUpdateParaAlign (pCmdUI, PFA_LEFT);
}
void CRichView::OnParaRight()
{
OnParaAlign (PFA_RIGHT);
}
void CRichView::OnUpdateParaRight(CCmdUI* pCmdUI)
{
OnUpdateParaAlign (pCmdUI, PFA_RIGHT);
}
void CRichView::OnParaCenter()
{
OnParaAlign (PFA_CENTER);
}
void CRichView::OnUpdateParaCenter(CCmdUI* pCmdUI)
{
OnUpdateParaAlign (pCmdUI, PFA_CENTER);
}
void CRichView::OnParaBullet()
{
PARAFORMAT pf;
CHARFORMAT cf;
memset (&pf, '\0', sizeof (PARAFORMAT));
pf.cbSize = sizeof (PARAFORMAT);
pf = GetParaFormatSelection ();
cf = GetCharFormatSelection ();
pf.dwMask = PFM_NUMBERING | PFM_OFFSET;
pf.wNumbering ^= PFN_BULLET;
if (pf.wNumbering & PFN_BULLET)
{
pf.dxOffset = cf.yHeight;
}
else
{
pf.dwMask |= PFM_STARTINDENT | PFM_RIGHTINDENT;
pf.dxOffset = 0;
pf.dxStartIndent = 0;
pf.dxRightIndent = 0;
}
SetParaFormat (pf);
}
void CRichView::OnUpdateParaBullet(CCmdUI* pCmdUI)
{
PARAFORMAT pf;
memset (&pf, '\0', sizeof (PARAFORMAT));
pf.cbSize = sizeof (PARAFORMAT);
pf = GetParaFormatSelection ();
if (pf.wNumbering & PFN_BULLET)
pCmdUI->SetCheck (TRUE);
else
pCmdUI->SetCheck (FALSE);
}
void CRichView::OnParaIndentminus()
{
PARAFORMAT pf;
CHARFORMAT cf;
memset (&pf, '\0', sizeof (PARAFORMAT));
pf.cbSize = sizeof (PARAFORMAT);
pf = GetParaFormatSelection ();
cf = GetCharFormatSelection ();
pf.dwMask = PFM_STARTINDENT | PFM_RIGHTINDENT;
if (pf.dxRightIndent)
pf.dxRightIndent -= cf.yHeight;
if (pf.dxStartIndent)
pf.dxStartIndent -= cf.yHeight;
SetParaFormat (pf);
}
void CRichView::OnParaIndentplus()
{
PARAFORMAT pf;
CHARFORMAT cf;
memset (&pf, '\0', sizeof (PARAFORMAT));
pf.cbSize = sizeof (PARAFORMAT);
pf = GetParaFormatSelection ();
cf = GetCharFormatSelection ();
pf.dwMask = PFM_STARTINDENT | PFM_RIGHTINDENT;
pf.dxStartIndent += cf.yHeight;
pf.dxRightIndent += cf.yHeight;
SetParaFormat (pf);
}
void CRichView::OnSelchangeFontlist()
{
CMainFrame *frame = DYNAMIC_DOWNCAST(CMainFrame, AfxGetApp()->GetMainWnd());
CString strFont;
frame->GetFontSelection (strFont);
CHARFORMAT cf = GetCharFormatSelection ();
cf.dwMask |= CFM_FACE;
strcpy (cf.szFaceName, (LPCSTR) strFont);
SetCharFormat (cf);
::SetFocus (this->m_hWnd);
}
void CRichView::OnSelchangePointsize()
{
CMainFrame *frame = DYNAMIC_DOWNCAST(CMainFrame, AfxGetApp()->GetMainWnd());
int nSize;
frame->GetSizeSelection (nSize);
CHARFORMAT cf = GetCharFormatSelection ();
cf.dwMask |= CFM_SIZE;
cf.yHeight = nSize * 20;
SetCharFormat (cf);
::SetFocus (this->m_hWnd);
}
void CRichView::OnSelChange(NMHDR* pNMHDR, LRESULT* pResult)
{
CHARFORMAT cf;
GetRichEditCtrl().GetSelectionCharFormat (cf);
CMainFrame *frame = DYNAMIC_DOWNCAST(CMainFrame, AfxGetApp()->GetMainWnd());
CString strFontName = cf.szFaceName;
frame->SetFontSelection (strFontName);
frame->SetPointSelection (cf.yHeight/20);
CRichEditView::OnSelChange (pNMHDR, pResult);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -