📄 coloreditview.cpp
字号:
OnPrepareDC(&dc);
m_font.GetLogFont(&lf);
CFont* oldFont = dc.SelectObject(&m_font);
GetClientRect(&client);
client += scrollPos;
CPoint caret = TextPosition2ScreenCoord(&dc, m_caret);
long x = scrollPos.x;
long y = scrollPos.y;
if (caret.y > client.bottom) {
y = caret.y-client.Height()/2;
} else if (caret.y < client.top) {
y = caret.y-client.Height()/2;
}
if (caret.x < client.left) {
if (caret.x < client.Width()) {
x = 0;
} else {
x = caret.x-client.Width()/2;
}
} else if (caret.x > client.right) {
if (caret.x < client.Width()) {
x = 0;
} else {
x = caret.x-client.Width()/2;
}
}
dc.SelectObject(oldFont);
ScrollToPosition(CPoint(x, y));
Invalidate();
}
void CColorEditView::SetSelection(long start, long end)
{
if (start < end) {
m_selectStart = start;
m_caret = m_selectEnd = end;
}
else {
m_caret = m_selectStart = end;
m_selectEnd = start;
}
}
void CColorEditView::ScrollToCaret()
{
LOGFONT lf;
CPaintDC dc(this);
CRect client;
CPoint scrollPos = GetScrollPosition();
CColorEditDoc* pDoc = GetDocument();
OnPrepareDC(&dc);
m_font.GetLogFont(&lf);
CFont* oldFont = dc.SelectObject(&m_font);
GetClientRect(&client);
client += scrollPos;
CPoint caret = TextPosition2ScreenCoord(&dc, m_caret);
long x = scrollPos.x;
long y = scrollPos.y;
if (caret.y > client.bottom) {
y = caret.y-client.Height()/2;
} else if (caret.y < client.top) {
y = caret.y-client.Height()/2;
}
if (caret.x < client.left) {
if (caret.x < client.Width()) {
x = 0;
} else {
x = caret.x-client.Width()/2;
}
} else if (caret.x > client.right) {
if (caret.x < client.Width()) {
x = 0;
} else {
x = caret.x-client.Width()/2;
}
}
dc.SelectObject(oldFont);
ScrollToPosition(CPoint(x, y));
Invalidate();
}
void CColorEditView::ScrollToSelection()
{
LOGFONT lf;
CPaintDC dc(this);
CRect client;
CPoint scrollPos = GetScrollPosition();
CColorEditDoc* pDoc = GetDocument();
OnPrepareDC(&dc);
m_font.GetLogFont(&lf);
CFont* oldFont = dc.SelectObject(&m_font);
GetClientRect(&client);
client += scrollPos;
CPoint selectEnd = TextPosition2ScreenCoord(&dc, m_selectEnd);
long x = scrollPos.x;
long y = scrollPos.y;
if (selectEnd.y > client.bottom) {
y = selectEnd.y-client.Height()/2;
} else if (selectEnd.y < client.top) {
y = selectEnd.y-client.Height()/2;
}
if (selectEnd.x < client.left) {
if (selectEnd.x < client.Width()) {
x = 0;
} else {
x = selectEnd.x-client.Width()/2;
}
} else if (selectEnd.x > client.right) {
if (selectEnd.x < client.Width()) {
x = 0;
} else {
x = selectEnd.x-client.Width()/2;
}
}
dc.SelectObject(oldFont);
ScrollToPosition(CPoint(x, y));
Invalidate();
}
long CColorEditView::GetSearchPosition()
{
if (m_selectEnd >= 0) {
return m_selectEnd;
}
return CurrentPosition();
}
CPoint CColorEditView::TextPosition2ScreenCoord(CDC* pDC, long pos)
{
CColorEditDoc* pDoc = GetDocument();
long line = pDoc->m_text.Offset2Line(pos);
long offset = pDoc->GetLineOffset(line);
CString string = pDoc->m_text.Mid(offset, pos-offset);
return CPoint(GetExtent(pDC, string).x+m_indent, GetExtent(pDC, _T(" ")).y*line);
}
BOOL CColorEditView::ReplaceSelection(CString string)
{
CColorEditDoc* pDoc = GetDocument();
if (m_selectStart != m_selectEnd) {
pDoc->m_text.Replace(m_selectStart, m_selectEnd - m_selectStart, string);
m_caret = m_selectEnd = m_selectStart;
pDoc->CheckPoint();
CalcScrollSize();
Invalidate();
return TRUE;
}
return FALSE;
}
void CColorEditView::OnEditFind()
{
CColorEditDoc* pDoc = GetDocument();
CSearch dlg;
dlg.m_pString = &pDoc->m_text;
dlg.m_view = this;
dlg.m_search_string = pDoc->Range(m_selectStart, m_selectEnd);
dlg.DoModal();
CreateSolidCaret(2, m_cHeight);
ShowCaret();
ScrollToCurrent();
pDoc->UpdateAllViews(NULL);
}
void CColorEditView::OnUpdateEditFind(CCmdUI* pCmdUI)
{
CColorEditDoc* pDoc = GetDocument();
pCmdUI->Enable(pDoc->GetLength() > 0);
}
void CColorEditView::DisableCheckPoint()
{
CColorEditDoc* pDoc = GetDocument();
pDoc->DisableCheckPoint();
}
void CColorEditView::EnableCheckPoint()
{
CColorEditDoc* pDoc = GetDocument();
pDoc->EnableCheckPoint();
}
void CColorEditView::CheckPoint()
{
CColorEditDoc* pDoc = GetDocument();
pDoc->CheckPoint();
}
void CColorEditView::OnEditCopy()
{
CColorEditDoc* pDoc = GetDocument();
CString text = pDoc->Range(m_selectStart, m_selectEnd);
COleDataSource* pSource = new COleDataSource();
CSharedFile sf(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT);
sf.Write(text, text.GetLength());
HGLOBAL hMem = sf.Detach();
if (!hMem) return;
pSource->CacheGlobalData(CF_TEXT, hMem);
pSource->SetClipboard();
}
void CColorEditView::OnUpdateEditCopy(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_selectStart < m_selectEnd);
}
void CColorEditView::OnEditCut()
{
CColorEditDoc* pDoc = GetDocument();
CString text = pDoc->Range(m_selectStart, m_selectEnd);
COleDataSource* pSource = new COleDataSource();
CSharedFile sf(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT);
sf.Write(text, text.GetLength());
HGLOBAL hMem = sf.Detach();
if (!hMem) return;
pSource->CacheGlobalData(CF_TEXT, hMem);
pSource->SetClipboard();
ReplaceSelection(_T(""));
}
void CColorEditView::OnUpdateEditCut(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_selectStart < m_selectEnd);
}
void CColorEditView::OnEditPaste()
{
COleDataObject obj;
if (obj.AttachClipboard()) {
if (obj.IsDataAvailable(CF_TEXT)) {
HGLOBAL hmem = obj.GetGlobalData(CF_TEXT);
CMemFile sf((BYTE*) ::GlobalLock(hmem), ::GlobalSize(hmem));
CString buffer;
LPSTR str = buffer.GetBufferSetLength(::GlobalSize(hmem));
sf.Read(str, ::GlobalSize(hmem));
::GlobalUnlock(hmem);
if (m_selectStart < m_selectEnd) {
ReplaceSelection(buffer);
} else {
CColorEditDoc* pDoc = GetDocument();
pDoc->m_text.Insert(m_caret, buffer);
pDoc->CheckPoint();
CalcScrollSize();
Invalidate();
}
}
}
}
void CColorEditView::OnUpdateEditPaste(CCmdUI* pCmdUI)
{
COleDataObject obj;
obj.AttachClipboard();
pCmdUI->Enable(obj.IsDataAvailable(CF_TEXT));
}
void CColorEditView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
if (pDC->IsPrinting()) {
CColorEditDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int nPrinterPixCX = pDC->GetDeviceCaps(HORZRES);
int nPrinterPixCY = pDC->GetDeviceCaps(VERTRES);
int nPrinterPixPerInchX = pDC->GetDeviceCaps(LOGPIXELSX);
int nPrinterPixPerInchY = pDC->GetDeviceCaps(LOGPIXELSY);
CClientDC dcDisplay(this);
int nLogPixPerInchX = dcDisplay.GetDeviceCaps(LOGPIXELSX);
int nLogPixPerInchY = dcDisplay.GetDeviceCaps(LOGPIXELSY);
int nPagePixWidth = (int)((DWORD)nPrinterPixCX * nLogPixPerInchX / nPrinterPixPerInchX);
int nPagePixHeight = (int)((DWORD)nPrinterPixCY * nLogPixPerInchY / nPrinterPixPerInchY);
m_nCurPage = pInfo->m_nCurPage;
LOGFONT lf;
m_font.GetLogFont(&lf);
int nTextHeight = lf.lfHeight;
m_nLinesPerPage = nPagePixHeight / nTextHeight - 1;
TRACE("m_nLinesPerPage = %d\r\n", m_nLinesPerPage);
m_nPageStartLine = (m_nCurPage-1)*m_nLinesPerPage;
m_nPageEndLine = m_nPageStartLine+m_nLinesPerPage-1;
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(nLogPixPerInchX, nLogPixPerInchY);
pDC->SetViewportExt(nPrinterPixPerInchX, nPrinterPixPerInchY);
} else {
CScrollView::OnPrepareDC(pDC, pInfo);
}
}
void CColorEditView::OnTimer(UINT nIDEvent)
{
if (nIDEvent == ScrollTimer) {
AutoScroll();
return;
}
CScrollView::OnTimer(nIDEvent);
}
void CColorEditView::AutoScroll()
{
CRect client;
GetClientRect(&client);
if (client.PtInRect(m_mousePosition)) return;
long p = SelectionPoint(m_mousePosition);
m_caret = p;
if (p <= m_selectStart) {
m_selectStart = p;
} else {
m_selectEnd = p;
}
SetCurrentPosition(p);
TRACE(_T("AutoScroll(%d)\r\n"), m_autoScrollCount++);
}
void CColorEditView::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
CMenu menu;
menu.LoadMenu(IDR_MENU1);
menu.GetSubMenu(0)->TrackPopupMenu(
TPM_LEFTALIGN|TPM_LEFTBUTTON|
TPM_RIGHTBUTTON,point.x,point.y,AfxGetMainWnd());
}
void CColorEditView::OnCheckError()
{
// TODO: Add your command handler code here
if(m_bracket>0)
AfxMessageBox("There are more '('s than ')'s!\n')' missed somewhere.\nPlease check and continue");
else if(m_bracket<0)
AfxMessageBox("Ther are more ')'s than '('s!\n'(' missed somewhere.\nPlease check and continue");
else
MessageBox("Congratulations\nThere is no error in brackets!","Welldone");
}
void CColorEditView::OnFontSettingMenu()
{
// TODO: Add your command handler code here
CFontSettingDialog dlg;
dlg.DoModal();
delete dlg;
}
void CColorEditView::OnColorSettingMenu()
{
// TODO: Add your command handler code here
CColorSettingDlg *dlg;
dlg = new CColorSettingDlg;
dlg->DoModal();
delete dlg;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -