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

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrDigit;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, Classes, Graphics, Controls, Forms,
  14.   VrClasses, VrControls;
  15. type
  16.   TVrDigit = class(TVrGraphicImageControl)
  17.   private
  18.     FValue: Integer;
  19.     FValueBinary: Byte;
  20.     FPalette: TVrPalette;
  21.     FActiveOnly: Boolean;
  22.     FOutlineColor: TColor;
  23.     FOnChange: TNotifyEvent;
  24.     procedure SetValue(Value: Integer);
  25.     procedure SetValueBinary(Value: Byte);
  26.     procedure SetActiveOnly(Value: Boolean);
  27.     procedure SetOutlineColor(Value: TColor);
  28.     procedure SetPalette(Value: TVrPalette);
  29.     procedure PaletteModified(Sender: TObject);
  30.   protected
  31.     procedure Change; dynamic;
  32.     procedure Paint; override;
  33.   public
  34.     constructor Create(AOwner: TComponent); override;
  35.   published
  36.     property Value: Integer read FValue write SetValue default 0;
  37.     property ValueBinary: Byte read FValueBinary write SetValueBinary default 125;
  38.     property Palette: TVrPalette read FPalette write SetPalette;
  39.     property ActiveOnly: Boolean read FActiveOnly write SetActiveOnly default false;
  40.     property Transparent default false;
  41.     property OutlineColor: TColor read FOutlineColor write SetOutlineColor default clBlack;
  42.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  43.     property Align;
  44.     property DragCursor;
  45. {$IFDEF VER110}
  46.     property Anchors;
  47.     property DragKind;
  48. {$ENDIF}
  49.     property DragMode;
  50.     property Color default clBlack;
  51. {$IFDEF VER110}
  52.     property Constraints;
  53. {$ENDIF}
  54.     property ParentColor default false;
  55.     property ParentShowHint;
  56.     property PopupMenu;
  57.     property ShowHint;
  58.     property Visible;
  59.     property OnClick;
  60. {$IFDEF VER130}
  61.     property OnContextPopup;
  62. {$ENDIF}    
  63.     property OnDblClick;    
  64.     property OnDragDrop;
  65.     property OnDragOver;
  66. {$IFDEF VER110}
  67.     property OnEndDock;
  68. {$ENDIF}
  69.     property OnEndDrag;
  70.     property OnMouseDown;
  71.     property OnMouseMove;
  72.     property OnMouseUp;
  73. {$IFDEF VER110}
  74.     property OnStartDock;
  75. {$ENDIF}
  76.     property OnStartDrag;
  77.   end;
  78. implementation
  79. const
  80.   BitMask: array[0..10] of byte =
  81.     (125, 80, 55, 87, 90, 79, 111, 81, 127, 95, 47);
  82. { TVrDigit }
  83. constructor TVrDigit.Create(AOwner: TComponent);
  84. begin
  85.   inherited Create(AOwner);
  86.   ControlStyle := ControlStyle + [csOpaque] - [csSetCaption];
  87.   Width := 50;
  88.   Height := 60;
  89.   Color := clBlack;
  90.   ParentColor := false;
  91.   FValue := 0;
  92.   FValueBinary := 125;
  93.   FPalette := TVrPalette.Create;
  94.   FPalette.OnChange := PaletteModified;
  95.   FActiveOnly := false;
  96.   FOutlineColor := clBlack;
  97. end;
  98. procedure TVrDigit.PaletteModified(Sender: TObject);
  99. begin
  100.   UpdateControlCanvas;
  101. end;
  102. procedure TVrDigit.Paint;
  103. var
  104.  Iy, Ix, W, H, G, Dp, X, Y: Integer;
  105.  Bit: Byte;
  106.  Active: Boolean;
  107. begin
  108.   ClearBitmapCanvas;
  109.   BitmapCanvas.Pen.Color := FOutlineColor;
  110.   W := ClientWidth - (ClientWidth div 6);
  111.   H := ClientHeight-1;
  112.   G := W div 12;
  113.   if (G > (H div 12)) then G := H div 12;
  114.   Bit := 1;
  115.   Dp := 2;
  116.   for Iy:= 0 to 2 do
  117.   begin
  118.     case Iy of
  119.       0 : y := 0;
  120.       1 : y := H div 2 - G;
  121.       2 : y := H - 2 * G;
  122.       else Y := 0;
  123.     end;
  124.     Active := (FValueBinary and Bit) = Bit;
  125.     if (not FActiveOnly) or (FActiveOnly and Active)  then
  126.     begin
  127.       BitmapCanvas.Brush.Color := FPalette.Colors[ord(Active)];
  128.       BitmapCanvas.Polygon([Point(0 + g + dp,     g + y),
  129.                             Point(2 * g + dp,     0 + y),
  130.                             Point(W - 2 * g - dp, 0 + y),
  131.                             Point(W - g - dp,     g + y),
  132.                             Point(W - 2 * g - dp, 2 * g + y),
  133.                             Point(2 * g + dp,     2 * g + y)]);
  134.     end;
  135.     Bit := Bit * 2;
  136.   end;
  137.   for Iy := 0 to 1 do
  138.     for Ix := 0 to 1 do
  139.     begin
  140.       if Ix = 0 then X := 0 else X := W - 2 * G;
  141.       if Iy = 0 then Y := 0 else Y := H div 2 - G;
  142.       Active := (FValueBinary and Bit) = Bit;
  143.       if (not FActiveOnly) or (FActiveOnly and Active)  then
  144.       begin
  145.         BitmapCanvas.Brush.Color := FPalette.Colors[ord(Active)];
  146.         BitmapCanvas.Polygon([Point(0 + x,     2 * g + Y + dp),
  147.                               Point(g + x,     g + Y + dp),
  148.                               Point(2 * g + x, 2 * g + Y + dp),
  149.                               Point(2 * g + x, H div 2 - g + y - dp),
  150.                               Point(g + x,     H div 2 + y - dp),
  151.                               Point(0 + x,     H div 2 - g + y - dp)]);
  152.       end;
  153.       Bit := Bit * 2;
  154.     end;
  155.   Active := (FValueBinary and Bit) = Bit;
  156.   if (not FActiveOnly) or (FActiveOnly and Active)  then
  157.   begin
  158.     BitmapCanvas.Brush.Color := FPalette.Colors[ord(Active)];
  159.     BitmapCanvas.Rectangle(W + G * 2, H - G * 2, W, H);
  160.   end;
  161.   inherited Paint;
  162. end;
  163. procedure TVrDigit.Change;
  164. begin
  165.   if Assigned(FOnChange) then FOnChange(Self);
  166. end;
  167. procedure TVrDigit.SetPalette(Value: TVrPalette);
  168. begin
  169.   FPalette.Assign(Value);
  170. end;
  171. procedure TVrDigit.SetActiveOnly(Value: Boolean);
  172. begin
  173.   if FActiveOnly <> Value then
  174.   begin
  175.     FActiveOnly := Value;
  176.     UpdateControlCanvas;
  177.   end;
  178. end;
  179. procedure TVrDigit.SetValue(Value: Integer);
  180. begin
  181.   if (FValue <> Value) and (Value in [0..9]) then
  182.   begin
  183.     FValue := Value;
  184.     FValueBinary := BitMask[FValue];
  185.     UpdateControlCanvas;
  186.     Change;
  187.   end;
  188. end;
  189. procedure TVrDigit.SetValueBinary(Value: Byte);
  190. var
  191.   I: Integer;
  192. begin
  193.   if FValueBinary <> Value then
  194.   begin
  195.     FValueBinary := Value;
  196.     for I := 0 to 9 do
  197.       if FValueBinary = BitMask[I] then SetValue(I)
  198.     else
  199.     begin
  200.       UpdateControlCanvas;
  201.       Change;
  202.     end;
  203.   end;
  204. end;
  205. procedure TVrDigit.SetOutlineColor(Value: TColor);
  206. begin
  207.   if FOutlineColor <> Value then
  208.   begin
  209.     FOutlineColor := Value;
  210.     UpdateControlCanvas;
  211.   end;
  212. end;
  213. end.