learn MFC_1 简单文字及插入符显示

来源:互联网 发布:淘宝的宝贝详情页在哪 编辑:程序博客网 时间:2024/05/19 10:07

按照例程在onchar中textout,发现插入符有残留现象,查阅资料得知应该在ondraw中处理。

后发现textout只能输出单行文字,且不能输出换行等字符,改用drawtext。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void CSDIDemoView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // TODO: Add your message handler code here and/or call default
    CClientDC dc(this);
    CSize textsize = dc.GetTextExtent((LPCTSTR)&nChar);
    CSDIDemoDoc *pDoc = GetDocument();
    pDoc->myString += nChar;
    if(nChar == VK_RETURN)  {//carriage return character
        ptCharacter.y +=textsize.cy;
        ptCharacter.x = 0;
        Invalidate(true);
    }
    else if(nChar == VK_BACK) {
        pDoc->myString.Delete(pDoc->myString.GetLength()-1,2);
        ptCharacter.x -= textsize.cx;
        ptCharacter.x -= textsize.cx;
        Invalidate(true);
    }
    else {
        ptCharacter.x += textsize.cx;
        Invalidate(true);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void CSDIDemoView::OnDraw(CDC* pDC)
{
    CSDIDemoDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
//  pDC->TextOut(0,0,pDoc->myString);
    CRect rc;
    GetClientRect(rc);  //Get Client RECT
    pDC->DrawText(pDoc->myString,&rc,DT_WORDBREAK|DT_LEFT|DT_TOP);
    //caret
    TEXTMETRIC tm;
    pDC->GetTextMetrics(&tm);
    CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);
    SetCaretPos(ptCharacter);
    ShowCaret();
    // TODO: add draw code for native data here
}
1
2
3
4
5
6
7
8
9
void CSDIDemoView::OnSetFocus(CWnd* pOldWnd)
{
 
    Invalidate(true);
    CView::OnSetFocus(pOldWnd);
     
    // TODO: Add your message handler code here
     
}

原创粉丝点击