Solved

font problem with forms and paintboxes

Posted on 1998-01-27
5
291 Views
Last Modified: 2013-12-02
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.
0
Comment
Question by:rossmcm
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1357665
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
 

Author Comment

by:rossmcm
ID: 1357666
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
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 100 total points
ID: 1357667
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
 

Author Comment

by:rossmcm
ID: 1357668
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
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1357669
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now