虽然THI s是一个古老的线程,我只花了大部分今天上午的寻找方法识别Windows的代码页给出一个特定的字符集ID(在当前的键盘布局/语言环境未设置为字符集)。我想到示例代码可能会用于其他人寻找类似信息。
在我的情况,我想映射到等效的Windows代码页,1253字符集的值,如161(希腊)挖的很多后,我想出了以下内容:
/*
* 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;
}
希望这对别人有用!
约翰
这种方法有一个缺陷:如果默认的键盘布局不对应于系统代码页(CP_ACP),那么代码页将是不正确的应用程序启动时。 – 2009-08-23 04:20:39