当前位置:文档管理控件网 > 技术中心 >正文

MFC简单文字处理示例


新建单文档项目:Text
声明:

private:
    CBitmap bitmap;
public:
    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
private:
    CPoint m_ptOrigin;
    CString m_strLine;
public:
    afx_msg void OnTimer(UINT_PTR nIDEvent);
private:
    int m_nWidth;

构造:
// CTextView 构造/析构

CTextView::CTextView()
    : m_ptOrigin(0)
    , m_strLine(_T(""))
    , m_nWidth(0)
{
    // TODO: 在此处添加构造代码

}

代码:
// CTextView 绘制

void CTextView::OnDraw(CDC* pDC)
{
    CTextDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    // TODO: 在此处为本机数据添加绘制代码
    CString str = _T("这是一个测试程序");
    pDC->TextOutW(50,50,str);

    CSize sz = pDC->GetTextExtent(str);

    str.LoadStringW(IDS_STRING101);
    pDC->TextOutW(0,100,str);

    pDC->BeginPath();
    pDC->Rectangle(50,50,50+sz.cx,50+sz.cy);
    pDC->EndPath();
    pDC->SelectClipPath(RGN_DIFF);

    for(int i=0; i<300; i+=10)
    {
        pDC->MoveTo(0,i);
        pDC->LineTo(300,i);
        pDC->MoveTo(i,0);
        pDC->LineTo(i,300);
    }
}




// CTextView 消息处理程序


int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;

    // TODO:  在此添加您专用的创建代码
    CClientDC dc(this);
    TEXTMETRIC tm;
    dc.GetTextMetrics(&tm);
    //创建光标
    //CreateSolidCaret(tm.tmAveCharWidth/8, tm.tmHeight);
    //使用CBitmap创建光标
    bitmap.LoadBitmapW(IDB_BITMAP1);
    CreateCaret(&bitmap);
    ShowCaret();

    SetTimer(1,100,NULL);

    return 0;
}


void CTextView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    CClientDC dc(this);
    CFont font;
    font.CreatePointFont(300,_T("楷体"),NULL);
    CFont *pOldFont = dc.SelectObject(&font);
    TEXTMETRIC tm;
    dc.GetTextMetrics(&tm);
    if(0x0d == nChar)
    {
        m_strLine.Empty();
        m_ptOrigin.y += tm.tmHeight;

    }
    else if(0x08 == nChar)
    {
        COLORREF clr = dc.SetTextColor(dc.GetBkColor());
        dc.TextOutW(m_ptOrigin.x, m_ptOrigin.y, m_strLine);
        m_strLine = m_strLine.Left(m_strLine.GetLength()-1);
        dc.SetTextColor(clr);
    }
    else
    {
        CString cs;
        cs.Format(_T("%c"),nChar);
        m_strLine += cs;

        //m_strLine += nChar;
        
    }
    CSize sz = dc.GetTextExtent(m_strLine);

    CPoint pt;
    pt.x = m_ptOrigin.x + sz.cx;
    pt.y = m_ptOrigin.y + sz.cy;
    SetCaretPos(pt);
    dc.TextOutW(m_ptOrigin.x, m_ptOrigin.y, m_strLine);

    dc.SelectObject(pOldFont);

    CView::OnChar(nChar, nRepCnt, nFlags);
}


void CTextView::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    //设置光标位置
    SetCaretPos(point);
    m_strLine.Empty();
    m_ptOrigin = point;

    CView::OnLButtonDown(nFlags, point);
}


void CTextView::OnTimer(UINT_PTR nIDEvent)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    m_nWidth += 5;
    CClientDC dc(this);
    TEXTMETRIC tm;
    dc.GetTextMetrics(&tm);
    CRect rect;
    rect.left = 0;
    rect.top = 200;
    rect.right = m_nWidth;
    rect.bottom = rect.top + tm.tmHeight;

    dc.SetTextColor(RGB(255,0,0));
    CString str;
    str.LoadStringW(IDS_STRING101);
    dc.DrawText(str,rect,DT_LEFT);

    CSize sz = dc.GetTextExtent(str);
    if(m_nWidth > sz.cx)
    {
        m_nWidth = 0;
        dc.SetTextColor(RGB(0,255,0));
        dc.TextOutW(0,200,str);
    }

    CView::OnTimer(nIDEvent);
}


上一篇:

下一篇: 文档管理控件Aspose.Words更新DOC, DOCX文档支持数字签名



单击获得帮助