一些WinAPI 处理 字符的函数和连接(GetACP和SetThreadLocale最重要,还有SetConsoleCP)

虽然东西都是现成的。但是也要脑子里有个概念。

// 地区与语言
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

  1. int main(int argc, char *argv[])

  2. {

  3. QCoreApplication a(argc, argv);

  4. QString unicodeString(QChar(0x9788));

  5. QTextStream qStdOut(stdout, QIODevice::WriteOnly);

  6. DWORD dwWritten;

  7.  

  8. // 1st try: replace oem with ascii

  9. #ifdef _WIN32

  10. std::cout << "Switch input to Ascii CodePage (1252): " << (::SetConsoleCP(::GetACP())?"ok":"fail") << std::endl;

  11. std::cout << "Switch output to Ascii CodePage (1252): " << (::SetConsoleOutputCP(::GetACP())?"ok":"fail") << std::endl;

  12. std::cout << "Current input CodePage: " << (unsigned int)::GetConsoleCP() << std::endl;

  13. std::cout << "Current output CodePage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;

  14. #endif

  15. qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');

  16. qStdOut.flush();

  17. qStdOut.setCodec("UTF-16");

  18. qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');

  19. qStdOut.flush();

  20. std::cout << "cout: " << (char*)unicodeString.utf16() << std::endl;

  21. std::cout << "cout: " << (char*)(unicodeString.toUtf8().constData()) << std::endl;

  22. std::wcout << L"wcout: " << (wchar_t*)unicodeString.utf16() << std::endl;

  23. std::wcout << L"wcout: " << (char*)(unicodeString.toUtf8().constData()) << std::endl;

  24. printf("printf: %ls\n", unicodeString.utf16());

  25. wprintf(L"wprintf: %ls\n", unicodeString.utf16());

  26. #ifdef _WIN32

  27. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);

  28. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);

  29. #endif

  30.  

  31. // 2nd try : set CP to utf-16

  32. #ifdef _WIN32

  33. std::cout << "\nSet input CP to ucs2: " << (::SetConsoleCP(1200)?"ok":"fail") << std::endl;

  34. std::cout << "Set output CP to ucs2: " << (::SetConsoleOutputCP(1200)?"ok":"fail") << std::endl;

  35. std::cout << "Current input codepage: " << (unsigned int)::GetConsoleCP() << std::endl;

  36. std::cout << "Current output codepage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;

  37. #endif

  38. qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');

  39. qStdOut.flush();

  40. std::cout << "cout: " << (char*)unicodeString.utf16() << std::endl;

  41. std::wcout << L"wcout: " << (wchar_t*)unicodeString.utf16() << std::endl;

  42. printf("printf: %ls\n", unicodeString.utf16());

  43. wprintf(L"wprintf: %ls\n", unicodeString.utf16());

  44. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);

  45. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);

  46.  

  47. // 3rd try : set CP to utf-8

  48. #ifdef _WIN32

  49. std::cout << "\nSet input CP to utf-8: " << (::SetConsoleCP(65001)?"ok":"fail") << std::endl;

  50. std::cout << "Set output CP to utf-8: " << (::SetConsoleOutputCP(65001)?"ok":"fail") << std::endl;

  51. std::cout << "Current input codepage: " << (unsigned int)::GetConsoleCP() << std::endl;

  52. std::cout << "Current output codepage: " << (unsigned int)::GetConsoleOutputCP() << std::endl;

  53. #endif

  54. qStdOut.setCodec("UTF-8");

  55. qStdOut << QString("QTextStream: ") << unicodeString << QChar('\n');

  56. qStdOut.flush();

  57. std::cout << "cout: " << (char*)unicodeString.toUtf8().constData() << std::endl;

  58. std::wcout << L"wcout: " << (char*)unicodeString.toUtf8().constData() << std::endl;

  59. printf("printf: %ls\n", unicodeString.toUtf8().constData());

  60. wprintf(L"wprintf: %ls\n", unicodeString.toUtf8().constData());

  61. #ifdef _WIN32

  62. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"WriteConsoleW: ", 15, &dwWritten, NULL);

  63. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), (unicodeString + '\n').utf16(), unicodeString.length()+1, &dwWritten, NULL);

  64. #endif

  65.  

  66. return a.exec();

  67. }

To copy to clipboard, switch view to plain text mode 


The resulting output is awfull...

Qt Code:

Switch view

    1. Switch input to Ascii CodePage (1252): ok

    2. Switch output to Ascii CodePage (1252): ok

    3. Current input CodePage: 1252

    4. Current output CodePage: 1252

    5. QTextStream: ?

    6. Q T e x t S t r e a m : &#710;—

    7. cout: &#710;—

    8. cout: é&#382;ˆ

    9. wcout: printf:

    10. wprintf: WriteConsoleW: &#38792;

    11.  

    12. Set input CP to ucs2: fail

    13. Set output CP to ucs2: fail

    14. Current input codepage: 1252

    15. Current output codepage: 1252

    16. Q T e x t S t r e a m : &#710;—

    17. cout: &#710;—

    18. printf:

    19. wprintf: WriteConsoleW: &#38792;

    20.  

    21. Set input CP to utf-8: ok

    22. Set output CP to utf-8: ok

    23. Current input codepage: 65001

    24. Current output codepage: 65001

    25. QTextStream: &#38792;

    26. cout: printf:

    27. wprintf: WriteConsoleW: &#38792;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值