Solved

Custom Component - Where's the canvas?

Posted on 2006-10-31
6
213 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
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.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

19 Experts available now in Live!

Get 1:1 Help Now