📄 exam8_1view.cpp
字号:
// Exam8_1View.cpp : implementation of the CExam8_1View class
//
#include "stdafx.h"
#include "Exam8_1.h"
#include "Exam8_1Doc.h"
#include "Exam8_1View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CExam8_1View
IMPLEMENT_DYNCREATE(CExam8_1View, CView)
BEGIN_MESSAGE_MAP(CExam8_1View, CView)
//{{AFX_MSG_MAP(CExam8_1View)
ON_WM_CHAR()
ON_COMMAND(ID_SELECT_FONT, OnSelectFont)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CExam8_1View construction/destruction
CExam8_1View::CExam8_1View()
{
// TODO: add construction code here
}
CExam8_1View::~CExam8_1View()
{
if(pFont)
delete pFont;
}
BOOL CExam8_1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CExam8_1View drawing
void CExam8_1View::OnDraw(CDC* pDC)
{
CExam8_1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//选择字体
CFont *oldFont;
oldFont=pDC->SelectObject(pFont);
//计算字的高度和宽度
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
lHeight=tm.tmHeight +tm.tmExternalLeading;
cWidth=tm.tmAveCharWidth ;
int y=0;//设定纵坐标为0
POSITION pos;
CString line;
if(!(pos=pDoc->lines.GetHeadPosition() ))
return ;
//循环输出各文本行
while(pos!=NULL)
{
line=pDoc->lines.GetNext(pos);
pDC->TextOut(0,y,line,line.GetLength());
//更新y坐标,下一行文本行的位置
y+=lHeight;
}
//恢复原来DC所用的字体
pDC->SelectObject(pFont);
}
/////////////////////////////////////////////////////////////////////////////
// CExam8_1View printing
BOOL CExam8_1View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CExam8_1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CExam8_1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CExam8_1View diagnostics
#ifdef _DEBUG
void CExam8_1View::AssertValid() const
{
CView::AssertValid();
}
void CExam8_1View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CExam8_1Doc* CExam8_1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CExam8_1Doc)));
return (CExam8_1Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CExam8_1View message handlers
void CExam8_1View::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
CDC *pDC=GetDC();
pFont=new CFont();
if(!(pFont->CreateFont (0,0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,
ANSI_CHARSET,OUT_TT_PRECIS,CLIP_TT_ALWAYS,
DEFAULT_QUALITY,DEFAULT_PITCH,"courier New")))
{
pFont->CreateStockObject (SYSTEM_FONT);
}
CFont* oldFont=pDC->SelectObject(pFont);
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
lHeight=tm.tmHeight +tm.tmExternalLeading;
cWidth=tm.tmAveCharWidth ;
pDC->SelectObject(oldFont);
}
void CExam8_1View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CExam8_1Doc* pDoc = GetDocument();
CClientDC dc(this);
//选择当前字体
CFont *oldFont;
oldFont=dc.SelectObject(pFont);CString line("");// 存放编辑器当前字符串
POSITION pos=NULL;//字符串链表位置指示
if(nChar=='\r')//若是回车增加一行
{
pDoc->nLineNum++;
}
else
{
//按行号返回字符串链表中的位置值
pos=pDoc->lines.FindIndex(pDoc->nLineNum);
if(!pos)
{ //没有找到该行号对应的行,因此是一个空行,加入到字符串链表中
line+=(char)nChar;
pDoc->lines.AddTail(CString(line));
}
else
{ //当前文本还没有换行结束,将字符加入到行末
line=pDoc->lines.GetAt(pos);
line+=(char)nChar;
pDoc->lines.SetAt(pos,line);
}
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
// 将修改后的文本行显示到屏幕
dc.TextOut(0,(int)pDoc->nLineNum*tm.tmHeight,line,line.GetLength());
}
pDoc->SetModifiedFlag(TRUE);// 设置文本修改标志
dc.SelectObject(oldFont);
CView::OnChar(nChar, nRepCnt, nFlags);
}
void CExam8_1View::OnSelectFont()
{
// TODO: Add your command handler code here
CFontDialog dlg;
if(dlg.DoModal()==IDOK)
{
LOGFONT LF;
dlg.GetCurrentFont(&LF);//获取当前字体信息
pFont->DeleteObject();//建立新的字体
pFont->CreateFontIndirect(&LF);
this->Invalidate();
UpdateWindow();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -