CRichEditCtrl::GetSelText() 无法正常工作
MFC 文件:winctrl4.cpp
(C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\src\mfc)
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,...
后,整个字符串不存在。
我在 my 中看到了类似的行为em> 自定义代码,由于 WCHAR
问题(一个字节中包含零),而预期是 CHAR
但这是 MFC/Win32 的一部分!可能我的 .rc 文件设置有问题吗?是否有与此相关的 Create 样式?或者既然我们为相关控件创建了一个 CFont,那么可能会搞砸吗?
MFC File: winctrl4.cpp
(C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\src\mfc)
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);
}
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是正确的 MFC 源代码,您是否编辑过它?使用 CStringA 和 LPSTR 是非常不合适的,实际代码使用 CString 和 LPTSTR 以便正确处理 Unicode。是的,正如发布的那样,代码只会返回一个字符。
查看版本有帮助。该错误在此 反馈文章。 如果您无法合理升级到VS2008 SP1,您可以从CRichEditCtrl 派生您自己的类并替换该函数。例如:
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.
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:
要获取宽字符字符串,您必须使用 EM_GETTEXTEX 消息。 CRichEditCtrl 源不包含利用此类消息的方法。
这是 GetSelText() 的正确实现,它实际上返回 Unicode 字符:
1200 这里 表示 UTF-16LE
To get a wide char string you have to use the EM_GETTEXTEX message. CRichEditCtrl source does not contain a method which utilizes such message.
Here is a correct implementation of GetSelText() which actually does return Unicode characters:
1200 here means UTF-16LE