Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Card;
- interface
- uses
- SysUtils, Classes, Controls, Graphics;
- type
- TCustomCard = class(TGraphicControl)
- private
- FBackground: TPicture;
- procedure SetBackground(Value: TPicture);
- procedure BackgroundChanged(Sender: TObject);
- protected
- procedure Paint; override;
- property Background: TPicture read FBackground write SetBackground;
- public
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- end;
- TCard = class(TCustomCard)
- published
- property Background;
- end;
- procedure Register;
- implementation
- { TCustomCard }
- constructor TCustomCard.Create(AOwner: TComponent);
- begin
- inherited;
- FBackground := TPicture.Create;
- FBackground.OnChange := BackgroundChanged;
- end;
- destructor TCustomCard.Destroy;
- begin
- FBackground.Free;
- inherited;
- end;
- procedure TCustomCard.SetBackground(Value: TPicture);
- begin
- // simple assignment of a TPicture will trigger its OnChange event, which
- // is here implemented as BackgroundChanged method, so the autosize by the
- // picture we'll do there
- FBackground.Assign(Value);
- end;
- procedure TCustomCard.BackgroundChanged(Sender: TObject);
- begin
- // this will be triggered whenever the picture is changed, so let's resize
- // the control by the image's size here; first check if the picture is not
- // 0 pixels in any dimension and if not, resize the control
- if (FBackground.Width > 0) and (FBackground.Height > 0) then
- SetBounds(Left, Top, FBackground.Width, FBackground.Height);
- Invalidate;
- end;
- procedure TCustomCard.Paint;
- begin
- // the TCanvas.StretchDraw here checks if the passed Graphic parameter not
- // nil, so we just call it here
- Canvas.StretchDraw(ClientRect, FBackground.Graphic);
- end;
- procedure Register;
- begin
- RegisterComponents('Samples', [TCard]);
- end;
- end.
- // To load a background from file (e.g. a JPEG format), just include JPEG unit
- // to the uses clause of a unit where you'll load it and use a code like this:
- uses
- JPEG;
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- Card1.Background.LoadFromFile('d:\Image.jpg');
- end;
- // or you can load the internal image using TGraphic assigment e.g. this way:
- uses
- JPEG;
- procedure TForm1.Button1Click(Sender: TObject);
- var
- JPEGImage: TJPEGImage;
- begin
- JPEGImage := TJPEGImage.Create;
- try
- JPEGImage.LoadFromFile('d:\Image.jpg');
- Card1.Background.Assign(JPEGImage);
- finally
- JPEGImage.Free;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment