![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
unit fskinTranEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
const
TMWM__SpecialInvalidate = WM_USER + 1111;
type
TSkinEdit = class(TEdit)
private
FClarity : Boolean;
procedure SetClarity(const Value: Boolean);
{ Private declarations }
protected
{ Protected declarations }
procedure CMEraseBkgnd (var Message: TWMEraseBkgnd); Message WM_ERASEBKGND;
procedure WMSetText (var Message: TWMSetText);
procedure SpecialInvalidate(var Message: TMessage); message TMWM__SpecialInvalidate;
procedure CNCTLCOLOREDIT (var Msg: TWMCTLCOLOREDIT); message CN_CTLCOLOREDIT;
procedure WMKeyDown (var Message: TWMKeyDown); message WM_KEYDOWN;
procedure WMKillFocus (Var Message: TWMKillFocus); Message WM_KillFocus;
procedure WMLButtonDown (Var Message: TWMLButtonDown); Message WM_LButtonDown;
procedure WMMove (Var Message: TWMLButtonDown); Message WM_Move;
procedure WMMouseMove (Var Message: TWMMouseMove); Message WM_MouseMove;
procedure CreateParams(var Params: TCreateParams); override;
procedure CreateWnd; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure Invalidate; override;
published
{ Published declarations }
property Clarity:Boolean read FClarity write SetClarity default True;
property BevelKind default BKFlat;
property BorderStyle default bsNone;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Sa', [TSkinEdit]);
end;
{ TClarityEdit }
procedure TSkinEdit.CMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
if Clarity then
Message.Result := 1;
end;
procedure TSkinEdit.CNCTLCOLOREDIT(var Msg: TWMCTLCOLOREDIT);
begin
if Clarity then
with msg do
begin
SetTextColor(ChildDC, Font.Color);
SetBkMode(ChildDC, Windows.TRANSPARENT);
Result:=GetStockObject(HOLLOW_BRUSH);
end
else inherited;
end;
constructor TSkinEdit.Create(AOwner: TComponent);
begin
inherited create(AOwner);
self.BorderStyle:=bsNone;
self.BevelKind:=bkflat;
Clarity:= True;
end;
procedure TSkinEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
if (CsDesigning in ComponentState) then exit;
with Params do
begin
ExStyle:=ExStyle or WS_EX_TRANSPARENT;
end;
end;
procedure TSkinEdit.CreateWnd;
begin
inherited CreateWnd;
if Clarity then
begin
SetWindowLong(Parent.Handle, GWL_STYLE,
GetWindowLong(Parent.Handle, GWL_STYLE) and not WS_CLIPCHILDREN);
end;
end;
procedure TSkinEdit.Invalidate;
begin
if Clarity then PostMessage(Handle,TMWM__SpecialInvalidate,0,0)
else inherited;
end;
procedure TSkinEdit.SetClarity(const Value: Boolean);
begin
FClarity := Value;
end;
procedure TSkinEdit.SpecialInvalidate(var Message: TMessage);
var
rect:TRect;
begin
if Parent<>nil then
begin
rect:=ClientRect;
rect.TopLeft:=Parent.ScreenToClient(ClientToScreen(rect.TopLeft));
rect.BottomRight:=Parent.ScreenToClient(ClientToScreen(rect.BottomRight));
InvalidateRect(Parent.Handle,@rect,true);
RedrawWindow(Handle,nil,0,RDW_FRAME+RDW_INVALIDATE)
end;
end;
procedure TSkinEdit.WMKeyDown(var Message: TWMKeyDown);
begin
SendMessage(Handle,TMWM__SpecialInvalidate,0,0);
inherited;
PostMessage(Handle,TMWM__SpecialInvalidate,0,0);
end;
procedure TSkinEdit.WMKillFocus(var Message: TWMKillFocus);
begin
inherited;
if not (csDesigning in ComponentState) then
PostMessage(Handle,TMWM__SpecialInvalidate,0,0)
end;
procedure TSkinEdit.WMLButtonDown(var Message: TWMLButtonDown);
begin
inherited;
if not (csDesigning in ComponentState) then
PostMessage(Handle,TMWM__SpecialInvalidate,0,0);
end;
procedure TSkinEdit.WMMouseMove(var Message: TWMMouseMove);
begin
inherited;
if not (csDesigning in ComponentState) then
PostMessage(Handle,TMWM__SpecialInvalidate,0,0);
end;
procedure TSkinEdit.WMMove(var Message: TWMLButtonDown);
begin
inherited;
//if not (csDesigning in ComponentState) then
PostMessage(Handle,TMWM__SpecialInvalidate,0,0);
end;
procedure TSkinEdit.WMSetText(var Message: TWMSetText);
begin
inherited;
if not (csDesigning in ComponentState) then
PostMessage(Handle,TMWM__SpecialInvalidate,0,0);
end;
end.
procedure HandleCTLColorEdit(var Msg: TWMCTLCOLOREDIT);message WM_CTLCOLOREDIT;
procedure TForm1.Button2Click(Sender: TObject);
var
BMP: TBitmap;
begin
BMP := TBitmap.Create;
BMP.LoadFromFile('C:\Documents and Settings\Administrator\桌面\未命名.bmp');
Memo1.Brush.Bitmap := BMP;
Memo1.Repaint;
Bmp.Free;
end;
procedure TForm1.HandleCTLColorEdit(var Msg: TWMCTLCOLOREDIT);
begin
if Msg.ChildWnd = Memo1.Handle then begin
SetBkMode(Msg.ChildDC, TRANSPARENT);
Msg.Result := Memo1.Brush.Handle;
end;
end;