Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

font problem with forms and paintboxes

Posted on 1998-01-27
5
Medium Priority
?
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

597 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