📄 skillbookview.cpp
字号:
// SkillBookView.cpp : implementation of the CSkillBookView class
//
#include "stdafx.h"
#include "SkillBook.h"
#include "SkillBookDoc.h"
#include "SkillBookView.h"
#include "MyPreviewView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ID_RICH_UNDO 2001
#define ID_RICH_CUT 2002
#define ID_RICH_COPY 2003
#define ID_RICH_PASTE 2004
#define ID_RICH_CLEAR 2005
#define ID_RICH_SELECTALL 2006
#define ID_RICH_SETFONT 2007
static LPCTSTR szKeywords = " #define #else #elif #elseif #endif #error #if #ifdef #ifndef #include #pragma #undef __asm __based __cdecl __declspec __except __fastcall __finally __inline __int16 __int32 __int64 __int8 __leave __multiple_inheritance __pascal __single_inheritance __stdcall __try __uuidof __virtual_inheritance _asm _cdecl _fastcall _pascal _stdcall afx_msg auto bool break case catch char class code_seg const const_cast continue default defined delete dllexport dllimport do double dynamic_cast else enum explicit extern false float for friend goto if inline int interface long main mutable naked namespace new off on once operator pack pascal pop private protected public push register reinterpret_cast return short signed sizeof static static_cast struct switch template this thread throw true try typedef typeid typename union unsigned using uuid virtual void volatile while wmain xalloc ";
static LPCTSTR szConstants = " FALSE TRUE NULL ";
/////////////////////////////////////////////////////////////////////////////
// CSkillBookView
IMPLEMENT_DYNCREATE(CSkillBookView, CRichEditView)
BEGIN_MESSAGE_MAP(CSkillBookView, CRichEditView)
//{{AFX_MSG_MAP(CSkillBookView)
ON_WM_CREATE()
ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
ON_COMMAND(ID_MENU_SAVEARTICLE, OnMenuSavearticle)
ON_COMMAND(ID_MENU_UNDO, OnMenuUndo)
ON_COMMAND(ID_MENU_CUT, OnMenuCut)
ON_COMMAND(ID_MENU_SELECTALL, OnMenuSelectall)
ON_COMMAND(ID_MENU_COPY, OnMenuCopy)
ON_COMMAND(ID_MENU_PASTE, OnMenuPaste)
ON_UPDATE_COMMAND_UI(ID_MENU_CUT, OnUpdateMenuCut)
ON_UPDATE_COMMAND_UI(ID_MENU_COPY, OnUpdateMenuCopy)
ON_UPDATE_COMMAND_UI(ID_MENU_PASTE, OnUpdateMenuPaste)
ON_UPDATE_COMMAND_UI(ID_MENU_UNDO, OnUpdateMenuUndo)
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_RICH_COPY, OnCopy)
ON_COMMAND(ID_RICH_CUT, OnCut)
ON_COMMAND(ID_RICH_PASTE, OnPaste)
ON_COMMAND(ID_RICH_SELECTALL, OnSelectall)
ON_COMMAND(ID_RICH_UNDO, OnUndo)
ON_COMMAND(ID_RICH_CLEAR, OnClear)
ON_COMMAND(ID_RICH_SETFONT, OnSelectfont)
ON_UPDATE_COMMAND_UI(ID_RICH_PASTE, OnUpdateMenuPaste)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
//}}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)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSkillBookView construction/destruction
CSkillBookView::CSkillBookView()
{
// TODO: add construction code here
m_chComment = 1;
SetStringQuotes(_T("\""));
SetKeywordColor(RGB(0, 0, 255), FALSE);
SetConstantColor(RGB(0, 0, 0), TRUE);
SetCommentColor(RGB(0, 128, 0), FALSE);
SetNumberColor(RGB(255, 0, 255), FALSE);
SetStringColor(RGB(128, 0, 255), FALSE);
SetCharacterColor(RGB(128, 0, 0), FALSE);
m_bComment = FALSE;
m_bInForcedChange = FALSE;
m_changeType = ctUndo;
m_crOldSel.cpMin = m_crOldSel.cpMax = 0;
}
CSkillBookView::~CSkillBookView()
{
}
BOOL CSkillBookView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
BOOL bPreCreated = CRichEditView::PreCreateWindow(cs);
cs.style &= ~(ES_AUTOHSCROLL | WS_HSCROLL); // Enable word-wrapping
return bPreCreated;
}
/////////////////////////////////////////////////////////////////////////////
// CSkillBookView drawing
void CSkillBookView::OnDraw(CDC* pDC)
{
CSkillBookDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CSkillBookView printing
BOOL CSkillBookView::OnPreparePrinting(CPrintInfo* pInfo)
{
CRichEditView::DoPreparePrinting(pInfo);
// default CRichEditView preparation
return CRichEditView::OnPreparePrinting(pInfo);
}
void CSkillBookView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// Default CRichEditView begin printing.
CRichEditView::OnBeginPrinting(pDC, pInfo);
}
void CSkillBookView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// Default CRichEditView end printing
CRichEditView::OnEndPrinting(pDC, pInfo);
}
/////////////////////////////////////////////////////////////////////////////
// CSkillBookView diagnostics
#ifdef _DEBUG
void CSkillBookView::AssertValid() const
{
CRichEditView::AssertValid();
}
void CSkillBookView::Dump(CDumpContext& dc) const
{
CRichEditView::Dump(dc);
}
CSkillBookDoc* CSkillBookView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSkillBookDoc)));
return (CSkillBookDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSkillBookView message handlers
void CSkillBookView::OnInitialUpdate()
{
CRichEditView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
::GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &m_lf);
m_lf.lfWeight = FW_NORMAL;
m_lf.lfHeight = 16;
m_lf.lfCharSet = GB2312_CHARSET;
strcpy(m_lf.lfFaceName, _T("Fixedsys"));
SetSynEditViewFont(m_lf);
SendMessage(EM_SETUNDOLIMIT, 1000, 0);
SendMessage(EM_SETTEXTMODE, TM_PLAINTEXT | TM_MULTILEVELUNDO | ES_MULTILINE, 0);
SetSynEditViewTabSize(4);
SetMargins(CRect(720, 720, 720, 720));
}
void CSkillBookView::OnMenuSavearticle()
{
// TODO: Add your command handler code here
CMainFrame *pMainFrame = (CMainFrame*)GetParent()->GetParent();
CLeftTreeView *pView = (CLeftTreeView *)pMainFrame->m_wndSplitter.GetPane(0, 0);
pView->SaveCurrentArticle();
}
void CSkillBookView::OnMenuCut()
{
// TODO: Add your command handler code here
GetRichEditCtrl().Cut();
}
void CSkillBookView::OnMenuPaste()
{
// TODO: Add your command handler code here
GetRichEditCtrl().Paste();
}
void CSkillBookView::OnMenuSelectall()
{
// TODO: Add your command handler code here
GetRichEditCtrl().SetSel(0, -1);
}
void CSkillBookView::OnMenuCopy()
{
// TODO: Add your command handler code here
GetRichEditCtrl().Copy();
}
void CSkillBookView::OnUpdateMenuCut(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
long nStartChar,nEndChar;
GetRichEditCtrl().GetSel(nStartChar, nEndChar);
if(nStartChar != nEndChar)
{
pCmdUI->Enable();
}
else
{
pCmdUI->Enable(FALSE);
}
}
void CSkillBookView::OnUpdateMenuCopy(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
long nStartChar,nEndChar;
GetRichEditCtrl().GetSel(nStartChar, nEndChar);
if(nStartChar != nEndChar)
{
pCmdUI->Enable();
}
else
{
pCmdUI->Enable(FALSE);
}
}
void CSkillBookView::OnUpdateMenuPaste(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
char *pszBuffer = NULL;
if(OpenClipboard())
{
pszBuffer = (char*)::GetClipboardData(CF_TEXT);
CloseClipboard();
}
if(NULL == pszBuffer)
{
pCmdUI->Enable(FALSE);
}
else
{
pCmdUI->Enable();
}
}
void CSkillBookView::OnMenuUndo()
{
// TODO: Add your command handler code here
GetRichEditCtrl().Undo();
}
void CSkillBookView::OnUpdateMenuUndo(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(GetRichEditCtrl().CanUndo())
{
pCmdUI->Enable();
}
else
{
pCmdUI->Enable(FALSE);
}
}
void CSkillBookView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//设置为焦点
SetFocus();
//创建一个弹出式菜单
CMenu popmenu;
popmenu.CreatePopupMenu();
//添加菜单项目
popmenu.AppendMenu(0, ID_RICH_UNDO, "恢复(&U)");
popmenu.AppendMenu(0, MF_SEPARATOR);
popmenu.AppendMenu(0, ID_RICH_CUT, "剪切(&T)");
popmenu.AppendMenu(0, ID_RICH_COPY, "复制(&C)");
popmenu.AppendMenu(0, ID_RICH_PASTE, "粘贴(&P)");
popmenu.AppendMenu(0, ID_RICH_CLEAR, "清除(&L)");
popmenu.AppendMenu(0, MF_SEPARATOR);
popmenu.AppendMenu(0, ID_RICH_SELECTALL, "全选(&A)");
popmenu.AppendMenu(0, MF_SEPARATOR);
popmenu.AppendMenu(0, ID_RICH_SETFONT, "字体设置(&F)...");
//初始化菜单项
UINT nUndo=(GetRichEditCtrl().CanUndo() ? 0 : MF_GRAYED );
popmenu.EnableMenuItem(ID_RICH_UNDO, MF_BYCOMMAND|nUndo);
UINT nSel=((GetRichEditCtrl().GetSelectionType()!=SEL_EMPTY) ? 0 : MF_GRAYED) ;
popmenu.EnableMenuItem(ID_RICH_CUT, MF_BYCOMMAND|nSel);
popmenu.EnableMenuItem(ID_RICH_COPY, MF_BYCOMMAND|nSel);
popmenu.EnableMenuItem(ID_RICH_CLEAR, MF_BYCOMMAND|nSel);
UINT nPaste=(CanPaste() ? 0 : MF_GRAYED) ;
popmenu.EnableMenuItem(ID_RICH_PASTE, MF_BYCOMMAND|nPaste);
//显示菜单
CPoint pt;
GetCursorPos(&pt);
popmenu.TrackPopupMenu(TPM_RIGHTBUTTON, pt.x, pt.y, this);
popmenu.DestroyMenu();
CRichEditView::OnRButtonDown(nFlags, point);
}
void CSkillBookView::OnSelectfont()
{
// TODO: Add your command handler code here
CHARFORMAT cf;
LOGFONT lf;
memset(&cf, 0, sizeof(CHARFORMAT));
memset(&lf, 0, sizeof(LOGFONT));
//判断是否选择了内容
BOOL bSelect = (GetRichEditCtrl().GetSelectionType() != SEL_EMPTY) ? TRUE : FALSE;
if (bSelect)
{
GetRichEditCtrl().GetSelectionCharFormat(cf);
}
else
{
GetRichEditCtrl().GetDefaultCharFormat(cf);
}
//得到相关字体属性
BOOL bIsBold = cf.dwEffects & CFE_BOLD;
BOOL bIsItalic = cf.dwEffects & CFE_ITALIC;
BOOL bIsUnderline = cf.dwEffects & CFE_UNDERLINE;
BOOL bIsStrickout = cf.dwEffects & CFE_STRIKEOUT;
//设置属性
lf.lfCharSet = cf.bCharSet;
lf.lfHeight = cf.yHeight/15;
lf.lfPitchAndFamily = cf.bPitchAndFamily;
lf.lfItalic = bIsItalic;
lf.lfWeight = (bIsBold ? FW_BOLD : FW_NORMAL);
lf.lfUnderline = bIsUnderline;
lf.lfStrikeOut = bIsStrickout;
sprintf(lf.lfFaceName, cf.szFaceName);
CFontDialog dlg(&lf);
dlg.m_cf.rgbColors = cf.crTextColor;
if(dlg.DoModal() == IDOK)
{
dlg.GetCharFormat(cf);//获得所选字体的属性
if (bSelect)
{
GetRichEditCtrl().SetSelectionCharFormat(cf); //为选定的内容设定所选字体
}
else
{
GetRichEditCtrl().SetWordCharFormat(cf); //为将要输入的内容设定字体
}
}
}
//设定编辑器字体
void CSkillBookView::SetSynEditViewFont(LOGFONT lf)
{
m_lf = lf;
SetSynCtrlFont();
}
void CSkillBookView::SetSynCtrlFont()
{
m_font.DeleteObject();
m_font.CreateFontIndirect(&m_lf);
SetFont(&m_font);
}
//改变RichEditCtrl的默认Tab宽度
void CSkillBookView::SetSynEditViewTabSize(int nSize)
{
int nNewTab;
int nSynCtrlTabSize = 0;
int nCharSpaceWidth,nCharTabWidth,nCharNumberWidth;
CPoint p1,p2;
PARAFORMAT pf;
CRichEditCtrl &redtCtrl = GetRichEditCtrl();
CString szText("");
CRect rect;
ShowWindow(FALSE);
GetWindowText(szText);
redtCtrl.SetWindowText(_T(" "));
p1 = redtCtrl.GetCharPos(0);
p2 = redtCtrl.GetCharPos(1);
nCharSpaceWidth = p2.x - p1.x; //空格宽度
redtCtrl.SetWindowText(_T("\t"));
p1 = redtCtrl.GetCharPos(0);
p2 = redtCtrl.GetCharPos(1);
nCharTabWidth = p2.x - p1.x; //TAB宽度
redtCtrl.SetWindowText(_T("8"));
p1 = redtCtrl.GetCharPos(0);
p2 = redtCtrl.GetCharPos(1);
nCharNumberWidth = p2.x - p1.x; //数字宽度
GetClientRect(&rect);
rect.left = nCharNumberWidth;
rect.top = 0;
GetRichEditCtrl().SetRect(&rect);
SetWindowText(szText);
ShowWindow(TRUE);
pf.cbSize = sizeof(PARAFORMAT);
redtCtrl.GetParaFormat(pf);
nSynCtrlTabSize = pf.rgxTabs[0];
if(nSynCtrlTabSize == 0)
{
nSynCtrlTabSize = 720;
}
nNewTab = int(nSynCtrlTabSize * 1.0 * nSize * nCharSpaceWidth / nCharTabWidth);
pf.cTabCount = MAX_TAB_STOPS;
pf.dwMask = PFM_TABSTOPS;
for(int nTab=0; nTab<pf.cTabCount; nTab++)
{
pf.rgxTabs[nTab] = (nTab + 1) * nNewTab ;
}
SetParaFormat(pf);
}
int CSkillBookView::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
if (CRichEditView::OnCreate(lpCreateStruct) == -1)
return -1;
GetRichEditCtrl().HideSelection(TRUE, FALSE);
SetStringQuotes(_T("\""));
SetSLComment(_T("//"));
AddKeywords(szKeywords);
AddConstants(szConstants);
return 0;
}
void CSkillBookView::OnChange()
{
int nLineCount = GetRichEditCtrl().GetLineCount();
CString szLineText("");
if(m_bInForcedChange)
{
return;
}
CHARRANGE crCurSel;
GetRichEditCtrl().GetSel(crCurSel);
if (m_changeType == ctMove && crCurSel.cpMin == crCurSel.cpMax)
{
// cut was canceled, so this is paste operation
m_changeType = ctPaste;
}
switch(m_changeType)
{
case ctReplSel:// old=(x,y) -> cur=(x+len,x+len)
case ctPaste: // old=(x,y) -> cur=(x+len,x+len)
FormatTextLines(m_crOldSel.cpMin, crCurSel.cpMax);
break;
case ctDelete: // old=(x,y) -> cur=(x,x)
case ctBack: // old=(x,y) -> cur=(x,x), newline del => old=(x,x+1) -> cur=(x-1,x-1)
case ctCut: // old=(x,y) -> cur=(x,x)
FormatTextLines(crCurSel.cpMin, crCurSel.cpMax);
break;
case ctUndo: // old=(?,?) -> cur=(x,y)
FormatTextLines(crCurSel.cpMin, crCurSel.cpMax);
break;
case ctMove: // old=(x,x+len) -> cur=(y-len,y) | cur=(y,y+len)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -