将WM_GETTEXT 和WM_GETTEXTLENGTH 的消息映射条目添加到派生的CEdit 类:
BEGIN_MESSAGE_MAP( CMyEdit, CEdit )
ON_WM_GETTEXT()
ON_WM_GETTEXTLENGTH()
END_MESSAGE_MAP()
当我们覆盖这些消息时,我们需要一种方法来获取编辑控件的原始文本,而无需进行无休止的递归。为此我们可以直接调用名为DefWindowProc的默认窗口过程:
CStringW CMyEdit::GetTextInternal()
{
CStringW text;
LRESULT len = DefWindowProcW( WM_GETTEXTLENGTH, 0, 0 );
if( len > 0 )
{
// WPARAM = len + 1 because the length must include the null terminator.
len = DefWindowProcW( WM_GETTEXT, len + 1, reinterpret_cast<LPARAM>( text.GetBuffer( len ) ) );
text.ReleaseBuffer( len );
}
return text;
}
以下方法获取原始窗口文本并对其进行转换。在这里一切皆有可能,包括在 hex 和 dec 之间转换的示例。为简单起见,我只是将文本括在破折号中。
CStringW CMyEdit::GetTransformedText()
{
CStringW text = GetTextInternal();
return L"--" + text + L"--";
}
现在是WM_GETTEXT 的实际处理程序,它将转换后的文本复制到输出缓冲区。
int CMyEdit::OnGetText( int cchDest, LPWSTR pDest )
{
// Sanity checks
if( cchDest <= 0 || ! pDest )
return 0;
CStringW text = GetTransformedText();
// Using StringCchCopyExW() to make sure that we don't write outside of the bounds of the pDest buffer.
// cchDest defines the maximum number of characters to be copied, including the terminating null character.
LPWSTR pDestEnd = nullptr;
HRESULT hr = StringCchCopyExW( pDest, cchDest, text.GetString(), &pDestEnd, nullptr, 0 );
// If our text is greater in length than cchDest - 1, the function will truncate the text and
// return STRSAFE_E_INSUFFICIENT_BUFFER.
if( SUCCEEDED( hr ) || hr == STRSAFE_E_INSUFFICIENT_BUFFER )
{
// The return value is the number of characters copied, not including the terminating null character.
return pDestEnd - pDest;
}
return 0;
}
WM_GETTEXTLENGTH 的处理程序不言自明:
UINT CMyEdit::OnGetTextLength()
{
return GetTransformedText().GetLength();
}