ADO Database Delphi

Title: Creating a Shadow
Question: A Shadow Component
Answer:
In this article I want to start building a shadow component. Our shadow will be an attached control. That means, our control will be attached by the user at design time, or at run-time to virtually any control on it's form.
The principal idea behind this control is to draw a rectangle behind the attached control.Our rectangle will create the illusion of shaddow.
I have declared a type like bellow:
type
TOrientation=(TopLeft,TopRight,BottomLeft,BottomRight);
where TOrientation is the orientation of our shadow. Also our shadow component must have: a color, a width and a control to attach. For this reason I have declared all as private variabile.
The most signifiant part of code for any attached control is represented by overriding the Notification event. It is define like bellow:
procedure Notification(AComponent:TComponent; Operation:TOperation);override;
Notification is handeled when a component is added, deleted etc. Why we need to override this? Let's suppose you have put a shadow component on the form and then you have attached it to a component. If you will delete your component(don't forget our control is attached to it) you will obtain a "Acces Violation" error. For this reason, when a component is removed, we need first to verify if our shadow is attached to it. If is attached, we will change the Attached property to nil. Look at the bellow code:
procedure TRbsShadow.Notification(AComponent:TComponent; Operation:TOperation);
begin
inherited notification(AComponent,Operation);
if (operation=opremove) and (acomponent=fcontrol) then
fcontrol:=nil;
end;
The drawing routine of our component is passed to four procedures:
procedure DrawTopLeft;
procedure DrawTopRight;
procedure DrawBottomLeft;
procedure DrawBottomRight;
The entire source code for the component is listed bellow. Just copy and paste in a blank unit and install the component.
unit RbsShadow;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TOrientation=(TopLeft,TopRight,BottomLeft,BottomRight);
type
TRbsShadow = class(TGraphicControl)
private
{ Private declarations }
FColor:TColor;
FOrientation:TOrientation;
FControl:TControl;
fwidth:integer;
procedure SetColor(Value:TColor);
procedure SetOrientation(Value:TOrientation);
procedure SetControl(Value:TControl);
procedure SetWidth(Value:Integer);
procedure DrawTopLeft;
procedure DrawTopRight;
procedure DrawBottomLeft;
procedure DrawBottomRight;
protected
{ Protected declarations }
procedure Notification(AComponent:TComponent; Operation:TOperation);override;
public
{ Public declarations }
constructor Create(aOwner:TComponent);override;
procedure Paint;override;
published
{ Published declarations }
property ShadowColor:TColor read fColor write SetColor;
property ShadowOrientation:TOrientation read fOrientation write SetOrientation;
property AttachTo:TControl read fControl write SetControl;
property ShadowDistance:integer read fwidth write SetWidth;
end;
procedure Register;
implementation
constructor TRbsShadow.Create(aOwner:TComponent);
begin
inherited create(aOwner);
//initialize component
fColor:=clblack;
fOrientation:=BottomRight;
fwidth:=3;
width:=75;
height:=25;
end;
procedure TRbsShadow.SetColor(Value:TColor);
begin
if Value=fcolor then exit;
fcolor:=value;
invalidate;
end;
procedure TRbsShadow.SetOrientation(Value:TOrientation);
begin
if Value=fOrientation then exit;
fOrientation:=Value;
invalidate;
end;
procedure TRbsShadow.SetControl(Value:Tcontrol);
begin
if (Value=fControl) or (value is TRbsShadow) then exit;
fControl:=Value;
invalidate;
end;
procedure TRbsShadow.SetWidth(value:integer);
begin
if value=fWidth then exit;
fWidth:=Value;
invalidate;
end;
procedure TRbsShadow.Paint;
begin
inherited;
SendToBack;
canvas.brush.Color:=fcolor;
if fcontrol=nil
then
begin
canvas.FillRect(clientrect);
exit;
end;
case fOrientation of
TopLeft:DrawTopLeft;
TopRight: DrawTopRight;
BottomLeft:DrawBottomLeft;
BottomRight:DrawBottomRight;
end;
end;
procedure TRbsShadow.DrawTopLeft;
begin
//set the position of control
left:=fcontrol.Left-fwidth;
top:=fcontrol.Top-fwidth;
width:=fcontrol.Width;
height:=fcontrol.Height;
canvas.FillRect(clientrect);
end;
procedure TRbsShadow.DrawTopRight;
begin
left:=fcontrol.Left+fwidth;
top:=fcontrol.Top-fwidth;
width:=fcontrol.Width;
height:=fcontrol.Height;
canvas.FillRect(clientrect);
end;
procedure TRbsShadow.DrawBottomLeft;
begin
left:=fcontrol.Left-fwidth;
top:=fcontrol.Top+fwidth;
width:=fcontrol.Width;
height:=fcontrol.Height;
canvas.FillRect(clientrect);
end;
procedure TRbsShadow.DrawBottomRight;
begin
left:=fcontrol.Left+fwidth;
top:=fcontrol.Top+fwidth;
width:=fcontrol.Width;
height:=fcontrol.Height;
canvas.FillRect(clientrect);
end;
procedure TRbsShadow.Notification(AComponent:TComponent; Operation:TOperation);
begin
inherited notification(AComponent,Operation);
if (operation=opremove) and (acomponent=fcontrol) then
fcontrol:=nil;
end;
procedure Register;
begin
RegisterComponents('Rombest', [TRbsShadow]);
end;
end.