各位高手请帮助我 如何使构件具有透明属性?(100分)

  • 主题发起人 主题发起人 norksark
  • 开始时间 开始时间
N

norksark

Unregistered / Unconfirmed
GUEST, unregistred user!
label,dbtext,image...都有个transparent属性,可以使它变得透明,请问如何
把DBGrid,DBCtrGrid等改造成具有透明属性的构件。推荐网站也可以。谢谢!
 
拜托大家,给一点提示吧,我快疯了,为能够过一个平安年,大家帮帮我吧,我把仅有的确100分送给你
 
高手快来,拜托大家了,应该不是很难的问题吧,为何没有人发言呢?
 
我也正想办法解决这个问题,第一次创建时,还可以,一移动,就不成。
 
关于你的要求在Windows98中几乎不太可能实现,从TWinControl继承下来的控件都不太可能,不过Win2k中可以实现!但程序不简单!
 
对的,
在2000里可以实现的。
 
洗耳恭听
 
应该可以的,在pb中datawindow都可以透明,delphi更应该可以,请各位高手帮帮忙,先谢谢了。
 
用D6在2000下要调用一个函数,具体是哪个我忘了,反正是个新的,只有2000才支持,98没有这个函数!
 
关键是控件会填充背景色
有个消息WM_ERASEBKGND是填充背景色的时候发生的
可以利用以下
不过我没有做过,不知道会不会成功
 
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.

透明控件源代码,不明白改变原点坐标做什么,谁知道的?

拖动我想也差不多吧,我不懂做,谁做好了能否公布一下
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
564
import
I
后退
顶部