CEdit换行和GetLine乱码
- CEdit换行:
继承自CEdit的类中处理PreTranslateMessage可换行,类似于:
BOOL CNewEdit::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_RETURN)
{
if (GetFocus() == this)//焦点在当前控件时发送一个'\n'消息
{
SendMessage(WM_CHAR, '\n', 1);
return TRUE;
}
}
}
return CEdit::PreTranslateMessage(pMsg);
}
创建时需要指定ES_MULTILINE和ES_WANTRETURN 风格:
m_newEdit.Create(WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_LEFT|WS_BORDER|ES_WANTRETURN ,m_rcRect,pWnd,1);
- CEdit::GetLine乱码:
CEdit::GetLine获取某一行的数据时有时候得到的是乱码,
TEXTMETRIC tm;
dcMem.GetTextMetrics(&tm);//获取字体信息
int nTxtHieht = (tm.tmHeight /*+ tm.tmExternalLeading*/);
int i, nLineCount = pCurText->get_m_newEdit().GetLineCount();//行数量
CString strText, strLine;
TCHAR lineTextBuf[2048] = {0};
int nCy = rcPos.top;
for (i = 0; i < nLineCount; i++, nCy += nTxtHieht)//多行
{
int lineIndex = pCurText->get_m_newEdit().LineIndex(i);//Index错误时返回-1
if (lineIndex == -1)//index error
continue;
int lineLen = pCurText->get_m_newEdit().LineLength(lineIndex);//Index错误时返回0
if (lineLen == 0)//zero length
continue;
memset(lineTextBuf, 0, 2048);
int lineBufLen = pCurText->get_m_newEdit().GetLine(i, lineTextBuf, 2048);//返回值是真正复制的长度,但是不会复制结尾符
if (lineBufLen == 0)//buflen
continue;
lineTextBuf[lineBufLen] = 0;//GetLine函数不会加上结尾符,需要自己加上 The copied line does not contain a null-termination character.//https://msdn.microsoft.com/en-us/library/7775836w(v=vs.100).aspx
strText = lineTextBuf;
dcMem.TextOut(rcPos.left, nCy, strText);
}