虽然这是一个旧线程,但我今天早上的大部分时间都在寻找一种方法来识别给定特定字符集ID 的 Windows代码页(当当前键盘布局/区域设置未设置为该字符集时)。我认为示例代码可能对寻找类似信息的其他人有用。
就我而言,我想将 161(希腊语)等字符集值映射到等效的 Windows 代码页 1253。经过大量挖掘后,我得出以下结论:
/*
* Convert a font charset value (e.g. 161 - Greek) into a Windows codepage (1253 for Greek)
*/
UINT CodepageFromCharset(UINT nCharset)
{
UINT nCodepage = CP_ACP;
CHARSETINFO csi = {0};
// Note, the symbol charset (2, CS_SYMBOL) translates to the symbol codepage (42, CP_SYMBOL).
// However, this codepage does NOT produce valid character translations so the ANSI charset
// (ANSI_CHARSET) is used instead. This appears to be a known problem.
// See this discussion: "More than you ever wanted to know about CP_SYMBOL"
// (http://www.siao2.com/2005/11/08/490495.aspx)
if (nCharset == SYMBOL_CHARSET) nCharset = 0;
DWORD* lpdw = (DWORD*)nCharset;
// Non-zero return value indicates success...
if (TranslateCharsetInfo(lpdw, &csi, TCI_SRCCHARSET) == 0)
{
// This should *not* happen but just in case make sure we use a valid default codepage.
#ifdef _UNICODE
csi.ciACP = 1200;
#else
csi.ciACP = CP_ACP;
#endif
}
return csi.ciACP;
}
希望这对其他人有用!
约翰