TLama

Untitled

Mar 28th, 2013
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.85 KB | None | 0 0
  1. unit ImageButton;
  2.  
  3. //2013 bummi
  4.  
  5. interface
  6. uses
  7.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  8.   StdCtrls, ImgList;
  9.  
  10. type
  11.   TButtonState = (bsMouseIn, bsMouseOut, bsPressed);
  12.   TImageButton = class(TGraphicControl)
  13.   private
  14.     FIndex: Integer;
  15.     FDownIndex: Integer;
  16.     FHoverIndex: Integer;
  17.     FImages: TCustomImageList;
  18.     FImagesChangeLink: TChangeLink;
  19.     FButtonState: TButtonState;
  20.     procedure ImagesChange(Sender: TObject);
  21.     procedure SetDownIndex(Value: Integer);
  22.     procedure SetHoverIndex(Value: Integer);
  23.     procedure SetIndex(Value: Integer);
  24.     procedure SetImages(Value: TCustomImageList);
  25.     procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
  26.     procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  27.     procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  28.     procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
  29.     procedure WMLButtonDown(var Message: TMessage); message WM_LBUTTONDOWN;
  30.     procedure WMLButtonUp(var Message: TMessage); message WM_LBUTTONUP;
  31.   protected
  32.     procedure Paint; override;
  33.   public
  34.     constructor Create(AOwner: TComponent); override;
  35.     destructor Destroy; override;
  36.   published
  37.     property Caption;
  38.     property DownIndex: Integer read FDownIndex write SetDownIndex;
  39.     property Font;
  40.     property HoverIndex: Integer read FHoverIndex write SetHoverIndex;
  41.     property Images: TCustomImageList read FImages write SetImages;
  42.     property Index: Integer read FIndex write SetIndex;
  43.   end;
  44.  
  45.   procedure Register;
  46.  
  47. implementation
  48.  
  49. constructor TImageButton.Create(AOwner: TComponent);
  50. begin
  51.   inherited Create(AOwner);
  52.   Parent := TWinControl(AOwner);
  53.   Width := 200;
  54.   Height := 200;
  55.   FButtonState := bsMouseOut;
  56.   FImagesChangeLink := TChangeLink.Create;
  57.   FImagesChangeLink.OnChange := ImagesChange;
  58. end;
  59.  
  60. Destructor TImageButton.Destroy;
  61. begin
  62.   FImagesChangeLink.Free;
  63.   inherited Destroy;
  64. end;
  65.  
  66. procedure TImageButton.Paint;
  67. var
  68.   S: string;
  69.   DestRect: TRect;
  70.   ImageIndex: Integer;
  71. begin
  72.   inherited;
  73.   ImageIndex := -1;
  74.   if Assigned(FImages) then
  75.   begin
  76.     case FButtonState of
  77.       bsMouseIn:
  78.         if FImages.Count > HoverIndex then
  79.           ImageIndex := HoverIndex;
  80.       bsMouseOut:
  81.         if FImages.Count > Index then
  82.           ImageIndex := Index;
  83.       bsPressed:
  84.         if FImages.Count > DownIndex then
  85.           ImageIndex := DownIndex;
  86.     end;
  87.     if ImageIndex > -1 then
  88.       FImages.Draw(Canvas, 0, 0, ImageIndex);
  89.   end
  90.   else
  91.     Canvas.Rectangle(ClientRect);
  92.  
  93.   S := Caption;
  94.   DestRect := ClientRect;
  95.   Canvas.Font.Assign(Font);
  96.   Canvas.Brush.Style := bsClear;
  97.   Canvas.TextRect(DestRect, S, [tfVerticalCenter, tfCenter, tfSingleLine]);
  98. end;
  99.  
  100. procedure TImageButton.ImagesChange(Sender: TObject);
  101. begin
  102.   if not (csDestroying in ComponentState) then
  103.     Invalidate;
  104. end;
  105.  
  106. procedure TImageButton.SetDownIndex(Value: Integer);
  107. begin
  108.   if FDownIndex <> Value then
  109.   begin
  110.     FDownIndex := Value;
  111.     Invalidate;
  112.   end;
  113. end;
  114.  
  115. procedure TImageButton.SetHoverIndex(Value: Integer);
  116. begin
  117.   if FHoverIndex <> Value then
  118.   begin
  119.     FHoverIndex := Value;
  120.     Invalidate;
  121.   end;
  122. end;
  123.  
  124. procedure TImageButton.SetImages(Value: TCustomImageList);
  125. begin
  126.   if FImages <> Value then
  127.   begin
  128.     if Assigned(FImages) then
  129.     begin
  130.       FImages.UnRegisterChanges(FImagesChangeLink);
  131.       FImages.RemoveFreeNotification(Self);
  132.     end;
  133.  
  134.     FImages := Value;
  135.  
  136.     if Assigned(FImages) then
  137.     begin
  138.       FImages.RegisterChanges(FImagesChangeLink);
  139.       FImages.FreeNotification(Self);
  140.       ImagesChange(nil);
  141.     end;
  142.   end;
  143. end;
  144.  
  145. procedure TImageButton.SetIndex(Value: Integer);
  146. begin
  147.   if FIndex <> Value then
  148.   begin
  149.     FIndex := Value;
  150.     Invalidate;
  151.   end;
  152. end;
  153.  
  154. procedure TImageButton.WMLButtonDown(var Message: TMessage);
  155. begin
  156.   inherited;
  157.   FButtonState := bsPressed;
  158.   Invalidate;
  159. end;
  160.  
  161. procedure TImageButton.WMLButtonUp(var Message: TMessage);
  162. begin
  163.   inherited;
  164.   FButtonState := bsMouseIn;
  165.   Invalidate;
  166. end;
  167.  
  168. Procedure TImageButton.CMMouseEnter(var Message: TMessage);
  169. Begin
  170.   inherited;
  171.   if not (csDesigning in ComponentState) and (FButtonState <> bsMouseIn) then
  172.   begin
  173.     FButtonState := bsMouseIn;
  174.     Invalidate;
  175.   end;
  176. end;
  177.  
  178. Procedure TImageButton.CMMouseLeave(var Message: TMessage);
  179. Begin
  180.   inherited;
  181.   if not (csDesigning in ComponentState) and (FButtonState <> bsMouseOut) then
  182.   begin
  183.     FButtonState := bsMouseOut;
  184.     Invalidate;
  185.   end;
  186. end;
  187.  
  188. procedure TImageButton.CMFontChanged(var Message: TMessage);
  189. begin
  190.   inherited;
  191.   Invalidate;
  192. end;
  193.  
  194. procedure TImageButton.CMTextChanged(var Message: TMessage);
  195. begin
  196.   inherited;
  197.   Invalidate;
  198. end;
  199.  
  200. procedure Register;
  201. begin
  202.   RegisterComponents('Own', [TImageButton])
  203. end;
  204.  
  205. end.
Advertisement
Add Comment
Please, Sign In to add comment