慕码人2483693
Windows API 都有两个版本,你直接用 Unicode 版本不就得了。为什么非要转两次呢?你转成本地编码,系统内部最终还是转成 Unicode 并调用了 Unicode 版本的 API,纯属折腾。比如 TextOut,你可以显式调用 TextOutW,调用之前,使用 MultiByteToWideChar 将 UTF-8 转换为 Windows Unicode (UTF-16LE)。例如,将 TextOut 包装为 GdiTextOut,分别包装为 UTF-8 和 UTF-16LE 两个版本:BOOL WINAPI GdiTextOut(
HDC hdc, int nXStart, int nYStart,
PCWSTR lpString, int cchString
){ return ::TextOutW(hdc, nXStart, nYStart, lpString, cchString);
}
BOOL WINAPI GdiTextOut(
HDC hdc, int nXStart, int nYStart,
PCSTR lpString, int cchString
){
WCHAR wszBuffer[1024] = { 0 };
DWORD cchBuffer = sizeof(wszBuffer) / sizeof(WCHAR); int cchResult = MultiByteToWideChar(CP_UTF8, 0, lpString, -1, wszBuffer, cchBuffer); if (cchResult > 0)
{ return GdiTextOut(hdc, nXStart, nYStart, wszBuffer, cchResult);
}
BOOL bResult = FALSE; int cchNeeded = MultiByteToWideChar(CP_UTF8, 0, lpString, -1, NULL, 0); if (cchNeeded > 0)
{
HANDLE hHeap = GetProcessHeap();
PWSTR lpBuffer = (PWSTR)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, cchNeeded * sizeof(WCHAR)); if (lpBuffer != NULL)
{ int cchResult = MultiByteToWideChar(CP_UTF8, 0, lpString, -1, lpBuffer, cchNeeded); if (cchResult > 0)
{
bResult = GdiTextOut(hdc, nXStart, nYStart, lpBuffer, cchResult);
}
HeapFree(hHeap, 0, (void *)lpBuffer);
}
} return bResult;
}