CEdit换行和GetLine乱码

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_MULTILINEES_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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值