Solved

Custom Component - Where's the canvas?

Posted on 2006-10-31
6
215 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

21 Experts available now in Live!

Get 1:1 Help Now