nxtSwitch
8/18/2014 - 5:22 PM

TransparentEdit.pas

type  
  TEdit = class(StdCtrls.TEdit)
  private
    FBrush: Cardinal;
    procedure WMCtlColorEdit(var Msg: TWMCtlColorEdit); message CN_CTLCOLOREDIT;
    procedure WMEraseBkgnd(var Msg: TWMEraseBkgnd); message WM_ERASEBKGND;
  public
    constructor Create(AOwner: TComponent);
    destructor Destroy; override;
  end;

implementation

{ TEdit }

constructor TEdit.Create(AOwner: TComponent);
begin
  with TForm(AOwner).GetFormImage do
  begin
    FBrush := CreatePatternBrush(Handle);
    Free;
  end;
  inherited Create(AOwner);
end;

destructor TEdit.Destroy;
begin
  DeleteObject(FBrush);
  inherited;
end;

procedure TEdit.WMCtlColorEdit(var Msg: TWMCtlColorEdit);
begin
  if FBrush <> 0 then
  begin
    SetBkMode(Msg.ChildDC, TRANSPARENT);
    SetTextColor(Msg.ChildDC, Self.Font.Color);
    SetBrushOrgEx(Msg.ChildDC, -Self.Left, -Self.Top, nil);
  end;

  Msg.Result := FBrush;
end;

procedure TEdit.WMEraseBkgnd(var Msg: TWMEraseBkgnd);
begin
  RedrawWindow(Self.Handle, nil, 0, RDW_INVALIDATE);
end;

end.