1.
BOOL CxxxxView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
BOOL ret=CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
//创建文本插入符
CClientDC dc(this);
TEXTMETRIC tm;                                        
dc.GetTextMetrics(&tm);                              
CreateSolidCaret(tm.tmAveCharWidth/8, tm.tmHeight);
ShowCaret();
//
return ret;
}
2. CxxxxView::CEditViewView()
{
// TODO: add construction code here
m_ptOrigin.x=0;
m_ptOrigin.y=0;
}
3.
void CEditViewView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
// TODO: Add your message handler code here and/or call default
afxDump << nChar << "\n";
CClientDC dc(this);
TEXTMETRIC tm;
dc.GetTextMetrics (&tm);
//
if (0x0d == nChar)
{
m_strLine.Empty();
m_ptOrigin.y += tm.tmHeight;
}
else if (0x08 == nChar)
{
m_strLine = m_strLine.Left(m_strLine.GetLength() - 1);
}
else
{
m_strLine += nChar;
m_ptOrigin.x += tm.tmAveCharWidth; }
SetCaretPos(m_ptOrigin);
//
Invalidate();
//
CView::OnChar(nChar, nRepCnt, nFlags);
}
4. 
void CEditViewView::OnDraw(CDC* pDC)
{
CEditViewDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
afxDump << m_strLine << "\n";
pDC->TextOut (m_ptOrigin.x, m_ptOrigin.y, m_strLine);
}

解决方案 »

  1.   

    差不多了:// EditViewView.cpp : implementation of the CEditViewView class
    //#include "stdafx.h"
    #include "EditView.h"#include "EditViewDoc.h"
    #include "EditViewView.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CEditViewViewIMPLEMENT_DYNCREATE(CEditViewView, CView)BEGIN_MESSAGE_MAP(CEditViewView, CView)
    //{{AFX_MSG_MAP(CEditViewView)
    ON_WM_CHAR()
    ON_WM_LBUTTONDOWN()
    //}}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()/////////////////////////////////////////////////////////////////////////////
    // CEditViewView construction/destructionCEditViewView::CEditViewView()
    {
    // TODO: add construction code here
    m_ptOrigin.x=0;
    m_ptOrigin.y=0;
    }CEditViewView::~CEditViewView()
    {
    }BOOL CEditViewView::PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs return CView::PreCreateWindow(cs);
    }/////////////////////////////////////////////////////////////////////////////
    // CEditViewView drawingvoid CEditViewView::OnDraw(CDC* pDC)
    {
    CEditViewDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
    CFont *pfntOld=CFont::FromHandle((HFONT)pDC->SelectObject(m_Font));
    pDC->TextOut (m_ptOrigin.x, m_ptOrigin.y, m_strLine);
    pDC->SelectObject(pfntOld);
    }/////////////////////////////////////////////////////////////////////////////
    // CEditViewView printingBOOL CEditViewView::OnPreparePrinting(CPrintInfo* pInfo)
    {
    // default preparation
    return DoPreparePrinting(pInfo);
    }void CEditViewView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add extra initialization before printing
    }void CEditViewView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add cleanup after printing
    }/////////////////////////////////////////////////////////////////////////////
    // CEditViewView diagnostics#ifdef _DEBUG
    void CEditViewView::AssertValid() const
    {
    CView::AssertValid();
    }void CEditViewView::Dump(CDumpContext& dc) const
    {
    CView::Dump(dc);
    }CEditViewDoc* CEditViewView::GetDocument() // non-debug version is inline
    {
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEditViewDoc)));
    return (CEditViewDoc*)m_pDocument;
    }
    #endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
    // CEditViewView message handlers
    void CEditViewView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
    {
    // TODO: Add your message handler code here and/or call default
    CClientDC dc(this);
    //
    CSize sz(0,0);
    CFont *pfntOld=CFont::FromHandle((HFONT)dc.SelectObject(m_Font));
    CPoint pt=GetCaretPos();
    // afxDump << pt.x << "\n";
    if (0x0d == nChar)
    {
    GetTextExtentPoint32(dc.m_hDC,m_strLine,m_strLine.GetLength(),&sz);
    // Origin.x not changed
    m_ptOrigin.y += sz.cy;
    // caret back 
    sz.cx = -sz.cx;
    // clear
    m_strLine.Empty();
    }
    else if (0x08 == nChar)
    {
    CString LastOne  = m_strLine.Right(1);
    m_strLine = m_strLine.Left(m_strLine.GetLength() - 1);
    GetTextExtentPoint32(dc.m_hDC,LastOne,sizeof(TCHAR),&sz);
    sz.cx=-sz.cx;
    sz.cy=0;
    }
    else
    {// CALCULATE caret
    m_strLine += nChar;
    GetTextExtentPoint32(dc.m_hDC,CString(nChar),sizeof(TCHAR),&sz);
    sz.cy=0;
    }
    dc.SelectObject(pfntOld->m_hObject);
    pt.x     +=sz.cx;
    pt.y     +=sz.cy;
    SetCaretPos(pt);
    pt=GetCaretPos();
    // redraw 
    Invalidate();
    //
    CView::OnChar(nChar, nRepCnt, nFlags);
    }void CEditViewView::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    m_strLine.Empty();
    m_ptOrigin = point;
    SetCaretPos(point);
    //
    CView::OnLButtonDown(nFlags, point);
    }void CEditViewView::OnInitialUpdate() 
    {
    CView::OnInitialUpdate();
    // font
    LOGFONT lf;
    memset(&lf, 0, sizeof(LOGFONT));   // zero out structure
    lf.lfHeight = 36;                // request a 12-pixel-height font
    strcpy(lf.lfFaceName, "Arial");    // request a face name "Arial"
    m_Font.CreateFontIndirect(&lf);   // create the font
    SetFont(&m_Font);
    //创建文本插入符
    CClientDC dc(this);
    CFont *pfntOld=CFont::FromHandle((HFONT)dc.SelectObject(m_Font));
    dc.GetTextMetrics(&m_tm);                              
    CreateSolidCaret(m_tm.tmAveCharWidth/8, m_tm.tmHeight);
    dc.SelectObject(pfntOld->m_hObject);
    //
    SetCaretPos(CPoint(0,0));
    ShowCaret();
    }