unit TransPanel;interfaceuses
Windows, Messages, SysUtils, Classes, Controls, Forms, Graphics, StdCtrls;type
TBlendValue = 0..100; TTransPanel = class(TCustomcontrol)
private
Fborder: Boolean;
FAlignment: TAlignment;
FBlendValue: TBlendValue;
FColor:TColor; procedure Setborder(Value: Boolean);
procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND;
procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
procedure SetAlignment(const Value: TAlignment);
procedure SetBlendValue(const Value: TBlendValue);
procedure SetColor(Value: TColor);
protected
Buffer: TBitmap; procedure Paint; override;
public
procedure CreateParams(var Params: TCreateParams); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Alignment: TAlignment read FAlignment write SetAlignment default taCenter;
property Anchors;
property Border: Boolean read Fborder write Setborder;
property Color: TColor read FColor write SetColor;
property BlendValue: TBlendValue read FBlendValue write SetBlendValue;
property Caption;
property Font;
property Visible;
property Enabled;
property Align;
property OnClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents(TransParent Components', [TTransPanel]);
end;{ TTransPanel }
constructor TTransPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Buffer := TBitmap.Create; Width := 185;
Height := 41;
FAlignment := taCenter; Color:=clBtnFace;
BlendValue:=100;
end;destructor TTransPanel.Destroy;
begin
Buffer.Free;
inherited Destroy;
end;procedure TTransPanel.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle + WS_EX_TRANSPARENT;
ControlStyle := ControlStyle - [csOpaque] + [csAcceptsControls];
end;procedure TTransPanel.Paint;
const
Alignments: array[TAlignment] of Longint = (DT_LEFT, DT_RIGHT, DT_CENTER);
var
Rect: TRect;
FontHeight: Integer;
Flags: Longint;
sBlendFunction: BlendFunction;
begin
inherited Paint; with sBlendFunction do
begin
BlendOp := AC_SRC_OVER;
BlendFlags := 0;
AlphaFormat := 0;
SourceConstantAlpha:= (255 * FBlendValue) div 100 ;
end; Rect := GetClientRect; Buffer.Width:=Width;
Buffer.Height:=Height;
with Buffer.Canvas do
begin
Brush.Color:=FColor;
FillRect(Rect);
end; with Canvas do
begin
Windows.AlphaBlend(Canvas.Handle,0,0,Width,
Height,Buffer.Canvas.Handle,0,0,
Buffer.Width,Buffer.Height,sBlendFunction); if Fborder or (csDesigning in ComponentState) then
begin
Canvas.Brush.Color := clBtnShadow;
Canvas.FrameRect(Rect);
end; end;
end;procedure TTransPanel.Setborder(Value: Boolean);
begin
if Value <> Fborder then
begin
Fborder := Value;
RecreateWnd;
end;
end;procedure TTransPanel.WMWindowPosChanging(var Message: TWMWindowPosChanging);
begin
inherited;
Invalidate;
end;procedure TTransPanel.WMEraseBkgnd(var Message: TWmEraseBkgnd);
begin
Message.Result:= 1;
end;procedure TTransPanel.SetAlignment(const Value: TAlignment);
begin
FAlignment := Value;
RecreateWnd;
end;procedure TTransPanel.SetColor(Value: TColor);
begin
if Value <> FColor then
begin
FColor := Value;
RecreateWnd;
end;
end;procedure TTransPanel.SetBlendValue(const Value: TBlendValue);
begin
if FBlendValue <> Value then
begin
FBlendValue := Value;
RecreateWnd;
end;
end;end.测试过程中发现的问题:
1、设计时,两个TransPanel之前的BringtoFront、SendtoBack无效。
2、设计时,两个TransPanel部分重叠,失去焦点,背景里不完全重画。
3、设计时,同时选中两个TransPanel,透明效果会慢慢无效。
4、设计时,TransPanel在Button上层覆盖半个Button,然后移开Button。会发现Button前景没有重画。总之问题还是很多。请达人帮忙完美一下
Windows, Messages, SysUtils, Classes, Controls, Forms, Graphics, StdCtrls;type
TBlendValue = 0..100; TTransPanel = class(TCustomcontrol)
private
Fborder: Boolean;
FAlignment: TAlignment;
FBlendValue: TBlendValue;
FColor:TColor; procedure Setborder(Value: Boolean);
procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND;
procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
procedure SetAlignment(const Value: TAlignment);
procedure SetBlendValue(const Value: TBlendValue);
procedure SetColor(Value: TColor);
protected
Buffer: TBitmap; procedure Paint; override;
public
procedure CreateParams(var Params: TCreateParams); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Alignment: TAlignment read FAlignment write SetAlignment default taCenter;
property Anchors;
property Border: Boolean read Fborder write Setborder;
property Color: TColor read FColor write SetColor;
property BlendValue: TBlendValue read FBlendValue write SetBlendValue;
property Caption;
property Font;
property Visible;
property Enabled;
property Align;
property OnClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents(TransParent Components', [TTransPanel]);
end;{ TTransPanel }
constructor TTransPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Buffer := TBitmap.Create; Width := 185;
Height := 41;
FAlignment := taCenter; Color:=clBtnFace;
BlendValue:=100;
end;destructor TTransPanel.Destroy;
begin
Buffer.Free;
inherited Destroy;
end;procedure TTransPanel.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle + WS_EX_TRANSPARENT;
ControlStyle := ControlStyle - [csOpaque] + [csAcceptsControls];
end;procedure TTransPanel.Paint;
const
Alignments: array[TAlignment] of Longint = (DT_LEFT, DT_RIGHT, DT_CENTER);
var
Rect: TRect;
FontHeight: Integer;
Flags: Longint;
sBlendFunction: BlendFunction;
begin
inherited Paint; with sBlendFunction do
begin
BlendOp := AC_SRC_OVER;
BlendFlags := 0;
AlphaFormat := 0;
SourceConstantAlpha:= (255 * FBlendValue) div 100 ;
end; Rect := GetClientRect; Buffer.Width:=Width;
Buffer.Height:=Height;
with Buffer.Canvas do
begin
Brush.Color:=FColor;
FillRect(Rect);
end; with Canvas do
begin
Windows.AlphaBlend(Canvas.Handle,0,0,Width,
Height,Buffer.Canvas.Handle,0,0,
Buffer.Width,Buffer.Height,sBlendFunction); if Fborder or (csDesigning in ComponentState) then
begin
Canvas.Brush.Color := clBtnShadow;
Canvas.FrameRect(Rect);
end; end;
end;procedure TTransPanel.Setborder(Value: Boolean);
begin
if Value <> Fborder then
begin
Fborder := Value;
RecreateWnd;
end;
end;procedure TTransPanel.WMWindowPosChanging(var Message: TWMWindowPosChanging);
begin
inherited;
Invalidate;
end;procedure TTransPanel.WMEraseBkgnd(var Message: TWmEraseBkgnd);
begin
Message.Result:= 1;
end;procedure TTransPanel.SetAlignment(const Value: TAlignment);
begin
FAlignment := Value;
RecreateWnd;
end;procedure TTransPanel.SetColor(Value: TColor);
begin
if Value <> FColor then
begin
FColor := Value;
RecreateWnd;
end;
end;procedure TTransPanel.SetBlendValue(const Value: TBlendValue);
begin
if FBlendValue <> Value then
begin
FBlendValue := Value;
RecreateWnd;
end;
end;end.测试过程中发现的问题:
1、设计时,两个TransPanel之前的BringtoFront、SendtoBack无效。
2、设计时,两个TransPanel部分重叠,失去焦点,背景里不完全重画。
3、设计时,同时选中两个TransPanel,透明效果会慢慢无效。
4、设计时,TransPanel在Button上层覆盖半个Button,然后移开Button。会发现Button前景没有重画。总之问题还是很多。请达人帮忙完美一下
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货