Windows APIネタです。CreateFontIndirect()関数を使ってフォントを作成する場合、もしアプリケーションの国際化を考えていらっしゃるのであればLOGFONT構造体のlfCharSet変数の設定をご注意いただきたいと思います。国際化を意識したフォント作成の例をコードで示します。
// logfont.c
// author: Akira Onishi
// cl logfont.c /link gdi32.lib user32.lib
#include <windows.h>
#include <stdio.h>
// デバイスコンテキストのハンドル、フォント名、フォントのポイント数を指定して
// 標準の太さの論理フォントの構造体を取得する
LOGFONT GetLogFontWithCurrentCharset( HDC hdc, LPTSTR pszFontFace, int nPoint )
{
LOGFONT logFont;
POINT pt, ptOrg;
memset(&logFont, 0, sizeof(LOGFONT));
logFont.lfCharSet = GetTextCharset(hdc); // ポイントはここだけ!
logFont.lfClipPrecision = 0;
logFont.lfEscapement = 0;
strcpy( logFont.lfFaceName, pszFontFace );
logFont.lfHeight = nPoint*10;
logFont.lfWidth = 0;
logFont.lfItalic = 0;
logFont.lfOrientation = 0;
logFont.lfOutPrecision = 0;
logFont.lfPitchAndFamily = DEFAULT_PITCH;
logFont.lfQuality = 0;
logFont.lfStrikeOut = 0;
logFont.lfUnderline = 0;
logFont.lfWeight = FW_NORMAL;
pt.x = 0;
pt.y = GetDeviceCaps(hdc, LOGPIXELSY) * logFont.lfHeight;
pt.y /= 720;
DPtoLP( hdc, &pt, 1);
ptOrg.x = 0;
ptOrg.y = 0;
DPtoLP( hdc, &ptOrg, 1);
logFont.lfHeight = -abs(pt.y - ptOrg.y);
return logFont;
}
void main(int argc, char * argv[])
{
LOGFONT logFont;
HDC hdc;
hdc = GetDC(NULL);
// 18ポイントのMS Shell DlgフォントのLOGFONT構造体を取得する
logFont = GetLogFontWithCurrentCharset( hdc, "MS Shell Dlg", 18 );
printf("lfHeight = %ld\n", logFont.lfHeight);
printf("lfWidth = %ld\n", logFont.lfWidth) ;
printf("lfEscapement = %ld\n", logFont.lfEscapement) ;
printf("lfOrientation = %ld\n", logFont.lfOrientation) ;
printf("lfWeight = %ld\n", logFont.lfWeight) ;
printf("lfItalic = %d\n", logFont.lfItalic) ;
printf("lfUnderline = %d\n", logFont.lfUnderline) ;
printf("lfStrikeOut = %d\n", logFont.lfStrikeOut) ;
printf("lfCharSet = %d\n", logFont.lfCharSet) ;
printf("lfOutPrecision = %d\n", logFont.lfOutPrecision) ;
printf("lfClipPrecision = %d\n", logFont.lfClipPrecision) ;
printf("lfQuality = %d\n", logFont.lfQuality) ;
printf("lfPitchAndFamily = %d\n", logFont.lfPitchAndFamily) ;
printf("lfFaceName = %s\n", logFont.lfFaceName) ;
}
ここで使っているMS Shell Dlgは、Windows 2000/XP/2003で使える論理フォント名です。Windows 98/ME/NT4のレジストリには登録されていませんのでご注意ください。
lfCharSet = 0と指定した場合、日本語OSは日本語で文字化けを引き起こします。不思議なのは、韓国語・簡体字中国語・繁体字中国語はlfCharSet = 0としても文字化けなくハングルや漢字が表示されてしまう・・・。何故なんだ>マイクロソフト様