虽然东西都是现成的。但是也要脑子里有个概念。
// 地区与语言
GetACP 取得 ANSI code page,法语XP+设置中文内核 = 936 // ShowMessage(IntToStr(GetACP));
GetThreadLocale 法语XP+设置中文内核 = 2052,纯法语 1036 // ShowMessage(IntToStr(GetThreadLocale));
SetThreadLocale
SetConsoleCP
ConvertDefaultLocale
SetLocaleInfo
GetLocaleInfo
GetLocaleInfoEx
GetCPInfo
MAKELCID
GetSystemDefaultLCID = 2052
GetUserDefaultLCID = 2052
LCIDToLocaleName
GetUserDefaultLocaleName // vista
GetSystemDefaultLocaleName
GetSystemDefaultLocaleName
LCIDToLocaleName
LCMapStringEx
National Language Support Functions
http://msdn.microsoft.com/en-us/library/windows/desktop/dd319081(v=vs.85).aspx
Code Page Identifiers
http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx
GetCharABCWidths
GetCharWidth32
--------------------------------------------------------------------------------------------------------
After googling a lot, I saw that lot of people have problems displaying unicode on the console. But I didn't found a solution.
I read that :
1. the console font must be set to lucida TT.
2. the code page must be forced to 1252 (or 65001 for utf-8).
Qt Code:
Switch view
-
int main(int argc, char *argv[])
-
{
-
QCoreApplication a(argc, argv);
-
QTextStream qStdOut(stdout, QIODevice::WriteOnly);
-
DWORD dwWritten;
-
-
// 1st try: replace oem with ascii
-
#ifdef _WIN32
-
std::cout << "Switch input to Ascii CodePage (1252): " << (::SetConsoleCP(::GetACP())?"ok":"fail") << std::endl;
-
std::cout << "Switch output to Ascii CodePage (1252): " << (::SetConsoleOutputCP(::GetACP())?"ok":"fail") << std::endl;
-
std::cout << "Current input CodePage: " << (unsigned int)::GetConsoleCP() << std::endl;
-
std::cout << "Current output CodePage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
-
#endif
-
qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
-
qStdOut.flush();
-
qStdOut.setCodec("UTF-16");
-
qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
-
qStdOut.flush();
-
std::cout << "cout: " << (char*)unicodeString.utf16() << std::endl;
-
std::cout << "cout: " << (char*)(unicodeString.toUtf8().constData()) << std::endl;
-
std::wcout << L"wcout: " << (wchar_t*)unicodeString.utf16() << std::endl;
-
std::wcout << L"wcout: " << (char*)(unicodeString.toUtf8().constData()) << std::endl;
-
printf("printf: %ls\n", unicodeString.utf16());
-
wprintf(L"wprintf: %ls\n", unicodeString.utf16());
-
#ifdef _WIN32
-
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
-
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
-
#endif
-
-
// 2nd try : set CP to utf-16
-
#ifdef _WIN32
-
std::cout << "\nSet input CP to ucs2: " << (::SetConsoleCP(1200)?"ok":"fail") << std::endl;
-
std::cout << "Set output CP to ucs2: " << (::SetConsoleOutputCP(1200)?"ok":"fail") << std::endl;
-
std::cout << "Current input codepage: " << (unsigned int)::GetConsoleCP() << std::endl;
-
std::cout << "Current output codepage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
-
#endif
-
qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
-
qStdOut.flush();
-
std::cout << "cout: " << (char*)unicodeString.utf16() << std::endl;
-
std::wcout << L"wcout: " << (wchar_t*)unicodeString.utf16() << std::endl;
-
printf("printf: %ls\n", unicodeString.utf16());
-
wprintf(L"wprintf: %ls\n", unicodeString.utf16());
-
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
-
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
-
-
// 3rd try : set CP to utf-8
-
#ifdef _WIN32
-
std::cout << "\nSet input CP to utf-8: " << (::SetConsoleCP(65001)?"ok":"fail") << std::endl;
-
std::cout << "Set output CP to utf-8: " << (::SetConsoleOutputCP(65001)?"ok":"fail") << std::endl;
-
std::cout << "Current input codepage: " << (unsigned int)::GetConsoleCP() << std::endl;
-
std::cout << "Current output codepage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;
-
#endif
-
qStdOut.setCodec("UTF-8");
-
qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');
-
qStdOut.flush();
-
std::cout << "cout: " << (char*)unicodeString.toUtf8().constData() << std::endl;
-
std::wcout << L"wcout: " << (char*)unicodeString.toUtf8().constData() << std::endl;
-
printf("printf: %ls\n", unicodeString.toUtf8().constData());
-
wprintf(L"wprintf: %ls\n", unicodeString.toUtf8().constData());
-
#ifdef _WIN32
-
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);
-
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);
-
#endif
-
-
return a.exec();
-
}
To copy to clipboard, switch view to plain text mode
The resulting output is awfull...
Qt Code:
Switch view
-
-
Switch input to Ascii CodePage (1252): ok
-
Switch output to Ascii CodePage (1252): ok
-
Current input CodePage: 1252
-
Current output CodePage: 1252
-
QTextStream: ?
-
Q T e x t S t r e a m : ˆ—
-
cout: ˆ—
-
cout: 鞈
-
wcout: printf:
-
wprintf: WriteConsoleW: 鞈
-
-
Set input CP to ucs2: fail
-
Set output CP to ucs2: fail
-
Current input codepage: 1252
-
Current output codepage: 1252
-
Q T e x t S t r e a m : ˆ—
-
cout: ˆ—
-
printf:
-
wprintf: WriteConsoleW: 鞈
-
-
Set input CP to utf-8: ok
-
Set output CP to utf-8: ok
-
Current input codepage: 65001
-
Current output codepage: 65001
-
QTextStream: 鞈
-
cout: printf:
-
wprintf: WriteConsoleW: 鞈
-