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.
rossmcmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JimBob091197Commented:
Hi

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
0
rossmcmAuthor Commented:
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.MyPaintboxClick (Sender : TObject) ;

begin
BringToFront ;
invalidate ;
end ;

procedure TMyPaintbox.MyPaintboxPaint (Sender : TObject) ;

begin
canvas.roundrect (0, 0, width, height, 2, 2) ;
canvas.textout (4, 4, canvas.font.name) ;
end ;

procedure TForm1.FormMouseDown(Sender: 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.MyPaintboxDblClick (Sender: TObject);
begin
canvas.font.name      := 'Arial' ;    { ignored }
canvas.font.size      := 10 ;
canvas.font.color     := clBlue ;
invalidate ;
end;


end.
0
JimBob091197Commented:
Hi

The TPaintBox's Canvas gets its font from the TPaintBox's font before the paint event is called.  I.e.  TPaintBox.Canvas.Font gets set to TPaintBox.Font.

To correct your problem, change (in FormMouseDown event)
  Canvas.Font.Name := 'Courier';
  Canvas.Font.Size := 20;
  Etc...
to:
  Font.Name := 'Courier';
  Font.Size := 20;
  Etc...

Also change MyPaintboxDblClick to:
procedure TMyPaintbox.MyPaintboxDblClick (Sender: TObject);
begin
  font.name := 'Arial' ; { ignored }
  font.size := 10 ;
  font.color := clBlue ;
  invalidate ;
end;


Hope this sorts it out!
Regards,
JB
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rossmcmAuthor Commented:
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.
0
JimBob091197Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.