TLama

Untitled

Sep 29th, 2013
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.48 KB | None | 0 0
  1. unit Card;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Classes, Controls, Graphics;
  7.  
  8. type
  9.   TCustomCard = class(TGraphicControl)
  10.   private
  11.     FBackground: TPicture;
  12.     procedure SetBackground(Value: TPicture);
  13.     procedure BackgroundChanged(Sender: TObject);
  14.   protected
  15.     procedure Paint; override;
  16.     property Background: TPicture read FBackground write SetBackground;
  17.   public
  18.     constructor Create(AOwner: TComponent); override;
  19.     destructor Destroy; override;
  20.   end;
  21.  
  22.   TCard = class(TCustomCard)
  23.   published
  24.     property Background;
  25.   end;
  26.  
  27. procedure Register;
  28.  
  29. implementation
  30.  
  31. { TCustomCard }
  32.  
  33. constructor TCustomCard.Create(AOwner: TComponent);
  34. begin
  35.   inherited;
  36.   FBackground := TPicture.Create;
  37.   FBackground.OnChange := BackgroundChanged;
  38. end;
  39.  
  40. destructor TCustomCard.Destroy;
  41. begin
  42.   FBackground.Free;
  43.   inherited;
  44. end;
  45.  
  46. procedure TCustomCard.SetBackground(Value: TPicture);
  47. begin
  48.   // simple assignment of a TPicture will trigger its OnChange event, which
  49.   // is here implemented as BackgroundChanged method, so the autosize by the
  50.   // picture we'll do there
  51.   FBackground.Assign(Value);
  52. end;
  53.  
  54. procedure TCustomCard.BackgroundChanged(Sender: TObject);
  55. begin
  56.   // this will be triggered whenever the picture is changed, so let's resize
  57.   // the control by the image's size here; first check if the picture is not
  58.   // 0 pixels in any dimension and if not, resize the control
  59.   if (FBackground.Width > 0) and (FBackground.Height > 0) then
  60.     SetBounds(Left, Top, FBackground.Width, FBackground.Height);
  61.   Invalidate;
  62. end;
  63.  
  64. procedure TCustomCard.Paint;
  65. begin
  66.   // the TCanvas.StretchDraw here checks if the passed Graphic parameter not
  67.   // nil, so we just call it here
  68.   Canvas.StretchDraw(ClientRect, FBackground.Graphic);
  69. end;
  70.  
  71. procedure Register;
  72. begin
  73.   RegisterComponents('Samples', [TCard]);
  74. end;
  75.  
  76. end.
  77.  
  78. // To load a background from file (e.g. a JPEG format), just include JPEG unit
  79. // to the uses clause of a unit where you'll load it and use a code like this:
  80. uses
  81.   JPEG;
  82.  
  83. procedure TForm1.Button1Click(Sender: TObject);
  84. begin
  85.   Card1.Background.LoadFromFile('d:\Image.jpg');
  86. end;
  87.  
  88. // or you can load the internal image using TGraphic assigment e.g. this way:
  89. uses
  90.   JPEG;
  91.  
  92. procedure TForm1.Button1Click(Sender: TObject);
  93. var
  94.   JPEGImage: TJPEGImage;
  95. begin
  96.   JPEGImage := TJPEGImage.Create;
  97.   try
  98.     JPEGImage.LoadFromFile('d:\Image.jpg');
  99.     Card1.Background.Assign(JPEGImage);
  100.   finally
  101.     JPEGImage.Free;
  102.   end;
  103. end;
Advertisement
Add Comment
Please, Sign In to add comment