sccheung
asked on
Error message:Control " has no parent control.
I learn component writing from the online help on Delphi. When I try to write my first component. But I get Control " has no parent window when I try to use it. Although it is not completed, it should at least display something, not a error message. The source is:
unit ButtonPro;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TButtonPro = class(TCustomControl)
private
{ Private declarations }
FNormalPic, FActivePic, FPressingPic, FDisabledPic, FDownPic: TBitmap;
FBtnDown, FStretch: Boolean;
FTranspColor, FBkColor: TColor;
procedure UpdatePic(NewPic: TBitmap);
procedure SetNormalPic(Value: TBitmap);
procedure SetActivePic(Value: TBitmap);
procedure SetPressingPic(Value: TBitmap);
procedure SetDisabledPic(Value: TBitmap);
procedure SetDownPic(Value: TBitmap);
procedure SetTranspColor(Value: TColor);
procedure SetBkColor(Value: TColor);
protected
{ Protected declarations }
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure WMLButtonDown(var Message: TMessage); message WM_LBUTTONDOWN;
procedure WMLButtonUp(var Message: TMessage); message WM_LBUTTONUP;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
Property NormalPicture: TBitmap read FNormalPic write SetNormalPic;
Property ActivePicture: TBitmap read FActivePic write SetActivePic;
Property PressingPicture: TBitmap read FPressingPic write SetPressingPic;
Property DisabledPicture: TBitmap read FDisabledPic write SetDisabledPic;
Property DownPicture: TBitmap read FDownPic write SetDownPic;
Property Down: Boolean read FBtnDown write FBtnDown;
Property Stretch: Boolean read FStretch write FStretch;
Property TransparentColor: TColor read FTranspColor write SetTranspColor default clBackground;
Property BackgroundColor: TColor read FBkColor write SetBkColor default clBackground;
Property Height default 50;
Property Width default 50;
Property Visible;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('My Components', [TButtonPro]);
end;
procedure TButtonPro.CMMouseEnter(va r Message: TMessage);
begin
UpdatePic(FActivePic);
inherited;
end;
procedure TButtonPro.CMMouseLeave(va r Message: TMessage);
begin
If Enabled = False then
UpdatePic(FDisabledPic)
else if FBtnDown = True then
UpdatePic(FDownPic)
else UpdatePic(FNormalPic);
inherited;
end;
procedure TButtonPro.WMLButtonDown(v ar Message: TMessage);
begin
inherited;
UpdatePic(FDownPic);
end;
procedure TButtonPro.WMLButtonUp(var Message: TMessage);
begin
inherited;
If not Enabled then
UpdatePic(FDisabledPic)
else if FBtnDown then
UpdatePic(FDownPic)
else UpdatePic(FNormalPic);
end;
constructor TButtonPro.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Height := 50;
Width := 50;
FNormalPic := TBitmap.Create;
FActivePic := TBitmap.Create;
FPressingPic := TBitmap.Create;
FDisabledPic := TBitmap.Create;
FDownPic := TBitmap.Create;
FBtnDown := False;
FTranspColor := clBackground;
FBkColor := clBackground;
UpdatePic(FNormalPic);
end;
destructor TButtonPro.Destroy;
begin
FNormalPic.Free;
FActivePic.Free;
FPressingPic.Free;
FDisabledPic.Free;
FDownPic.Free;
inherited Destroy;
end;
procedure TButtonPro.UpdatePic(NewPi c: TBitmap);
var TmpRect: TRect;
begin
TmpRect := Rect(0,0,NewPic.Width,NewP ic.Height) ;
Canvas.Brush.Color := FBkColor;
If NewPic.Empty then Canvas.FillRect(BoundsRect )
else If FStretch then Canvas.BrushCopy(BoundsRec t,NewPic,T mpRect,FTr anspColor)
else Canvas.BrushCopy(TmpRect,N ewPic,TmpR ect,FTrans pColor);
Invalidate;
end;
procedure TButtonPro.SetBkColor(Valu e: TColor);
begin
FBkColor := Value;
UpdatePic(FNormalPic);
end;
procedure TButtonPro.SetTranspColor( Value: TColor);
begin
FTranspColor := Value;
UpdatePic(FNormalPic);
end;
procedure TButtonPro.SetNormalPic(Va lue: TBitmap);
begin
FNormalPic.Assign(Value);
UpdatePic(FNormalPic);
If not Stretch then Height := FNormalPic.Height;
end;
procedure TButtonPro.SetActivePic(Va lue: TBitmap);
begin
FActivePic.Assign(Value);
end;
procedure TButtonPro.SetPressingPic( Value: TBitmap);
begin
FPressingPic.Assign(Value) ;
end;
procedure TButtonPro.SetDisabledPic( Value: TBitmap);
begin
FDisabledPic.Assign(Value) ;
end;
procedure TButtonPro.SetDownPic(Valu e: TBitmap);
begin
FDownPic.Assign(Value);
end;
end.
What's wrong with my code? Please tell me. It is very easy for any people who know component writing.
unit ButtonPro;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TButtonPro = class(TCustomControl)
private
{ Private declarations }
FNormalPic, FActivePic, FPressingPic, FDisabledPic, FDownPic: TBitmap;
FBtnDown, FStretch: Boolean;
FTranspColor, FBkColor: TColor;
procedure UpdatePic(NewPic: TBitmap);
procedure SetNormalPic(Value: TBitmap);
procedure SetActivePic(Value: TBitmap);
procedure SetPressingPic(Value: TBitmap);
procedure SetDisabledPic(Value: TBitmap);
procedure SetDownPic(Value: TBitmap);
procedure SetTranspColor(Value: TColor);
procedure SetBkColor(Value: TColor);
protected
{ Protected declarations }
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure WMLButtonDown(var Message: TMessage); message WM_LBUTTONDOWN;
procedure WMLButtonUp(var Message: TMessage); message WM_LBUTTONUP;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
Property NormalPicture: TBitmap read FNormalPic write SetNormalPic;
Property ActivePicture: TBitmap read FActivePic write SetActivePic;
Property PressingPicture: TBitmap read FPressingPic write SetPressingPic;
Property DisabledPicture: TBitmap read FDisabledPic write SetDisabledPic;
Property DownPicture: TBitmap read FDownPic write SetDownPic;
Property Down: Boolean read FBtnDown write FBtnDown;
Property Stretch: Boolean read FStretch write FStretch;
Property TransparentColor: TColor read FTranspColor write SetTranspColor default clBackground;
Property BackgroundColor: TColor read FBkColor write SetBkColor default clBackground;
Property Height default 50;
Property Width default 50;
Property Visible;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('My Components', [TButtonPro]);
end;
procedure TButtonPro.CMMouseEnter(va
begin
UpdatePic(FActivePic);
inherited;
end;
procedure TButtonPro.CMMouseLeave(va
begin
If Enabled = False then
UpdatePic(FDisabledPic)
else if FBtnDown = True then
UpdatePic(FDownPic)
else UpdatePic(FNormalPic);
inherited;
end;
procedure TButtonPro.WMLButtonDown(v
begin
inherited;
UpdatePic(FDownPic);
end;
procedure TButtonPro.WMLButtonUp(var
begin
inherited;
If not Enabled then
UpdatePic(FDisabledPic)
else if FBtnDown then
UpdatePic(FDownPic)
else UpdatePic(FNormalPic);
end;
constructor TButtonPro.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Height := 50;
Width := 50;
FNormalPic := TBitmap.Create;
FActivePic := TBitmap.Create;
FPressingPic := TBitmap.Create;
FDisabledPic := TBitmap.Create;
FDownPic := TBitmap.Create;
FBtnDown := False;
FTranspColor := clBackground;
FBkColor := clBackground;
UpdatePic(FNormalPic);
end;
destructor TButtonPro.Destroy;
begin
FNormalPic.Free;
FActivePic.Free;
FPressingPic.Free;
FDisabledPic.Free;
FDownPic.Free;
inherited Destroy;
end;
procedure TButtonPro.UpdatePic(NewPi
var TmpRect: TRect;
begin
TmpRect := Rect(0,0,NewPic.Width,NewP
Canvas.Brush.Color := FBkColor;
If NewPic.Empty then Canvas.FillRect(BoundsRect
else If FStretch then Canvas.BrushCopy(BoundsRec
else Canvas.BrushCopy(TmpRect,N
Invalidate;
end;
procedure TButtonPro.SetBkColor(Valu
begin
FBkColor := Value;
UpdatePic(FNormalPic);
end;
procedure TButtonPro.SetTranspColor(
begin
FTranspColor := Value;
UpdatePic(FNormalPic);
end;
procedure TButtonPro.SetNormalPic(Va
begin
FNormalPic.Assign(Value);
UpdatePic(FNormalPic);
If not Stretch then Height := FNormalPic.Height;
end;
procedure TButtonPro.SetActivePic(Va
begin
FActivePic.Assign(Value);
end;
procedure TButtonPro.SetPressingPic(
begin
FPressingPic.Assign(Value)
end;
procedure TButtonPro.SetDisabledPic(
begin
FDisabledPic.Assign(Value)
end;
procedure TButtonPro.SetDownPic(Valu
begin
FDownPic.Assign(Value);
end;
end.
What's wrong with my code? Please tell me. It is very easy for any people who know component writing.
I see you did not declare the procedure UpdatePic.
I've only had a quick glance at your code but heres a few things to try:
1) The error message you describe is often caused when you try to do things to a canvas during design mode. Try preventing some of the canvas operations with :
If Not(csDesigning in ComponentState) then begin
+++CODE+++
end;
2) From a quick glance it seems like you should probably be using the Paint method to perform the canvas operations anyway. Put all canvas code within an overridden paint method and call 'refresh' to update the apearance of your component. This will also have the advantage of having your component automatically repaint itself if obscured by another window.
If any of this helps then I'll see what more I can do.
Steven.
1) The error message you describe is often caused when you try to do things to a canvas during design mode. Try preventing some of the canvas operations with :
If Not(csDesigning in ComponentState) then begin
+++CODE+++
end;
2) From a quick glance it seems like you should probably be using the Paint method to perform the canvas operations anyway. Put all canvas code within an overridden paint method and call 'refresh' to update the apearance of your component. This will also have the advantage of having your component automatically repaint itself if obscured by another window.
If any of this helps then I'll see what more I can do.
Steven.
First of all, it looks like you should have descended from TGraphicControl not TCustomControl (if I understand OK).
Then, you don't say exactly when the error occurs. Is it when you deposit the control on the form? When you call one of its methods? Do you create it manually?
Then, you don't say exactly when the error occurs. Is it when you deposit the control on the form? When you call one of its methods? Do you create it manually?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
icampbel,
I should have descended from TGraphicControl not TCustomControl? What's the difference between them? What will happen if I use the wrong one?
I should have descended from TGraphicControl not TCustomControl? What's the difference between them? What will happen if I use the wrong one?
TCustomControl is derived from TWinControl. If you are not going to require windows input focus, and it looks like you don't, then you can save resources by using TGraphicControl (like TImage does).
Also, arh... has a good suggestion about removing UpdatePic from the constructor.
Cheers,
Ian C.
Also, arh... has a good suggestion about removing UpdatePic from the constructor.
Cheers,
Ian C.