CRichEditCtrl::GetSelText() 无法正常工作 [英] CRichEditCtrl::GetSelText() is not working right

查看:21
本文介绍了CRichEditCtrl::GetSelText() 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CString CRichEditCtrl::GetSelText() const
{
    ASSERT(::IsWindow(m_hWnd));
    CHARRANGE cr;
    cr.cpMin = cr.cpMax = 0;
    ::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);
    CStringA strText;
    LPSTR lpsz=strText.GetBufferSetLength((cr.cpMax - cr.cpMin + 1)*2); 
    lpsz[0] = NULL;
    ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)lpsz);
    strText.ReleaseBuffer();
    return CString(strText);
}

我遇到了一个奇怪的问题,当我调用它时,它只返回所选字符串的第一个字符.cr 已正确设置,但在 ::SendMessage(m_hWnd, EM_GETSELTEXT,... 之后整个字符串不存在.

I am having a weird problem, when I call this it only returns the first character of the selected string. cr is correctly being set but after ::SendMessage(m_hWnd, EM_GETSELTEXT,... the whole string is not present.

CHAR预料之中.但这是 MFC/Win32 的一部分!我的 .rc 文件是否有可能设置错误?是否有与此相关的创建风格?或者,既然我们为相关控件创建了一个 CFont,那么 会不会搞砸?

I saw similar behavior in my custom code due to WCHAR issues (two-byte character containing a zero in one byte) when CHAR was expected. But this is part of MFC/Win32! Is it possible my .rc file sets something wrong? Is there a Create style relating to this? Or since we create a CFont for the control in question, could that screw it up?

推荐答案

这不是正确的MFC源代码,你编辑了吗?使用 CStringA 和 LPSTR 是非常不合适的,实际代码使用 CString 和 LPTSTR 以便正确处理 Unicode.是的,正如发布的那样,代码只会返回一个字符.

This is not the correct MFC source code, have you edited it? Using CStringA and LPSTR is quite inappropriate, the real code uses CString and LPTSTR so that Unicode is correctly handled. Yes, as posted the code would only return one character.

查看版本有所帮助.此 反馈文章. 如果您无法合理升级到 VS2008 SP1,您可以从 CRichEditCtrl 派生出您自己的类并替换该函数.例如:

Seeing the version helped. The bug is described in this feedback article. If you can't reasonably upgrade to VS2008 SP1, you could derive your own class from CRichEditCtrl and replace the function. For example:

CString CRichEditCtrlFix::GetSelText() const
{
    ASSERT(::IsWindow(m_hWnd));
    CHARRANGE cr;
    cr.cpMin = cr.cpMax = 0;
    ::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);
    CString strText;
    LPTSTR lpsz=strText.GetBufferSetLength((cr.cpMax - cr.cpMin + 1) * 2);
    lpsz[0] = NULL;
    ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)lpsz);
    strText.ReleaseBuffer();
    return CString(strText);
}

这篇关于CRichEditCtrl::GetSelText() 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆