Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

Lazarus IDE and 组件 下载地址版权申明
查看: 3737|回复: 5

[界面] 移植的标尺控件

[复制链接]

该用户从未签到

发表于 2012-6-8 17:37:59 | 显示全部楼层 |阅读模式
完整代码:
  1. unit ruler;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,LResources;
  6. type
  7.   TTicksKind = (tkHorizontal, tkVertical);
  8.   TTicksStyle = (tkTopLeft, tkBottomRight);
  9.   TTicksAlign = (tkUpLeft, tkDwonRight);
  10.   Truler = class(TGraphicControl)
  11.   private
  12.     { Private declarations }
  13.     FTicksColor1: TColor;
  14.     FTicksColor2: TColor;
  15.     FTicksColor3: TColor;
  16.     FBKColor: TColor;
  17.     FDialColor1: TColor;
  18.     FDialColor2: TColor;
  19.     FDialColor3: TColor;
  20.     FTicksNum: Integer;
  21.     FMaxVal: Integer;
  22.     FMinVal: Integer;
  23.     FSubDialNum: Integer;
  24.     FShowSubDial: Boolean;
  25.     FTicksLength: Integer;
  26.     FDialLength: Integer;
  27.     FPercent1: Integer;
  28.     FPercent2: Integer;
  29.     FKind: TTicksKind;
  30.     FStyle: TTicksStyle;
  31.     FTicksAlign : TTicksAlign;
  32.     FBorder: integer;
  33.     TicksStep: Integer;
  34.     FShowSign: Boolean;
  35.     procedure SetBKColor(Value: TColor);
  36.     procedure SetTicksNum(Value: Integer);
  37.     procedure SetTicksColor1(Value: TColor);
  38.     procedure SetMaxVal(const Value: Integer);
  39.     procedure SetMinVal(const Value: Integer);
  40.     procedure SetSubDialNum(const Value: Integer);
  41.     procedure SetShowSubDial(Value: Boolean);
  42.     procedure SetDialLength(const Value: Integer);
  43.     procedure SetTicksLength(const Value: Integer);
  44.     procedure SetDialColor1(const Value: TColor);
  45.     function GetTransparent: Boolean;
  46.     procedure SetTransparent(const Value: Boolean);
  47.     procedure SetPercent1(const Value: integer);
  48.     procedure SetPercent2(const Value: integer);
  49.     procedure SetDialColor2(const Value: TColor);
  50.     procedure SetDialColor3(const Value: TColor);
  51.     procedure SetTicksColor2(const Value: TColor);
  52.     procedure SetTicksColor3(const Value: TColor);
  53.     procedure SetKind(const Value: TTicksKind);
  54.     procedure SetStyle(const Value: TTicksStyle);
  55.     procedure SetTicksAlign(const Value: TTicksAlign);
  56.     procedure SetBorder(const Value: Integer);
  57.     procedure SetShowSign(const Value: Boolean);
  58.     procedure ChangeColor(Value: Integer);
  59.   protected
  60.     { Protected declarations }
  61.     procedure Paint; override;
  62.     procedure DrawBK;
  63.     procedure DrawVTicks;
  64.     procedure DrawHTicks;
  65.   public
  66.     { Public declarations }
  67.     constructor Create(AOwner:TComponent); override;
  68.   published
  69.     { Published declarations }
  70.     property BKColor: TColor read FBKColor write SetBKColor default clBlack;
  71.     property TicksNum: Integer read FTicksNum write SetTicksNum default 10;
  72.     property MaxVal: Integer read FMaxVal write SetMaxVal default 100;
  73.     property MinVal: Integer read FMinVal write SetMinVal default 0;
  74.     property TicksColor1: TColor read FTicksColor1 write SetTicksColor1 default clLime;
  75.     property TicksColor2: TColor read FTicksColor2 write SetTicksColor2 default clYellow;
  76.     property TicksColor3: TColor read FTicksColor3 write SetTicksColor3 default clRed;
  77.     property SubDialNum: Integer read FSubDialNum write SetSubDialNum default 5;
  78.     property ShowSubDial: Boolean read FShowSubDial write SetShowSubDial default true;
  79.     property TicksLength: Integer read FTicksLength write SetTicksLength default 6;
  80.     property DialLength: Integer read FDialLength write SetDialLength default 3;
  81.     property DialColor1: TColor read FDialColor1 write SetDialColor1 default clWhite;
  82.     property DialColor2: TColor read FDialColor2 write SetDialColor2 default clWhite;
  83.     property DialColor3: TColor read FDialColor3 write SetDialColor3 default clWhite;
  84.     property Transparent: Boolean read GetTransparent write SetTransparent default false;
  85.     property Percent1: integer read FPercent1 write SetPercent1 default 40;
  86.     property Percent2: integer read FPercent2 write SetPercent2 default 60;
  87.     property Kind: TTicksKind read FKind write SetKind default tkVertical;
  88.     property Style: TTicksStyle read FStyle write SetStyle default tkTopLeft;
  89.     property TicksAlign: TTicksAlign read FTicksAlign write SetTicksAlign default tkUpLeft;
  90.     Property Font;
  91.     property Border: Integer read FBorder write SetBorder default 10;
  92.     property ShowSign: Boolean read FShowSign write SetShowSign default false;
  93.   end;
  94. procedure Register;
  95. implementation
  96. procedure Register;
  97. begin
  98.     {$I ruler_icon.lrs}
  99.   RegisterComponents('fpccn', [Truler]);
  100. end;
  101. { Truler }
  102. procedure Truler.ChangeColor(Value: Integer);
  103. var
  104.     PNum1,PNum2 : Integer;
  105. begin
  106.     if Fkind = tkVertical then
  107.     begin
  108.         PNum1 := FBorder + Round((Height - FBorder * 2) / 100 * FPercent1);
  109.         PNum2 := FBorder + Round((Height - FBorder * 2) / 100 * (FPercent1 + FPercent2));
  110.     end
  111.     else
  112.     begin
  113.         PNum1 := FBorder + Round((Width - FBorder * 2) / 100 * FPercent1);
  114.         PNum2 := FBorder + Round((Width - FBorder * 2) / 100 * (FPercent1 + FPercent2));
  115.     end;
  116.     if Value <= PNum1 then
  117.     begin
  118.         Canvas.Font.Color := FTicksColor1;
  119.         Canvas.Pen.Color := FDialColor1;
  120.         exit;
  121.     end;
  122.     if (Value > PNum1) and (Value <= PNum2) then
  123.     begin
  124.         Canvas.Font.Color := FTicksColor2;
  125.         Canvas.Pen.Color := FDialColor2;
  126.         exit;
  127.     end;
  128.     if (Value > PNum2) then
  129.     begin
  130.         Canvas.Font.Color := FTicksColor3;
  131.         Canvas.Pen.Color := FDialColor3;
  132.         exit;
  133.     end;
  134. end;
  135. constructor Truler.Create(AOwner: TComponent);
  136. begin
  137.     inherited Create(AOwner);
  138.     ControlStyle := ControlStyle + [csOpaque, csReplicatable];
  139.     Height := 200;
  140.     Width := 25;
  141.     FBKColor := clBlack;
  142.     TicksNum := 10;
  143.     FMaxVal := 100;
  144.     FminVal := 0;
  145.     FTicksColor1 := clLime;
  146.     FTicksColor2 := clYellow;
  147.     FTicksColor3 := clRed;
  148.     FSubDialNum := 5;
  149.     FShowSubDial := true;
  150.     FTicksLength := 6;
  151.     FDialLength := 3;
  152.     FDialColor1 := clWhite;
  153.     FDialColor2 := clWhite;
  154.     FDialColor3 := clWhite;
  155.     FPercent1 := 40;
  156.     FPercent2 := 60;
  157.     FKind := tkVertical;
  158.     FStyle := tkTopLeft;
  159.     FTicksAlign := tkUpLeft;
  160.     FBorder := 10;
  161.     FShowSign := false;
  162. end;
  163. procedure Truler.DrawBK;
  164. begin
  165.     With Canvas do
  166.     begin
  167.         if not Transparent then
  168.         begin
  169.             Brush.Style := bsSolid;
  170.             Brush.Color := fBkColor;
  171.             FillRect(ClientRect);
  172.         end
  173.         else
  174.         begin
  175.             Brush.Style := bsClear;
  176.         end;
  177.         Pen.Style := psSolid;
  178.         Pen.Color := FBKColor;
  179.     end;
  180. end;
  181. procedure Truler.DrawHTicks;
  182. Var
  183.     i, n, DPos: Integer;
  184.     CapStr: String;
  185. begin
  186.     TicksStep := (Width - FBorder * 2) Div TicksNum;
  187.     Canvas.Font := Font;
  188.     With Canvas do
  189.     begin
  190.         for i := 0 to FTicksNum do
  191.         begin
  192.             if Style = tkBottomRight then
  193.                 DPos := Round(FMaxVal - (FMaxVal - FMinVal)/ FTicksNum* i)
  194.             else
  195.                 DPos := Round(FMinVal + (FMaxVal - FMinVal)/ FTicksNum* i);
  196.             CapStr := IntToStr(DPos);
  197.             if FShowSign and (DPos > 0) then
  198.                 Capstr := '+'+ Capstr;
  199.             ChangeColor(i * TicksStep + FBorder);
  200.             if FTicksAlign = tkUpLeft then
  201.             begin
  202.                 TextOut(i * TicksStep - TextWidth(CapStr) div 2+ FBorder, TicksLength, CapStr);
  203.                 MoveTo(i * TicksStep + FBorder,0);
  204.                 LineTo(i *TicksStep + FBorder,FTicksLength);
  205.                 If (i < FTicksNum) and FShowSubDial then
  206.                 For n := 1 to FSubDialNum do
  207.                 begin
  208.                     ChangeColor(i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n));
  209.                     MoveTo(i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n),0);
  210.                     LineTo(i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n),FDialLength);
  211.                 end;
  212.             end
  213.             else
  214.             begin
  215.                 TextOut(i * TicksStep - TextWidth(CapStr) div 2+ FBorder,Height - FTicksLength - TextHeight(CapStr) - 1, CapStr);
  216.                 MoveTo(i * TicksStep + FBorder,Height - FTicksLength);
  217.                 LineTo(i *TicksStep + FBorder,Height);
  218.                 If (i < FTicksNum) and FShowSubDial then
  219.                 For n := 1 to FSubDialNum do
  220.                 begin
  221.                     ChangeColor(i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n));
  222.                     MoveTo(i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n),Height - FDialLength);
  223.                     LineTo(i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n),Height);
  224.                 end;
  225.             end;
  226.         end;
  227.     end;
  228. end;
  229. procedure Truler.DrawVTicks;
  230. Var
  231.     i, n, DPos: Integer;
  232.     CapStr : String;
  233. begin
  234.     TicksStep := (Height - FBorder * 2) Div TicksNum;
  235.     Canvas.Font := Font;
  236.     With Canvas do
  237.     begin
  238.         for i := 0 to FTicksNum do
  239.         begin
  240.             if Style = tkBottomRight then
  241.                 DPos := Round(FMaxVal - (FMaxVal - FMinVal)/ FTicksNum* i)
  242.             else
  243.                 DPos := Round(FMinVal + (FMaxVal - FMinVal)/ FTicksNum* i);
  244.             ChangeColor(i * TicksStep + FBorder);
  245.             CapStr := IntToStr(DPos);
  246.             if FShowSign and (DPos > 0) then
  247.                 Capstr := '+'+ Capstr;
  248.             if FTicksAlign = tkUpLeft then
  249.             begin
  250.                 TextOut(FTicksLength, i * TicksStep - TextHeight(CapStr) Div 2 + FBorder, CapStr);
  251.                 MoveTo(0,i * TicksStep + FBorder);
  252.                 LineTo(FTicksLength,i *TicksStep + FBorder);
  253.                 If (i < FTicksNum) and FShowSubDial then
  254.                 For n := 1 to FSubDialNum do
  255.                 begin
  256.                     ChangeColor(i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n));
  257.                     MoveTo(0,i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n));
  258.                     LineTo(FDialLength,i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n));
  259.                 end;
  260.             end
  261.             else
  262.             begin
  263.                 TextOut(Width - FTicksLength - TextWidth(CapStr)-1, i * TicksStep - TextHeight(CapStr) Div 2 + FBorder, CapStr);
  264.                 MoveTo(Width - FTicksLength,i * TicksStep + FBorder);
  265.                 LineTo(Width,i *TicksStep + FBorder);
  266.                 If (i < FTicksNum) and FShowSubDial  then
  267.                 For n := 1 to FSubDialNum do
  268.                 begin
  269.                     ChangeColor(i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n));
  270.                     MoveTo(Width - FDialLength,i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n));
  271.                     LineTo(Width,i * TicksStep + FBorder + Round(TicksStep/SubDialNum*n));
  272.                 end;
  273.             end;
  274.         end;
  275.     end;
  276. end;
  277. function Truler.GetTransparent: Boolean;
  278. begin
  279.     Result := not (csOpaque in ControlStyle);
  280. end;
  281. procedure Truler.Paint;
  282. begin
  283.     DrawBK;
  284.     if FKind = tkHorizontal then
  285.        DrawHTicks
  286.     else
  287.        DrawVTicks;
  288. end;
  289. procedure Truler.SetBKColor(Value: TColor);
  290. begin
  291.     if FBKColor <> Value then
  292.     begin
  293.         FBKColor := Value;
  294.         Invalidate;
  295.     end;
  296. end;
  297. procedure Truler.SetBorder(const Value: Integer);
  298. begin
  299.     if FBorder <> Value then
  300.     begin
  301.         FBorder := Value;
  302.         Invalidate;
  303.     end;
  304. end;
  305. procedure Truler.SetDialColor1(const Value: TColor);
  306. begin
  307.     if FDialColor1 <> Value then
  308.     begin
  309.         FDialColor1 := Value;
  310.         Invalidate;
  311.     end;
  312. end;
  313. procedure Truler.SetDialColor2(const Value: TColor);
  314. begin
  315.     if FDialColor2 <> Value then
  316.     begin
  317.         FDialColor2 := Value;
  318.         Invalidate;
  319.     end;
  320. end;
  321. procedure Truler.SetDialColor3(const Value: TColor);
  322. begin
  323.     if FDialColor3 <> Value then
  324.     begin
  325.         FDialColor3 := Value;
  326.         Invalidate;
  327.     end;
  328. end;
  329. procedure Truler.SetDialLength(const Value: Integer);
  330. begin
  331.     If FdialLength <> Value then
  332.     begin
  333.         FDialLength := Value;
  334.         Invalidate;
  335.     end;
  336. end;
  337. procedure Truler.SetKind(const Value: TTicksKind);
  338. begin
  339.   if FKind <> Value then
  340.   begin
  341.     FKind := Value;
  342.     if not (csLoading in ComponentState) then SetBounds(Left, Top, Height, Width);
  343.     Invalidate;
  344.   end;
  345. end;
  346. procedure Truler.SetMaxVal(const Value: Integer);
  347. begin
  348.     if (FMaxVal <> Value) and (Value > MinVal) then
  349.     begin
  350.         FMaxVal := Value;
  351.         Invalidate;
  352.     end;
  353. end;
  354. procedure Truler.SetMinVal(const Value: Integer);
  355. begin
  356.     if (FMinVal <> Value) and (Value < MaxVal) then
  357.     begin
  358.         FMinVal := Value;
  359.         Invalidate;
  360.     end;
  361. end;
  362. procedure Truler.SetPercent1(const Value: integer);
  363. begin
  364.     if FPercent1 <> Value then
  365.     begin
  366.         FPercent1 := Value;
  367.         Invalidate;
  368.     end;
  369. end;
  370. procedure Truler.SetPercent2(const Value: integer);
  371. begin
  372.     if FPercent2 <> Value then
  373.     begin
  374.         FPercent2 := Value;
  375.         Invalidate;
  376.     end;
  377. end;
  378. procedure Truler.SetShowSign(const Value: Boolean);
  379. begin
  380.     if FShowSign <> Value then
  381.     begin
  382.         FShowSign := Value;
  383.         Invalidate;
  384.     end;
  385. end;
  386. procedure Truler.SetShowSubDial(Value: Boolean);
  387. begin
  388.     if FShowSubDial <> Value then
  389.     begin
  390.         FShowSubDial := Value;
  391.         Invalidate;
  392.     end;
  393. end;
  394. procedure Truler.SetStyle(const Value: TTicksStyle);
  395. begin
  396.     if FStyle <> Value then
  397.     begin
  398.         FStyle := Value;
  399.         Invalidate;
  400.     end;
  401. end;
  402. procedure Truler.SetSubDialNum(const Value: Integer);
  403. begin
  404.     if FSubDialNum <> Value then
  405.     begin
  406.         FSubDialNum := Value;
  407.         Invalidate;
  408.     end;
  409. end;
  410. procedure Truler.SetTicksAlign(const Value: TTicksAlign);
  411. begin
  412.     if FTicksAlign <> Value then
  413.     begin
  414.         FTicksAlign := Value;
  415.         Invalidate;
  416.     end;
  417. end;
  418. procedure Truler.SetTicksColor1(Value: TColor);
  419. begin
  420.     if FTicksColor1 <> Value then
  421.     begin
  422.         FTicksColor1 := Value;
  423.         Invalidate;
  424.     end;
  425. end;
  426. procedure Truler.SetTicksColor2(const Value: TColor);
  427. begin
  428.     if FTicksColor2 <> Value then
  429.     begin
  430.         FTicksColor2 := Value;
  431.         Invalidate;
  432.     end;
  433. end;
  434. procedure Truler.SetTicksColor3(const Value: TColor);
  435. begin
  436.     if FTicksColor3 <> Value then
  437.     begin
  438.         FTicksColor3 := Value;
  439.         Invalidate;
  440.     end;
  441. end;
  442. procedure Truler.SetTicksLength(const Value: Integer);
  443. begin
  444.     if FTicksLength <> Value then
  445.     begin
  446.         FTicksLength := Value;
  447.         Invalidate;
  448.     end;
  449. end;
  450. procedure Truler.SetTicksNum(Value: Integer);
  451. begin
  452.     if FTicksNum <> Value then
  453.     begin
  454.         FTicksNum := Value;
  455.         Invalidate;
  456.     end;
  457. end;
  458. procedure Truler.SetTransparent(const Value: Boolean);
  459. begin
  460.   if Transparent <> Value then
  461.   begin
  462.     if Value then
  463.       ControlStyle := ControlStyle - [csOpaque] else
  464.       ControlStyle := ControlStyle + [csOpaque];
  465.     Invalidate;
  466.   end;
  467. end;
  468. end.
复制代码
效果图:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册(注册审核可向QQ群索取)

x

评分

参与人数 1威望 +2 收起 理由
猫工 + 2 优秀文章,支持!n神马都是浮云!

查看全部评分

回复

使用道具 举报

该用户从未签到

发表于 2012-6-9 10:28:09 | 显示全部楼层
社区有你更精彩,不加分……
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2012-6-9 13:58:02 | 显示全部楼层
谢谢分享,先收藏了!!!!!!!
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2012-6-10 01:28:33 | 显示全部楼层

回 1楼(东兰梦舞) 的帖子

东兰梦舞:社区有你更精彩,不加分…… (2012-06-09 10:28) 
太抠门了。
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2012-6-10 18:57:37 | 显示全部楼层
优秀文章,支持!n神马都是浮云!
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2012-6-20 20:54:38 | 显示全部楼层
好东西。 用上了。。。
回复 支持 反对

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2025-5-2 21:11 , Processed in 0.038326 second(s), 15 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表