Einzelnen Beitrag anzeigen

Benutzerbild von Merlin1988
Merlin1988

Registriert seit: 10. Aug 2005
33 Beiträge
 
Delphi 6 Enterprise
 
#5

Re: AlphaBlending und Canvas der Oberklasse

  Alt 29. Nov 2005, 16:26
Delphi-Quellcode:
unit BildLabel;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, Graphics, Forms, StdCtrls;
  
type
  TBildLabel = class(TGraphicControl)
  private
    FPicture: TBitmap;
    FAlphaValue: Byte;
    procedure SetPicture(const Value: TBitmap);
    procedure SetAlphaValue(const Value: Byte);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Paint; override;
  published
    property Picture: TBitmap read FPicture write SetPicture;
    property AlphaValue: Byte read FAlphaValue write SetAlphaValue;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('My Own', [TBildLabel]);
end;

{ TBildLabel }

constructor TBildLabel.Create(AOwner: TComponent);
begin
  inherited;
  FPicture := TBitmap.Create;
  fAlphaValue := 0;
end;

destructor TBildLabel.Destroy;
begin
  FPicture.Free;
  inherited;
end;

procedure TBildLabel.Paint;
var Bitmap: TBitmap;
    BlendFunction: TBlendFunction;
begin
  //Buffer-Bitmap erstellen
  Bitmap := TBitmap.Create;
  Bitmap.Width := Width;
  Bitmap.Height := Height;
  //Picture in Buffer-Bitmap kopieren
  Bitmap.Canvas.StretchDraw(Bitmap.Canvas.ClipRect,FPicture);

  //Buffer-Bitmap in den Canvas laden
  BlendFunction.BlendOp := AC_SRC_OVER;
  BlendFunction.BlendFlags := 0;
  BlendFunction.SourceConstantAlpha := fAlphaValue;
  BlendFunction.AlphaFormat := 0;

  windows.AlphaBlend(Canvas.Handle, 0, 0, Width, Height, Bitmap.Canvas.Handle,
                     0, 0, Width, Height, BlendFunction);

  Bitmap.Free;
end;

procedure TBildLabel.SetAlphaValue(const Value: Byte);
begin
  if FAlphaValue <> Value then begin
    FAlphaValue := Value;
    Invalidate;
  end;
end;

procedure TBildLabel.SetPicture(const Value: TBitmap);
begin
  if FPicture <> Value then begin
    FPicture.Assign(Value);
    Invalidate;
  end;
end;

end.
Die Komponente ist diesmal eine andere, ich hab aber folgendes Problem:

Zur Designzeit ist alles perfekt, zur Laufzeit aber blendet er nicht richtig. Heißt er blendet immer auf einen weißen Hintergrund, anstatt auf den Hintergrund der sich hinter ihm befindet !!??

Use the Source, Luke.
  Mit Zitat antworten Zitat