VrLabel.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 VrLabel;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   StdCtrls, VrTypes, VrClasses, VrControls, VrSysUtils;
  15. type
  16.   TVrLabelStyle = (lsNone, lsRaised, lsLowered, lsShadow);
  17.   TVrLabel = class(TVrGraphicImageControl)
  18.   private
  19.     FAlignment: TAlignment;
  20.     FAutoSize: Boolean;
  21.     FLayout: TTextLayout;
  22.     FColorHighlight: TColor;
  23.     FColorShadow: TColor;
  24.     FStyle: TVrLabelStyle;
  25.     FShadowDepth: Integer;
  26.     FBitmap: TBitmap;
  27.     FAngle: TVrTextAngle;
  28.     FRad: Double;
  29.     FTextSize: TPoint;
  30.     procedure SetAlignment(Value: TAlignment);
  31.     procedure SetColorHighlight(Value: TColor);
  32.     procedure SetColorShadow(Value: TColor);
  33.     procedure SetStyle(Value: TVrLabelStyle);
  34.     procedure SetLayout(Value: TTextLayout);
  35.     procedure SetAutoSize(Value: Boolean);
  36.     procedure SetShadowDepth(Value: Integer);
  37.     procedure SetBitmap(Value: TBitmap);
  38.     procedure SetAngle(Value: TVrTextAngle);
  39.     procedure BitmapChanged(Sender: TObject);
  40.     procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  41.   protected
  42.     procedure AdjustLabelSize;
  43.     procedure GetLayoutCoords(var X, Y: Integer);
  44.     procedure Paint; override;
  45.   public
  46.     constructor Create(AOwner: TComponent); override;
  47.     destructor Destroy; override;
  48.   published
  49.     property Alignment: TAlignment read FAlignment write SetAlignment default taCenter;
  50.     property ColorHighlight: TColor read FColorHighlight write SetColorHighlight default clWhite;
  51.     property ColorShadow: TColor read FColorShadow write SetColorShadow default clGray;
  52.     property Style: TVrLabelStyle read FStyle write SetStyle default lsRaised;
  53.     property Layout: TTextLayout read FLayout write SetLayout default tlCenter;
  54.     property AutoSize: Boolean read FAutoSize write SetAutoSize default false;
  55.     property ShadowDepth: Integer read FShadowDepth write SetShadowDepth default 2;
  56.     property Bitmap: TBitmap read FBitmap write SetBitmap;
  57.     property Angle: TVrTextAngle read FAngle write SetAngle default 0;
  58.     property Transparent default false;
  59.     property Align;
  60. {$IFDEF VER110}
  61.     property Anchors;
  62.     property BiDiMode;
  63.     property Constraints;
  64. {$ENDIF}
  65.     property Color;
  66.     property Caption;
  67.     property Font;
  68.     property DragCursor;
  69.     property DragMode;
  70. {$IFDEF VER110}
  71.     property DragKind;
  72. {$ENDIF}
  73.     property ParentColor;
  74.     property ParentFont;
  75.     property ParentShowHint;
  76. {$IFDEF VER110}
  77.     property ParentBiDiMode;
  78. {$ENDIF}
  79.     property PopupMenu;
  80.     property ShowHint;
  81.     property Visible;
  82.     property OnClick;
  83. {$IFDEF VER130}
  84.     property OnContextPopup;
  85. {$ENDIF}    
  86.     property OnDblClick;
  87.     property OnDragDrop;
  88.     property OnDragOver;
  89. {$IFDEF VER110}
  90.     property OnEndDock;
  91. {$ENDIF}
  92.     property OnEndDrag;
  93.     property OnMouseDown;
  94.     property OnMouseMove;
  95.     property OnMouseUp;
  96. {$IFDEF VER110}
  97.     property OnStartDock;
  98. {$ENDIF}
  99.     property OnStartDrag;
  100.   end;
  101. implementation
  102. { TVrLabel }
  103. constructor TVrLabel.Create(AOwner: TComponent);
  104. begin
  105.   inherited Create(AOwner);
  106.   ControlStyle := ControlStyle + [csOpaque, csSetCaption];
  107.   Width := 100;
  108.   Height := 25;
  109.   Color := clBtnFace;
  110.   Font.Name := 'Arial';
  111.   Font.Size := 14;
  112.   FAlignment := taCenter;
  113.   FLayout := tlCenter;
  114.   FColorHighlight := clWhite;
  115.   FColorShadow := clGray;
  116.   FStyle := lsRaised;
  117.   FAutoSize := false;
  118.   FShadowDepth := 2;
  119.   FAngle := 0;
  120.   FRad := 0;
  121.   FBitmap := TBitmap.Create;
  122.   FBitmap.OnChange := BitmapChanged;
  123. end;
  124. destructor TVrLabel.Destroy;
  125. begin
  126.   FBitmap.Free;
  127.   inherited Destroy;
  128. end;
  129. procedure TVrLabel.AdjustLabelSize;
  130. var
  131.   NewWidth, NewHeight: Integer;
  132. begin
  133.   with BitmapCanvas do
  134.   begin
  135.     if FAutoSize then
  136.     begin
  137.       NewWidth := 4 + Trunc(FTextSize.X * Abs(cos(FRad)) +
  138.         FTextSize.Y * Abs(sin(FRad)));
  139.       NewHeight := 4 + Trunc(FTextSize.Y * Abs(cos(FRad)) +
  140.         FTextSize.X * Abs(sin(FRad)));
  141.       BoundsRect := Bounds(Left, Top, NewWidth, NewHeight);
  142.     end;
  143.   end;
  144. end;
  145. procedure TVrLabel.GetLayoutCoords(var X, Y: Integer);
  146. var
  147.   P: TPoint;
  148.   LeftX: Integer;
  149. begin
  150.   X := 0;
  151.   Y := (ClientHeight - FTextSize.Y) div 2;
  152.   with BitmapCanvas do
  153.   begin
  154.     P := GetTextSize(BitmapCanvas, Caption);
  155.     X := 2;
  156.     if (FAngle > 90) and (FAngle < 270) then
  157.       X := X + Trunc(FTextSize.X * Abs(cos(FRad)));
  158.     if (FAngle > 180) then
  159.       X := X + Trunc(FTextSize.Y * Abs(sin(FRad)));
  160.     LeftX := X;
  161.     case FAlignment of
  162.       taCenter:
  163.         begin
  164.           X := Trunc(0.5 * ClientWidth -
  165.             0.5 * FTextSize.X * cos(FRad) - 0.5 * FTextSize.Y * sin(FRad));
  166.         end;
  167.       taRightJustify :
  168.         begin
  169.           X := 2 + Trunc(FTextSize.X * Abs(cos(FRad)) +
  170.             FTextSize.Y * Abs(sin(FRad)));
  171.           X := ClientWidth - X;
  172.           if (FAngle > 90) and (FAngle < 270) then
  173.             X := X + Trunc(FTextSize.X * Abs(cos(FRad)));
  174.           if (FAngle > 180) then
  175.             X := X + Trunc(FTextSize.Y * Abs(sin(FRad)));
  176.         end;
  177.     end;
  178.     X := MaxIntVal(LeftX, X);
  179.     case FLayout of
  180.       tlTop:
  181.         begin
  182.           Y := 2;
  183.           if FAngle < 180 then
  184.             Y := Y + Trunc(FTextSize.X * Abs(sin(FRad)));
  185.           if (FAngle > 90) and (FAngle < 270) then
  186.             Y := Y + Trunc(FTextSize.Y * Abs(cos(FRad)) );
  187.          end;
  188.       tlCenter:
  189.         begin
  190.           Y := Trunc(0.5 * ClientHeight -
  191.             0.5 * FTextSize.Y * cos(FRad) + 0.5 * FTextSize.X * sin(FRad));
  192.         end;
  193.       tlBottom:
  194.         begin
  195.           Y := 2 + Trunc(FTextSize.Y * Abs(cos(FRad)) +
  196.             FTextSize.X * Abs(sin(FRad)));
  197.           Y := ClientHeight - Y;
  198.           if FAngle < 180 then
  199.             Y := Y + Trunc(FTextSize.X * Abs(sin(FRad)));
  200.           if (FAngle > 90) and (FAngle < 270) then
  201.             Y := Y + Trunc(FTextSize.Y * Abs(cos(FRad)) );
  202.         end;
  203.     end;
  204.   end;
  205. end;
  206. procedure TVrLabel.Paint;
  207. var
  208.   X, Y: Integer;
  209. begin
  210.   with inherited BitmapCanvas do
  211.   begin
  212.     Font.Assign(Self.Font);
  213.     FTextSize := GetTextSize(BitmapCanvas, Caption);
  214.     SetCanvasTextAngle(BitmapCanvas, FAngle);
  215.     AdjustLabelSize;
  216.     ClearBitmapCanvas;
  217.     GetLayoutCoords(X, Y);
  218.     case FStyle of
  219.       lsNone:
  220.         begin
  221.           if Transparent then Brush.Style := bsClear
  222.           else Brush.Style := bsSolid;
  223.           TextOut(X, Y, Caption);
  224.         end;
  225.       lsRaised:
  226.         Draw3DText(BitmapCanvas, X, Y, Caption, FColorHighlight, FColorShadow);
  227.       lsLowered:
  228.         Draw3DText(BitmapCanvas, X, Y, Caption, FColorShadow, FColorHighlight);
  229.       lsShadow:
  230.         DrawShadowTextExt(BitmapCanvas, X, Y, Caption,
  231.           FColorShadow, FShadowDepth, FShadowDepth);
  232.     end;
  233.     if not FBitmap.Empty then
  234.       StretchPaintOnText(BitmapCanvas, ClientRect,
  235.         X, Y, Caption, FBitmap, FAngle);
  236.   end;
  237.   inherited Paint;
  238. end;
  239. procedure TVrLabel.SetAlignment(Value: TAlignment);
  240. begin
  241.   if FAlignment <> Value then
  242.   begin
  243.     FAlignment := Value;
  244.     UpdateControlCanvas;
  245.   end;
  246. end;
  247. procedure TVrLabel.SetColorHighlight(Value: TColor);
  248. begin
  249.   if FColorHighlight <> Value then
  250.   begin
  251.     FColorHighlight := Value;
  252.     UpdateControlCanvas;
  253.   end;
  254. end;
  255. procedure TVrLabel.SetColorShadow(Value: TColor);
  256. begin
  257.   if FColorShadow <> Value then
  258.   begin
  259.     FColorShadow := Value;
  260.     UpdateControlCanvas;
  261.   end;
  262. end;
  263. procedure TVrLabel.SetStyle(Value: TVrLabelStyle);
  264. begin
  265.   if FStyle <> Value then
  266.   begin
  267.     FStyle := Value;
  268.     UpdateControlCanvas;
  269.   end;
  270. end;
  271. procedure TVrLabel.SetLayout(Value: TTextLayout);
  272. begin
  273.   if FLayout <> Value then
  274.   begin
  275.     FLayout := Value;
  276.     UpdateControlCanvas;
  277.   end;
  278. end;
  279. procedure TVrLabel.SetAutoSize(Value: Boolean);
  280. begin
  281.   if FAutoSize <> Value then
  282.   begin
  283.     FAutoSize := Value;
  284.     UpdateControlCanvas;
  285.   end;
  286. end;
  287. procedure TVrLabel.SetShadowDepth(Value: Integer);
  288. begin
  289.   if FShadowDepth <> Value then
  290.   begin
  291.     FShadowDepth := Value;
  292.     UpdateControlCanvas;
  293.   end;
  294. end;
  295. procedure TVrLabel.SetBitmap(Value: TBitmap);
  296. begin
  297.   FBitmap.Assign(Value);
  298. end;
  299. procedure TVrLabel.SetAngle(Value: TVrTextAngle);
  300. begin
  301.   if FAngle <> Value then
  302.   begin
  303.     FAngle := Value;
  304.     FRad := Value * (PI / 180);
  305.     UpdateControlCanvas;
  306.   end;
  307. end;
  308. procedure TVrLabel.BitmapChanged(Sender: TObject);
  309. begin
  310.   UpdateControlCanvas;
  311. end;
  312. procedure TVrLabel.CMTextChanged(var Message: TMessage);
  313. begin
  314.   UpdateControlCanvas;
  315.   inherited;
  316. end;
  317. end.