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
|
Unit Unit1;
Interface
Uses Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, ExtCtrls, StdCtrls, Math, PNGImage, HSLUtils;
Type
TForm1 = Class(TForm)
Button1: TButton;
Memo1: TMemo;
Label1: TLabel;
Procedure FormCreate(Sender: TObject);
Procedure FormDestroy(Sender: TObject);
Procedure FormMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
Procedure Button1Click(Sender: TObject);
End;
Var
Form1: TForm1;
IMPLEMENTATION
{$R *.DFM}
Var PNGForm: TForm;
BmpForm: TBitmap;
{----------------------------------------------------------------}
{ Fusionne les controls et l'arrière-plan }
{----------------------------------------------------------------}
Procedure FusionForm(Var FusBitmap: TBitmap);
Var TmpBitmap: TBitmap;
pd, pa: pByteArray;
tcol: TRGB;
i, j: Integer;
Begin
TmpBitmap := TBitmap.Create;
TmpBitmap.Width := Form1.Width;
TmpBitmap.Height := Form1.Height;
TmpBitmap.PixelFormat := pf24bit;
TmpBitmap.Canvas.CopyRect(TmpBitmap.Canvas.ClipRect, Form1.Canvas, Form1.Canvas.ClipRect);
FusBitmap := Nil;
FusBitmap := TBitmap.Create;
FusBitmap.Assign(BmpForm);
FusBitmap.PixelFormat := pf32bit;
tcol := GetRGB(Form1.TransparentColorValue);
For j := 0 To BmpForm.Height - 1 Do Begin
pd := FusBitmap.ScanLine[j];
pa := TmpBitmap.ScanLine[j];
For i := 0 To FusBitmap.Width - 1 Do Begin
If (pa[i * 3 + 2] <> tcol.R) Or
(pa[i * 3 + 1] <> tcol.G) Or
(pa[i * 3 + 0] <> tcol.B) Then Begin
pd[i * 4 + 0] := pa[i * 3 + 0];
pd[i * 4 + 1] := pa[i * 3 + 1];
pd[i * 4 + 2] := pa[i * 3 + 2];
pd[i * 4 + 3] := $FF;
End;
End;
End;
TmpBitmap := Nil;
End;
{----------------------------------------------------------------}
{ Function pour obtenir l'image 32bits à la place d'une form }
{----------------------------------------------------------------}
Function UpdateLayeredWindow(hwnd: HWND; hdcDst: HDC; pptDst: PPoint;
psize: PSize; hdcSrc: HDC; pptSrc: PPoint; crKey: TColor;
pblend: PBlendFunction; dwFlags: DWORD): BOOL; stdcall; external 'user32.dll';
{----------------------------------------------------------------}
{ Procédure appellant la fonction UpdateLayeredWindow }
{----------------------------------------------------------------}
Procedure UpDateForm(Form: TForm; Bmp: TBitmap; Opacite: Byte = $FF);
Const WS_EX_LAYERED = $80000;
Var Size: PSIZE;
TopLeft, BmpTopLeft: TPoint;
Blend: TBlendFunction;
Begin
With Form Do Begin
SetWindowLong(Handle, GWL_EXSTYLE,
GetWindowLong(Handle, GWL_EXSTYLE) Or WS_EX_LAYERED);
New(Size);
Size.cx := Width;
Size.cy := Height;
TopLeft := BoundsRect.TopLeft;
BmpTopLeft := Point(0, 0);
With Blend Do Begin
BlendOp := 0;
BlendFlags := 0;
SourceConstantAlpha := Opacite;
AlphaFormat := 1;
End;
Bmp.PixelFormat := pf32bit;
UpdateLayeredWindow(Handle, GetDC(0), @TopLeft, Size,
Bmp.Canvas.handle, @BmpTopLeft, 0, @Blend, 2);
End;
End;
{----------------------------------------------------------------}
{ Fade Out du programme }
{----------------------------------------------------------------}
Procedure FadeOutForm(Delay: Integer = 800);
Const Division = 40;
Var x: Integer;
opacite: Byte;
FusBitmap: TBitmap;
Begin
Form1.BringToFront;
FusionForm(FusBitmap);
UpDateForm(PNGForm, Fusbitmap);
Form1.Hide;
For x := 1 To Division Do Begin
opacite := Abs($FF - Round(Max(Min($FF / Division * x, $FF), 0)));
UpDateForm(PNGForm, Fusbitmap, opacite);
Sleep(Delay Div Division);
End;
UpDateForm(PNGForm, BmpForm, 0);
End;
{----------------------------------------------------------------}
{ Fade In du programme }
{----------------------------------------------------------------}
Procedure FadeInForm(Delay: Integer = 800);
Const Division = 20;
Var x: Integer;
opacite: Byte;
FusBitmap: TBitmap;
Begin
Form1.AlphaBlend := True;
Form1.AlphaBlendValue := 0;
PNGForm.Show;
Form1.Show;
Application.ProcessMessages;
FusionForm(FusBitmap);
UpDateForm(PNGForm, Fusbitmap, 0);
Form1.Hide;
Form1.AlphaBlend := False;
Application.ProcessMessages;
For x := 1 To Division Do Begin
opacite := Round(Max(Min($FF / Division * x, $FF), 0));
UpDateForm(PNGForm, Fusbitmap, opacite);
Sleep(Delay Div Division);
End;
Form1.Show;
UpDateForm(PNGForm, BmpForm);
End;
{----------------------------------------------------------------}
{ Chargement d'une image PNG dans un TBitmap 32bits }
{----------------------------------------------------------------}
Function MyLoadPNG(fn: String; Var FinalBitmap: TBitmap): Boolean;
Var PNG: TPNGObject;
AlphaBitmap: TBitmap;
pd, pa: pByteArray;
i, j, a: Integer;
Begin
Result := False;
FinalBitmap := Nil;
FinalBitmap := TBitmap.Create;
If (FileExists(fn) = False) Then Begin
ShowMessage('Image ' + ExtractFilename(fn) + ' introuvable.');
Exit;
End;
PNG := TPNGObject.Create;
Try PNG.LoadFromFile(fn);
Except
ShowMessage('Erreur format PNG (' + ExtractFilename(fn) + ')');
PNG := Nil;
Exit;
End;
AlphaBitmap := TBitmap.Create;
AlphaBitmap.Height := PNG.Height;
AlphaBitmap.Width := PNG.Width;
AlphaBitmap.PixelFormat := pf24bit;
FinalBitmap.Assign(PNG);
FinalBitmap.PixelFormat := pf32bit;
If (Png.Transparent) Then Begin
For i := 0 To PNG.Height - 1 Do
For j := 0 To PNG.Width - 1 Do
If (PNG.AlphaScanline[i][j] >= 240) Then
AlphaBitmap.Canvas.Pixels[j, i] := $FFFFFF Else
AlphaBitmap.Canvas.Pixels[j, i] := HSLRangeToRGB(0, 0, PNG.AlphaScanline[i][j]);
End Else Begin
AlphaBitmap.Canvas.Brush.Style := bsSolid;
AlphaBitmap.Canvas.Brush.Color := $FFFFFF;
AlphaBitmap.Canvas.FillRect(AlphaBitmap.Canvas.ClipRect);
End;
For j := 0 To PNG.Height - 1 Do Begin
pd := FinalBitmap.ScanLine[j];
pa := AlphaBitmap.ScanLine[j];
For i := 0 To PNG.Width - 1 Do Begin
a := pa[i * 3];
If (a < 240) Then Begin
pd[i * 4 + 0] := Round(Max(Min(pd[i * 4 + 0] * a / $FF, $FF), 0));
pd[i * 4 + 1] := Round(Max(Min(pd[i * 4 + 1] * a / $FF, $FF), 0));
pd[i * 4 + 2] := Round(Max(Min(pd[i * 4 + 2] * a / $FF, $FF), 0));
End;
pd[i * 4 + 3] := a;
End;
End;
AlphaBitmap := Nil;
PNG := Nil;
Result := True;
End;
{----------------------------------------------------------------}
{ Dessin d'un TBitmap 32bits sur un autre (fusion) }
{----------------------------------------------------------------}
Procedure MyDrawPNG(X, Y: Integer; AlphaBitmap, FinalBitmap: TBitmap);
Var pd, pa: pByteArray;
i, ix, j, a, b, ad: Integer;
Begin
If (Assigned(AlphaBitmap) = False) Then Exit;
If (Assigned(FinalBitmap) = False) Then Exit;
FinalBitmap.PixelFormat := pf32bit;
For j := 0 To AlphaBitmap.Height - 1 Do
If (j + Y <= FinalBitmap.Height - 1) And (j + Y >= 0) Then Begin
pd := FinalBitmap.ScanLine[j + Y];
pa := AlphaBitmap.ScanLine[j];
For i := 0 To AlphaBitmap.Width - 1 Do Begin
a := pa[i * 4 + 3];
b := Abs($FF - a);
ix := Max(Min(i + X, FinalBitmap.Width - 1), 0);
ad := pd[ix * 4 + 3];
If (a >= 240) Then Begin
pd[ix * 4 + 0] := pa[i * 4 + 0];
pd[ix * 4 + 1] := pa[i * 4 + 1];
pd[ix * 4 + 2] := pa[i * 4 + 2];
pd[ix * 4 + 3] := $FF;
End Else If (a >= 0) Then Begin
If (ad < 240) Then
b := Round(Max(Min(a + (ad * b) / $FF, $FF), 0));;
pd[ix * 4 + 0] := Round(Max(Min(
pa[i * 4 + 0] + b * pd[ix * 4 + 0] / $FF, $FF), 0));
pd[ix * 4 + 1] := Round(Max(Min(
pa[i * 4 + 1] + b * pd[ix * 4 + 1] / $FF, $FF), 0));
pd[ix * 4 + 2] := Round(Max(Min(
pa[i * 4 + 2] + b * pd[ix * 4 + 2] / $FF, $FF), 0));
If (ad < 240) Then pd[ix * 4 + 3] := b;
End;
End;
End;
End;
{----------------------------------------------------------------}
{ Création & initialisation des forms }
{----------------------------------------------------------------}
Procedure TForm1.FormCreate(Sender: TObject);
Var bmp: TBitmap;
Begin
DoubleBuffered := True;
Color := clFuchsia;
TransparentColorValue := Color;
BorderStyle := bsNone;
// Chargement de l'image PNG pour l'arrière-plan, celle de AndNotOr
If (MyLoadPNG('background.png', BmpForm)) Then Begin
Width := BmpForm.Width;
Height := BmpForm.Height;
End;
// Création de la form d'arrière-plan //
PNGForm := TForm.Create(self);
With PNGForm Do Begin
Parent := Form1.Parent;
Name := 'PNGForm';
Caption := Form1.Caption;
FormStyle := Form1.FormStyle;
BorderStyle := Form1.BorderStyle;
BorderIcons := [];
Position := Form1.Position;
Left := Form1.Left;
Top := Form1.Top;
Width := Form1.Width;
Height := Form1.Height;
OnMouseDown := FormMouseDown;
DoubleBuffered := True;
Visible := False;
End;
// Exemple d'ajoût d'image PNG //
MyLoadPNG('6c.png', bmp); //Petit logo de hommer
MyDrawPNG(0, 28, bmp, BmpForm);
bmp := Nil;
// Fusion de l'image finale sur la form & FadIn //
UpDateForm(PNGForm, BmpForm, 0);
FadeInForm;
End;
{----------------------------------------------------------------}
{ Finalisation du programme }
{----------------------------------------------------------------}
Procedure TForm1.FormDestroy(Sender: TObject);
Begin
BmpForm := Nil;
End;
{----------------------------------------------------------------}
{ Déplacement de la fenêtre (image et controls fusionnés) }
{----------------------------------------------------------------}
Procedure TForm1.FormMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
Var FusBitmap: TBitmap;
Begin
Form1.BringToFront;
If (Button <> mbLeft) Then Exit;
// Fusionne les controls et l'arrière-plan //
FusionForm(FusBitmap);
// Applique la nouvelle image et cache les controls //
UpDateForm(PNGForm, FusBitmap);
Form1.Hide;
PNGForm.Show;
FusBitmap := Nil;
// Boucle de déplacement de la form //
Screen.Cursor := crSizeAll;
X := PNGForm.Left - PNGForm.ScreenToClient(Mouse.CursorPos).X;
Y := PNGForm.Top - PNGForm.ScreenToClient(Mouse.CursorPos).Y;
Repeat
PNGForm.Left := X + ScreenToClient(Mouse.CursorPos).X;
PNGForm.Top := Y + ScreenToClient(Mouse.CursorPos).Y;
Application.ProcessMessages;
Sleep(10);
Until ((GetAsyncKeyState(VK_LBUTTON) And $8000) = 0);
Form1.Left := PNGForm.Left;
Form1.Top := PNGForm.Top;
Screen.Cursor := crDefault;
// Restaure l'arrière-plan original et réaffiche les controls //
Form1.Show;
UpDateForm(PNGForm, BmpForm);
End;
{----------------------------------------------------------------}
{ Exemple d'objet actif -> Bouton EXIT }
{----------------------------------------------------------------}
Procedure TForm1.Button1Click(Sender: TObject);
Begin
FadeOutForm;
Application.Terminate;
End;
{----------------------------------------------------------------}
End. |
Partager