我这里有一个继承Tedit的透明Edit,你可以参考下
unit TransparentEdit;
{$R-}
interface
uses
Windows, Messages, Controls, StdCtrls, Classes;
const
TMWM__SpecialInvalidate = WM_USER + 1111;
type
TTransparentEdit = class(TEdit)
private
FAlignment: TAlignment; //增加对齐属性
procedure SpecialInvalidate(var Message:TMessage); message TMWM__SpecialInvalidate;
procedure CNCTLCOLOREDIT(var Message:TWMCTLCOLOREDIT); message CN_CTLCOLOREDIT;
procedure WMSetText(var Message:TWMSetText); message WM_SETTEXT;
procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
protected
function GetAlignment: TAlignment; virtual;
procedure SetAlignment(newValue: TAlignment); virtual;
procedure CreateParams(var Params: TCreateParams); override;
public
constructor Create(AOwner: TComponent); override;
published
property Alignment: TAlignment read GetAlignment write SetAlignment;
end;
procedure Register;
implementation
{ TTransparentEdit }
procedure TTransparentEdit.CNCTLCOLOREDIT(var Message:TWMCTLCOLOREDIT);
begin
with Message do
begin
SetBkMode(ChildDC,TRANSPARENT);
Result:=GetStockObject(HOLLOW_BRUSH)
end
end;
procedure TTransparentEdit.WMSetText(var Message:TWMSetText);
begin
inherited;
if not (csDesigning in ComponentState) then
PostMessage(Handle,TMWM__SpecialInvalidate,0,0)
end;
procedure TTransparentEdit.SpecialInvalidate(var Message:TMessage);
var r:TRect;
begin
if Parent<>nil then
begin
r:=ClientRect;
r.TopLeft:=Parent.ScreenToClient(ClientToScreen(r.TopLeft));
r.BottomRight:=Parent.ScreenToClient(ClientToScreen(r.BottomRight));
InvalidateRect(Parent.Handle,@r,true);
RedrawWindow(Handle,nil,0,RDW_FRAME+RDW_INVALIDATE)
end;
end;
procedure TTransparentEdit.WMKeyDown(var Message: TWMKeyDown);
begin
SendMessage(Handle,TMWM__SpecialInvalidate,0,0);
inherited;
PostMessage(Handle,TMWM__SpecialInvalidate,0,0);
end;
procedure TTransparentEdit.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
Message.Result := 1;
end;
constructor TTransparentEdit.Create(AOwner: TComponent);
begin
inherited;
FAlignment := taLeftJustify;
ControlStyle := [csDesignInteractive, csClickEvents, csCaptureMouse,
csOpaque, csReplicatable, csNoStdEvents, csSetCaption];// csDoubleClicks,
end;
procedure TTransparentEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[TAlignment] of WORD = (ES_LEFT, ES_RIGHT, ES_CENTER);
begin
inherited CreateParams(Params);
with Params do
begin
ExStyle:=ExStyle or WS_EX_TRANSPARENT and not WS_EX_WINDOWEDGE
and not WS_EX_STATICEDGE and not WS_EX_DLGMODALFRAME and not WS_EX_CLIENTEDGE;
Style := Style or Alignments[FAlignment];//增加对齐方式
end;
end;
function TTransparentEdit.GetAlignment: TAlignment;
{ Returns the value of data member FAlignment. }
begin
result := FAlignment;
end; { GetAlignment }
procedure TTransparentEdit.SetAlignment(newValue: TAlignment);
{ Sets data member FAlignment to newValue. }
begin
if FAlignment <> newValue then
begin
FAlignment := newValue;
if not (csLoading in componentstate) then ReCreateWnd;
end;
end; { SetAlignment }
procedure Register;
begin
RegisterComponents('My Component', [TTransparentEdit]);
end;
end.