需要用int WideCharToMultiByte( UINT CodePage, // code page DWORD dwFlags, // performance and mapping flags LPCWSTR lpWideCharStr, // wide-character string int cchWideChar, // number of chars in string LPSTR lpMultiByteStr, // buffer for new string int cbMultiByte, // size of buffer LPCSTR lpDefaultChar, // default for unmappable chars LPBOOL lpUsedDefaultChar // set when default char used ); 这个函数转换
LPCTSTR 是const wchar_t *
不能直接转换的,用WideCharToMultiByte吧。或者用ATL的W2T之类的宏
UINT CodePage, // code page
DWORD dwFlags, // performance and mapping flags
LPCWSTR lpWideCharStr, // wide-character string
int cchWideChar, // number of chars in string
LPSTR lpMultiByteStr, // buffer for new string
int cbMultiByte, // size of buffer
LPCSTR lpDefaultChar, // default for unmappable chars
LPBOOL lpUsedDefaultChar // set when default char used
);
这个函数转换
CString 操作指南
CString m_strName = _T("TEST");
int mlen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strName, nTextLen + 1, NULL, 0); 错误 3 error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'CString' to 'LPCSTR'
CString m_strName = _T("TEST");
int nTextLen = _tcslen(m_strName);
int mlen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strName, nTextLen + 1, NULL, 0); 错误 3 error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'CString' to 'LPCSTR'
int mlen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strName, nTextLen + 1, NULL, 0);
WCHAR* output = new WCHAR[mlen];
if(output)
{
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strName, nTextLen + 1, output, mlen);
g_xpStyle.DrawThemeText(hTheme, pDC->m_hDC, EBP_NORMALGROUPHEAD, 0, output, -1, DT_LEFT|DT_VCENTER | DT_SINGLELINE|DT_END_ELLIPSIS, 0, &rcHeaderTextArea);
delete output;
}打开UNICODE支持时,编译出错,意思是MultiByteToWideChar函数的第三个参数m_strName不能从CString转换为LPCSTR。
#ifndef UNICODE
int nTextLen = _tcslen(m_strName);
int mlen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strName, nTextLen + 1, NULL, 0);
WCHAR* output = new WCHAR[mlen];
if(output)
{
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strName, nTextLen + 1, output, mlen);
g_xpStyle.DrawThemeText(hTheme, pDC->m_hDC, EBP_NORMALGROUPHEAD, 0, output, -1, DT_LEFT|DT_VCENTER | DT_SINGLELINE|DT_END_ELLIPSIS, 0, &rcHeaderTextArea);
delete output;
}
#else
g_xpStyle.DrawThemeText(hTheme, pDC->m_hDC, EBP_NORMALGROUPHEAD, 0, m_strName, -1, DT_LEFT|DT_VCENTER | DT_SINGLELINE|DT_END_ELLIPSIS, 0, &rcHeaderTextArea);
#endif