先创建一个单元 TransEdit:
unit TransEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
TWM_RbsInvalidate=WM_USER+1;
type
TransparentEdit = class(TEdit)
private
{ Private declarations }
procedure RbsInvalidate(var Message:TMessage);
message
TWM_RbsInvalidate;
procedure CNCTLCOLOREDIT(var Message:TWMCTLCOLOREDIT);
message
CN_CTLCOLOREDIT;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd);
message WM_ERASEBKGND;
procedure WMMove(var Message: TMessage);
message WM_MOVE;
protected
{ Protected declarations }
FTransparent: boolean;
procedure CreateWnd;
override;
procedure CreateParams(var Params: TCreateParams);
override;
proceduredo
Exit;
override;
proceduredo
Enter;
override;
public
{ Public declarations }
constructor Create(AOwner: TComponent);
override;
procedure Invalidate;
override;
published
{ Published declarations }
end;
implementation
constructor TransparentEdit.Create(AOwner:TComponent);
begin
inherited create(AOwner);
ftransparent:=true;
end;
procedure TransparentEdit.CreateWnd;
begin
inherited CreateWnd;
if fTransparent then
begin
SetWindowLong(Parent.Handle, GWL_STYLE,
GetWindowLong(Parent.Handle, GWL_STYLE) and not WS_CLIPCHILDREN);
end;
end;
procedure TransparentEdit.RbsInvalidate(var Message:TMessage);
var r:TRect;
begin
if (Parent<>nil) and FTransparent then
begin
r:=ClientRect;
r.TopLeft:=Parent.ScreenToClient(ClientToScreen(r.TopLeft));
r.BottomRight:=Parent.ScreenToClient(ClientToScreen(r.BottomRight));
RedrawWindow(Handle,nil,0,RDW_FRAME+RDW_INVALIDATE);
end;
end;
procedure TransparentEdit.CNCTLCOLOREDIT(var Message:TWMCTLCOLOREDIT);
begin
if FTransparent then
with Messagedo
begin
SetBkMode(ChildDC,Windows.TRANSPARENT);
Result:=GetStockObject(HOLLOW_BRUSH)
end
else
inherited;
end;
procedure TransparentEdit.WMEraseBkgnd(var Message:TWMERASEBKGND);
begin
if FTransparent and not (csDesigning in ComponentState) then
PostMessage(Handle,TWM_RbsInvalidate,0,0)
else
inherited;
end;
procedure TransparentEdit.WMMove(var message:TMessage);
begin
inherited;
if FTransparent then
SendMessage(Handle,TWM_RbsInvalidate,0,0)
else
Invalidate;
end;
procedure TransparentEdit.CreateParams(var Params:TCreateParams);
begin
inherited CreateParams(Params);
if (CsDesigning in ComponentState) then
exit;
with Paramsdo
begin
ExStyle:=ExStyle or WS_EX_TRANSPARENT;
end;
end;
procedure TransparentEdit.DoExit;
begin
inherited;
FTransparent:=true;
SetCursor(0);
RecreateWnd;
end;
procedure TransparentEdit.DoEnter;
var exstyle,stdstyle:longint;
begin
inherited;
Ftransparent:=false;
StdStyle:= Windows.GetWindowLong(handle, GWL_EXSTYLE);
exStyle:= StdStyle and not WS_EX_TRANSPARENT;
Windows.SetWindowLong(handle, GWL_EXSTYLE, exStyle);
invalidate;
end;
procedure TransparentEdit.Invalidate;
begin
if FTransparent then
SendMessage(Handle,TWM_RbsInvalidate,0,0)
else
inherited;
end;
end.
=================
然后在你需要的地方引用 TransEdit;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TransEdit;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ed: TransparentEdit;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ed := TransparentEdit.Create(Self);
with eddo
begin
Parent := Form1;
Left := 100;
Top := 20;
Text := 'Edit1';
Color := clYellow;
end;
end;
end.
以上已测试通过。