[BCB] LOGFONTとTFontの変換
LOGFONT lf;
//TFont -> LOGFONT
GetObject(Canvas->Font->Handle, sizeof(LOGFONT), &lf);
//LOGFONT -> TFont
Canvas->Font->Handle = CreateFontIndirect(&lf);
| 固定リンク
LOGFONT lf;
//TFont -> LOGFONT
GetObject(Canvas->Font->Handle, sizeof(LOGFONT), &lf);
//LOGFONT -> TFont
Canvas->Font->Handle = CreateFontIndirect(&lf);
| 固定リンク
システムメニューやボタンなどで使用されるフォントを取得する方法です。以下は、システムのGUIで使用されるフォントを取得する例です。
void __fastcall TForm1::FormCreate(TObject *Sender)
{
HFONT hFont;
TLogFont LogFont;
hFont=GetStockObject(DEFAULT_GUI_FONT);
GetObject(hFont,sizeof(LogFont),&LogFont);
}
DEFAULT_GUI_FONTの部分には、以下の値を指定することもできます。
OEM_FIXED_FONT = 10;
ANSI_FIXED_FONT = 11;
ANSI_VAR_FONT = 12;
SYSTEM_FONT = 13;
DEVICE_DEFAULT_FONT = 14;
DEFAULT_PALETTE = 15;
SYSTEM_FIXED_FONT = 16;
DEFAULT_GUI_FONT = 17;
通常、FormやLabelのフォントを変えるときは、コンポーネントをマウスで選択してフォントを変更します。変更するものが少ないときはこれでいいのですが、アプリで使用しているフォントを全部、まとめて変更したいと言うときは、この方法だと、時間がかかりますし、変更もれが出る場合もあります。こういうときは、「*.dfm」ファイルを直接、編集してしまうと楽です。
dfmファイルはただのテキストファイルですのでメモ帳などで開くことができます。やり方です。
(1) 変更したいフォームの.dfmファイルをメモ帳などで開きます。(例:Unit.dfm)
(2) Font.Name = 'MS Pゴシック'といった行がフォントを指定している場所ですので、これを変更したいフォント名に置換してやります。(例:Font.Name = 'MS ゴシック')
正規検索の置換がつかえるエディタを使用してFont.Name = '*'に該当する箇所をすべて変換するようにすれば、もれなく変換することが可能です。
この方法は、海外製のソフトを日本語対応に変換したりするときにも使える方法です。
procedure TForm1.ReadFromInifile(IniFilename: String);
var
ini:TInifile;
FStyle:TFontStyles;
Fs :Byte absolute FStyle;
FPitch:TFontPitch;
Fp :Byte absolute FPitch;
begin
ini:=TInifile.Create(IniFilename);
try
Fs:= ini.ReadInteger('Option','FontStyle',0);
Fp:= ini.ReadInteger('Option','FontPitch',0);
if Fs<>0 then FFont.Style:= FStyle;
if Fp<>0 then FFont.Pitch:= FPitch;
FFont.Color:= StringToColor(Ini.ReadString('Option','FontColor','clBlack'));
FFont.Name := ini.ReadString('Option','FontName','MS ゴシック');
FFont.Size := ini.ReadInteger('Option','FontSize',13);
FFont.Charset:=ini.ReadInteger('Option','FontCharset',SHIFTJIS_CHARSET);
finally
ini.Free;
end;
end;
procedure TForm1.WriteToInifile(IniFilename: String);
var
ini:TInifile;
FStyle:TFontStyles;
Fs :Byte absolute FStyle;
FPitch:TFontPitch;
Fp :Byte absolute FPitch;
begin
ini:=TInifile.Create(IniFilename);
try
FStyle:=FFont.Style;
FPitch:=FFont.Pitch;
ini.WriteInteger('Option','FontStyle',Fs);
ini.WriteInteger('Option','FontPitch',Fp);
ini.ReadString('Option','FontColor',ColorToString(FFont.Color));
ini.WriteString('Option','FontName',FFont.Name);
ini.WriteInteger('Option','FontSize',FFont.Size);
ini.WriteInteger('Option','FontCharset',FFont.Charset);
finally
ini.Free;
end;
end;
CheckBox1->Checked=Memo1->Font->Style.Contains(fsBold);
CheckBox2->Checked=Memo1->Font->Style.Contains(fsItalic);
CheckBox3->Checked=Memo1->Font->Style.Contains(fsUnderline);
//---------------------------------------------------------------------------
void __fastcall TForm1::LinkLabelMouseEnter(TObject *Sender)
{
((TLabel *)Sender)->Font->Color=clRed;
((TLabel *)Sender)->Font->Style=TFontStyles() << fsUnderline;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LinkLabelMouseLeave(TObject *Sender)
{
((TLabel *)Sender)->Font->Color=clNavy;
((TLabel *)Sender)->Font->Style=TFontStyles();
}
//---------------------------------------------------------------------------
C++Builder | C++言語・Pascal言語 | Delphi | TActionList | TColor | TEdit | TeX | TForm | TIniFile | TLabel | TListBox | TListView | TMemo | TProgressBar | TSplitter | TStatusBar | TStringGrid | TTrackBar | TWebBrowser | Windows | その他 | アルゴリズム | システム関連 | ネットワーク関連 | ファイル、フォルダの処理 | フォント関連 | フォーム・ダイアログ関連 | フリーソフト | プログラミング全般 | マウス、キーボードの処理 | 文字列の処理 | 日付時刻の処理 | 画像関連 | 開発環境(IDE)関連