一个使TEdit透明的例子

unit BKTransparentEdit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

const
  TWM_BKInvalidate=WM_USER+1;

type
  TBKTransparentEdit = class(TEdit)
  private
    { Private declarations }
    procedure BKInvalidate(var Message:TMessage); message
              TWM_BKInvalidate;
    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;
    procedure DoExit; override;
    procedure DoEnter; override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    procedure Invalidate; override;

  published
    { Published declarations }
  end;

procedure Register;

implementation

constructor TBKTransparentEdit.Create(AOwner:TComponent);
begin
  inherited Create(AOwner);
  FTransparent:=true;
end;

procedure TBKTransparentEdit.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 TBKTransparentEdit.BKInvalidate(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 TBKTransparentEdit.CNCTLCOLOREDIT(var Message:TWMCTLCOLOREDIT);
begin
  if FTransparent then
  with Message do
  begin
    SetBkMode(ChildDC,Windows.TRANSPARENT);
    Result:=GetStockObject(HOLLOW_BRUSH)
  end
  else
    inherited;
end;

procedure TBKTransparentEdit.WMEraseBkgnd(var Message:TWMERASEBKGND);
begin
  if FTransparent and not (csDesigning in ComponentState) then
    PostMessage(Handle,TWM_BKInvalidate,0,0)
  else
    inherited;
end;

procedure TBKTransparentEdit.WMMove(var message:TMessage);
begin
  inherited;
  if FTransparent then
    SendMessage(Handle,TWM_BKInvalidate,0,0)
  else
    Invalidate;
end;

procedure TBKTransparentEdit.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 TBKTransparentEdit.DoExit;
begin
inherited;
   FTransparent:=true;
   SetCursor(0);
   RecreateWnd;
end;

procedure TBKTransparentEdit.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 TBKTransparentEdit.Invalidate;
begin
  if FTransparent then
    SendMessage(Handle,TWM_BKInvalidate,0,0)
  else
    inherited;
end;

procedure Register;
begin
  RegisterComponents('BKTransparentComps', [TBKTransparentEdit]);
end;

end.

阅读更多

delphi TEdit透明

05-17

unit tansEdit;rninterfacernrnusesrn Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,rn StdCtrls, ComCtrls,Types;rnrntypern TCtrl = class(TWinControl);rnrn TRDTransEdit = class(TEdit)rn privatern FAlignText: TAlignment;rn FTransparent: Boolean;rn FPainting: Boolean;rn procedure SetAlignText(Value: TAlignment);rn procedure SetTransparent(Value: Boolean);rn procedure WMPaint(var Message: TWMPaint); message WM_PAINT;rn procedure WMNCPaint(var Message: TMessage); message WM_NCPAINT;rn procedure WMEraseBkGnd(var Message: TWMEraseBkGnd); message WM_ERASEBKGND;rn procedure CNCtlColorEdit(var Message: TWMCtlColorEdit); message CN_CTLCOLOREDIT;rn procedure CNCtlColorStatic(var Message: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;rn procedure CNCTLCOLORMSGBOX(var Message: TWMCtlColorStatic); message CN_CTLCOLORMSGBOX;rnrn procedure CMParentColorChanged(var Message: TMessage); message CM_PARENTCOLORCHANGED;rn procedure WMSize(var Message: TWMSize); message WM_SIZE;rn procedure WMMove(var Message: TWMMove); message WM_MOVE;rn protectedrn procedure RepaintWindow;rn procedure CreateParams(var Params: TCreateParams); override;rn procedure Change; override;rn procedure SetParent(AParent: TWinControl); override;rn publicrn constructor Create(AOwner: TComponent); override;rn destructor Destroy; override;rn procedure WndProc(var Msg: TMessage);override;rn publishedrn property AlignText: TAlignment read FAlignText write SetAlignText default taLeftJustify;rn property Transparent: Boolean read FTransparent write SetTransparent default false;rn end;rnrnprocedure Register;rnrnimplementationrnrnconstrn BorderRec: array[TBorderStyle] of Integer = (1, -1);rnrnprocedure Register;rnbeginrn RegisterComponents('OurComponent', [TRDTransEdit]);rnend;rnrnfunction GetScreenClient(Control: TControl): TPoint;rnvarrn p: TPoint;rnbeginrn p := Control.ClientOrigin;rn ScreenToClient(Control.Parent.Handle, p);rn Result := p;rnend;rnrnconstructor TRDTransEdit.Create(AOwner: TComponent);rnbeginrn inherited Create(AOwner);rn FAlignText := taLeftJustify;rn FTransparent := false;rn FPainting := false;rnend;rnrndestructor TRDTransEdit.Destroy;rnbeginrn inherited Destroy;rnend;rnrnprocedure TRDTransEdit.SetAlignText(Value: TAlignment);rnbeginrn if FAlignText <> Value thenrn beginrn FAlignText := Value;rn RecreateWnd;rn Invalidate;rn end;rnend;rnrnprocedure TRDTransEdit.SetTransparent(Value: Boolean);rnbeginrn if FTransparent <> Value thenrn beginrn FTransparent := Value;rn Invalidate;rn end;rnend;rnprocedure DoTransparent(ADC: HDC; AWinControl: TWinControl);rnvarrn nSaveIndex: HDC;rn pt: TPoint;rn xParent: TWinControl;rn xControl: TControl;rn I: Integer;rnrnrnrn procedure _DoTransparent_Helper(_DC: HDC; _WinCtrl: TWinControl);rn varrn _xSaveIndex: HDC;rn _xSubSaveIndex: HDC;rn _I: Integer;rn _SubCtrl: TControl;rn beginrn if (_WinCtrl = nil) or (_DC = 0) then beginrn Exit;rn end;rnrn // 非设计时并且不可见rn if (not (csDesigning in _WinCtrl.ComponentState)) and (not _WinCtrl.Visible) then beginrn Exit;rn end;rnrn // 设计时, 但是设计不可见rn if (csDesigning in _WinCtrl.ComponentState)rn and ((csDesignerHide in _WinCtrl.ControlState)rn or (csNoDesignVisible in _WinCtrl.ControlStyle)) then beginrn Exit;rn end;rnrn if not Windows.RectVisible(_DC, Types.Rect(_WinCtrl.Left, _WinCtrl.Top,rn _WinCtrl.Left + _WinCtrl.Width,rn _WinCtrl.Top + _WinCtrl.Height)) then beginrn Exit;rn end;rnrn _xSaveIndex := Windows.SaveDC(_DC);rn tryrn// _WinCtrl.ControlState := _WinCtrl.ControlState + [csPaintCopy];rnrn MoveWindowOrg(_DC, _WinCtrl.Left, _WinCtrl.Top);rn Windows.IntersectClipRect(_DC, 0, 0, _WinCtrl.Width, _WinCtrl.Height);rn _WinCtrl.Perform( WM_ERASEBKGND, Integer(_DC), Integer(_DC) );rn _WinCtrl.Perform( wm_PrintClient, Integer(_DC), prf_Client );rn _WinCtrl.Perform( WM_PAINT, Integer(_DC), 0 );rnrn for _I := 0 to _WinCtrl.ControlCount - 1 do beginrn _SubCtrl := _WinCtrl.Controls[_i];rnrn if _SubCtrl is TWinControl then beginrn _xSubSaveIndex := Windows.SaveDC(_DC);rn tryrn _DoTransparent_Helper(_DC, TWinControl(_SubCtrl));rn finallyrn Windows.RestoreDC(_DC, _xSubSaveIndex);rn end;rnrn end;rn end;rn finallyrn Windows.RestoreDC(_DC, _xSaveIndex);rn _WinCtrl.ControlState := _WinCtrl.ControlState - [csPaintCopy];rn end;rn end;rnrnrnbeginrn if (AWinControl = nil) or (AWinControl.Parent = nil) or (ADC = 0) then beginrn Exit;rn end;rnrn xParent := AWinControl.Parent;rnrnrn nSaveIndex := Windows.SaveDC( ADC );rn try tryrn Windows.GetViewportOrgEx(ADC, pt);rn Windows.SetViewportOrgEx(ADC, pt.X - AWinControl.Left, pt.Y - AWinControl.Top, nil);rn Windows.IntersectClipRect( ADC, 0, 0, AWinControl.Parent.ClientWidth,rn AWinControl.Parent.ClientHeight);rnrn if xParent.DockSite and AWinControl.UseDockManagerrn and (xParent.DockManager <> nil) then beginrn xParent.DockManager.PaintSite(ADC);rn end;rnrn if xParent.ControlCount = 0 then beginrn Exit;rn end;rnrn xParent.Perform( WM_ERASEBKGND, Integer(ADC), Integer(ADC));rn xParent.Perform( wm_PrintClient, Integer(ADC), prf_Client );rn// xParent.Perform( WM_PAINT, Integer(ADC), 0 );rnrn for I := 0 to xParent.ControlCount - 1 do beginrn xControl := xParent.Controls[i];rnrn // TGraphicControlrn if xControl is TWinControl then beginrn if xControl = AWinControl then beginrn Break;rn end;rnrn _DoTransparent_Helper(ADC, TWinControl(xControl));rn end;rn end;rn exceptrn end;rn finallyrn Windows.RestoreDC( ADC, nSaveIndex );rn end;rnend;rnprocedure TRDTransEdit.WMEraseBkGnd(var Message: TWMEraseBkGnd);rnvarrn DC: hDC;rn i: integer;rn p: TPoint;rnbeginrn if FTransparent thenrn beginrn// if Assigned(Parent) thenrn// beginrn// DC := Message.DC;rn// i := SaveDC(DC);rn// p := GetScreenClient(self);rn// p.x := -p.x;rn// p.y := -p.y;rn// MoveWindowOrg(DC, p.x, p.y);rn// SendMessage(Parent.Handle, $0014, DC, 0);rn// TCtrl(Parent).PaintControls(DC, nil);rn// RestoreDC(DC, i);rn// end;rn DoTransparent(Message.DC,Self);rn Message.Result := 1;rn endrn elsern inherited;rnend;rnrnprocedure TRDTransEdit.WMPaint(var Message: TWMPaint);rnbeginrn inherited;rn if FTransparent thenrn if not FPainting thenrn RepaintWindow;rnend;rnrnprocedure TRDTransEdit.WMNCPaint(var Message: TMessage);rnbeginrn inherited;rnend;rnrnprocedure TRDTransEdit.CNCtlColorEdit(var Message: TWMCtlColorEdit);rnbeginrn inherited;rn if FTransparent then beginrn SetBkMode(Message.ChildDC, 1);rn Message.Result:= GetStockObject(NULL_BRUSH);rn end;rnend;rnrnprocedure TRDTransEdit.CNCTLCOLORMSGBOX(var Message: TWMCtlColorStatic);rnbeginrn inherited;rn if FTransparent then beginrn SetBkMode(Message.ChildDC, 1);rn Message.Result:= GetStockObject(NULL_BRUSH);rn end;rnend;rnrnprocedure TRDTransEdit.CNCtlColorStatic(var Message: TWMCtlColorStatic);rnbeginrn inherited;rn if FTransparent then beginrn SetBkMode(Message.ChildDC, 1);rn Message.Result:= GetStockObject(NULL_BRUSH);rn end;rnend;rnrnprocedure TRDTransEdit.CMParentColorChanged(var Message: TMessage);rnbeginrn inherited;rn if FTransparent thenrn Invalidate;rnend;rnrnprocedure TRDTransEdit.WMSize(var Message: TWMSize);rnbeginrn inherited;rn Invalidate;rnend;rnrnprocedure TRDTransEdit.WndProc(var Msg: TMessage);rnbeginrnrn inherited WndProc(Msg);rn case Msg.Msg ofrn CN_CTLCOLORMSGBOX..CN_CTLCOLORSTATIC:beginrn Msg.Result:= GetStockObject(NULL_BRUSH);rn end;rn end;rnend;rnrnprocedure TRDTransEdit.WMMove(var Message: TWMMove);rnbeginrn inherited;rn Invalidate;rnend;rnrnprocedure TRDTransEdit.RepaintWindow;rnvarrn DC,rDC: hDC;rn TmpBitmap, Bitmap: hBitmap;rnbeginrn if FTransparent thenrn beginrn FPainting := true;rn HideCaret(Handle);rn rDC := GetDC(Handle);rn DC := CreateCompatibleDC(rDC);rn TmpBitmap := CreateCompatibleBitmap(rDC, Succ(ClientWidth), Succ(ClientHeight));rn Bitmap := SelectObject(DC, TmpBitmap);rn PaintTo(DC, 0, 0);rn BitBlt(rDC, BorderRec[BorderStyle], BorderRec[BorderStyle], ClientWidth, ClientHeight, DC, 1, 1,rn SRCCOPY);rn SelectObject(DC, Bitmap);rn DeleteDC(DC);rnrnrn ReleaseDC(Handle, rDC);rn DeleteObject(TmpBitmap);rn ShowCaret(Handle);rn FPainting := false;rn end;rnend;rnrnprocedure TRDTransEdit.CreateParams(var Params: TCreateParams);rnconstrn Alignments: array[TAlignment] of DWord = (ES_LEFT, ES_RIGHT, ES_CENTER);rnbeginrn inherited CreateParams(Params);rn Params.Style := Params.Style or ES_MULTILINE or Alignments[FAlignText];rn if self.FTransparent then beginrn Params.Style := Params.Style or WS_EX_TRANSPARENT;rn end;rnend;rnrnprocedure TRDTransEdit.Change;rnbeginrn RepaintWindow;rn inherited Change;rnend;rnrnprocedure TRDTransEdit.SetParent(AParent: TWinControl);rnbeginrn inherited SetParent(AParent);rnend;rnrnrnend.rn

没有更多推荐了,返回首页