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

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrCheckLed;
  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.   TVrCheckInt = 10..40;
  17.   TVrCheckLed = class(TVrCustomImageControl)
  18.   private
  19.     FChecked: Boolean;
  20.     FPalette: TVrPalette;
  21.     FSpacing: Integer;
  22.     FMargin: Integer;
  23.     FLayout: TVrImageTextLayout;
  24.     FCheckWidth: TVrCheckInt;
  25.     FCheckHeight: TVrCheckInt;
  26.     FOnChange: TNotifyEvent;
  27.     MouseButtonDown: Boolean;
  28.     ImageRect: TRect;
  29.     TextBounds: TRect;
  30.     LastState: Boolean;
  31.     HasMouse: Boolean;
  32.     procedure SetCheckWidth(Value: TVrCheckInt);
  33.     procedure SetCheckHeight(Value: TVrCheckInt);
  34.     procedure SetChecked(Value: Boolean);
  35.     procedure SetLayout(Value: TVrImageTextLayout);
  36.     procedure SetMargin(Value: Integer);
  37.     procedure SetSpacing(Value: Integer);
  38.     procedure SetPalette(Value: TVrPalette);
  39.     procedure PaletteModified(Sender: TObject);
  40.     procedure WMSize(var Message: TMessage); message WM_SIZE;
  41.     procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  42.     procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
  43.     procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
  44.     procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
  45.     procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
  46.     procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  47.     procedure CMDialogChar(var Msg: TCMDialogChar); message CM_DIALOGCHAR;
  48.   protected
  49.     procedure CreateParams(var Params: TCreateParams); override;
  50.     procedure CalcPaintParams(Repaint: Boolean);
  51.     procedure DrawGlyph(Index: Integer; R: TRect; ACanvas: TCanvas);
  52.     procedure Paint; override;
  53.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  54.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  55.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  56.     procedure Keypress(var Key: Char); override;
  57.     procedure Change; dynamic;
  58.     procedure ShowFocus;
  59.   public
  60.     constructor Create(AOwner: TComponent); override;
  61.     destructor Destroy; override;
  62.   published
  63.     property CheckWidth: TVrCheckInt read FCheckWidth write SetCheckWidth default 20;
  64.     property CheckHeight: TVrCheckInt read FCheckHeight write SetCheckHeight default 13;
  65.     property Checked: Boolean read FChecked write SetChecked default false;
  66.     property Layout: TVrImageTextLayout read FLayout write SetLayout default ImageLeft;
  67.     property Margin: Integer read FMargin write SetMargin default -1;
  68.     property Spacing: Integer read FSpacing write SetSpacing default 5;
  69.     property Palette: TVrPalette read FPalette write SetPalette;
  70.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  71. {$IFDEF VER110}
  72.     property Anchors;
  73.     property BiDiMode;
  74.     property Constraints;
  75. {$ENDIF}
  76.     property Caption;
  77.     property Color default clBtnFace;
  78.     property DragCursor;
  79. {$IFDEF VER110}
  80.     property DragKind;
  81. {$ENDIF}
  82.     property DragMode;
  83.     property Enabled;
  84.     property Font;
  85.     property ParentFont;
  86. {$IFDEF VER110}
  87.     property ParentBiDiMode;
  88. {$ENDIF}
  89.     property ParentColor default false;
  90.     property ParentShowHint;
  91.     property PopupMenu;
  92.     property ShowHint;
  93.     property TabOrder;
  94.     property TabStop default false;
  95.     property Visible;
  96.     property OnClick;
  97. {$IFDEF VER130}
  98.     property OnContextPopup;
  99. {$ENDIF}
  100.     property OnDragDrop;
  101.     property OnDragOver;
  102. {$IFDEF VER110}
  103.     property OnEndDock;
  104. {$ENDIF}
  105.     property OnEndDrag;
  106.     property OnEnter;
  107.     property OnExit;
  108.     property OnKeyDown;
  109.     property OnKeyPress;
  110.     property OnKeyUp;
  111.     property OnMouseDown;
  112.     property OnMouseMove;
  113.     property OnMouseUp;
  114. {$IFDEF VER110}
  115.     property OnStartDock;
  116. {$ENDIF}
  117.     property OnStartDrag;
  118.   end;
  119. implementation
  120. { TVrCheckLed }
  121. constructor TVrCheckLed.Create(AOwner: TComponent);
  122. begin
  123.   inherited Create(AOwner);
  124.   ControlStyle := [csCaptureMouse, csClickEvents,
  125.     csOpaque, csSetCaption, csReplicatable];
  126.   Width := 125;
  127.   Height := 25;
  128.   Color := clBtnFace;
  129.   ParentColor := false;
  130.   TabStop := false;
  131.   FChecked := false;
  132.   FCheckHeight := 13;
  133.   FCheckWidth := 20;
  134.   FPalette := TVrPalette.Create;
  135.   FPalette.OnChange := PaletteModified;
  136.   HasMouse := false;
  137.   FSpacing := 5;
  138.   FMargin := -1;
  139.   FLayout := ImageLeft;
  140. end;
  141. destructor TVrCheckLed.Destroy;
  142. begin
  143.   FPalette.Free;
  144.   inherited Destroy;
  145. end;
  146. procedure TVrCheckLed.CreateParams(var Params: TCreateParams);
  147. begin
  148.   inherited CreateParams(Params);
  149.   with Params do
  150.     WindowClass.style := WindowClass.style and not (CS_HREDRAW or CS_VREDRAW);
  151. end;
  152. procedure TVrCheckLed.DrawGlyph(Index: Integer; R: TRect; ACanvas: TCanvas);
  153. begin
  154.   if Index = 2 then
  155.     DrawFrame3D(ACanvas, R, clBlack, clBtnHighlight, 1)
  156.   else DrawFrame3D(ACanvas, R, clBtnHighlight, clBlack, 1);
  157.   DrawFrame3D(ACanvas, R, clBtnHighlight, clBtnShadow, 1);
  158.   DrawFrame3D(ACanvas, R, clBtnFace, clBtnFace, 1);
  159.   DrawFrame3D(ACanvas, R, clBlack, clBtnHighlight, 1);
  160.   case Index of
  161.     0: ACanvas.Brush.Color := FPalette.Low;
  162.     1: ACanvas.Brush.Bitmap :=
  163.          CreateDitherPattern(FPalette.Low, FPalette.High);
  164.     2: ACanvas.Brush.Color := FPalette.High;
  165.   end;
  166.   ACanvas.FillRect(R);
  167.   FreeObject(ACanvas.Brush.Bitmap);
  168. end;
  169. procedure TVrCheckLed.Paint;
  170. begin
  171.   ClearBitmapCanvas;
  172.   if (HasMouse) and not FChecked then
  173.     DrawGlyph(1, ImageRect, BitmapCanvas)
  174.   else
  175.   if FChecked then DrawGlyph(2, ImageRect, BitmapCanvas)
  176.   else DrawGlyph(0, ImageRect, BitmapCanvas);
  177.   BitmapCanvas.Font := Self.Font;
  178.   BitmapCanvas.Brush.Color := Self.Color;
  179.   DrawButtonText(BitmapCanvas, Caption, TextBounds, Enabled);
  180.   inherited Paint;
  181.   ShowFocus;
  182. end;
  183. procedure TVrCheckLed.CalcPaintParams(Repaint: Boolean);
  184. var
  185.   ImagePos: TPoint;
  186. begin
  187.   Canvas.Font := Self.Font;
  188.   CalcImageTextLayout(Canvas, ClientRect, Point(1, 1), Caption, FLayout,
  189.     FMargin, FSpacing, Point(FCheckWidth, FCheckHeight), ImagePos, TextBounds);
  190.   ImageRect := Bounds(ImagePos.X, ImagePos.Y, FCheckWidth, FCheckHeight);
  191.   if Repaint then UpdateControlCanvas;
  192. end;
  193. procedure TVrCheckLed.PaletteModified(Sender: TObject);
  194. begin
  195.   UpdateControlCanvas;
  196. end;
  197. procedure TVrCheckLed.WMSize(var Message: TMessage);
  198. begin
  199.   inherited;
  200.   CalcPaintParams(True);
  201. end;
  202. procedure TVrCheckLed.CMFontChanged(var Message: TMessage);
  203. begin
  204.   inherited;
  205.   CalcPaintParams(True);
  206. end;
  207. procedure TVrCheckLed.MouseDown(Button: TMouseButton; Shift: TShiftState;
  208.   X, Y: Integer);
  209. begin
  210.   inherited MouseDown(Button, Shift, X, Y);
  211.   if (Button = mbLeft) and Enabled then
  212.     if ptInRect(ImageRect, Point(X, Y)) then
  213.     begin
  214.       MouseButtonDown := true;
  215.       LastState := FChecked;
  216.       FChecked := not FChecked;
  217.       UpdateControlCanvas;
  218.     end;
  219.   if TabStop then SetFocus;
  220. end;
  221. procedure TVrCheckLed.MouseMove(Shift: TShiftState; X, Y: Integer);
  222. var
  223.   InRect: Boolean;
  224. begin
  225.   inherited MouseMove(Shift, X, Y);
  226.   InRect := ptInRect(ImageRect, Point(X, Y));
  227.   if HasMouse <> InRect then
  228.   begin
  229.     HasMouse := InRect;
  230.     UpdateControlCanvas;
  231.   end;
  232. end;
  233. procedure TVrCheckLed.MouseUp(Button: TMouseButton; Shift: TShiftState;
  234.   X, Y: Integer);
  235. begin
  236.   inherited MouseUp(Button, Shift, X, Y);
  237.   if MouseButtonDown then
  238.     if not ptInRect(ImageRect, Point(X, Y)) then
  239.     begin
  240.       FChecked := LastState;
  241.       UpdateControlCanvas;
  242.     end else Change;
  243.   MouseButtonDown := false;
  244. end;
  245. procedure TVrCheckLed.CMTextChanged(var Message: TMessage);
  246. begin
  247.   inherited;
  248.   if HandleAllocated then
  249.     CalcPaintParams(true);
  250. end;
  251. procedure TVrCheckLed.ShowFocus;
  252. var
  253.   R: TRect;
  254. begin
  255.   if Focused then
  256.   begin
  257.     R := TextBounds;
  258.     InflateRect(R, 2, 2);
  259.     Canvas.DrawFocusRect(R);
  260.   end;
  261. end;
  262. procedure TVrCheckLed.WMSetFocus(var Message: TWMSetFocus);
  263. begin
  264.   inherited;
  265.   ShowFocus;
  266. end;
  267. procedure TVrCheckLed.WMKillFocus(var Message: TWMKillFocus);
  268. begin
  269.   UpdateControlCanvas;
  270.   inherited;
  271. end;
  272. procedure TVrCheckLed.CMEnabledChanged(var Message: TMessage);
  273. begin
  274.   inherited;
  275.   UpdateControlCanvas;
  276. end;
  277. procedure TVrCheckLed.CMMouseLeave(var Message: TMessage);
  278. begin
  279.   inherited;
  280.   HasMouse := false;
  281.   UpdateControlCanvas;
  282. end;
  283. procedure TVrCheckLed.Keypress(var Key: Char);
  284. begin
  285.   if Key = #32 then
  286.   begin
  287.     FChecked := not FChecked;
  288.     UpdateControlCanvas;
  289.   end;
  290.   inherited;
  291. end;
  292. procedure TVrCheckLed.Change;
  293. begin
  294.   if Assigned(FOnChange) then FOnChange(Self);
  295. end;
  296. procedure TVrCheckLed.SetPalette(Value: TVrPalette);
  297. begin
  298.   FPalette.Assign(Value);
  299. end;
  300. procedure TVrCheckLed.SetCheckWidth(Value: TVrCheckInt);
  301. begin
  302.   if FCheckWidth <> Value then
  303.   begin
  304.     FCheckWidth := Value;
  305.     CalcPaintParams(True);
  306.   end;
  307. end;
  308. procedure TVrCheckLed.SetCheckHeight(Value: TVrCheckInt);
  309. begin
  310.   if FCheckHeight <> Value then
  311.   begin
  312.     FCheckHeight := Value;
  313.     CalcPaintParams(True);
  314.   end;
  315. end;
  316. procedure TVrCheckLed.SetChecked(Value: Boolean);
  317. begin
  318.   if FChecked <> Value then
  319.   begin
  320.     FChecked := Value;
  321.     UpdateControlCanvas;
  322.     Change;
  323.   end;
  324. end;
  325. procedure TVrCheckLed.SetLayout(Value: TVrImageTextLayout);
  326. begin
  327.   if FLayout <> Value then
  328.   begin
  329.     FLayout := Value;
  330.     CalcPaintParams(True);
  331.   end;
  332. end;
  333. procedure TVrCheckLed.SetMargin(Value: Integer);
  334. begin
  335.   if FMargin <> Value then
  336.   begin
  337.     FMargin := Value;
  338.     CalcPaintParams(True);
  339.   end;
  340. end;
  341. procedure TVrCheckLed.SetSpacing(Value: Integer);
  342. begin
  343.   if FSpacing <> Value then
  344.   begin
  345.     FSpacing := Value;
  346.     CalcPaintParams(True);
  347.   end;
  348. end;
  349. procedure TVrCheckLed.CMDialogChar(var Msg: TCMDialogChar);
  350. begin
  351.   with Msg do
  352.     if IsAccel(CharCode, Caption) and Enabled and CanFocus then
  353.     begin
  354.       if TabStop then SetFocus;
  355.       Checked := not Checked;
  356.       Result := 1;
  357.     end else inherited;
  358. end;
  359. end.