rossmcm
asked on
font problem with forms and paintboxes
font problem with forms and paintboxes
I have an app that uses a form and objects derived from a paintbox that are created at run-time. In the constructor for the objects I assign to the event handlers and some of the properties. The remainder of the properties are assigned from a text file after the object is created. The problem is that I cannot assign to the font of the paintbox canvas. I read the font name, size and color from the text file and assign them to the paintbox canvas font but the font always reverts back to the same font - the font of the underlying form. This occurs even if I subsequently change the underlying form's font - any objects subsequently created still take on the font that the form had at design-time. All other properties behave fine - apart from the font.
I have set the ParentFont property to FALSE in the constructor.
A paintbox added to the form at design time does not suffer from the problem - only ones created at run-time.
I have an app that uses a form and objects derived from a paintbox that are created at run-time. In the constructor for the objects I assign to the event handlers and some of the properties. The remainder of the properties are assigned from a text file after the object is created. The problem is that I cannot assign to the font of the paintbox canvas. I read the font name, size and color from the text file and assign them to the paintbox canvas font but the font always reverts back to the same font - the font of the underlying form. This occurs even if I subsequently change the underlying form's font - any objects subsequently created still take on the font that the form had at design-time. All other properties behave fine - apart from the font.
I have set the ParentFont property to FALSE in the constructor.
A paintbox added to the form at design time does not suffer from the problem - only ones created at run-time.
ASKER
Hi,
I agree I must be doing something wrong! The code you supplied works fine. What follows below doesn't. I will be interested to know why.
My paint method for the paintbox is a method of the paintbox, rather than the form as it was in your example. That seems to make no difference.
Thanks for your time
Ross.
.......................... .......... ....
unit Pbform;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, stdctrls, extctrls ;
type
TForm1 = class(TForm)
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMyPaintbox = class (TPaintBox)
private
procedure MyPaintboxClick (Sender : TObject) ;
procedure MyPaintboxDblClick (Sender : TObject) ;
procedure MyPaintboxPaint (Sender :TObject) ;
constructor Create (Owner : TComponent) ; override ;
destructor Destroy ; override ;
end ;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
MyPaintbox : TMyPaintbox ;
constructor TMyPaintbox.Create (Owner : TComponent) ;
begin
inherited Create (owner) ;
width := 100 ;
height := 25 ;
parent := TForm1 (owner) ;
visible := true ;
enabled := true ;
OnClick := MyPaintboxClick ;
OnDblClick := MyPaintboxDblClick ;
OnPaint := MyPaintboxPaint ;
ParentFont := false ;
canvas.font.name := 'Courier' ;
canvas.font.size := 6 ;
canvas.font.color := clBlack ;
end ;
destructor TMyPaintbox.Destroy ;
begin
inherited Destroy ;
end ;
procedure TMyPaintbox.MyPaintboxClic k (Sender : TObject) ;
begin
BringToFront ;
invalidate ;
end ;
procedure TMyPaintbox.MyPaintboxPain t (Sender : TObject) ;
begin
canvas.roundrect (0, 0, width, height, 2, 2) ;
canvas.textout (4, 4, canvas.font.name) ;
end ;
procedure TForm1.FormMouseDown(Sende r: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
MyPaintbox := TMyPaintbox.create (self) ;
with MyPaintbox do
begin
left := X ;
top := Y ;
canvas.font.name := 'Courier' ; { ignored }
canvas.font.size := 20 ;
canvas.font.color := clRed ;
end ;
end;
procedure TMyPaintbox.MyPaintboxDblC lick (Sender: TObject);
begin
canvas.font.name := 'Arial' ; { ignored }
canvas.font.size := 10 ;
canvas.font.color := clBlue ;
invalidate ;
end;
end.
I agree I must be doing something wrong! The code you supplied works fine. What follows below doesn't. I will be interested to know why.
My paint method for the paintbox is a method of the paintbox, rather than the form as it was in your example. That seems to make no difference.
Thanks for your time
Ross.
..........................
unit Pbform;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, stdctrls, extctrls ;
type
TForm1 = class(TForm)
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMyPaintbox = class (TPaintBox)
private
procedure MyPaintboxClick (Sender : TObject) ;
procedure MyPaintboxDblClick (Sender : TObject) ;
procedure MyPaintboxPaint (Sender :TObject) ;
constructor Create (Owner : TComponent) ; override ;
destructor Destroy ; override ;
end ;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
MyPaintbox : TMyPaintbox ;
constructor TMyPaintbox.Create (Owner : TComponent) ;
begin
inherited Create (owner) ;
width := 100 ;
height := 25 ;
parent := TForm1 (owner) ;
visible := true ;
enabled := true ;
OnClick := MyPaintboxClick ;
OnDblClick := MyPaintboxDblClick ;
OnPaint := MyPaintboxPaint ;
ParentFont := false ;
canvas.font.name := 'Courier' ;
canvas.font.size := 6 ;
canvas.font.color := clBlack ;
end ;
destructor TMyPaintbox.Destroy ;
begin
inherited Destroy ;
end ;
procedure TMyPaintbox.MyPaintboxClic
begin
BringToFront ;
invalidate ;
end ;
procedure TMyPaintbox.MyPaintboxPain
begin
canvas.roundrect (0, 0, width, height, 2, 2) ;
canvas.textout (4, 4, canvas.font.name) ;
end ;
procedure TForm1.FormMouseDown(Sende
Shift: TShiftState; X, Y: Integer);
begin
MyPaintbox := TMyPaintbox.create (self) ;
with MyPaintbox do
begin
left := X ;
top := Y ;
canvas.font.name := 'Courier' ; { ignored }
canvas.font.size := 20 ;
canvas.font.color := clRed ;
end ;
end;
procedure TMyPaintbox.MyPaintboxDblC
begin
canvas.font.name := 'Arial' ; { ignored }
canvas.font.size := 10 ;
canvas.font.color := clBlue ;
invalidate ;
end;
end.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That cured it all right. Many thanks. I am baffled however as to why the paintbox itself has a font property - what is this used for ?
Thanks again,
Ross.
Thanks again,
Ross.
I think the paintbox has a font property so that at design-time it appears in the Object Inspector. (The canvas property doesn't appear there...)
JB
JB
There must be something else wrong. The following code works fine at run-time: (PBox is a control inherited from TPaintBox.)
procedure TForm1.FormCreate(Sender: TObject);
var
PBox: TNewPaintBox;
begin
PBox := TPaintBox.Create(Self);
PBox.Parent := Self;
PBox.ParentFont := False;
PBox.Font.Style := [fsBold];
PBox.Font.Name := 'Times New Roman';
PBox.Font.Size := 18;
PBox.OnPaint := DoPaint;
end;
procedure TForm1.DoPaint(Sender: TObject);
begin
with TPaintBox(Sender) do
Canvas.TextOut(10, 10, 'Hello');
end;
Could you provide some of your code?
JB