home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 March / Chip_1999-03_cd.bin / zkuste / delphi / D234C13 / RALIB.ZIP / RALib / Lib / RAImage.pas < prev    next >
Pascal/Delphi Source File  |  1998-08-19  |  6KB  |  220 lines

  1. unit RAImage;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
  7.  
  8. type
  9.   TRAImage = class(TCustomControl)
  10.   private
  11.     FPicture: TPicture;
  12.     FBorderStyle: TBorderStyle;
  13.     FStretch: Boolean;
  14.     FCenter: Boolean;
  15.     procedure PictureChanged(Sender: TObject);
  16.     procedure SetBorderStyle(Value: TBorderStyle);
  17.     procedure SetCenter(Value: Boolean);
  18.     procedure SetPicture(Value: TPicture);
  19.     procedure SetStretch(Value: Boolean);
  20.     procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
  21.     procedure CMExit(var Message: TCMExit); message CM_EXIT;
  22.     procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
  23.   protected
  24.     procedure CreateParams(var Params: TCreateParams); override;
  25.     function GetPalette: HPALETTE; override;
  26.     procedure Paint; override;
  27.   public
  28.     constructor Create(AOwner: TComponent); override;
  29.     destructor Destroy; override;
  30.   published
  31.     property Align;
  32.     property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle default bsSingle;
  33.     property Center: Boolean read FCenter write SetCenter default True;
  34.     property Color;
  35.     property Ctl3D;
  36.     property DragCursor;
  37.     property DragMode;
  38.     property Enabled;
  39.     property Font;
  40.     property ParentColor default true;
  41.     property ParentCtl3D;
  42.     property ParentFont;
  43.     property ParentShowHint;
  44.     property PopupMenu;
  45.     property ShowHint;
  46.     property Stretch: Boolean read FStretch write SetStretch default False;
  47.     property Picture: TPicture read FPicture write SetPicture;
  48.     property TabOrder;
  49.     property TabStop default True;
  50.     property Visible;
  51.     property OnClick;
  52.     property OnDblClick;
  53.     property OnDragDrop;
  54.     property OnDragOver;
  55.     property OnEndDrag;
  56.     property OnEnter;
  57.     property OnExit;
  58.     property OnKeyDown;
  59.     property OnKeyPress;
  60.     property OnKeyUp;
  61.     property OnMouseDown;
  62.     property OnMouseMove;
  63.     property OnMouseUp;
  64.     property OnStartDrag;
  65.  {$IFDEF RA_D4H}
  66.   public
  67.     property DockManager;
  68.   published
  69.     property Anchors;
  70.     property AutoSize;
  71.     property BiDiMode;
  72.     property Constraints;
  73.     property UseDockManager default True;
  74.     property DockSite;
  75.     property DragKind;
  76.     property ParentBiDiMode;
  77.     property OnCanResize;
  78.     property OnConstrainedResize;
  79.     property OnDockDrop;
  80.     property OnDockOver;
  81.     property OnEndDock;
  82.     property OnGetSiteInfo;
  83.     property OnStartDock;
  84.     property OnUnDock; 
  85.  {$ENDIF RA_D4H}
  86.   end;
  87.  
  88. implementation
  89.  
  90. constructor TRAImage.Create(AOwner: TComponent);
  91. begin
  92.   inherited Create(AOwner);
  93.   ControlStyle := ControlStyle + [csOpaque];
  94.   Width := 105;
  95.   Height := 105;
  96.   TabStop := True;
  97.   ParentColor := true;
  98.   FPicture := TPicture.Create;
  99.   FPicture.OnChange := PictureChanged;
  100.   FBorderStyle := bsSingle;
  101.   FCenter := True;
  102. end;
  103.  
  104. destructor TRAImage.Destroy;
  105. begin
  106.   FPicture.Free;
  107.   inherited Destroy;
  108. end;
  109.  
  110. function TRAImage.GetPalette: HPALETTE;
  111. begin
  112.   if FPicture.Graphic is TBitmap then
  113.     Result := TBitmap(FPicture.Graphic).Palette
  114.   else Result := 0;
  115. end;
  116.  
  117. procedure TRAImage.SetBorderStyle(Value: TBorderStyle);
  118. begin
  119.   if FBorderStyle <> Value then
  120.   begin
  121.     FBorderStyle := Value;
  122.     RecreateWnd;
  123.   end;
  124. end;
  125.  
  126. procedure TRAImage.SetCenter(Value: Boolean);
  127. begin
  128.   if FCenter <> Value then
  129.   begin
  130.     FCenter := Value;
  131.     Invalidate;
  132.   end;
  133. end;
  134.  
  135. procedure TRAImage.SetPicture(Value: TPicture);
  136. begin
  137.   FPicture.Assign(Value);
  138. end;
  139.  
  140. procedure TRAImage.SetStretch(Value: Boolean);
  141. begin
  142.   if FStretch <> Value then
  143.   begin
  144.     FStretch := Value;
  145.     Invalidate;
  146.   end;
  147. end;
  148.  
  149. procedure TRAImage.Paint;
  150. var
  151.   H : Integer;
  152.   R : TRect;
  153.   DrawPict : TPicture;
  154. begin
  155.   with Canvas do begin
  156.     Brush.Style := bsSolid;
  157.     Brush.Color := Color;
  158.     DrawPict := TPicture.Create;
  159.     H := 0;
  160.     try
  161.       DrawPict.Assign(FPicture);
  162.       if Focused and (DrawPict.Graphic is TBitmap) and (DrawPict.Bitmap.Palette <> 0) then begin
  163.         H := SelectPalette(Handle, DrawPict.Bitmap.Palette, False);
  164.         RealizePalette(Handle);
  165.       end;
  166.       if Stretch then
  167.         if (DrawPict.Graphic = nil) or DrawPict.Graphic.Empty then
  168.           FillRect(ClientRect)
  169.         else
  170.           StretchDraw(ClientRect, DrawPict.Graphic)
  171.       else begin
  172.         SetRect(R, 0, 0, DrawPict.Width, DrawPict.Height);
  173.         if Center then OffsetRect(R, (ClientWidth - DrawPict.Width) div 2,
  174.           (ClientHeight - DrawPict.Height) div 2);
  175.         StretchDraw(R, DrawPict.Graphic);
  176.         ExcludeClipRect(Handle, R.Left, R.Top, R.Right, R.Bottom);
  177.         FillRect(ClientRect);
  178.         SelectClipRgn(Handle, 0);
  179.       end;
  180.     finally
  181.       if H <> 0 then SelectPalette(Handle, H, True);
  182.       DrawPict.Free;
  183.     end;
  184.     if (GetParentForm(Self).ActiveControl = Self) and
  185.       not (csDesigning in ComponentState) then DrawFocusRect(ClientRect);
  186.   end;
  187. end;
  188.  
  189. procedure TRAImage.PictureChanged(Sender: TObject);
  190. begin
  191.   Invalidate;
  192. end;
  193.  
  194. procedure TRAImage.CreateParams(var Params: TCreateParams);
  195. begin
  196.   inherited CreateParams(Params);
  197.   if FBorderStyle = bsSingle then
  198.     Params.ExStyle := Params.ExStyle or WS_EX_CLIENTEDGE;
  199. end;
  200.  
  201. procedure TRAImage.CMEnter(var Message: TCMEnter);
  202. begin
  203.   Invalidate; { Draw the focus marker }
  204.   inherited;
  205. end;
  206.  
  207. procedure TRAImage.CMExit(var Message: TCMExit);
  208. begin
  209.   Invalidate; { Erase the focus marker }
  210.   inherited;
  211. end;
  212.  
  213. procedure TRAImage.WMLButtonDown(var Message: TWMLButtonDown);
  214. begin
  215.   if TabStop and CanFocus then SetFocus;
  216.   inherited;
  217. end;
  218.  
  219. end.
  220.