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

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrButtons;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   VrTypes, VrClasses, VrControls, VrSysUtils;
  15. type
  16.   TVrHyperButton = class(TVrHyperlinkControl)
  17.   private
  18.     FGlyph: TBitmap;
  19.     FBorderColor: TColor;
  20.     FBorderHighlight: TColor;
  21.     FBorderShadow: TColor;
  22.     FColorEnter: TColor;
  23.     FColorLeave: TColor;
  24.     FSpacing: Integer;
  25.     FMargin: Integer;
  26.     FLayout: TVrImageTextLayout;
  27.     FDisabledText: TColor;
  28.     FDisabledAnimate: Boolean;
  29.     FSize: TPoint;
  30.     FImageRect: TRect;
  31.     FTextBounds: TRect;
  32.     FHasMouse: Boolean;
  33.     FDown: Boolean;
  34.     FPressed: Boolean;
  35.     procedure SetGlyph(Value: TBitmap);
  36.     procedure SetBorderColor(Value: TColor);
  37.     procedure SetBorderHighlight(Value: TColor);
  38.     procedure SetBorderShadow(Value: TColor);
  39.     procedure SetColorEnter(Value: TColor);
  40.     procedure SetColorLeave(Value: TColor);
  41.     procedure SetLayout(Value: TVrImageTextLayout);
  42.     procedure SetMargin(Value: Integer);
  43.     procedure SetSpacing(Value: Integer);
  44.     procedure GlyphChanged(Sender: TObject);
  45.     procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  46.     procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
  47.     procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
  48.   protected
  49.     procedure Paint; override;
  50.     procedure Click; override;
  51.     procedure CalcPaintParams;
  52.     function GetPalette: HPalette; override;
  53.     procedure MouseEnter; override;
  54.     procedure MouseLeave; override;
  55.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  56.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  57.   public
  58.     constructor Create(AOwner: TComponent); override;
  59.     destructor Destroy; override;
  60.   published
  61.     property Glyph: TBitmap read FGlyph write SetGlyph;
  62.     property BorderColor: TColor read FBorderColor write SetBorderColor;
  63.     property BorderHighlight: TColor read FBorderHighlight write SetBorderHighlight;
  64.     property BorderShadow: TColor read FBorderShadow write SetBorderShadow;
  65.     property ColorEnter: TColor read FColorEnter write SetColorEnter;
  66.     property ColorLeave: TColor read FColorLeave write SetColorLeave;
  67.     property Layout: TVrImageTextLayout read FLayout write SetLayout default ImageLeft;
  68.     property Margin: Integer read FMargin write SetMargin default -1;
  69.     property Spacing: Integer read FSpacing write SetSpacing default 5;
  70.     property DisabledText: TColor read FDisabledText write FDisabledText default clInActiveCaption;
  71.     property DisabledAnimate: Boolean read FDisabledAnimate write FDisabledAnimate default True;
  72.     property Transparent;
  73.     property OnMouseEnter;
  74.     property OnMouseLeave;
  75. {$IFDEF VER110}
  76.     property Anchors;
  77.     property Constraints;
  78. {$ENDIF}
  79.     property Caption;
  80.     property DragCursor;
  81. {$IFDEF VER110}
  82.     property DragKind;
  83. {$ENDIF}
  84.     property DragMode;
  85.     property Enabled;
  86.     property Font;
  87.     property ParentFont default false;
  88.     property ParentShowHint;
  89.     property PopupMenu;
  90.     property ShowHint;
  91.     property Visible;
  92.     property OnClick;
  93. {$IFDEF VER130}
  94.     property OnContextPopup;
  95. {$ENDIF}
  96.     property OnDragDrop;
  97.     property OnDragOver;
  98. {$IFDEF VER110}
  99.     property OnEndDock;
  100. {$ENDIF}
  101.     property OnEndDrag;
  102.     property OnMouseDown;
  103.     property OnMouseMove;
  104.     property OnMouseUp;
  105. {$IFDEF VER110}
  106.     property OnStartDock;
  107. {$ENDIF}
  108.     property OnStartDrag;
  109.   end;
  110.   TVrShadowButtonStyle = (ssRectangle, ssRoundRect);
  111.   TVrShadowDirection = (sdTopLeft, sdTopRight, sdBottomLeft, sdBottomRight);
  112.   TVrShadowBrush = class(TBrush)
  113.   public
  114.     constructor Create;
  115.   published
  116.     property Color default clBlack;
  117.   end;
  118.   TVrShadowPen = class(TPen)
  119.   public
  120.     constructor Create;
  121.   published
  122.     property Color default clLime;
  123.   end;
  124.   TVrShadowButton = class(TVrGraphicImageControl)
  125.   private
  126.     FDepth: Integer;
  127.     FShadowColor: TColor;
  128.     FShadowOutline: TColor;
  129.     FBrush: TVrShadowBrush;
  130.     FPen: TVrShadowPen;
  131.     FStyle: TVrShadowButtonStyle;
  132.     FDirection: TVrShadowDirection;
  133.     FTextAlign: TVrTextAlignment;
  134.     Down: Boolean;
  135.     Pressed: Boolean;
  136.     CurrentRect: TRect;
  137.     procedure SetBrush(Value: TVrShadowBrush);
  138.     procedure SetPen(Value: TVrShadowPen);
  139.     procedure SetDepth(Value: Integer);
  140.     procedure SetShadowColor(Value: TColor);
  141.     procedure SetShadowOutline(Value: TColor);
  142.     procedure SetStyle(Value: TVrShadowButtonStyle);
  143.     procedure SetDirection(Value: TVrShadowDirection);
  144.     procedure SetTextAlign(Value: TVrTextAlignment);
  145.     procedure StyleChanged(Sender: TObject);
  146.     procedure AdjustBtnRect(var Rect: TRect; Offset: Integer);
  147.     procedure DoMouseDown(XPos, YPos: Integer);
  148.     procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  149.     procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
  150.     procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
  151.     procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
  152.     procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LButtonDown;
  153.     procedure WMMouseMove(var Message: TWMMouseMove); message WM_MouseMove;
  154.     procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LButtonUp;
  155.     procedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message WM_LButtonDblClk;
  156.   protected
  157.     procedure DrawButton;
  158.     procedure Paint; override;
  159.     procedure Click; override;
  160.   public
  161.     constructor Create(AOwner: TComponent); override;
  162.     destructor Destroy; override;
  163.   published
  164.     property Brush: TVrShadowBrush read FBrush write SetBrush;
  165.     property Pen: TVrShadowPen read FPen write SetPen;
  166.     property Depth: Integer read FDepth write SetDepth default 4;
  167.     property ShadowColor: TColor read FShadowColor write SetShadowColor default clBtnShadow;
  168.     property ShadowOutline: TColor read FShadowOutline write SetShadowOutline default clBtnShadow;
  169.     property Style: TVrShadowButtonStyle read FStyle write SetStyle default ssRoundRect;
  170.     property Direction: TVrShadowDirection read FDirection write SetDirection default sdBottomRight;
  171.     property TextAlign: TVrTextAlignment read FTextAlign write SetTextAlign default vtaCenter;
  172.     property Transparent default false;
  173. {$IFDEF VER110}
  174.     property Anchors;
  175.     property BiDiMode;
  176.     property Constraints;
  177. {$ENDIF}
  178.     property Caption;
  179.     property Color;
  180.     property DragCursor;
  181. {$IFDEF VER110}
  182.     property DragKind;
  183. {$ENDIF}
  184.     property DragMode;
  185.     property Enabled;
  186.     property Font;
  187. {$IFDEF VER110}
  188.     property ParentBiDiMode;
  189. {$ENDIF}
  190.     property ParentColor default true;
  191.     property ParentFont default false;
  192.     property ParentShowHint;
  193.     property PopupMenu;
  194.     property ShowHint;
  195.     property Visible;
  196.     property OnClick;
  197. {$IFDEF VER130}
  198.     property OnContextPopup;
  199. {$ENDIF}
  200.     property OnDragDrop;
  201.     property OnDragOver;
  202. {$IFDEF VER110}
  203.     property OnEndDock;
  204. {$ENDIF}
  205.     property OnEndDrag;
  206.     property OnMouseDown;
  207.     property OnMouseMove;
  208.     property OnMouseUp;
  209. {$IFDEF VER110}
  210.     property OnStartDock;
  211. {$ENDIF}
  212.     property OnStartDrag;
  213.   end;
  214.   TVrDemoButton = class(TVrCustomImageControl)
  215.   private
  216.     FBitmap: TBitmap;
  217.     FFontEnter: TFont;
  218.     FFontLeave: TFont;
  219.     FBevelWidth: TVrByteInt;
  220.     FOutlineWidth: Integer;
  221.     FOutlineColor: TColor;
  222.     FShadowColor: TColor;
  223.     FHighlightColor: TColor;
  224.     FFocusColor: TColor;
  225.     FTextAlignment: TVrTextAlignment;
  226.     FDisabledTextColor: TColor;
  227.     FFont3D: TVrFont3D;
  228.     FHasMouse: Boolean;
  229.     FFocused: Boolean;
  230.     Down: Boolean;
  231.     Pressed: Boolean;
  232.     procedure SetBitmap(Value: TBitmap);
  233.     procedure SetFontEnter(Value: TFont);
  234.     procedure SetFontLeave(Value: TFont);
  235.     procedure SetOutlineColor(Value: TColor);
  236.     procedure SetShadowColor(Value: TColor);
  237.     procedure SetHighlightColor(Value: TColor);
  238.     procedure SetBevelWidth(Value: TVrByteInt);
  239.     procedure SetOutlineWidth(Value: Integer);
  240.     procedure SetTextAlignment(Value: TVrTextAlignment);
  241.     procedure SetDisabledTextColor(Value: TColor);
  242.     procedure SetFocusColor(Value: TColor);
  243.     procedure SetFont3D(Value: TVrFont3D);
  244.     procedure FontChanged(Sender: TObject);
  245.     procedure BitmapChanged(Sender: TObject);
  246.     procedure DoMouseDown(XPos, YPos: Integer);
  247.     procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  248.     procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
  249.     procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
  250.     procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LButtonDown;
  251.     procedure WMMouseMove(var Message: TWMMouseMove); message WM_MouseMove;
  252.     procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LButtonUp;
  253.     procedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message WM_LButtonDblClk;
  254.     procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
  255.     procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  256.     procedure CMFocusChanged(var Message: TCMFocusChanged); message CM_FOCUSCHANGED;
  257.   protected
  258.     procedure Paint; override;
  259.     function GetPalette: HPalette; override;
  260.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  261.     procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  262.     procedure Click; override;
  263.   public
  264.     constructor Create(AOwner: TComponent); override;
  265.     destructor Destroy; override;
  266.   published
  267.     property FontEnter: TFont read FFontEnter write SetFontEnter;
  268.     property FontLeave: TFont read FFontLeave write SetFontLeave;
  269.     property Bitmap: TBitmap read FBitmap write SetBitmap;
  270.     property OutlineColor: TColor read FOutlineColor write SetOutlineColor default clBlack;
  271.     property ShadowColor: TColor read FShadowColor write SetShadowColor default clBtnShadow;
  272.     property HighlightColor: TColor read FHighlightColor write SetHighlightColor default clBtnHighlight;
  273.     property BevelWidth: TVrByteInt read FBevelWidth write SetBevelWidth default 2;
  274.     property OutlineWidth: Integer read FOutlineWidth write SetOutlineWidth default 1;
  275.     property TextAlignment: TVrTextAlignment read FTextAlignment write SetTextAlignment default vtaCenter;
  276.     property DisabledTextColor: TColor read FDisabledTextColor write SetDisabledTextColor default clInActiveCaption;
  277.     property FocusColor: TColor read FFocusColor write SetFocusColor default clBlue;
  278.     property Font3D: TVrFont3D read FFont3D write SetFont3D;
  279. {$IFDEF VER110}
  280.     property Anchors;
  281.     property BiDiMode;
  282.     property Constraints;
  283. {$ENDIF}
  284.     property Caption;
  285.     property Color default clBtnFace;
  286.     property DragCursor;
  287. {$IFDEF VER110}
  288.     property DragKind;
  289. {$ENDIF}
  290.     property DragMode;
  291.     property Enabled;
  292. {$IFDEF VER110}
  293.     property ParentBiDiMode;
  294. {$ENDIF}
  295.     property ParentColor default false;
  296.     property ParentShowHint;
  297.     property PopupMenu;
  298.     property ShowHint;
  299.     property TabOrder;
  300.     property TabStop default false;
  301.     property Visible;
  302.     property OnClick;
  303. {$IFDEF VER130}
  304.     property OnContextPopup;
  305. {$ENDIF}
  306.     property OnDragDrop;
  307.     property OnDragOver;
  308. {$IFDEF VER110}
  309.     property OnEndDock;
  310. {$ENDIF}
  311.     property OnEndDrag;
  312.     property OnEnter;
  313.     property OnExit;
  314.     property OnKeyDown;
  315.     property OnKeyPress;
  316.     property OnKeyUp;
  317.     property OnMouseDown;
  318.     property OnMouseMove;
  319.     property OnMouseUp;
  320. {$IFDEF VER110}
  321.     property OnStartDock;
  322. {$ENDIF}
  323.     property OnStartDrag;
  324.   end;
  325. implementation
  326. { TVrHyperButton }
  327. constructor TVrHyperButton.Create(AOwner: TComponent);
  328. begin
  329.   inherited Create(AOwner);
  330.   Controlstyle := Controlstyle + [csOpaque, csSetCaption] - [csDoubleClicks];
  331.   Width := 100;
  332.   Height := 25;
  333.   FBorderColor := clGray;
  334.   FBorderHighlight := clWindow;
  335.   FBorderShadow := clBlack;
  336.   FColorEnter := $000ABED8;
  337.   FColorLeave := clBtnFace;
  338.   FDisabledText := clInActiveCaption;
  339.   FDisabledAnimate := True;
  340.   FSpacing := 5;
  341.   FMargin := -1;
  342.   FLayout := ImageLeft;
  343.   FGlyph := TBitmap.Create;
  344.   FGlyph.OnChange := GlyphChanged;
  345. end;
  346. destructor TVrHyperButton.Destroy;
  347. begin
  348.   FGlyph.Free;
  349.   inherited Destroy;
  350. end;
  351. function TVrHyperButton.GetPalette: HPalette;
  352. begin
  353.   if FGlyph.Empty then Result := inherited GetPalette
  354.   else Result := FGlyph.Palette;
  355. end;
  356. procedure TVrHyperButton.CMTextChanged(var Message: TMessage);
  357. begin
  358.   inherited;
  359.   UpdateControlCanvas;
  360. end;
  361. procedure TVrHyperButton.Paint;
  362. var
  363.   PaintRect: TRect;
  364.   Animate: Boolean;
  365. begin
  366.   CalcPaintParams;
  367.   ClearBitmapCanvas;
  368.   Animate := (Enabled) or ((not Enabled) and DisabledAnimate);
  369.   if (FHasMouse and Animate) then
  370.   begin
  371.     BitmapCanvas.Pen.Color := ColorEnter;
  372.     BitmapCanvas.Brush.Color := ColorEnter;
  373.     BitmapCanvas.Brush.Style := bsSolid;
  374.   end
  375.   else
  376.   begin
  377.     BitmapCanvas.Pen.Color := BorderColor;
  378.     BitmapCanvas.Brush.Color := ColorLeave;
  379.     BitmapCanvas.Brush.Style := bsClear;
  380.   end;
  381.   BitmapCanvas.Rectangle(0, 0, Width, Height);
  382.   PaintRect := ClientRect;
  383.   if (FHasMouse and Animate) or FPressed then
  384.   begin
  385.     if not FPressed then
  386.       DrawFrame3D(BitmapCanvas, PaintRect,
  387.         BorderHighlight, BorderShadow, 1)
  388.     else
  389.       DrawFrame3D(BitmapCanvas, PaintRect,
  390.         BorderShadow, BorderHighlight, 1)
  391.   end else InflateRect(PaintRect, -1, -1);
  392.   if FPressed then
  393.   begin
  394.     OffsetRect(FImageRect, 1, 1);
  395.     OffsetRect(FTextBounds, 1, 1);
  396.   end;
  397.   if not FGlyph.Empty then
  398.   begin
  399.     BitmapCanvas.Brush.Style := bsClear;
  400.     BitmapCanvas.BrushCopy(FImageRect, FGlyph, BitmapRect(FGlyph),
  401.       FGlyph.TransparentColor);
  402.   end;
  403.   if not Enabled then
  404.     BitmapCanvas.Font.Color := DisabledText;
  405.   BitmapCanvas.Brush.Style := bsClear;
  406.   DrawText(BitmapCanvas.Handle, PChar(Caption), -1, FTextBounds,
  407.     DT_SINGLELINE + DT_VCENTER + DT_CENTER);
  408.   inherited Paint;
  409. end;
  410. procedure TVrHyperButton.CalcPaintParams;
  411. var
  412.   Rect: TRect;
  413.   ImagePos: TPoint;
  414. begin
  415.   Rect := ClientRect;
  416.   InflateRect(Rect, -1, -1); //fix borderwidth
  417.   FSize.X := FGlyph.Width;
  418.   FSize.Y := FGlyph.Height;
  419.   BitmapCanvas.Font.Assign(Self.Font);
  420.   CalcImageTextLayout(BitmapCanvas, Rect, Point(1, 1), Caption, FLayout,
  421.     FMargin, FSpacing, Point(FSize.X, FSize.Y), ImagePos, FTextBounds);
  422.   if FGlyph.Empty then OffsetRect(FTextBounds, 0, -1);
  423.   FImageRect := Bounds(ImagePos.X, ImagePos.Y, FSize.X, FSize.Y);
  424. end;
  425. procedure TVrHyperButton.CMFontChanged(var Message: TMessage);
  426. begin
  427.   inherited;
  428.   BitmapCanvas.Font.Assign(Self.Font);
  429.   UpdateControlCanvas;
  430. end;
  431. procedure TVrHyperButton.CMEnabledChanged(var Message: TMessage);
  432. begin
  433.   UpdateControlCanvas;
  434. end;
  435. procedure TVrHyperButton.GlyphChanged(Sender: TObject);
  436. begin
  437.   UpdateControlCanvas;
  438. end;
  439. procedure TVrHyperButton.SetLayout(Value: TVrImageTextLayout);
  440. begin
  441.   if FLayout <> Value then
  442.   begin
  443.     FLayout := Value;
  444.     UpdateControlCanvas;
  445.   end;
  446. end;
  447. procedure TVrHyperButton.SetMargin(Value: Integer);
  448. begin
  449.   if FMargin <> Value then
  450.   begin
  451.     FMargin := Value;
  452.     UpdateControlCanvas;
  453.   end;
  454. end;
  455. procedure TVrHyperButton.SetSpacing(Value: Integer);
  456. begin
  457.   if FSpacing <> Value then
  458.   begin
  459.     FSpacing := Value;
  460.     UpdateControlCanvas;
  461.   end;
  462. end;
  463. procedure TVrHyperButton.SetBorderColor(Value: TColor);
  464. begin
  465.   if FBorderColor <> Value then
  466.   begin
  467.     FBorderColor := Value;
  468.     UpdateControlCanvas;
  469.   end;
  470. end;
  471. procedure TVrHyperButton.SetBorderHighlight(Value: TColor);
  472. begin
  473.   if FBorderHighlight <> Value then
  474.   begin
  475.     FBorderHighlight := Value;
  476.     UpdateControlCanvas;
  477.   end;
  478. end;
  479. procedure TVrHyperButton.SetBorderShadow(Value: TColor);
  480. begin
  481.   if FBorderShadow <> Value then
  482.   begin
  483.     FBorderShadow := Value;
  484.     UpdateControlCanvas;
  485.   end;
  486. end;
  487. procedure TVrHyperButton.SetColorEnter(Value: TColor);
  488. begin
  489.   if FColorEnter <> Value then
  490.   begin
  491.     FColorEnter := Value;
  492.     UpdateControlCanvas;
  493.   end;
  494. end;
  495. procedure TVrHyperButton.SetColorLeave(Value: TColor);
  496. begin
  497.   if FColorLeave <> Value then
  498.   begin
  499.     FColorLeave := Value;
  500.     UpdateControlCanvas;
  501.   end;
  502. end;
  503. procedure TVrHyperButton.SetGlyph(Value: TBitmap);
  504. begin
  505.   FGlyph.Assign(Value);
  506. end;
  507. procedure TVrHyperButton.Click;
  508. begin
  509. end;
  510. procedure TVrHyperButton.MouseDown(Button: TMouseButton; Shift: TShiftState;
  511.   X, Y: Integer);
  512. begin
  513.   if (Button = mbLeft) then
  514.   begin
  515.     FDown := True;
  516.     FPressed := True;
  517.     UpdateControlCanvas;
  518.   end;
  519.   inherited MouseDown(Button, Shift, X, Y);
  520. end;
  521. procedure TVrHyperButton.MouseUp(Button: TMouseButton; Shift: TShiftState;
  522.   X, Y: Integer);
  523. var
  524.   DoClick: Boolean;
  525. begin
  526.   FDown := false;
  527.   DoClick := false;
  528.   if FPressed then
  529.   begin
  530.     FPressed := false;
  531.     UpdateControlCanvas;
  532.     DoClick := True;
  533.   end;
  534.   inherited MouseUp(Button, Shift, X, Y);
  535.   if DoClick then
  536.     inherited Click;
  537. end;
  538. procedure TVrHyperButton.MouseEnter;
  539. begin
  540.   FHasMouse := True;
  541.   if FDown then FPressed := True;
  542.   UpdateControlCanvas;
  543.   inherited;
  544. end;
  545. procedure TVrHyperButton.MouseLeave;
  546. begin
  547.   FHasMouse := false;
  548.   if FDown then FPressed := false;
  549.   UpdateControlCanvas;
  550.   inherited;
  551. end;
  552. { TVrShadowBrush }
  553. constructor TVrShadowBrush.Create;
  554. begin
  555.   inherited Create;
  556.   Color := clBlack;
  557. end;
  558. { TVrShadowPen }
  559. constructor TVrShadowPen.Create;
  560. begin
  561.   inherited Create;
  562.   Color := clLime;
  563. end;
  564. {TVrShadowButton}
  565. constructor TVrShadowButton.Create(AOwner: TComponent);
  566. begin
  567.   inherited Create(AOwner);
  568.   ControlStyle := ControlStyle + [csOpaque] - [csDoubleClicks];
  569.   Width := 110;
  570.   Height := 30;
  571.   Font.Color := clWhite;
  572.   ParentColor := true;
  573.   ParentFont := false;
  574.   FDepth := 4;
  575.   FShadowColor := clBtnShadow;
  576.   FShadowOutline := clBtnShadow;
  577.   FBrush := TVrShadowBrush.Create;
  578.   FBrush.OnChange := StyleChanged;
  579.   FPen := TVrShadowPen.Create;
  580.   FPen.OnChange := StyleChanged;
  581.   FStyle := ssRoundRect;
  582.   FDirection := sdBottomRight;
  583.   FTextAlign := vtaCenter;
  584. end;
  585. destructor TVrShadowButton.Destroy;
  586. begin
  587.   FBrush.Free;
  588.   FPen.Free;
  589.   inherited Destroy;
  590. end;
  591. procedure TVrshadowButton.Click;
  592. begin
  593. end;
  594. procedure TVrShadowButton.SetBrush(Value: TVrShadowBrush);
  595. begin
  596.   FBrush.Assign(Value);
  597. end;
  598. procedure TVrShadowButton.SetPen(Value: TVrShadowPen);
  599. begin
  600.   FPen.Assign(Value);
  601. end;
  602. procedure TVrShadowButton.SetDepth(Value: Integer);
  603. begin
  604.   if (FDepth <> Value) and (Value > 2) then
  605.   begin
  606.     FDepth := Value;
  607.     UpdateControlCanvas;
  608.   end;
  609. end;
  610. procedure TVrShadowButton.SetShadowColor(Value: TColor);
  611. begin
  612.   if FShadowColor <> Value then
  613.   begin
  614.     FShadowColor := Value;
  615.     UpdateControlCanvas;
  616.   end;
  617. end;
  618. procedure TVrShadowButton.SetShadowOutline(Value: TColor);
  619. begin
  620.   if FShadowOutline <> Value then
  621.   begin
  622.     FShadowOutline := Value;
  623.     UpdateControlCanvas;
  624.   end;
  625. end;
  626. procedure TVrShadowButton.SetStyle(Value: TVrShadowButtonStyle);
  627. begin
  628.   if FStyle <> Value then
  629.   begin
  630.     FStyle := Value;
  631.     UpdateControlCanvas;
  632.   end;
  633. end;
  634. procedure TVrShadowButton.SetDirection(Value: TVrShadowDirection);
  635. begin
  636.   if FDirection <> Value then
  637.   begin
  638.     FDirection := Value;
  639.     UpdateControlCanvas;
  640.   end;
  641. end;
  642. procedure TVrShadowButton.SetTextAlign(Value: TVrTextAlignment);
  643. begin
  644.   if FTextAlign <> Value then
  645.   begin
  646.     FTextAlign := Value;
  647.     UpdateControlCanvas;
  648.   end;
  649. end;
  650. procedure TVrShadowButton.StyleChanged(Sender: TObject);
  651. begin
  652.   UpdateControlCanvas;
  653. end;
  654. procedure TVrShadowButton.AdjustBtnRect(var Rect: TRect; Offset: Integer);
  655. begin
  656.   case FDirection of
  657.     sdBottomRight: OffsetRect(Rect, Offset, Offset);
  658.     sdBottomLeft: OffsetRect(Rect, -Offset, Offset);
  659.     sdTopLeft: OffsetRect(Rect, -Offset, -Offset);
  660.     sdTopRight: OffsetRect(Rect, Offset, -Offset);
  661.   end;
  662. end;
  663. procedure TVrShadowButton.DrawButton;
  664. const
  665.   ShapeStyles: array[TVrShadowButtonStyle] of TVrShapeType =
  666.     (stRectangle, stRoundRect);
  667. var
  668.   ImageRect, ShadowRect: TRect;
  669. begin
  670.   ImageRect := ClientRect;
  671.   with BitmapImage do
  672.   begin
  673.     InflateRect(ImageRect, -FPen.Width, -FPen.Width);
  674.     case FDirection of
  675.       sdBottomRight:
  676.         begin
  677.           Dec(ImageRect.Right, FDepth);
  678.           Dec(ImageRect.Bottom, FDepth);
  679.         end;
  680.       sdBottomLeft:
  681.         begin
  682.           Inc(ImageRect.Left, FDepth);
  683.           Dec(ImageRect.Bottom, FDepth);
  684.         end;
  685.       sdTopLeft:
  686.         begin
  687.           Inc(ImageRect.Top, FDepth);
  688.           Inc(ImageRect.Left, FDepth);
  689.         end;
  690.       sdTopRight:
  691.         begin
  692.           Inc(ImageRect.Top, FDepth);
  693.           Dec(ImageRect.Right, FDepth);
  694.         end;
  695.     end;
  696.     with Canvas do
  697.     begin
  698.       Pen.Width := 1;
  699.       Pen.Color := FShadowOutline;
  700.       Pen.Mode := pmCopy;
  701.       Pen.Style := psSolid;
  702.       Brush.Style := bsSolid;
  703.       Brush.Color := FShadowColor;
  704.     end;
  705.     ShadowRect := ImageRect;
  706.     AdjustBtnRect(ShadowRect, FDepth);
  707.     if Down then
  708.       AdjustBtnRect(ShadowRect, -1);
  709.     DrawShape(Canvas, ShapeStyles[FStyle], ShadowRect.Left, ShadowRect.Top,
  710.         ShadowRect.Right - ShadowRect.Left, ShadowRect.Bottom - ShadowRect.Top);
  711.     if Down then AdjustBtnRect(ImageRect, 2);
  712.     Canvas.Brush.Assign(FBrush);
  713.     Canvas.Pen.Assign(FPen);
  714.     Canvas.Font.Assign(Self.Font);
  715.     if not Enabled then Canvas.Font.Color := clInActiveCaption;
  716.     DrawShape(Canvas, ShapeStyles[FStyle], ImageRect.Left, ImageRect.Top,
  717.       ImageRect.Right - ImageRect.Left, ImageRect.Bottom - ImageRect.Top);
  718.     InflateRect(ImageRect, -Pen.Width, -Pen.Width);
  719.     Canvas.Brush.Style := bsClear;
  720.     DrawText(Canvas.Handle, PChar(Caption), -1, ImageRect,
  721.       DT_SINGLELINE + VrTextAlign[TextAlign]);
  722.     CurrentRect := ImageRect;
  723.   end;
  724. end;
  725. procedure TVrShadowButton.Paint;
  726. begin
  727.   ClearBitmapCanvas;
  728.   DrawButton;
  729.   inherited Paint;
  730. end;
  731. procedure TVrShadowButton.CMTextChanged(var Message: TMessage);
  732. begin
  733.   inherited;
  734.   UpdateControlCanvas;
  735. end;
  736. procedure TVrShadowButton.CMFontChanged(var Message: TMessage);
  737. begin
  738.   inherited;
  739.   BitmapCanvas.Font.Assign(Self.Font);
  740.   UpdateControlCanvas;
  741. end;
  742. procedure TVrShadowButton.DoMouseDown(XPos, YPos: Integer);
  743. var
  744.   P: TPoint;
  745. begin
  746.   P := Point(XPos, YPos);
  747.   if PtInRect(CurrentRect, P) then
  748.   begin
  749.     Pressed := True;
  750.     Down := True;
  751.     MouseCapture := true;
  752.     UpdateControlCanvas;
  753.   end;
  754. end;
  755. procedure TVrShadowButton.WMLButtonDown(var Message: TWMLButtonDown);
  756. begin
  757.   inherited;
  758.   DoMouseDown(Message.XPos, Message.YPos);
  759. end;
  760. procedure TVrShadowButton.WMLButtonDblClk(var Message: TWMLButtonDblClk);
  761. begin
  762.   inherited;
  763.   DoMouseDown(Message.XPos, Message.YPos);
  764. end;
  765. procedure TVrShadowButton.WMMouseMove(var Message: TWMMouseMove);
  766. var
  767.   P: TPoint;
  768. begin
  769.   inherited;
  770.   if Pressed then
  771.   begin
  772.     P := Point(Message.XPos, Message.YPos);
  773.     if PtInRect(CurrentRect, P) <> Down then
  774.     begin
  775.       Down := not Down;
  776.       UpdateControlCanvas;
  777.     end;
  778.   end;
  779. end;
  780. procedure TVrShadowButton.WMLButtonUp(var Message: TWMLButtonUp);
  781. var
  782.   DoClick: Boolean;
  783. begin
  784.   MouseCapture := false;
  785.   DoClick := Pressed and Down;
  786.   Down := false;
  787.   Pressed := false;
  788.   if DoClick then
  789.   begin
  790.     UpdateControlCanvas;
  791.     inherited Click;
  792.   end;
  793.   inherited;
  794. end;
  795. procedure TVrShadowButton.CMDialogChar(var Message: TCMDialogChar);
  796. begin
  797.   if Enabled and IsAccel(Message.CharCode, Caption) then
  798.     inherited Click;
  799. end;
  800. procedure TVrShadowButton.CMEnabledChanged(var Message: TMessage);
  801. begin
  802.   UpdateControlCanvas;
  803. end;
  804. { TVrDemoButton }
  805. constructor TVrDemoButton.Create(AOwner: TComponent);
  806. begin
  807.   inherited Create(AOwner);
  808.   ControlStyle := ControlStyle + [csOpaque] - [csDoubleClicks];
  809.   Width := 150;
  810.   Height := 25;
  811.   Color := clBtnFace;
  812.   ParentColor := false;
  813.   FBitmap := TBitmap.Create;
  814.   FBitmap.OnChange := BitmapChanged;
  815.   FFont3D := TVrFont3D.Create;
  816.   FFont3D.OnChange := FontChanged;
  817.   FFontEnter := TFont.Create;
  818.   with FFontEnter do
  819.   begin
  820.     Name := 'Arial';
  821.     Size := 12;
  822.     Color := clRed;
  823.     Style := Style + [fsBold];
  824.     OnChange := FontChanged;
  825.   end;
  826.   FFontLeave := TFont.Create;
  827.   with FFontLeave do
  828.   begin
  829.     Name := 'Arial';
  830.     Size := 12;
  831.     Color := clBlack;
  832.     Style := Style + [fsBold];
  833.     OnChange := FontChanged;
  834.   end;
  835.   FBevelWidth := 2;
  836.   FOutlineWidth := 1;
  837.   FShadowColor := clBtnShadow;
  838.   FHighlightColor := clBtnHighlight;
  839.   FOutlineColor := clBlack;
  840.   FTextAlignment := vtaCenter;
  841.   FDisabledTextColor := clInActiveCaption;
  842.   FFocusColor := clBlue;
  843. end;
  844. destructor TVrDemoButton.Destroy;
  845. begin
  846.   FBitmap.Free;
  847.   FFont3D.Free;
  848.   FFontEnter.Free;
  849.   FFontLeave.Free;
  850.   inherited Destroy;
  851. end;
  852. procedure TVrDemoButton.FontChanged(Sender: TObject);
  853. begin
  854.   UpdateControlCanvas;
  855. end;
  856. procedure TVrDemoButton.BitmapChanged(Sender: TObject);
  857. begin
  858.   UpdateControlCanvas;
  859. end;
  860. function TVrDemoButton.GetPalette: HPalette;
  861. begin
  862.   if FBitmap.Empty then Result := inherited GetPalette
  863.   else Result := FBitmap.Palette;
  864. end;
  865. procedure TVrDemoButton.Paint;
  866. var
  867.   R: TRect;
  868. begin
  869.   if FBitmap.Empty then ClearBitmapCanvas
  870.   else
  871.     begin
  872.       R := ClientRect;
  873.       if Down then OffsetRect(R, 1, 1);
  874.       DrawTiledBitmap(BitmapCanvas, R, Bitmap);
  875.     end;
  876.   R := ClientRect;
  877.   if FFocused then
  878.     DrawFrame3D(BitmapCanvas, R, FocusColor, FocusColor, OutlineWidth)
  879.   else DrawFrame3D(BitmapCanvas, R, OutlineColor, OutlineColor, OutlineWidth);
  880.   if Down then
  881.     DrawFrame3D(BitmapCanvas, R, ShadowColor, HighlightColor, BevelWidth)
  882.   else DrawFrame3D(BitmapCanvas, R, HighlightColor, ShadowColor, BevelWidth);
  883.   with BitmapCanvas do
  884.   begin
  885.     if FHasMouse and Enabled then
  886.       Font.Assign(FontEnter)
  887.     else Font.Assign(FontLeave);
  888.     if not Enabled then
  889.       Font.Color := DisabledTextColor;
  890.     Brush.Style := bsClear;
  891.     if Down then OffsetRect(R, 1, 1);
  892.     FFont3D.Paint(BitmapCanvas, R, Caption,
  893.       DT_SINGLELINE + VrTextAlign[TextAlignment]);
  894.   end;
  895.   inherited Paint;
  896. end;
  897. procedure TVrDemoButton.SetBitmap(Value: TBitmap);
  898. begin
  899.   FBitmap.Assign(Value);
  900. end;
  901. procedure TVrDemoButton.SetFontEnter(Value: TFont);
  902. begin
  903.   FFontEnter.Assign(Value);
  904. end;
  905. procedure TVrDemoButton.SetFontLeave(Value: TFont);
  906. begin
  907.   FFontLeave.Assign(Value);
  908. end;
  909. procedure TVrDemoButton.SetOutlineColor(Value: TColor);
  910. begin
  911.   if FOutlineColor <> Value then
  912.   begin
  913.     FOutlineColor := Value;
  914.     UpdateControlCanvas;
  915.   end;
  916. end;
  917. procedure TVrDemoButton.SetShadowColor(Value: TColor);
  918. begin
  919.   if FShadowColor <> Value then
  920.   begin
  921.     FShadowColor := Value;
  922.     UpdateControlCanvas;
  923.   end;
  924. end;
  925. procedure TVrDemoButton.SetHighlightColor(Value: TColor);
  926. begin
  927.   if FHighlightColor <> Value then
  928.   begin
  929.     FHighlightColor := Value;
  930.     UpdateControlCanvas;
  931.   end;
  932. end;
  933. procedure TVrDemoButton.SetBevelWidth(Value: TVrByteInt);
  934. begin
  935.   if FBevelWidth <> Value then
  936.   begin
  937.     FBevelWidth := Value;
  938.     UpdateControlCanvas;
  939.   end;
  940. end;
  941. procedure TVrDemoButton.SetOutlineWidth(Value: Integer);
  942. begin
  943.   if FOutlineWidth <> Value then
  944.   begin
  945.     FOutlineWidth := Value;
  946.     UpdateControlCanvas;
  947.   end;
  948. end;
  949. procedure TVrDemoButton.SetTextAlignment(Value: TVrTextAlignment);
  950. begin
  951.   if FTextAlignment <> Value then
  952.   begin
  953.     FTextAlignment := Value;
  954.     UpdateControlCanvas;
  955.   end;
  956. end;
  957. procedure TVrDemoButton.SetDisabledTextColor(Value: TColor);
  958. begin
  959.   if FDisabledTextColor <> Value then
  960.   begin
  961.     FDisabledTextColor := Value;
  962.     UpdateControlCanvas;
  963.   end;
  964. end;
  965. procedure TVrDemoButton.SetFocusColor(Value: TColor);
  966. begin
  967.   if FFocusColor <> Value then
  968.   begin
  969.     FFocusColor := Value;
  970.     UpdateControlCanvas;
  971.   end;
  972. end;
  973. procedure TVrDemoButton.SetFont3D(Value: TVrFont3D);
  974. begin
  975.   FFont3D.Assign(Value);
  976. end;
  977. procedure TVrDemoButton.CMTextChanged(var Message: TMessage);
  978. begin
  979.   inherited;
  980.   UpdateControlCanvas;
  981. end;
  982. procedure TVrDemoButton.DoMouseDown(XPos, YPos: Integer);
  983. var
  984.   P: TPoint;
  985. begin
  986.   P := Point(XPos, YPos);
  987.   if PtInRect(ClientRect, P) then
  988.   begin
  989.     Pressed := True;
  990.     Down := True;
  991.     MouseCapture := true;
  992.     if TabStop then SetFocus;
  993.     UpdateControlCanvas;
  994.   end;
  995. end;
  996. procedure TVrDemoButton.Click;
  997. begin
  998. end;
  999. procedure TVrDemoButton.WMLButtonDown(var Message: TWMLButtonDown);
  1000. begin
  1001.   inherited;
  1002.   DoMouseDown(Message.XPos, Message.YPos);
  1003. end;
  1004. procedure TVrDemoButton.WMLButtonDblClk(var Message: TWMLButtonDblClk);
  1005. begin
  1006.   inherited;
  1007.   DoMouseDown(Message.XPos, Message.YPos);
  1008. end;
  1009. procedure TVrDemoButton.WMMouseMove(var Message: TWMMouseMove);
  1010. var
  1011.   P: TPoint;
  1012. begin
  1013.   inherited;
  1014.   if Pressed then
  1015.   begin
  1016.     P := Point(Message.XPos, Message.YPos);
  1017.     if PtInRect(ClientRect, P) <> Down then
  1018.     begin
  1019.       Down := not Down;
  1020.       UpdateControlCanvas;
  1021.     end;
  1022.   end;
  1023. end;
  1024. procedure TVrDemoButton.WMLButtonUp(var Message: TWMLButtonUp);
  1025. var
  1026.   DoClick: Boolean;
  1027. begin
  1028.   MouseCapture := false;
  1029.   DoClick := Pressed and Down;
  1030.   Down := false;
  1031.   Pressed := false;
  1032.   if DoClick then
  1033.   begin
  1034.     UpdateControlCanvas;
  1035.     inherited Click;
  1036.   end;
  1037.   inherited;
  1038. end;
  1039. procedure TVrDemoButton.CMDialogChar(var Message: TCMDialogChar);
  1040. begin
  1041.   if Enabled and IsAccel(Message.CharCode, Caption) then
  1042.     inherited Click;
  1043. end;
  1044. procedure TVrDemoButton.CMEnabledChanged(var Message: TMessage);
  1045. begin
  1046.   inherited;
  1047.   UpdateControlCanvas;
  1048. end;
  1049. procedure TVrDemoButton.CMMouseEnter(var Message: TMessage);
  1050. begin
  1051.   inherited;
  1052.   FHasMouse := True;
  1053.   UpdateControlCanvas;
  1054. end;
  1055. procedure TVrDemoButton.CMMouseLeave(var Message: TMessage);
  1056. begin
  1057.   inherited;
  1058.   FHasMouse := false;
  1059.   UpdateControlCanvas;
  1060. end;
  1061. procedure TVrDemoButton.CMFocusChanged(var Message: TCMFocusChanged);
  1062. var
  1063.   Active: Boolean;
  1064. begin
  1065.   with Message do Active := (Sender = Self);
  1066.   if Active <> FFocused then
  1067.   begin
  1068.     FFocused := Active;
  1069.     UpdateControlCanvas;
  1070.   end;
  1071.   inherited;
  1072. end;
  1073. procedure TVrDemoButton.KeyDown(var Key: Word; Shift: TShiftState);
  1074. begin
  1075.   inherited KeyDown(Key, Shift);
  1076.   if (not Down) and (Key = VK_SPACE) then DoMouseDown(0, 0);
  1077. end;
  1078. procedure TVrDemoButton.KeyUp(var Key: Word; Shift: TShiftState);
  1079. begin
  1080.   inherited KeyUp(Key, Shift);
  1081.   if Key = VK_SPACE then
  1082.   begin
  1083.     MouseCapture := false;
  1084.     if Pressed and Down then
  1085.     begin
  1086.       Down := False;
  1087.       UpdateControlCanvas;
  1088.       inherited Click;
  1089.     end;
  1090.     Pressed := False;
  1091.   end;
  1092. end;
  1093. end.