Solved

Custom Component - Where's the canvas?

Posted on 2006-10-31
6
220 Views
Last Modified: 2010-04-05
Hi,

I'm creating a new custom components (TCustomControl decendent) in BDS2006 (.Net for VCL).  I have a procedure which is called with the components caption is changed.  This procedure needs the Canvas to be available as it uses the TextHeight and TextWidth to work out the co-ordinates for the new caption.

The problem is, when I first drop the component onto a form, I get an exception because the canvas isn't available.  Here's the order that things are done:

1. Drop component on form
2. Create method called
3. Caption is set to a default
4. Event triggered for set_Caption
5. ProcessCaption procedure is called
exception raised

I'd be hoping that there'd be a:
6. Control is painted.

What have I got to do to be a ble to get the canvas to be available to the control in more places than just the Paint procedure?  Do I need to declare it as a property or something?

Cheers!

Pag.
0
Comment
Question by:PAG_Promax
[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
  • 3
6 Comments
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 17855669
add the following to your custom control:

...
public
  property Canvas;
0
 

Author Comment

by:PAG_Promax
ID: 17855910
Thanks for that.  Amazing how simple things can be :)
0
 

Author Comment

by:PAG_Promax
ID: 17855922
Oops.  Take that back.  It's giving me :

---------------------------
bds
---------------------------
Control '' has no parent window.
---------------------------
OK  
---------------------------


All I am doing is setting the width of the control to the Canvas.TextWidth('Test') in the OnCreate method (right at the end of the procedure).

Any ideas?

Thanks,

Pag.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 250 total points
ID: 17856868
You need to set the parent property of your control to a valid TWinControl descendant. Something like this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TMyControl = class(TCustomControl)
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    property Canvas;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TMyControl.Create(self) do
  begin
    left:= 10;
    Top:= 10;
    Width:= 50;
    Height:= 50;
  end;
end;

{ TMyControl }

constructor TMyControl.Create(AOwner: TComponent);
begin
  inherited;
  if AOwner is TWinControl
    then Parent:= TWinControl(AOwner);
end;

procedure TMyControl.Paint;
begin
  Canvas.FillRect(ClientRect);
end;

end.
0
 

Author Comment

by:PAG_Promax
ID: 17862816
My component is actually a  TGraphicControl descendent.  Is that a problem?

I've tried what you suggested, and the error message has gone, so I'm assuming it's working properly.

It makes sense what you've suggested to.  So thanks for that.  The points are yours!!

Cheers

Pag
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 17864209
It's fine if your control descends from TGraphicControl. My previous comment related to the parent control. The parent control must descend from TWinControl.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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