按照例程在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 } |
本文介绍了一种在Windows应用程序中使用DrawText替代TextOut的方法,以实现多行文本的输入与显示。通过OnChar处理键盘输入并在OnDraw中重绘窗口内容,实现了基本的文本编辑功能。
1508

被折叠的 条评论
为什么被折叠?



