![]() |
RichEditCtrl in Visual C++ 7.0 supports Unicode. To
obtain Unicode strings from the RichEditCtrl box, use
EM_GETTEXTEX message instead of
GetDlgItemText.
SendMessage( EM_GETTEXTEX, (LPARAM) &getTextEx, // GETTEXTEX structure (WPARAM) lpszWChar // output WCHAR array );
This page explains how to obtain Unicode strings using
int nLength = edit->GetTextLengthEx(GTL_DEFAULT,1200); LPWSTR lpszWChar = new WCHAR[nLength+1];
The third parameter for the
GETTEXTEX getTextEx; getTextEx.cb=(nLength+1)*sizeof(WCHAR); getTextEx.codepage=1200; getTextEx.flags=GT_DEFAULT; getTextEx.lpDefaultChar=NULL; getTextEx.lpUsedDefChar=NULL;
By calling the
CRichEditCtrl* edit=(CRichEditCtrl*)GetDlgItem(id); edit->SendMessage(EM_GETTEXTEX, (WPARAM)&getTextEx, (LPARAM)lpszWChar); | ||
Converting the Unicode String into UTF-8 FormatUseATL::AtlUnicodeToUTF8 function to convert the Unicode
string into UTF-8 format. The ATL::AtlUnicodeToUTF8
function is declared in atlenc.h header file.
AtlUnicodeToUTF8( LPCWSTR lpszWChar, // the original Unicode string int nLength, // the length of the Unicode string LPSTR data, // output buffer array int len // the length of the buffer ); The function takes four parameters. The first parameter points to the Unicode string. The second parameter sets the length of the original Unicode string. The third parameter is the buffer where the converted UTF-8 string will be stored. The fourth parameter sets the length of the buffer.
The following code shows how to get the length of the buffer array by
calling the same
int len=ATL::AtlUnicodeToUTF8(lpszWChar, nLength, 0, 0);
The buffer array should be of the size
char *data=new char[len+1]; AtlUnicodeToUTF8(lpszWChar, nLength, data, len);
Lastly, set the null terminal character at the end of the buffer and
delete the
delete [] lpszWChar; data[len]='\0';
The code below shows the actual implementation of the above procedure.
char* CQuoteDlg::GetUtf8String(UINT id)
{
CRichEditCtrl* edit=(CRichEditCtrl*)GetDlgItem(id);
int nLength = edit->GetTextLengthEx(GTL_DEFAULT,1200);
LPWSTR lpszWChar = new WCHAR[nLength+1];
GETTEXTEX getTextEx;
getTextEx.cb=(nLength+1)*sizeof(WCHAR);
getTextEx.codepage=1200;
getTextEx.flags=GT_DEFAULT;
getTextEx.lpDefaultChar=NULL;
getTextEx.lpUsedDefChar=NULL;
edit->SendMessage(EM_GETTEXTEX,
(WPARAM)&getTextEx, (LPARAM)lpszWChar);
int len=ATL::AtlUnicodeToUTF8(lpszWChar, nLength, 0, 0);
char *data=new char[len+1];
AtlUnicodeToUTF8(lpszWChar, nLength, data, len);
delete [] lpszWChar;
data[len]='\0';
return data;
}
|