📄 htextvw.cpp
字号:
// htextvw.cpp : implementation of the CHtext2View class
//
#include "stdafx.h"
#include "htext2.h"
#include "htextdoc.h"
#include "htextvw.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHtext2View
IMPLEMENT_DYNCREATE(CHtext2View, CScrollView)
BEGIN_MESSAGE_MAP(CHtext2View, CScrollView)
//{{AFX_MSG_MAP(CHtext2View)
ON_WM_LBUTTONDOWN()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHtext2View construction/destruction
CHtext2View::CHtext2View()
{
// TODO: add construction code here
}
CHtext2View::~CHtext2View()
{
}
/////////////////////////////////////////////////////////////////////////////
// CHtext2View drawing
void CHtext2View::OnDraw(CDC* pDC)
{
CHtext2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//Change display font.
CFont theFont;
int fontHeight = 24;
theFont.CreateFont(fontHeight, 0,
0,0,400,
FALSE,FALSE,0,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS,
"Ariel");
pDC->SelectObject(&theFont);
DisplayTopic(pDC, pDoc);
}
void CHtext2View::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
// Set the scroll view's extent and the page and line scroll sizes..
CSize sizeTotal, sizePage, sizeLine;
CRect Rect;
CWnd::GetClientRect(&Rect);
sizeTotal.cx = Rect.right - 20;
sizeTotal.cy = Rect.bottom * 4;
sizePage.cx = sizeTotal.cx;
sizePage.cy = sizeTotal.cy /5;
sizeLine.cx = sizeLine.cy = 20;
SetScrollSizes(MM_TEXT, sizeTotal);
}
void CHtext2View::DisplayTopic(CDC* pDC, CHtext2Doc* pDoc)
{
CString Temp, NextWord;
const char EndWord[] = " .;:!?\n"; //Characters that mark end of word.
int stop, lineHeight, windowWidth, WordALink = FALSE;
const int margin = 10;
CSize wordWidth;
TEXTMETRIC TM;
CRect Rect;
CPoint here;
COLORREF oldColor;
//Initialize the link array.
pDoc->InitLinkArray();
//Get the height of a line of text.
pDC->GetTextMetrics(&TM);
lineHeight = TM.tmHeight + TM.tmExternalLeading;
//Get the width of the window.
CWnd::GetClientRect(&Rect);
windowWidth = Rect.right;
//Move current position to provide top and left margins.
here.x = here.y = margin;
//Get the topic text to display.
Temp = pDoc->GetTopic();
//Find the end of the first word and extract the word.
stop = Temp.FindOneOf(EndWord) + 1;
NextWord = Temp.Left(stop);
//Loop through the topic text until all words are displayed.
for ( ; NextWord != "" ; )
{
//Is there a new line at the end of the word? If so, strip it
//off and move to the start of the next line.
if (NextWord.Find('\n') != -1)
{
//MessageBox("Found");
NextWord = NextWord.Left(NextWord.GetLength()-1);
here.y += lineHeight;
here.x = margin;
}
//If the word is link, strip off the ## marker and
//set the link flag.
if (NextWord.Left(2) == "##")
{
NextWord = NextWord.Right(NextWord.GetLength() - 2);
WordALink = TRUE;
}
//Get the word's display width.
wordWidth = pDC->GetTextExtent(NextWord, NextWord.GetLength());
//Will it extend past the right margin? If so, start new line.
if ((wordWidth.cx + here.x) > windowWidth )
{
here.x = margin;
here.y += lineHeight;
}
//If the word is a link, display it in red and and update the
//link array.
if (WordALink)
{
oldColor = pDC->SetTextColor(RGB(200,0,0));
pDC->TextOut(here.x,here.y,NextWord);
pDC->SetTextColor(oldColor);
CRect LinkIs;
LinkIs.left = here.x;
LinkIs.top = here.y;
LinkIs.right = here.x + wordWidth.cx;
LinkIs.bottom = here.y + wordWidth.cy;
pDoc->CreateLink(NextWord, LinkIs);
WordALink = FALSE;
}
else //If the word is not a link, display it in black.
{
pDC->TextOut(here.x,here.y,NextWord);
}
here.x += wordWidth.cx;
Temp = Temp.Right(Temp.GetLength() - stop);
stop = Temp.FindOneOf(EndWord) + 1;
NextWord = Temp.Left(stop);
}
}
/////////////////////////////////////////////////////////////////////////////
// CHtext2View diagnostics
#ifdef _DEBUG
void CHtext2View::AssertValid() const
{
CScrollView::AssertValid();
}
void CHtext2View::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CHtext2Doc* CHtext2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHtext2Doc)));
return (CHtext2Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CHtext2View message handlers
void CHtext2View::OnLButtonDown(UINT nFlags, CPoint point)
{
//Called when the user presses the left mouse button.
CHtext2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CString text;
//Offset mouse position by the scroll position.
point.Offset(GetScrollPosition());
//Call CHtext2Doc's IsLink() member function to see if user
//clicked in a hotlink.
text = pDoc->IsLink(point);
//If so, display the link text.
if (text != "")
{
text = "You clicked on the hyperlink '" + text + "'.";
MessageBox(text);
}
//Call the default message handler.
CScrollView::OnLButtonDown(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -