VrHotImage.pas
上传用户:hbszzs
上传日期:2008-08-20
资源大小:628k
文件大小:9k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrHotImage;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   VrTypes, VrConst, VrClasses, VrControls, VrSysUtils;
  15. type
  16.   TVrHotRect = class(TVrPersistent)
  17.   private
  18.     FColor: TColor;
  19.     FWidth: Integer;
  20.     FVisible: Boolean;
  21.     procedure SetColor(Value: TColor);
  22.     procedure SetWidth(Value: Integer);
  23.     procedure SetVisible(Value: Boolean);
  24.   public
  25.     constructor Create;
  26.     procedure Assign(Source: TPersistent); override;
  27.   published
  28.     property Color: TColor read FColor write SetColor default clYellow;
  29.     property Width: Integer read FWidth write SetWidth default 1;
  30.     property Visible: Boolean read FVisible write SetVisible default True;
  31.   end;
  32.   TVrHotImageDrawStyle = (dsCenter, dsStretch);
  33.   TVrHotImage = class(TVrHyperLinkControl)
  34.   private
  35.     FImageLeave: TBitmap;
  36.     FImageEnter: TBitmap;
  37.     FColorEnter: TColor;
  38.     FColorLeave: TColor;
  39.     FDrawStyle: TVrHotImageDrawStyle;
  40.     FTextAlignment: TVrTextAlignment;
  41.     FHotRect: TVrHotRect;
  42.     FHasMouse: Boolean;
  43.     procedure SetImageLeave(Value: TBitmap);
  44.     procedure SetImageEnter(Value: TBitmap);
  45.     procedure SetColorEnter(Value: TColor);
  46.     procedure SetColorLeave(Value: TColor);
  47.     procedure SetDrawStyle(Value: TVrHotImageDrawStyle);
  48.     procedure SetTextAlignment(Value: TVrTextAlignment);
  49.     procedure SetHotRect(Value: TVrHotRect);
  50.     procedure BitmapChanged(Sender: TObject);
  51.     procedure HotRectChanged(Sender: TObject);
  52.     procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  53.     procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
  54.   protected
  55.     procedure MouseEnter; override;
  56.     procedure MouseLeave; override;
  57.     procedure Paint; override;
  58.   public
  59.     constructor Create(AOwner: TComponent); override;
  60.     destructor Destroy; override;
  61.   published
  62.     property ImageLeave: TBitmap read FImageLeave write SetImageLeave;
  63.     property ImageEnter: TBitmap read FImageEnter write SetImageEnter;
  64.     property ColorEnter: TColor read FColorEnter write SetColorEnter default clBlue;
  65.     property ColorLeave: TColor read FColorLeave write SetColorLeave default clBlack;
  66.     property DrawStyle: TVrHotImageDrawStyle read FDrawStyle write SetDrawStyle default dsCenter;
  67.     property TextAlignment: TVrTextAlignment read FTextAlignment write SetTextAlignment default vtaBottom;
  68.     property HotRect: TVrHotRect read FHotRect write SetHotRect;
  69.     property Transparent default false;
  70.     property OnMouseEnter;
  71.     property OnMouseLeave;
  72.     property Align;
  73. {$IFDEF VER110}
  74.     property Anchors;
  75.     property BiDiMode;
  76.     property Constraints;
  77. {$ENDIF}
  78.     property Caption;
  79.     property DragCursor;
  80.     property DragMode;
  81. {$IFDEF VER110}
  82.     property DragKind;
  83. {$ENDIF}
  84.     property Enabled;
  85.     property Font;
  86.     property ParentShowHint;
  87. {$IFDEF VER110}
  88.     property ParentBiDiMode;
  89. {$ENDIF}
  90.     property PopupMenu;
  91.     property ShowHint;
  92.     property Visible;
  93.     property OnClick;
  94. {$IFDEF VER130}
  95.     property OnContextPopup;
  96. {$ENDIF}    
  97.     property OnDragDrop;
  98.     property OnDragOver;
  99. {$IFDEF VER110}
  100.     property OnEndDock;
  101. {$ENDIF}
  102.     property OnEndDrag;
  103.     property OnMouseDown;
  104.     property OnMouseMove;
  105.     property OnMouseUp;
  106. {$IFDEF VER110}
  107.     property OnStartDock;
  108. {$ENDIF}
  109.     property OnStartDrag;
  110.   end;
  111. implementation
  112. { TVrHotRect }
  113. constructor TVrHotRect.Create;
  114. begin
  115.   inherited Create;
  116.   FColor := clYellow;
  117.   FWidth := 1;
  118.   FVisible := True;
  119. end;
  120. procedure TVrHotRect.SetColor(Value: TColor);
  121. begin
  122.   if FColor <> Value then
  123.   begin
  124.     FColor :=  Value;
  125.     Changed;
  126.   end;
  127. end;
  128. procedure TVrHotRect.SetWidth(Value: Integer);
  129. begin
  130.   if FWidth <> Value then
  131.   begin
  132.     FWidth :=  Value;
  133.     Changed;
  134.   end;
  135. end;
  136. procedure TVrHotRect.SetVisible(Value: Boolean);
  137. begin
  138.   if FVisible <> Value then
  139.   begin
  140.     FVisible := Value;
  141.     Changed;
  142.   end;
  143. end;
  144. procedure TVrHotRect.Assign(Source: TPersistent);
  145. begin
  146.   if Source is TVrHotRect then
  147.   begin
  148.     BeginUpdate;
  149.     try
  150.       Color := TVrHotRect(Source).Color;
  151.       Width := TVrHotRect(Source).Width;
  152.       Visible := TVrHotRect(Source).Visible;
  153.     finally
  154.       EndUpdate;
  155.     end;
  156.     Exit;
  157.   end;
  158.   inherited Assign(Source);
  159. end;
  160. { TVrHotImage }
  161. constructor TVrHotImage.Create(AOwner: TComponent);
  162. begin
  163.   inherited Create(AOwner);
  164.   ControlStyle := ControlStyle + [csOpaque] - [csDoubleClicks];
  165.   Width := 85;
  166.   Height := 85;
  167.   Color := clBlack;
  168.   Font.Name := 'Arial';
  169.   Font.Style := Font.Style + [fsBold];
  170.   Font.Color := clWhite;
  171.   Transparent := false;
  172.   FColorEnter := clBlue;
  173.   FColorLeave := clBlack;
  174.   FDrawStyle := dsCenter;
  175.   FTextAlignment := vtaBottom;
  176.   FImageLeave := TBitmap.Create;
  177.   FImageLeave.OnChange := BitmapChanged;
  178.   FImageEnter := TBitmap.Create;
  179.   FImageEnter.OnChange := BitmapChanged;
  180.   FHotRect := TVrHotRect.Create;
  181.   FHotRect.OnChange := HotRectChanged;
  182. end;
  183. destructor TVrHotImage.Destroy;
  184. begin
  185.   FHotRect.Free;
  186.   FImageLeave.Free;
  187.   FImageEnter.Free;
  188.   inherited Destroy;
  189. end;
  190. procedure TVrHotImage.BitmapChanged(Sender: TObject);
  191. begin
  192.   UpdateControlCanvas;
  193. end;
  194. procedure TVrHotImage.HotRectChanged(Sender: TObject);
  195. begin
  196.   UpdateControlCanvas;
  197. end;
  198. procedure TVrHotImage.SetImageLeave(Value: TBitmap);
  199. begin
  200.   FImageLeave.Assign(Value);
  201. end;
  202. procedure TVrHotImage.SetImageEnter(Value: TBitmap);
  203. begin
  204.   FImageEnter.Assign(Value);
  205. end;
  206. procedure TVrHotImage.SetHotRect(Value: TVrHotRect);
  207. begin
  208.   FHotRect.Assign(Value);
  209. end;
  210. procedure TVrHotImage.SetDrawStyle(Value: TVrHotImageDrawStyle);
  211. begin
  212.   if FDrawStyle <> Value then
  213.   begin
  214.     FDrawStyle := Value;
  215.     UpdateControlCanvas;
  216.   end;
  217. end;
  218. procedure TVrHotImage.SetTextAlignment(Value: TVrTextAlignment);
  219. begin
  220.   if FTextAlignment <> Value then
  221.   begin
  222.     FTextAlignment := Value;
  223.     UpdateControlCanvas;
  224.   end;
  225. end;
  226. procedure TVrHotImage.SetColorEnter(Value: TColor);
  227. begin
  228.   if FColorEnter <> Value then
  229.   begin
  230.     FColorEnter := Value;
  231.     UpdateControlCanvas;
  232.   end;
  233. end;
  234. procedure TVrHotImage.SetColorLeave(Value: TColor);
  235. begin
  236.   if FColorLeave <> Value then
  237.   begin
  238.     FColorLeave := Value;
  239.     UpdateControlCanvas;
  240.   end;
  241. end;
  242. procedure TVrHotImage.Paint;
  243. var
  244.   P: TPoint;
  245.   MidX, MidY: Integer;
  246.   Image: TBitmap;
  247.   TxtRect, ImageRect: TRect;
  248.   TransparentColor: TColor;
  249. begin
  250.   if Transparent then ClearBitmapCanvas;
  251.   with BitmapCanvas do
  252.   begin
  253.     if FHasMouse and Enabled then
  254.     begin
  255.       Image := ImageEnter;
  256.       Brush.Color := FColorEnter;
  257.       TransparentColor := FColorEnter;
  258.     end
  259.     else
  260.     begin
  261.       Image := ImageLeave;
  262.       Brush.Color := FColorLeave;
  263.       TransparentColor := FColorLeave;
  264.     end;
  265.     if not Transparent then
  266.     begin
  267.       FillRect(ClientRect);
  268.       Brush.Style := bsSolid;
  269.     end else Brush.Style := bsClear;
  270.     if not Image.Empty then
  271.     begin
  272.       P := Point(Image.Width, Image.Height);
  273.       if DrawStyle = dsCenter then
  274.       begin
  275.         MidX := (ClientWidth - P.X) div 2;
  276.         MidY := (ClientHeight - P.Y) div 2;
  277.         ImageRect := Bounds(MidX, MidY, P.X, P.Y)
  278.       end else ImageRect := ClientRect;
  279.       if Transparent then
  280.         BrushCopy(ImageRect, Image, Bounds(0, 0, P.X, P.Y), TransparentColor)
  281.         else StretchDraw(ImageRect, Image);
  282.     end;
  283.     TxtRect := ClientRect;
  284.     if (HotRect.Visible) then
  285.     begin
  286.       InflateRect(TxtRect, -HotRect.Width - 2, -HotRect.Width - 2);
  287.       if FHasMouse then
  288.       begin
  289.         ImageRect := ClientRect;
  290.         DrawFrame3D(Self.BitmapCanvas, ImageRect,
  291.           HotRect.Color, HotRect.Color, HotRect.Width);
  292.       end;
  293.     end;
  294.     Brush.Style := bsClear;
  295.     Font.Assign(Self.Font);
  296.     DrawText(BitmapCanvas.Handle, PChar(Caption), -1, TxtRect,
  297.       DT_SINGLELINE + VrTextAlign[TextAlignment]);
  298.   end;
  299.   ShowDesignFrame(BitmapCanvas);
  300.   inherited Paint;
  301. end;
  302. procedure TVrHotImage.MouseEnter;
  303. begin
  304.   FHasMouse := True;
  305.   UpdateControlCanvas;
  306.   inherited;
  307. end;
  308. procedure TVrHotImage.MouseLeave;
  309. begin
  310.   FHasMouse := false;
  311.   UpdateControlCanvas;
  312.   inherited;
  313. end;
  314. procedure TVrHotImage.CMTextChanged(var Message: TMessage);
  315. begin
  316.   inherited;
  317.   UpdateControlCanvas;
  318. end;
  319. procedure TVrHotImage.CMFontChanged(var Message: TMessage);
  320. begin
  321.   inherited;
  322.   UpdateControlCanvas;
  323. end;
  324. end.