블로그 글을 그대로 올립니다...
TPageControl에서 탭부분만 폰트를 다른것 쓰려고 하면, 있을것 같은 TabFont 같은 프러퍼티가 없습니다. 델파이가 지원하지 않는건지, 아니면 Windows에서 지원하지 않는건지 잘 모르겠지만.. .하여간 지원하지 않습니다.
그런데 이걸 어떻게 해볼라고 뒤져보면 생각보다 힘듭니다. 그러다가 OwnerDraw 기능으로 하면 되겠지 하고 하면 어라? Theme기능이 안되네.. 쩝.. 이거 잘못하면 배보다 배꼽이 커집니다. 나는 그냥 탭 폰트만 바꾸고 싶었을 뿐인데, 그것도 아주 화려하게 색 넣을 필요도 없이 간단히 볼드만 넣고 싶을 뿐인데.....
그래서 구글링을 좀 하면 이런, 이런 자료가 나오지요. 모두들 OwnerDraw:=True로 만들어놓고 열심히 ThemeService를 이용해서 그립니다. 소스를 보자니, 이거참 나중에 이거 가져왔다 유지보수하려먼 머리 깨질것 같습니다. 그냥 탭 폰트 볼드만 주고 싶을 뿐인데....
그래.. 그럼 TPageControl의 폰트는 볼드주고 안의 모든 컨트럴들은 ParentFont := False를 주면... 하고 생각해주지만 이거 역시 그냥 볼드를 주는데, 복잡해지는것 같고, ParentFont := False라는 제약을 두는거 왠지 이상합니다.. 쩝...
그러다가 퍼뜩 든 생각.. 그냥 TabFont 프러퍼티주고, 그냥 오버라이드해서 Font로 설정하는것 내부 TabSheet까지 같은 폰트로 설정하는것 막아버리는거 되는거아닌가?... 그 생각으로 간단히 맹글어봤더니 어라? 그냥 쉽게 됩니다. 혹시나 이런 문제고 고민했던 분들... 그냥 쉽게 살아요.. ^^;
아래는 소스 그대로 올립니다. 정리하고 콤포넌트 만들기 귀찮아서...
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TPageControl = class(ComCtrls.TPageControl)
private
FTabFont: TFont;
procedure CMFontChanged(var Message); message CM_FONTCHANGED;
procedure SetTabFont(const Value: TFont);
procedure TabFontChanged(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property TabFont: TFont read FTabFont write SetTabFont;
end;
TForm1 = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Label1: TLabel;
FontDialog1: TFontDialog;
FontDialog2: TFontDialog;
Button1: TButton;
Button2: TButton;
Button3: TButton;
FontDialog3: TFontDialog;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private { Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TPageControl }
type
TControlAccess = class(TControl);
procedure TPageControl.CMFontChanged(var Message);
begin
NotifyControls(CM_PARENTFONTCHANGED);
end;
constructor TPageControl.Create(AOwner: TComponent);
begin
inherited;
FTabFont := TFont.Create;
FTabFont.OnChange := TabFontChanged;
FTabFont.Assign(Font);
end;
destructor TPageControl.Destroy;
begin
FreeAndNil(FTabFont);
inherited;
end;
procedure TPageControl.SetTabFont(const Value: TFont);
begin
FTabFont.Assign(Value);
end;
procedure TPageControl.TabFontChanged(Sender: TObject);
begin
if HandleAllocated then
begin
Perform(WM_SETFONT, WPARAM(FTabFont.Handle), 0);
Perform(WM_SIZE, 0, 0);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Sender = Button1 then
begin
if FontDialog1.Execute then
PageControl1.TabFont := FontDialog1.Font;
end else if Sender = Button2 then begin
if FontDialog2.Execute then
PageControl1.Font := FontDialog2.Font;
end else begin
if FontDialog3.Execute then
Font := FontDialog3.Font;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FontDialog1.Font := PageControl1.TabFont;
FontDialog2.Font := PageControl1.Font;
FontDialog3.Font := Font;
end;
end.