?? readerview.cpp
字號:
// ReaderView.cpp : implementation of the CReaderView class
//
#include "stdafx.h"
#include "Reader.h"
#include "ReaderDoc.h"
#include "ReaderView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CReaderView
IMPLEMENT_DYNCREATE(CReaderView, CScrollView)
BEGIN_MESSAGE_MAP(CReaderView, CScrollView)
//{{AFX_MSG_MAP(CReaderView)
ON_WM_SETCURSOR()
ON_WM_VSCROLL()
ON_WM_SIZE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CReaderView construction/destruction
CReaderView::CReaderView()
{
MARGIN=5;
bSized=FALSE;
nSizedPos=0;
}
CReaderView::~CReaderView()
{
}
BOOL CReaderView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CReaderView drawing
void CReaderView::OnDraw(CDC* pDC)
{
CReaderDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
DrawText(0,0);
}
/////////////////////////////////////////////////////////////////////////////
// CReaderView printing
BOOL CReaderView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CReaderView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CReaderView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CReaderView diagnostics
#ifdef _DEBUG
void CReaderView::AssertValid() const
{
CScrollView::AssertValid();
}
void CReaderView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CReaderDoc* CReaderView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CReaderDoc)));
return (CReaderDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CReaderView message handlers
BOOL CReaderView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (nHitTest == HTCLIENT && pWnd == this) {
::SetCursor(::LoadCursor(NULL, IDC_IBEAM));
return TRUE;
}
return CScrollView::OnSetCursor(pWnd, nHitTest, message);
}
void CReaderView::DrawText(int x,int y)
{
//return if font has not yet been created;
CReaderDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CClientDC dc(this);
OnPrepareDC(&dc);
if(!pDoc->GetTextLength())
return;
int LineHeight;
TEXTMETRIC TM;
int Y=0;
//obtain text metrics;
dc.GetTextMetrics(&TM);
LineHeight=TM.tmHeight+TM.tmExternalLeading;
//set test attributes;
dc.SetBkMode(TRANSPARENT);
//dispay text lines;
for(int Line=0;Line<pDoc->GetLineNumber();Line++)
{
Y+=LineHeight;
CString strTemp=pDoc->strLine[Line];
dc.TextOut(x+MARGIN,Y-LineHeight,strTemp,strTemp.GetLength());
}
}
void CReaderView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
CScrollView::OnUpdate(pSender,lHint,pHint);
CReaderDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CClientDC dc(this);
int LineWidth=0;
CSize Size;
TEXTMETRIC TM;
dc.GetTextMetrics(&TM);
for(int Line=0;Line<pDoc->GetLineNumber();Line++)
{
Size=dc.GetTextExtent(pDoc->strLine[Line],pDoc->strLine[Line].GetLength());
if(Size.cx>LineWidth)
LineWidth=Size.cx;
}
Size.cx=LineWidth+MARGIN;
Size.cy=(TM.tmHeight+TM.tmExternalLeading)*(pDoc->GetLineNumber()+1)+MARGIN;
CSize step;
step=sizeDefault;
step.cy=(TM.tmHeight+TM.tmExternalLeading);
SetScrollSizes(MM_TEXT,Size,sizeDefault,step);
}
void CReaderView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
}
void CReaderView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
CReaderDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(pDoc->GetLineNumber()<10)
{
CScrollView::OnVScroll(nSBCode, (int)nPos, pScrollBar);
return;
}
CClientDC dc(this);
OnPrepareDC(&dc);
TEXTMETRIC TM;
dc.GetTextMetrics(&TM);
SCROLLINFO ScrollInfo;
BOOL bGetInfo=GetScrollInfo(SB_VERT,&ScrollInfo,SIF_ALL);
//如果僅僅在OnUpdate()函數中加入如下語句是不夠的:
//SetScrollSizes(MM_TEXT,Size,sizeDefault,step);
//當顯示小文本(本例中當行數<2048時)尚可;
//當顯示大文本時,則僅僅顯示其中一部分。
//最多可顯示2048行,原因何在?
//仔細一查,發現原來是CWnd::OnVScroll的缺省
//實現是16 bit 的。必須手工將其轉換到32 bit.
//以下兩句代碼可解決:
ScrollInfo.nMax=(TM.tmHeight+TM.tmExternalLeading)*(pDoc->GetLineNumber()+1)+MARGIN;
if(bSized)
{
//對于大文本文檔,當前滾動位置>0x7fff時,
//此時如果改變文檔尺寸(響應OnSize消息),
//比如最大最小化窗口時,會發現當前滾動位置在文
//檔尺寸變化前后不一致。即文檔尺寸變化后的當前
//滾動位置總是小于0x7fff位。以下代碼用于解決此問題。
ScrollInfo.nTrackPos=nSizedPos;
ScrollInfo.nPos=nSizedPos;
bSized=FALSE;
}
SetScrollInfo(SB_VERT,&ScrollInfo,SIF_ALL);
//下面一行代碼也很重要,因為nTrackPos
//返回SB_THUMBTRACK消息帶來的臨時位置
//你可以不加,然后試一試拖動滾動條,
//當向下拖過的位置大于0x7fff(32767)時
//(顯然是大文件),此時發現UINT 的變量
//nPos在作怪了!
nPos=ScrollInfo.nTrackPos;
//別忘了再強制轉換一下!
CScrollView::OnVScroll(nSBCode, (int)nPos, pScrollBar);
}
void CReaderView::OnSize(UINT nType, int cx, int cy)
{
nSizedPos=GetScrollPos(SB_VERT);
bSized=TRUE;
PostMessage(WM_VSCROLL);
CScrollView::OnSize(nType, cx, cy);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -