我想写一个panel后面有阴影的控件像速达入库单上那个Panel一样在右边和下边有阴影,我写了一下但没有效不知何故,贴出源码
unit panelcolor;
interface
uses
SysUtils, Classes, Controls, ExtCtrls,Forms,Graphics,Windows,Messages,Dialogs;
type
tpanelcolor = class(tpanel)
private
FShadeNumer: integer;
FShadeColor: TColor;
procedure SetShadeNumer(const Value: integer);
procedure SetShadeColor(const Value: TColor);
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
f1:TForm;
constructor Create();
destructor Destroy; override;
procedure ShadeId(c: TControl; Width: Integer; Color: TColor);
published
{ Published declarations }
property ShadeColor:TColor read FShadeColor write SetShadeColor;
property ShadeNumer:integer read FShadeNumer write SetShadeNumer default 1;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [tpanelcolor]);
end;
constructor tpanelcolor.Create;
begin
end;
destructor tpanelcolor.Destroy;
begin
inherited;
end;
procedure tpanelcolor.SetShadeColor(const Value: TColor);
begin
FShadeColor := Value;
ShadeId(self,ShadeNumer,ShadeColor);
end;
procedure tpanelcolor.SetShadeNumer(const Value: integer);
begin
FShadeNumer := Value;
if value>0 then
ShadeId(self,ShadeNumer,ShadeColor);
end;
procedure tpanelcolor.ShadeId( c: TControl; Width: Integer; Color: TColor);
var
rect: TRect;
old: TColor;
begin
if (c.Visible) then
begin
rect := c.BoundsRect;
rect.Left := rect.Left + Width;
rect.Top := rect.Top + Width;
rect.Right := rect.Right + Width;
rect.Bottom := rect.Bottom + Width;
canvas.Brush.Color :=color;
canvas.FillRect(rect);
end;
end;
end.
unit panelcolor;
interface
uses
SysUtils, Classes, Controls, ExtCtrls,Forms,Graphics,Windows,Messages,Dialogs;
type
tpanelcolor = class(tpanel)
private
FShadeNumer: integer;
FShadeColor: TColor;
procedure SetShadeNumer(const Value: integer);
procedure SetShadeColor(const Value: TColor);
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
f1:TForm;
constructor Create();
destructor Destroy; override;
procedure ShadeId(c: TControl; Width: Integer; Color: TColor);
published
{ Published declarations }
property ShadeColor:TColor read FShadeColor write SetShadeColor;
property ShadeNumer:integer read FShadeNumer write SetShadeNumer default 1;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [tpanelcolor]);
end;
constructor tpanelcolor.Create;
begin
end;
destructor tpanelcolor.Destroy;
begin
inherited;
end;
procedure tpanelcolor.SetShadeColor(const Value: TColor);
begin
FShadeColor := Value;
ShadeId(self,ShadeNumer,ShadeColor);
end;
procedure tpanelcolor.SetShadeNumer(const Value: integer);
begin
FShadeNumer := Value;
if value>0 then
ShadeId(self,ShadeNumer,ShadeColor);
end;
procedure tpanelcolor.ShadeId( c: TControl; Width: Integer; Color: TColor);
var
rect: TRect;
old: TColor;
begin
if (c.Visible) then
begin
rect := c.BoundsRect;
rect.Left := rect.Left + Width;
rect.Top := rect.Top + Width;
rect.Right := rect.Right + Width;
rect.Bottom := rect.Bottom + Width;
canvas.Brush.Color :=color;
canvas.FillRect(rect);
end;
end;
end.
1,继承建议从TCustomPanel为好,可以更加灵活的控件属性的显示
tpanelcolor = class(tpanel)
2 构造函数和析构函数的问题,如果在里面没有初始变量,这两个函数可以不要
constructor tpanelcolor.Create;
begin //再说这也是错。
end;〕
3 画控件的问题,应该覆盖父类的Paint方法,来重画。像你上面只是用一个普通的方法来画
则控件一上用重画,又恢复到原来的样子了。
具体要怎么说,还是先看看制作组件的例子吧,或者看一下TCustomPanEl的源代码也有好处。
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;