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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
| unit unitAlphaTreeView;
interface
uses
Windows, Forms, Messages, SysUtils, Classes, Graphics, Controls, ExtCtrls, ComCtrls, StdCtrls;
type
THoundred=0..100;
TBlendType=(btParentControl,btDeskTop);
PRGBArray = ^TRGBArray;
TRGBArray = Array[0..1000000] of TRGBTriple;
TAlphaTreeView = class(TTreeView)
private
FParentForm:TForm;
FAlphaBlend: Boolean;
FDoubleBuffer: TBitmap;
FOnCollapsed:TNotifyEvent;
FOnExpanded:TNotifyEvent;
PaintLock:boolean;
FBlendType: TBlendType;
FAlphaBlendValue: THoundred;
FCanCreateDoubleBuffer:boolean;
OldWndProc:TFarProc;
NewWndProc:Pointer;
NeedRefresh,Moving:Boolean;
FParentHooked:boolean;
FOnCustDraw: TTVCustomDrawEvent;
FOnCustDrawItem: TTVCustomDrawItemEvent;
procedure CreateDoubleBuffer;
procedure CustomPaint;
procedure HookParentForm;
procedure UnhookParentForm;
procedure CallDefault(var Msg:TMessage);
procedure SetBlendType(const Value: TBlendType);
procedure SetAlphaBlendValue(const Value: THoundred);
procedure SetAlphaBlend(IsAlphaBlend: Boolean);
protected
procedure NewParentWndProc(var Msg: TMessage);
function GetParentForm: TForm;
public
constructor Create(AOwner:TComponent); override;
destructor Destroy; override;
procedure Loaded;
procedure CustDrawButton(ARect: TRect; Node: TTreeNode);
procedure CustDrawItem(Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
procedure CustDraw(Sender: TCustomTreeView; const ARect: TRect; var DefaultDraw: Boolean);
procedure CustRefresh(Sender: TObject; Node: TTreeNode);
published
property AlphaBlend:Boolean read FAlphaBlend write FAlphaBlend;
property AlphaBlendValue:THoundred read FAlphaBlendValue write SetAlphaBlendValue;
property BlendType:TBlendType read FBlendType write SetBlendType;
property OnCustomDrawItem:TTVCustomDrawItemEvent read FOnCustDrawItem write FOnCustDrawItem;
property OnCustomDraw:TTVCustomDrawEvent read FOnCustDraw write FOnCustDraw;
property OnCollapsed : TNotifyEvent read FOnCollapsed write FOnCollapsed;
property OnExpanded : TNotifyEvent read FOnExpanded write FOnExpanded;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('AlphaComponents', [TAlphaTreeView]);
end;
function TAlphaTreeView.GetParentForm: TForm;
var AWinControl:TWinControl;
begin
AWinControl:=Self.Parent;
if AWinControl <> nil then
while not (AWinControl is TForm) do AWinControl := AWinControl.Parent;
result:=(AWinControl as TForm);
end;
{ TAlphaTreeView }
// Méthode invoquée à la création du composant
constructor TAlphaTreeView.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
//ControlStyle:=ControlStyle-[csOpaque];
ParentColor:=false;
FOnCustDraw:=nil;
FOnCustDrawItem:=nil;
FOnCollapsed:=nil;
FOnExpanded:=nil;
FParentHooked:=false;
// Les 4 lignes suivantes permettent d'appeler des méthodes déclanchées par des événements
Inherited OnCustomDraw:=CustDraw;
Inherited OnCustomDrawItem:=CustDrawItem;
Inherited OnCollapsed:=CustRefresh;
Inherited OnExpanded:=CustRefresh;
// Initialise les options du controle
FAlphaBlend:=true;
FAlphaBlendValue:=25;
FBlendType:=btParentControl;
// Créer un buffer graphique
FDoubleBuffer:=TBitmap.Create;
FDoubleBuffer.PixelFormat := pf24bit;
FCanCreateDoubleBuffer := True;
FParentForm:=nil;
if (Owner is TForm) then FParentForm:=(Owner as TForm);
HookParentForm;
CreateDoubleBuffer;
Repaint;
end;
// Méthode invoquée à la destruction du composant
destructor TAlphaTreeView.Destroy;
begin
UnhookParentForm;
Application.ProcessMessages;
FDoubleBuffer.free;
inherited Destroy;
end;
procedure DoBlend(ABitmap:TBitmap;AColor:TColor;AlphaBlendV:THoundred);
var SL: PRGBArray;
X, Y: Integer;
rv,gv,bv:byte;
InvBlend:THoundred;
begin
InvBlend:=100-AlphaBlendV;
rv:=InvBlend*GetRValue(AColor);
gv:=InvBlend*GetGValue(AColor);
bv:=InvBlend*GetBValue(AColor);
with ABitmap do
for Y := 0 to Height - 1 do
begin
SL := ScanLine[Y];
for X := 0 to Width - 1 do
begin
try
SL[X].rgbtRed :=(AlphaBlendV * SL[X].rgbtRed +rv) div 100;
SL[X].rgbtGreen:=(AlphaBlendV * SL[X].rgbtGreen+gv) div 100;
SL[X].rgbtBlue :=(AlphaBlendV * SL[X].rgbtBlue +bv) div 100;
except
end;
end
end;
end;
// Procédure qui charge le buffer graphique
procedure TAlphaTreeView.CreateDoubleBuffer;
var
DC: hDC;
ScreenWidth, ScreenHeight,TicksNow: Integer;
begin
if (csDesigning in ComponentState) or (FDoubleBuffer = nil) then Exit;
if not Assigned(FParentForm) then exit;
FCanCreateDoubleBuffer := False;
case BlendType of
btParentControl:begin
//le dessin du treeview doit être interdit pendant le "PainTo" du parent
//donc on désactive CustomPaint et CustDrawItem
PaintLock:=true;
if not Assigned(Parent) then exit;
FDoubleBuffer.Width:=Parent.Width;
FDoubleBuffer.Height:=Parent.Height;
Parent.PaintTo(FDoubleBuffer.Canvas.Handle,-left,-top);
PaintLock:=false;
DoBlend(FDoubleBuffer,color,FAlphaBlendValue);
end;
btDeskTop:begin
FDoubleBuffer.Width := GetSystemMetrics(sm_CXScreen);
FDoubleBuffer.Height := GetSystemMetrics(sm_CYScreen);
ShowWindow(FParentForm.Handle, SW_Hide);
SetActiveWindow(0);
TicksNow:=GetTickCount;
repeat
Application.ProcessMessages
until GetTickCount-TicksNow>=100;
DC := GetDC(0);
BitBlt(FDoubleBuffer.Canvas.Handle, 0, 0, FDoubleBuffer.Width, FDoubleBuffer.Height, DC, 0, 0, SrcCopy);
ReleaseDC(0, DC);
DoBlend(FDoubleBuffer,color,FAlphaBlendValue);
ShowWindow(FParentForm.Handle, sw_Show);
end;
end;
end;
// Fonction qui permet de récupérer la form du control AlphaTreeview
// Procédure perso pour désinner AlphaTreeView
procedure TAlphaTreeView.CustomPaint;
var X,Y,XSource,YSource: integer;
pixcol : tcolor;
APoint:TPoint;
begin
if ((not FAlphaBlend) or (csDesigning in ComponentState) or (FDoubleBuffer = nil) or (not Assigned(Parent))) then exit;
//le dessin du treeview est interdit pendant le "PainTo" du parent
//(voir "CreateDoubleBuffer")
if PaintLock then exit;
APoint.X:=0;
APoint.Y:=0;
// Si la propriété AlphaBlend du controle est vraie
if (FAlphaBlend) then
begin
case FBlendType of
btParentControl:begin
//conversion des coordonnées du contrôle en coordonnées de son parent
if Assigned(Parent) then
APoint:=Parent.ScreenToClient(ClientToScreen(APoint));
XSource:=APoint.X;
YSource:=APoint.Y;
end;
btDeskTop:begin
//conversion des coordonnées du contrôle en coordonnées ecran
APoint:=ClientToScreen(APoint);
XSource:=APoint.X;
YSource:=APoint.Y;
end;
end;
BitBlt(Canvas.Handle, 0, 0, Width, Height,FDoubleBuffer.Canvas.Handle, XSource, YSource, SrcCopy);
end;
FCanCreateDoubleBuffer := True;
end;
procedure TAlphaTreeView.Loaded;
var p:pointer;
begin
inherited Loaded;
HookParentForm;
CreateDoubleBuffer;
Repaint;
end;
// Procédure utilisée pour dessiner le contrôle
procedure TAlphaTreeView.CustDraw(Sender: TCustomTreeView; const ARect: TRect; var DefaultDraw: Boolean);
begin
CustomPaint;
end;
procedure TAlphaTreeView.SetAlphaBlend(IsAlphaBlend: Boolean);
begin
if FAlphaBlend<>IsAlphaBlend then
begin
FAlphaBlend:=IsAlphaBlend;
CreateDoubleBuffer;
Repaint; //appelle custdraw et custdrawitem via TWincontrol
end;
end;
// Procédure pour dessiner les lignes/boutons de AlphaTreeView
procedure TAlphaTreeView.CustDrawButton(ARect: TRect; Node: TTreeNode);
var
cx, cy, cx2, cy2 : Integer;
begin
// cx := ARect.Left + Indent div 2;
// cy := ARect.Top + (ARect.Bottom - ARect.Top) div 2;
// cx,cy représente le coin supérieur gauche
cx := ARect.Left;
cy := ARect.Top;
with Canvas do
begin
Pen.Color := clGray;
// Tout d'abord, si le noeud à des enfants, on dessine un bouton pour étendre/réduire
if Node.HasChildren then
begin
// Dessine le cadre du bouton d'extension/réduction
cx2:=cx+5;
cy2:=cy+2;
PenPos := Point(cx2, cy2);
LineTo(cx2,cy2+8);
LineTo(cx2+8,cy2+8);
LineTo(cx2+8,cy2);
LineTo(cx2,cy2);
Pen.Color := clBlack;
// Dessine la barre horizontale ('-')
PenPos := Point(cx2+2, cy2+4);
LineTo(cx2+7, cy2+4);
// Dessine la barre verticale si le noeud est réduit ('+')
if not Node.Expanded then
begin
PenPos := Point(cx2+4, cy2+2);
LineTo(cx2+4, cy2+7);
end;
Pen.Color := clGray;
if Node.Parent <> nil then
begin
cx2 := cx + 9;
cy2 := cy - 2;
while (cy2 < cy + 2) do
begin
PenPos := Point(cx2,cy2);
LineTo(cx2,cy2+1);
cy2 := cy2 + 2;
end;
end;
end
// Sinon (pas d'enfant), trace une barre verticale en pointillés
else
begin
cx2 := cx + 9;
if (Node.AbsoluteIndex = 0) then cy2 := cy + 6
else cy2 := cy - 2;
if (Node.getNextSibling <> nil) then
begin
while (cy2 < ARect.Bottom + 3) do
begin
PenPos := Point(cx2,cy2);
LineTo(cx2,cy2+1);
cy2 := cy2 + 2;
end;
end
else begin
while (cy2 < cy + 5) do
begin
PenPos := Point(cx2,cy2);
LineTo(cx2,cy2+1);
cy2 := cy2 + 2;
end;
end;
end;
// Pour tous les noeuds, trace une ligne horizontale en pointillés (entre le bouton et le label du noeud)
if Node.HasChildren then cx2 := cx + 15
else cx2 := cx + 9;
cy2 := cy + 6;
while (cx2 < cx + 18) do
begin
PenPos := Point(cx2,cy2);
LineTo(cx2+1,cy2);
cx2 := cx2 + 2;
end;
if ((Node.GetNextVisible <> nil) and (Node.GetNextVisible.Level = Node.Level))
or (Node.GetNextSibling <> nil) then
begin
cx2 := cx + 9;
cy2 := cy + 12;
while cy2 < cy + 18 do
begin
PenPos := Point(cx2, cy2);
LineTo(cx2,cy2+1);
cy2 := cy2 + 2;
end;
end;
// Connecte les sous-noeuds au noeud parent
Node := Node.Parent;
if Node <> nil then
begin
cx2 := 9;
cy2 := ARect.Top - 2;
while (cy2 < ARect.Bottom - 1) do
begin
PenPos := Point(cx2, cy2);
LineTo(cx2, cy2 + 1);
cy2 := cy2 + 2;
end;
end;
end;
inherited
end;
// Procédure pour dessiner les noeuds de AlphaTreeView
procedure TAlphaTreeView.CustDrawItem(Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
var
NodeRect: TRect;
begin
//le dessin du treeview est interdit pendant le "PainTo" du parent
//(voir "CreateDoubleBuffer")
//if PaintLock then exit;
DefaultDraw := false; // nécessaire pour forcer le dessin manuel des items
with Canvas do
begin
if cdsSelected in State then
begin
NodeRect := Node.DisplayRect(True);
FillRect(NodeRect);
end;
NodeRect := Node.DisplayRect(False);
Brush.Style := bsClear;
NodeRect.Top := NodeRect.Top+1;
NodeRect.Left := NodeRect.Left + (Node.Level * Indent);
// Appel à la procédure de dessin des lignes
CustDrawButton(NodeRect, Node);
NodeRect.Left := NodeRect.Left + Indent;
TextOut(NodeRect.Left, NodeRect.Top, Node.Text);
end;
inherited
end;
procedure TAlphaTreeView.CustRefresh(Sender: TObject; Node: TTreeNode);
begin
CustomPaint;
end;
procedure TAlphaTreeView.SetBlendType(const Value: TBlendType);
begin
FBlendType := Value;
CreateDoubleBuffer;
Repaint; //appelle custdraw et custdrawitem via TWincontrol
end;
procedure TAlphaTreeView.NewParentWndProc(var Msg: TMessage);
begin
CallDefault(Msg);
case Msg.Msg of
wm_Activate: if (Lo(Msg.wParam) <> wa_Inactive) and
FCanCreateDoubleBuffer then CreateDoubleBuffer;
wm_Move: begin
//CreateDoubleBuffer;
Repaint; //appelle custdraw et custdrawitem via TWincontrol
end;
end;
end;
procedure TAlphaTreeView.SetAlphaBlendValue(const Value: THoundred);
begin
if FAlphaBlendValue <> Value then
begin
FAlphaBlendValue := Value;
CreateDoubleBuffer;
RePaint; //appelle custdraw et custdrawitem via TWincontrol
end;
end;
procedure TAlphaTreeView.CallDefault(var Msg: TMessage);
begin
Msg.Result:=CallWindowProc(OldWndProc,FParentForm.Handle,Msg.Msg,Msg.wParam,Msg.lParam);
end;
procedure TAlphaTreeView.HookParentForm;
begin
if not Assigned(FParentForm) then Exit;
OldWndProc:=TFarProc(GetWindowLong(FParentForm.Handle,GWL_WndProc));
NewWndProc:=MakeObjectInstance(NewParentWndProc);
SetWindowLong(FParentForm.Handle,GWL_WndProc,LongInt(NewWndProc));
end;
procedure TAlphaTreeView.UnhookParentForm;
begin
//provoque un plantage...
if Assigned(FParentForm) and Assigned(OldWndProc) then
if FParentForm.HandleAllocated then
SetWindowLong(FParentForm.Handle,GWL_WndProc,LongInt(OldWndProc));
if Assigned(NewWndProc) then
FreeObjectInstance(NewWndProc);
NewWndProc:=nil;
OldWndProc:=nil
end;
end. |
Partager