Advertisement
jpfassis

Edit Transparente - Delphi

Apr 18th, 2020
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.43 KB | None | 0 0
  1. unit UMyEditTransparent;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9.  
  10. type
  11.   TTranspaEdit = class(TEdit)
  12.   private
  13.     { Private declarations }
  14.     FPictureFond   : TPicture;
  15.     FPictureFondVisible : boolean;
  16.     FAlignText      : TAlignment;
  17.     FTransparent    : Boolean;
  18.     FPainting       : Boolean;
  19.     procedure SetPictureFond(Value: TPicture);
  20.     procedure SetAlignText(Value: TAlignment);
  21.     procedure SetTransparent(Value: Boolean);
  22.  
  23.   protected
  24.     { Protected declarations }
  25.     procedure RepaintWindow;
  26.     procedure Change; override;
  27.     procedure SetParent(AParent: TWinControl); override;
  28.     procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  29.     procedure WMNCPaint (var Message: TMessage); message WM_NCPAINT;
  30.     procedure WMEraseBkGnd(var Message: TWMEraseBkGnd); message WM_ERASEBKGND;
  31.     procedure CNCtlColorEdit(var Message: TWMCtlColorEdit); message CN_CTLCOLOREDIT;
  32.     procedure CNCtlColorStatic(var Message: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;
  33.     procedure CMParentColorChanged(var Message: TMessage); message CM_PARENTCOLORCHANGED;
  34.     procedure WMSize(var Message: TWMSize); message WM_SIZE;
  35.     procedure WMMove(var Message: TWMMove); message WM_MOVE;
  36.     procedure PaintParent(ACanvas: TCanvas);
  37.   public
  38.     { Public declarations }
  39.     procedure   CreateParams(var Params: TCreateParams);override;
  40.     constructor Create(AOwner: TComponent); override;
  41.     procedure   SetPictureFondVisible(Value: boolean);
  42.     destructor  Destroy; override;
  43.   published
  44.     { Published declarations }
  45.     property Align;
  46.     property AlignText   : TAlignment read FAlignText   write SetAlignText   default taLeftJustify;
  47.     property Transparent : Boolean    read FTransparent write SetTransparent default False;
  48.     property Picture     : TPicture   read FPictureFond write SetPictureFond;
  49.     property PictureVisible: boolean  read FPictureFondVisible write SetPictureFondVisible default False;
  50. end;
  51.  
  52. procedure Register;
  53.  
  54. implementation
  55.  
  56. procedure Register;
  57. begin
  58.   RegisterComponents('My Componentes', [TTranspaEdit]);
  59. end;
  60.  
  61. type
  62.  
  63. TParentControl = class(TWinControl);
  64.  
  65. const
  66.  
  67. BorderRec: array[TBorderStyle] of Integer = (1, -1);
  68.  
  69. constructor TTranspaEdit.Create(AOwner: TComponent);
  70. begin
  71.   inherited Create(AOwner);
  72.     FPictureFond := TPicture.Create;
  73.     FAlignText   := taLeftJustify;
  74.     FTransparent := false;
  75.     FPainting    := false;
  76.     Invalidate;
  77. end;
  78.  
  79. procedure  TTranspaEdit.SetParent(AParent: TWinControl);
  80. begin
  81.   inherited SetParent(AParent);
  82.     Invalidate;
  83. end;
  84.  
  85. procedure TTranspaEdit.SetPictureFond(Value : TPicture);
  86. begin
  87.   if FPictureFond <> Value then
  88.   begin FPictureFond.Assign(Value);
  89.     FTransparent       :=True;
  90.     FPictureFondVisible:=True;
  91.     Invalidate;
  92.   end;
  93. end;
  94.  
  95. procedure TTranspaEdit.SetPictureFondVisible(Value: Boolean);
  96. begin
  97.   if FPictureFondVisible <> Value then
  98.   begin
  99.     FPictureFondVisible := Value;
  100.     Invalidate;
  101.   end;
  102. end;
  103.  
  104. destructor TTranspaEdit.Destroy;
  105. begin
  106.   FPictureFond.Free;
  107.   inherited Destroy;
  108. end;
  109.  
  110. procedure TTranspaEdit.SetAlignText(Value: TAlignment);
  111. begin
  112.   if FAlignText <> Value then
  113.   begin
  114.     FAlignText := Value;
  115.     RecreateWnd;
  116.     Invalidate;
  117.   end;
  118. end;
  119.  
  120. procedure TTranspaEdit.SetTransparent(Value: Boolean);
  121. begin
  122.   if FTransparent <> Value then
  123.   begin
  124.     FTransparent := Value;
  125.     Invalidate;
  126.   end;
  127. end;
  128.  
  129. procedure TTranspaEdit.WMEraseBkGnd(var Message: TWMEraseBkGnd);
  130. var
  131.   DC: hDC;
  132.   canvas : TCanvas;
  133.   oldBrush : TBrush;
  134. begin
  135. if FTransparent and not(csDesigning in componentstate) then
  136. begin
  137. if Not FPictureFondVisible then
  138.   begin
  139.       canvas := TCanvas.create;
  140.       try canvas.handle := message.dc;
  141.           PaintParent(Canvas);
  142.       finally
  143.           canvas.free;
  144.       end;
  145.   end
  146.   else if FPictureFondVisible Then
  147.   begin
  148.         DC:=message.dc;
  149.         SetBkMode(DC, 1); //SetBkMode(DC, 1 = TRANSPARENT);
  150.         Canvas := TCanvas.Create;
  151.         try
  152.             oldbrush := Canvas.Brush;
  153.             canvas.handle := dc;
  154.             Canvas.Font.Color  := Self.Font.Color;
  155.             Canvas.Brush.Style := bsClear ;
  156.             Canvas.draw(0,0,FPictureFond.Bitmap);
  157.             Canvas.Brush := oldbrush;
  158.         finally
  159.             Canvas.Free;
  160.         end;
  161.   end;
  162.   end else
  163.   begin
  164.         try
  165.             canvas := TCanvas.create;
  166.             canvas.handle := message.dc;
  167.             canvas.brush.color := Color;
  168.             canvas.brush.style := bsSolid;
  169.             canvas.fillrect(clientrect);
  170.         finally
  171.             canvas.free;
  172.         end;
  173.   end;
  174. end;
  175.  
  176.  
  177. procedure TTranspaEdit.WMPaint(var Message: TWMPaint);
  178. begin
  179. inherited;
  180.    if FTransparent then
  181.    begin
  182.     Brush.Bitmap:=FPictureFond.Bitmap;
  183.     if not FPainting then RepaintWindow;
  184.    end;
  185. end;
  186.  
  187.  
  188.  
  189. procedure TTranspaEdit.WMNCPaint(var Message: TMessage);
  190. begin
  191.   inherited;
  192. end;
  193.  
  194. procedure TTranspaEdit.CNCtlColorEdit(var Message: TWMCtlColorEdit);
  195. begin
  196.   inherited;
  197.     if FTransparent then
  198.      SetBkMode(Message.ChildDC, 1);
  199. end;
  200.  
  201. procedure TTranspaEdit.CNCtlColorStatic(var Message: TWMCtlColorStatic);
  202. begin
  203. inherited;
  204.   if FTransparent then
  205.     SetBkMode(Message.ChildDC, 1);
  206. end;
  207.  
  208. procedure TTranspaEdit.CMParentColorChanged(var Message: TMessage);
  209. begin
  210. inherited;
  211.   if FTransparent then
  212.    Invalidate;
  213. end;
  214.  
  215. procedure TTranspaEdit.WMSize(var Message: TWMSize);
  216. var
  217.   r : TRect;
  218. begin
  219. inherited;
  220.   r := ClientRect;
  221.   InvalidateRect(handle,@r,false);
  222. end;
  223.  
  224. procedure TTranspaEdit.WMMove(var Message: TWMMove);
  225. var
  226.   r : TRect;
  227. begin
  228.   inherited;
  229.     Invalidate;
  230.     r := ClientRect;
  231.     InvalidateRect(handle,@r,false);
  232. end;
  233.  
  234. procedure TTranspaEdit.RepaintWindow;
  235. var
  236.   DC: hDC;
  237.   TmpBitmap, Bitmap: hBitmap;
  238. begin
  239.   if FTransparent then
  240.   begin
  241.     FPainting := true;
  242.     HideCaret(Handle);
  243.     DC := CreateCompatibleDC(GetDC(Handle));
  244.     TmpBitmap := CreateCompatibleBitmap( GetDC(Handle), Succ(ClientWidth),
  245.                                          Succ(ClientHeight));
  246.     Bitmap := SelectObject(DC, TmpBitmap);
  247.     PaintTo(DC, 0, 0);
  248.     BitBlt( GetDC(Handle), BorderRec[BorderStyle] + BorderWidth,
  249.             BorderRec[BorderStyle] + BorderWidth, ClientWidth,
  250.             ClientHeight, DC, 1, 1, SRCCOPY);
  251.     SelectObject(DC, Bitmap);
  252.     DeleteDC(DC);
  253.     ReleaseDC(Handle, GetDC(Handle));
  254.     DeleteObject(TmpBitmap);
  255.     ShowCaret(Handle);
  256.     FPainting := false;
  257.   end;
  258. end;
  259.  
  260. procedure TTranspaEdit.CreateParams(var Params: TCreateParams);
  261. const
  262.   Alignments: array [TAlignment] of DWord = (ES_LEFT, ES_RIGHT, ES_CENTER);
  263. begin
  264.   inherited CreateParams(Params);
  265.   Params.Style := Params.Style or ES_MULTILINE or Alignments[FAlignText];
  266. end;
  267.  
  268. procedure TTranspaEdit.Change;
  269. begin
  270.   RepaintWindow;
  271.   inherited Change;
  272. end;
  273.  
  274. procedure TTranspaEdit.PaintParent(ACanvas: TCanvas);
  275. var
  276.   I, Count, X, Y, SaveIndex: integer;
  277.   DC: cardinal;
  278.   R, SelfR, CtlR: TRect;
  279.   Control : TControl;
  280. begin
  281.     Control := Self;
  282.  
  283.     if Control.Parent = nil then Exit;
  284.     Count := Control.Parent.ControlCount;
  285.     DC := ACanvas.Handle;
  286.     SelfR := Bounds(Control.Left, Control.Top, Control.Width, Control.Height);
  287.     X := -Control.Left; Y := -Control.Top;
  288.     // Copy parent control image
  289.     SaveIndex := SaveDC(DC);
  290.     SetViewportOrgEx(DC, X, Y, nil);
  291.     IntersectClipRect(DC, 0, 0, Control.Parent.ClientWidth, Control.Parent.ClientHeight);
  292.     TParentControl(Control.Parent).Perform(WM_ERASEBKGND,DC,0);
  293.     TParentControl(Control.Parent).PaintWindow(DC);
  294.     RestoreDC(DC, SaveIndex);
  295.  
  296.     //Copy images of graphic controls
  297.     for I := 0 to Count - 1 do
  298.     begin if (Control.Parent.Controls[i] <> nil) then
  299.           begin if Control.Parent.Controls[i] = Control then break;
  300.                 with Control.Parent.Controls[i] do
  301.                 begin CtlR := Bounds(Left, Top, Width, Height);
  302.                       if Bool(IntersectRect(R, SelfR, CtlR)) and Visible then
  303.                       begin SaveIndex := SaveDC(DC);
  304.                             SetViewportOrgEx(DC, Left + X, Top + Y, nil);
  305.                             IntersectClipRect(DC, 0, 0, Width, Height);
  306.                             Perform(WM_ERASEBKGND,DC,0);
  307.                             Perform(WM_PAINT, integer(DC), 0);
  308.                             RestoreDC(DC, SaveIndex);
  309.                       end;
  310.                 end;
  311.           end;
  312.     end;
  313.  
  314. end;
  315.  
  316. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement