1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
unit uTransEditPict5;
//******************************************************************************
// Permet de créer un Composant descendant de TEdit et dont le fond est :
// - soit Opaque et de couleur unie,
// - soit formé par une Image de fond
// - soit Transparent et dans ce cas c''est l''arrière-plan de l''Edit qui apparait,
//
// Code modifié à partir de celui du TZ9Edit disponible
// ici : http://www.lummie.co.uk/painting-the-background-of-tedit/
//******************************************************************************
// Pour l'utilistion, poser un Composant TTranspaEditPict5 sur la fiche
// l''image choisie via l''ellipse associée à la proprité propriété Picture
// apparaît seulement lors du lancement de l''application
{$mode objfpc}{$H+}
interface
uses Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
LCLStrConsts, LCLIntf, LCLProc, LCLType, LMessages, Types,
{$IFDEF WINDOWS}
Windows, Messages
{$ELSE}
WinProc,Wintypes
{$ENDIF}
;
type
TTranspaEditPict5 = class(TEdit) // TZ9Edit Modifié voir les //<**
private
{ Private declarations }
FPictureFond : TPicture; //<**
FPictureFondVisible : boolean;
FAlignText : TAlignment;
FTransparent : Boolean;
FPainting : Boolean;
procedure SetPictureFond(Value: TPicture); //<**
procedure SetAlignText(Value: TAlignment);
procedure SetTransparent(Value: Boolean);
protected
{ Protected declarations }
procedure RepaintWindow;
procedure Change; override;
procedure SetParent(AParent: TWinControl); override;
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
procedure WMNCPaint (var Message: TMessage); message WM_NCPAINT;
procedure WMEraseBkGnd(var Message: TWMEraseBkGnd); message WM_ERASEBKGND;
procedure CNCtlColorEdit(var Message: TWMCtlColorEdit); message CN_CTLCOLOREDIT;
procedure CNCtlColorStatic(var Message: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;
procedure CMParentColorChanged(var Message: TMessage); message CM_PARENTCOLORCHANGED;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
procedure WMMove(var Message: TWMMove); message WM_MOVE;
procedure PaintParent(ACanvas: TCanvas);
public
{ Public declarations }
procedure CreateParams(var Params: TCreateParams);override;
constructor Create(AOwner: TComponent); override;
procedure SetPictureFondVisible(Value: boolean); //<**
destructor Destroy; override;
published
{ Published declarations }
property Align;
property AlignText : TAlignment read FAlignText write SetAlignText default taLeftJustify;
property Transparent : Boolean read FTransparent write SetTransparent default False;
property Picture : TPicture read FPictureFond write SetPictureFond; //<**
property PictureVisible: boolean read FPictureFondVisible write SetPictureFondVisible default False; //<**
end;
procedure Register;
implementation
procedure Register;
begin RegisterComponents('Compos Transparents', [TTranspaEditPict5]);
end;
type TParentControl = class(TWinControl);
const BorderRec: array[TBorderStyle] of Integer = (1, -1);
constructor TTranspaEditPict5.Create(AOwner: TComponent);
begin inherited Create(AOwner);
FPictureFond := TPicture.Create;
FAlignText := taLeftJustify;
FTransparent := false;
FPainting := false;
Invalidate;
end;
procedure TTranspaEditPict5.SetParent(AParent: TWinControl);
begin inherited SetParent(AParent);
Invalidate;
end;
procedure TTranspaEditPict5.SetPictureFond(Value : TPicture);
begin if FPictureFond <> Value then
begin FPictureFond.Assign(Value);
FTransparent :=True;
FPictureFondVisible:=True;
Invalidate;
end;
end;
procedure TTranspaEditPict5.SetPictureFondVisible(Value: Boolean); //<**
begin if FPictureFondVisible <> Value then
begin FPictureFondVisible := Value;
Invalidate;
end;
end;
destructor TTranspaEditPict5.Destroy;
begin FPictureFond.Free; //<**
inherited Destroy;
end;
procedure TTranspaEditPict5.SetAlignText(Value: TAlignment);
begin if FAlignText <> Value then
begin FAlignText := Value;
// RecreateWnd(self);
Invalidate;
end;
end;
procedure TTranspaEditPict5.SetTransparent(Value: Boolean);
begin if FTransparent <> Value then
begin FTransparent := Value;
Invalidate;
end;
end;
procedure TTranspaEditPict5.WMEraseBkGnd(var Message: TWMEraseBkGnd);
var DC: hDC; canvas : TCanvas; oldBrush : TBrush;
begin if FTransparent and not(csDesigning in componentstate) then
begin if Not FPictureFondVisible then
begin canvas := TCanvas.create;
try canvas.handle := message.dc;
PaintParent(Canvas);
finally
canvas.free;
end;
end else
if FPictureFondVisible Then
begin DC:=message.dc;
SetBkMode(DC, 1); //SetBkMode(DC, 1 = TRANSPARENT);
Canvas := TCanvas.Create;
try oldbrush := Canvas.Brush;
canvas.handle := dc;
Canvas.Font.Color := Self.Font.Color;
Canvas.Brush.Style := bsClear ;
Canvas.draw(0,0,FPictureFond.Bitmap);
Canvas.Brush := oldbrush;
finally
Canvas.Free;
end;
end;
end else
begin canvas := TCanvas.create;
try canvas.handle := message.dc;
canvas.brush.color := Color;
canvas.brush.style := bsSolid;
canvas.fillrect(clientrect);
finally
canvas.free;
end;
end;
end;
procedure TTranspaEditPict5.WMPaint(var Message: TWMPaint);
begin
inherited;
if FTransparent then
begin
Brush.Bitmap:=FPictureFond.Bitmap;
if not FPainting then RepaintWindow;
end;
end;
procedure TTranspaEditPict5.WMNCPaint(var Message: TMessage);
begin inherited;
end;
procedure TTranspaEditPict5.CNCtlColorEdit(var Message: TWMCtlColorEdit);
begin inherited;
if FTransparent then SetBkMode(Message.ChildDC, 1);
end;
procedure TTranspaEditPict5.CNCtlColorStatic(var Message: TWMCtlColorStatic);
begin inherited;
if FTransparent then SetBkMode(Message.ChildDC, 1);
end;
procedure TTranspaEditPict5.CMParentColorChanged(var Message: TMessage);
begin inherited;
if FTransparent then Invalidate;
end;
procedure TTranspaEditPict5.WMSize(var Message: TWMSize);
var r : TRect;
begin inherited;
r := ClientRect;
InvalidateRect(handle,@r,false);
end;
procedure TTranspaEditPict5.WMMove(var Message: TWMMove);
var r : TRect;
begin inherited;
Invalidate;
r := ClientRect;
InvalidateRect(handle,@r,false);
end;
procedure TTranspaEditPict5.RepaintWindow;
var DC: hDC; TmpBitmap, Bitmap: hBitmap;
begin if FTransparent then
begin FPainting := true;
HideCaret(Handle);
DC := CreateCompatibleDC(GetDC(Handle));
TmpBitmap := CreateCompatibleBitmap( GetDC(Handle), Succ(ClientWidth),
Succ(ClientHeight));
Bitmap := SelectObject(DC, TmpBitmap);
PaintTo(DC, 0, 0);
BitBlt( GetDC(Handle), BorderRec[BorderStyle] + BorderWidth,
BorderRec[BorderStyle] + BorderWidth, ClientWidth,
ClientHeight, DC, 1, 1, SRCCOPY);
SelectObject(DC, Bitmap);
DeleteDC(DC);
ReleaseDC(Handle, GetDC(Handle));
DeleteObject(TmpBitmap);
ShowCaret(Handle);
FPainting := false;
end;
end;
procedure TTranspaEditPict5.CreateParams(var Params: TCreateParams);
const Alignments: array [TAlignment] of DWord = (ES_LEFT, ES_RIGHT, ES_CENTER);
begin inherited CreateParams(Params);
Params.Style := Params.Style or ES_MULTILINE or Alignments[FAlignText];
end;
procedure TTranspaEditPict5.Change;
begin RepaintWindow;
inherited Change;
end;
procedure TTranspaEditPict5.PaintParent(ACanvas: TCanvas);
var I, Count, X, Y, SaveIndex: integer;
DC: cardinal;
R, SelfR, CtlR: TRect;
Control : TControl;
begin Control := Self;
if Control.Parent = nil then Exit;
Count := Control.Parent.ControlCount;
DC := ACanvas.Handle;
SelfR := Bounds(Control.Left, Control.Top, Control.Width, Control.Height);
X := -Control.Left; Y := -Control.Top;
// Copy parent control image
SaveIndex := SaveDC(DC);
SetViewportOrgEx(DC, X, Y, nil);
IntersectClipRect(DC, 0, 0, Control.Parent.ClientWidth, Control.Parent.ClientHeight);
TParentControl(Control.Parent).Perform(WM_ERASEBKGND,DC,0);
TParentControl(Control.Parent).PaintWindow(DC);
RestoreDC(DC, SaveIndex);
//Copy images of graphic controls
for I := 0 to Count - 1 do
begin if (Control.Parent.Controls[i] <> nil) then
begin if Control.Parent.Controls[i] = Control then break;
with Control.Parent.Controls[i] do
begin CtlR := Bounds(Left, Top, Width, Height);
if Bool(IntersectRect(R, SelfR, CtlR)) and Visible then
begin SaveIndex := SaveDC(DC);
SetViewportOrgEx(DC, Left + X, Top + Y, nil);
IntersectClipRect(DC, 0, 0, Width, Height);
Perform(WM_ERASEBKGND,DC,0);
Perform(WM_PAINT, integer(DC), 0);
RestoreDC(DC, SaveIndex);
end;
end;
end;
end;
end;
END. |
Partager