unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
procedure FormPaint(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function BlendColors( ForeColor, BackColor: TColor; Alpha: Byte ): TColor;
var
ForeRed, ForeGreen, ForeBlue: Byte;
BackRed, BackGreen, BackBlue: Byte;
BlendRed, BlendGreen, BlendBlue: Byte;
AlphaValue: Single;
begin
AlphaValue := Alpha / 255;
ForeColor := ColorToRGB( ForeColor );
ForeRed := GetRValue( ForeColor );
ForeGreen := GetGValue( ForeColor );
ForeBlue := GetBValue( ForeColor );
BackColor := ColorToRGB( BackColor );
BackRed := GetRValue( BackColor );
BackGreen := GetGValue( BackColor );
BackBlue := GetBValue( BackColor );
BlendRed := Round( AlphaValue * ForeRed + ( 1 - AlphaValue ) * BackRed );
BlendGreen := Round( AlphaValue * ForeGreen + ( 1 - AlphaValue ) * BackGreen );
BlendBlue := Round( AlphaValue * ForeBlue + ( 1 - AlphaValue ) * BackBlue );
Result := RGB( BlendRed, BlendGreen, BlendBlue );
end;
procedure DrawDropShadow( Canvas: TCanvas; Bounds: TRect; Depth: Integer; ShadowColor: TColor = clBlack );
var
A, D, I: Integer;
procedure DrawShadow( Offset, Alpha: Integer );
var
X, Y: Integer;
begin
// 4 ***
// *
// *
// 3 *
// * *
// 1 * 2 *
// ***********************
// Step 1
X := Bounds.Left + 2*Depth - Offset;
for Y := Bounds.Bottom - 1 to Bounds.Bottom - 1 + Offset - 1 do
Canvas.Pixels[ X, Y ] := BlendColors( ShadowColor, Canvas.Pixels[ X, Y ], Alpha );
Inc( X );
Y := Bounds.Bottom - 1 + Offset - 1;
Canvas.Pixels[ X, Y ] := BlendColors( ShadowColor, Canvas.Pixels[ X, Y ], Alpha );
// Step 2
Y := Bounds.Bottom - 1 + Offset;
for X := Bounds.Left + 2*Depth - Offset + 1 to Bounds.Right + Offset - 2 do
Canvas.Pixels[ X, Y ] := BlendColors( ShadowColor, Canvas.Pixels[ X, Y ], Alpha );
Dec( Y );
X := Bounds.Right + Offset - 2;
Canvas.Pixels[ X, Y ] := BlendColors( ShadowColor, Canvas.Pixels[ X, Y ], Alpha );
// Step 3
Y := Bounds.Top + 2*Depth - Offset;
for X := Bounds.Right - 1 to Bounds.Right - 1 + Offset - 1 do
Canvas.Pixels[ X, Y ] := BlendColors( ShadowColor, Canvas.Pixels[ X, Y ], Alpha );
Inc( Y );
X := Bounds.Right - 1 + Offset - 1;
Canvas.Pixels[ X, Y ] := BlendColors( ShadowColor, Canvas.Pixels[ X, Y ], Alpha );
// Step 4
X := Bounds.Right - 1 + Offset;
for Y := Bounds.Top + 2 * Depth - Offset + 1 to Bounds.Bottom + Offset - 2 do
Canvas.Pixels[ X, Y ] := BlendColors( ShadowColor, Canvas.Pixels[ X, Y ], Alpha );
end;
begin
if Depth <= 0 then
Exit;
D := 128 div Depth;
A := 128;
for I := 1 to Depth do
begin
DrawShadow( I, A );
Dec( A, D );
end;
end;
procedure TForm1.FormPaint(Sender: TObject);
begin
DrawDropShadow(Canvas,Panel1.BoundsRect,6);
end;
end.
object Form1: TForm1
Left = 192
Top = 107
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnPaint = FormPaint
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 168
Top = 72
Width = 217
Height = 193
Caption = 'Panel1'
TabOrder = 0
end
end