unit TransPanel;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,Graphics;
type
TTransPanel = class(TCustomPanel)
private
FTransparent: Boolean;
FTransparencyLevel: Byte;
procedure SetTransparent(Value: Boolean);
procedure SetTransparencyLevel(Value: Byte);
procedure CMParentColorChanged(var Message: TMessage); message CM_PARENTCOLORCHANGED;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
function TrControl(SelfControl: TWinControl; TransparencyLevel: Byte): TWinControl;
function GetCanvas: TCanvas;
procedure PaintBackground(Control: TWinControl; DC: HDC);
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property MyCanvas: TCanvas read GetCanvas;
published
property Transparent: Boolean read FTransparent write SetTransparent default False;
property TransparencyLevel: Byte read FTransparencyLevel write SetTransparencyLevel default 0;
property Align;
property Alignment;
property Anchors;
property BevelInner;
property BevelOuter default bvLowered;
property BevelWidth;
property BorderWidth;
property BorderStyle;
property Caption;
property Color;
property Constraints;
property DragMode;
property Enabled;
property Font;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnClick;
property OnConstrainedResize;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEnter;
property OnExit;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnResize;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyControl', [TTransPanel]);
end;
constructor TTransPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Color := clBtnShadow;
BevelOuter := bvLowered;
Width := 66;
end;
destructor TTransPanel.Destroy;
begin
inherited Destroy;
end;
procedure TTransPanel.CMParentColorChanged(var Message: TMessage);
begin
inherited;
if FTransparent then
Invalidate;
end;
procedure TTransPanel.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
if FTransparent then
PaintBackground(TrControl(Self, FTransparencyLevel), Message.DC) else
inherited;
end;
function TTransPanel.GetCanvas: TCanvas;
begin
Result := Self.Canvas;
end;
procedure TTransPanel.SetTransparent(Value: Boolean);
begin
if FTransparent <> Value then
begin
FTransparent := Value;
Repaint;
end;
end;
procedure TTransPanel.SetTransparencyLevel(Value: Byte);
begin
if FTransparencyLevel <> Value then
begin
FTransparencyLevel := Value;
Invalidate;
end;
end;
procedure TTransPanel.Paint;
const
Alignments: array[TAlignment] of Longint = (DT_LEFT, DT_RIGHT, DT_CENTER);
var
Rect: TRect;
TopColor, BottomColor: TColor;
FontHeight: Integer;
Flags: Longint;
procedure AdjustColors(Bevel: TPanelBevel);
begin
if Bevel = bvLowered then
begin
TopColor := clBtnShadow;
BottomColor := clBtnHighlight;
end else
begin
TopColor := clBtnHighlight;
BottomColor := clBtnShadow;
end;
end;
begin
Rect := GetClientRect;
with Canvas do
begin
if FTransparent then
PaintBackground(TrControl(Self, FTransparencyLevel), Handle) else
begin
Brush.Color := Color;
FillRect(Rect);
Brush.Style := bsClear;
end;
if BevelOuter <> bvNone then
begin
AdjustColors(BevelOuter);
Frame3D(Canvas, Rect, TopColor, BottomColor, BevelWidth);
end;
Frame3D(Self.Canvas, Rect, Self.Color, Self.Color, BorderWidth);
if BevelInner <> bvNone then
begin
AdjustColors(BevelInner);
Frame3D(Canvas, Rect, TopColor, BottomColor, BevelWidth);
end;
Font := Self.Font;
FontHeight := TextHeight('W');
with Rect do
begin
Top := ((Bottom + Top) - FontHeight) div 2;
Bottom := Top + FontHeight;
end;
Flags := DT_EXPANDTABS or DT_VCENTER or Alignments[Alignment];
{$IFDEF VER120}
Flags := DrawTextBiDiModeFlags(Flags);
{$ENDIF}
SetBkMode(Handle, Windows.TRANSPARENT);
DrawText(Handle, PChar(Caption), -1, Rect, Flags);
end;
end;
function TTransPanel.TrControl(SelfControl: TWinControl; TransparencyLevel: Byte): TWinControl;
var
Index: Byte;
begin
Result := SelfControl;
if TransparencyLevel > 0 then
for Index := 0 to Pred(TransparencyLevel) do
if Result.Parent <> nil then
Result := Result.Parent else
Break;
end;
procedure TTransPanel.PaintBackground(Control: TWinControl; DC: HDC);
var
P: TPoint;
SaveIndex: Integer;
begin
if Assigned(Control) and Assigned(Control.Parent) then
begin
SaveIndex := SaveDC(DC);
try
P := Control.ClientOrigin;
P := Control.Parent.ScreenToClient(P);
MoveWindowOrg(DC, P.X, P.Y); //这句不明白为什么要改变控件原点坐标,有什么意义,减少刷新区域???
SendMessage(Control.Parent.Handle, WM_ERASEBKGND,DC, 0);
finally
RestoreDC(DC, SaveIndex);
end;
end;
end;
end.
透明控件源代码,不明白改变原点坐标做什么,谁知道的?
拖动我想也差不多吧,我不懂做,谁做好了能否公布一下