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

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrLeds;
  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.   TVrLedType = (ltRounded, ltRectangle, ltLargeRect);
  17.   TVrLedDrawStyle = (dsDesign, dsCustom);
  18.   TVrCustomLed = class(TVrGraphicImageControl)
  19.   private
  20.     FLedType: TVrLedType;
  21.     FPalette: TVrPalette;
  22.     FActive: Boolean;
  23.     FSpacing: Integer;
  24.     FMargin: Integer;
  25.     FLayout: TVrImageTextLayout;
  26.     FGlyphs: TBitmap;
  27.     FDrawStyle: TVrLedDrawStyle;
  28.     FOnChange: TNotifyEvent;
  29.     FImageRect: TRect;
  30.     FTextBounds: TRect;
  31.     FBitmap: TBitmap;
  32.     procedure SetActive(Value: Boolean);
  33.     procedure SetLedType(Value: TVrLedType);
  34.     procedure SetLayout(Value: TVrImageTextLayout);
  35.     procedure SetMargin(Value: Integer);
  36.     procedure SetSpacing(Value: Integer);
  37.     procedure SetPalette(Value: TVrPalette);
  38.     procedure SetGlyphs(Value: TBitmap);
  39.     procedure SetDrawStyle(Value: TVrLedDrawStyle);
  40.     procedure PaletteModified(Sender: TObject);
  41.     procedure GlyphsChanged(Sender: TObject);
  42.     procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  43.   protected
  44.     procedure LoadBitmaps; virtual;
  45.     procedure Paint; override;
  46.     procedure CalcPaintParams;
  47.     procedure Changed; dynamic;
  48.     function GetPalette: HPalette; override;
  49.     property Active: Boolean read FActive write SetActive default false;
  50.     property Palette: TVrPalette read FPalette write SetPalette;
  51.     property LedType: TVrLedType read FLedType write SetLedType default ltRounded;
  52.     property Layout: TVrImageTextLayout read FLayout write SetLayout default ImageLeft;
  53.     property Margin: Integer read FMargin write SetMargin default -1;
  54.     property Spacing: Integer read FSpacing write SetSpacing default 5;
  55.     property Glyphs: TBitmap read FGlyphs write SetGlyphs;
  56.     property DrawStyle: TVrLedDrawStyle read FDrawStyle write SetDrawStyle default dsDesign;
  57.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  58.   public
  59.     constructor Create(AOwner: TComponent); override;
  60.     destructor Destroy; override;
  61.   end;
  62.   TVrLed = class(TVrCustomLed)
  63.   published
  64.     property Active;
  65.     property Palette;
  66.     property LedType;
  67.     property Layout;
  68.     property Margin;
  69.     property Spacing;
  70.     property Transparent default false;
  71.     property DrawStyle;
  72.     property Glyphs;
  73.     property OnChange;
  74.     property Align;
  75. {$IFDEF VER110}
  76.     property Anchors;
  77.     property BiDiMode;
  78.     property Constraints;
  79. {$ENDIF}
  80.     property Caption;
  81.     property Color;
  82.     property DragCursor;
  83. {$IFDEF VER110}
  84.     property DragKind;
  85. {$ENDIF}
  86.     property DragMode;
  87.     property Font;
  88.     property Hint;
  89. {$IFDEF VER110}
  90.     property ParentBiDiMode;
  91. {$ENDIF}
  92.     property ParentColor;
  93.     property ParentFont;
  94.     property ParentShowHint;
  95.     property PopupMenu;
  96.     property ShowHint;
  97.     property Visible;
  98.     property OnClick;
  99. {$IFDEF VER130}
  100.     property OnContextPopup;
  101. {$ENDIF}    
  102.     property OnDragDrop;
  103.     property OnDragOver;
  104. {$IFDEF VER110}
  105.     property OnEndDock;
  106. {$ENDIF}
  107.     property OnEndDrag;
  108.     property OnMouseDown;
  109.     property OnMouseMove;
  110.     property OnMouseUp;
  111. {$IFDEF VER110}
  112.     property OnStartDock;
  113. {$ENDIF}
  114.     property OnStartDrag;
  115.   end;
  116.   TVrUserLedDrawEvent = procedure(Sender: TObject; Canvas: TCanvas;
  117.     Rect: TRect) of object;
  118.   TVrUserLed = class(TVrGraphicControl)
  119.   private
  120.     FBevel: TVrBevel;
  121.     FPalette: TVrPalette;
  122.     FActive: Boolean;
  123.     FOutlineColor: TColor;
  124.     FOutlineWidth: Integer;
  125.     FDrawStyle: TVrDrawStyle;
  126.     FOnChange: TNotifyEvent;
  127.     FOnDraw: TVrUserLedDrawEvent;
  128.     procedure SetActive(Value: Boolean);
  129.     procedure SetOutlineColor(Value: TColor);
  130.     procedure SetOutlineWidth(Value: Integer);
  131.     procedure SetDrawStyle(Value: TVrDrawStyle);
  132.     procedure SetPalette(Value: TVrPalette);
  133.     procedure SetBevel(Value: TVrBevel);
  134.     procedure PaletteModified(Sender: TObject);
  135.     procedure BevelChanged(Sender: TObject);
  136.   protected
  137.     procedure Change; dynamic;
  138.     procedure Paint; override;
  139.   public
  140.     constructor Create(AOwner: TComponent); override;
  141.     destructor Destroy; override;
  142.   published
  143.     property Bevel: TVrBevel read FBevel write SetBevel;
  144.     property Palette: TVrPalette read FPalette write SetPalette;
  145.     property Active: Boolean read FActive write SetActive default false;
  146.     property OutlineColor: TColor read FOutlineColor write SetOutlineColor default clBlack;
  147.     property OutlineWidth: Integer read FOutlineWidth write SetOutlineWidth default 0;
  148.     property DrawStyle: TVrDrawStyle read FDrawStyle write SetDrawStyle default dsNormal;
  149.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  150.     property OnDraw: TVrUserLedDrawEvent read FOnDraw write FOnDraw;
  151.     property Align;
  152. {$IFDEF VER110}
  153.     property Anchors;
  154.     property Constraints;
  155. {$ENDIF}
  156.     property DragCursor;
  157. {$IFDEF VER110}
  158.     property DragKind;
  159. {$ENDIF}
  160.     property DragMode;
  161.     property Hint;
  162.     property ParentShowHint;
  163.     property PopupMenu;
  164.     property ShowHint;
  165.     property Visible;
  166.     property OnClick;
  167. {$IFDEF VER130}
  168.     property OnContextPopup;
  169. {$ENDIF}
  170.     property OnDblClick;
  171.     property OnDragDrop;
  172.     property OnDragOver;
  173. {$IFDEF VER110}
  174.     property OnEndDock;
  175. {$ENDIF}
  176.     property OnEndDrag;
  177.     property OnMouseDown;
  178.     property OnMouseMove;
  179.     property OnMouseUp;
  180. {$IFDEF VER110}
  181.     property OnStartDock;
  182. {$ENDIF}
  183.     property OnStartDrag;
  184.   end;
  185. implementation
  186. {$R VRLEDS.D32}
  187. const
  188.   LedTypeName: array[TVrLedType] of PChar = ('RND', 'RECT', 'BIG');
  189. {TVrCustomLed}
  190. constructor TVrCustomLed.Create(AOwner: TComponent);
  191. begin
  192.   inherited Create(AOwner);
  193.   ControlStyle := ControlStyle + [csOpaque, csReplicatable,
  194.     csClickEvents] - [csDoubleClicks, csSetCaption];
  195.   Width := 25;
  196.   Height := 25;
  197.   ParentColor := false;
  198.   Color := clBlack;
  199.   Font.Color := clWhite;
  200.   FActive := false;
  201.   FLedType := ltRounded;
  202.   FSpacing := 5;
  203.   FMargin := -1;
  204.   FLayout := ImageLeft;
  205.   FPalette := TVrPalette.Create;
  206.   with FPalette do
  207.   begin
  208.     Low := clMaroon;
  209.     High := clRed;
  210.     OnChange := PaletteModified;
  211.   end;
  212.   FDrawStyle := dsDesign;
  213.   FGlyphs := TBitmap.Create;
  214.   FGlyphs.OnChange := GlyphsChanged;
  215.   FBitmap := TBitmap.Create;
  216.   LoadBitmaps;
  217. end;
  218. destructor TVrCustomLed.Destroy;
  219. begin
  220.   FPalette.Free;
  221.   FGlyphs.Free;
  222.   FBitmap.Free;
  223.   inherited Destroy;
  224. end;
  225. procedure TVrCustomLed.LoadBitmaps;
  226. var
  227.   ResName: array[0..40] of Char;
  228. begin
  229.   if DrawStyle = dsDesign then
  230.   begin
  231.     FBitmap.Handle := LoadBitmap(hInstance,
  232.       StrFmt(ResName, 'LI_%s', [LedTypeName[FLedType]]));
  233.     FPalette.ToBMP(FBitmap, clMaroon, clRed);
  234.   end else FBitmap.Assign(FGlyphs);
  235. end;
  236. function TVrCustomLed.GetPalette: HPalette;
  237. begin
  238.   Result := FBitmap.Palette;
  239. end;
  240. procedure TVrCustomLed.PaletteModified(Sender: TObject);
  241. begin
  242.   LoadBitmaps;
  243.   UpdateControlCanvas;
  244. end;
  245. procedure TVrCustomLed.GlyphsChanged(Sender: TObject);
  246. begin
  247.   LoadBitmaps;
  248.   UpdateControlCanvas;
  249. end;
  250. procedure TVrCustomLed.Changed;
  251. begin
  252.   if Assigned(FOnChange) then FOnChange(Self);
  253. end;
  254. procedure TVrCustomLed.CMTextChanged(var Message: TMessage);
  255. begin
  256.   inherited;
  257.   UpdateControlCanvas;
  258. end;
  259. procedure TVrCustomLed.Paint;
  260. var
  261.   Offset: Integer;
  262.   ImageWidth: Integer;
  263.   TransparentColor: TColor;
  264. begin
  265.   ClearBitmapCanvas;
  266.   CalcPaintParams;
  267.   ImageWidth := FBitmap.Width div 2;
  268.   Offset := ImageWidth * ord(FActive);
  269.   with BitmapCanvas do
  270.   begin
  271.     Brush.Style := bsClear;
  272.     if DrawStyle = dsDesign then TransparentColor := clBlack
  273.     else TransparentColor := Self.Color;
  274.     BrushCopy(FImageRect, FBitmap,
  275.       Bounds(Offset, 0, ImageWidth, FBitmap.Height), TransparentColor);
  276.     Font := Self.Font;
  277.     DrawText(Handle, PChar(Caption), Length(Caption), FTextBounds,
  278.       DT_LEFT or DT_VCENTER or DT_SINGLELINE);
  279.   end;
  280.   inherited Paint;
  281. end;
  282. procedure TVrCustomLed.CalcPaintParams;
  283. var
  284.   Offset, ImagePos: TPoint;
  285. begin
  286.   Canvas.Font.Assign(Self.Font);
  287.   Offset := Point(0,0);
  288.   CalcImageTextLayout(Canvas, ClientRect, Offset, Caption, FLayout,
  289.     FMargin, FSpacing, Point(FBitmap.Width div 2, FBitmap.Height),
  290.     ImagePos, FTextBounds);
  291.   FImageRect := Bounds(ImagePos.X, ImagePos.Y,
  292.     FBitmap.Width div 2, FBitmap.Height);
  293. end;
  294. procedure TVrCustomLed.SetActive(Value: Boolean);
  295. begin
  296.   if FActive <> Value then
  297.   begin
  298.     FActive := Value;
  299.     UpdateControlCanvas;
  300.     Changed;
  301.   end;
  302. end;
  303. procedure TVrCustomLed.SetLedType(Value: TVrLedType);
  304. begin
  305.   if FLedType <> Value then
  306.   begin
  307.     FLedType := Value;
  308.     LoadBitmaps;
  309.     UpdateControlCanvas;
  310.   end;
  311. end;
  312. procedure TVrCustomLed.SetLayout(Value: TVrImageTextLayout);
  313. begin
  314.   if FLayout <> Value then
  315.   begin
  316.     FLayout := Value;
  317.     UpdateControlCanvas;
  318.   end;
  319. end;
  320. procedure TVrCustomLed.SetMargin(Value: Integer);
  321. begin
  322.   if FMargin <> Value then
  323.   begin
  324.     FMargin := Value;
  325.     UpdateControlCanvas;
  326.   end;
  327. end;
  328. procedure TVrCustomLed.SetSpacing(Value: Integer);
  329. begin
  330.   if FSpacing <> Value then
  331.   begin
  332.     FSpacing := Value;
  333.     UpdateControlCanvas;
  334.   end;
  335. end;
  336. procedure TVrCustomLed.SetPalette(Value: TVrPalette);
  337. begin
  338.   FPalette.Assign(Value);
  339. end;
  340. procedure TVrCustomLed.SetGlyphs(Value: TBitmap);
  341. begin
  342.   FGlyphs.Assign(Value);
  343. end;
  344. procedure TVrCustomLed.SetDrawStyle(Value: TVrLedDrawStyle);
  345. begin
  346.   if FDrawStyle <> Value then
  347.   begin
  348.     FDrawStyle := Value;
  349.     LoadBitmaps;
  350.     UpdateControlCanvas;
  351.   end;
  352. end;
  353. { TVrUserLed }
  354. constructor TVrUserLed.Create(AOwner: TComponent);
  355. begin
  356.   inherited Create(AOwner);
  357.   ControlStyle := ControlStyle + [csOpaque, csReplicatable] -
  358.     [csSetCaption, csDoubleClicks];
  359.   Width := 20;
  360.   Height := 10;
  361.   FActive := false;
  362.   FOutlineColor := clBlack;
  363.   FOutlineWidth := 0;
  364.   FDrawStyle := dsNormal;
  365.   FPalette := TVrPalette.Create;
  366.   with FPalette do
  367.   begin
  368.     Low := clMaroon;
  369.     High := clRed;
  370.     OnChange := PaletteModified;
  371.   end;
  372.   FBevel := TVrBevel.Create;
  373.   with FBevel do
  374.   begin
  375.     InnerStyle := bsNone;
  376.     InnerHighlight := clLime;
  377.     InnerShadow := clGreen;
  378.     OnChange := BevelChanged;
  379.   end;
  380. end;
  381. destructor TVrUserLed.Destroy;
  382. begin
  383.   FPalette.Free;
  384.   FBevel.Free;
  385.   inherited Destroy;
  386. end;
  387. procedure TVrUserLed.PaletteModified(Sender: TObject);
  388. begin
  389.   UpdateControlCanvas;
  390. end;
  391. procedure TVrUserLed.BevelChanged(Sender: TObject);
  392. begin
  393.   UpdateControlCanvas;
  394. end;
  395. procedure TVrUserLed.Change;
  396. begin
  397.   if Assigned(FOnChange) then FOnChange(Self);
  398. end;
  399. procedure TVrUserLed.Paint;
  400. var
  401.   R: TRect;
  402. begin
  403.   if FDrawStyle = dsOwnerDraw then
  404.   begin
  405.     ClearClientCanvas;
  406.     if Assigned(FOnDraw) then
  407.       FOnDraw(Self, Canvas, ClientRect);
  408.     Exit;
  409.   end;
  410.   R := ClientRect;
  411.   if FOutlineWidth > 0 then
  412.     DrawFrame3D(Canvas, R, FOutlineColor, FOutlineColor, FOutlineWidth);
  413.   FBevel.Paint(Canvas, R);
  414.   with inherited Canvas do
  415.   begin
  416.     Brush.Color := FPalette.Colors[ord(FActive)];
  417.     FillRect(R);
  418.   end;
  419. end;
  420. procedure TVrUserLed.SetPalette(Value: TVrPalette);
  421. begin
  422.   FPalette.Assign(Value);
  423. end;
  424. procedure TVrUserLed.SetBevel(Value: TVrBevel);
  425. begin
  426.   FBevel.Assign(Value);
  427. end;
  428. procedure TVrUserLed.SetActive(Value: Boolean);
  429. begin
  430.   if FActive <> Value then
  431.   begin
  432.     FActive := Value;
  433.     UpdateControlCanvas;
  434.     Change;
  435.   end;
  436. end;
  437. procedure TVrUserLed.SetOutlineColor(Value: TColor);
  438. begin
  439.   if FOutlineColor <> Value then
  440.   begin
  441.     FOutlineColor := Value;
  442.     UpdateControlCanvas;
  443.   end;
  444. end;
  445. procedure TVrUserLed.SetDrawStyle(Value: TVrDrawStyle);
  446. begin
  447.   if FDrawStyle <> Value then
  448.   begin
  449.     FDrawStyle := Value;
  450.     UpdateControlCanvas;
  451.   end;
  452. end;
  453. procedure TVrUserLed.SetOutlineWidth(Value: Integer);
  454. begin
  455.   if FOutlineWidth <> Value then
  456.   begin
  457.     FOutlineWidth := Value;
  458.     UpdateControlCanvas;
  459.   end;
  460. end;
  461. end.