delphi – TTrackBar定制职位?

前端之家收集整理的这篇文章主要介绍了delphi – TTrackBar定制职位?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在Win 7缩放轨道栏中复制PAINT应用程序的行为:
(我知道这是一个常见的跟踪栏控件)

100%位于中心.
它有11个可用职位:

等等…

12.5%,25%,50%,100%,200%,300%,400%,500%,600%,700%,800%

所以我的缩放值(ZoomArray)是:
0.125,0.25,0.5,1,2,3,4,5,6,7,8

这很容易,我可以将最小值设置为1,最大值为11,并获取我需要的值:
ZoomArray [TrackBar1.Position]

问题是如何保持100%在中心,唯一可用的位置是上面的?

我试图在数组中使用虚拟值将1保留在中心,例如:
0.125,1 -1,-1,8
并且在“变更”事件中重新定位轨迹栏,但是我的逻辑似乎没有正确的工作.

有任何想法吗?

解决方法

这是从TTrackbar中导出新控件的一个替代方法,去除滚动消息中的自动提示和处理滑动,其行为与Paint中的控件几乎相同.用D2007编译,试图点评一下:
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows,Messages,SysUtils,Classes,Controls,Forms,ComCtrls,StdCtrls;
  7.  
  8. type
  9. TCNHScroll = TWMHScroll;
  10.  
  11. TTrackBar = class(comctrls.TTrackBar) // interposer class for quick test
  12. protected
  13. procedure CreateParams(var Params: TCreateParams); override;
  14. procedure CreateWnd; override;
  15. procedure CNHScroll(var Message: TCNHScroll); message CN_HSCROLL;
  16. public
  17. constructor Create(AOwner: TComponent); override;
  18. end;
  19.  
  20. TForm1 = class(TForm)
  21. Label1: TLabel;
  22. TrackBar1: TTrackBar;
  23. procedure TrackBar1Change(Sender: TObject);
  24. end;
  25.  
  26. var
  27. Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. uses
  32. commctrl;
  33.  
  34. {$R *.dfm}
  35.  
  36. procedure TForm1.TrackBar1Change(Sender: TObject);
  37. begin
  38. // account for non-linear scaling for a sensible value
  39. if TrackBar1.Position <= 8 then
  40. Label1.Caption := IntToStr(TrackBar1.Position * 125)
  41. else
  42. Label1.Caption := IntToStr(TrackBar1.Position * 1000 - 7000)
  43. end;
  44.  
  45. { TTrackBar }
  46.  
  47. constructor TTrackBar.Create(AOwner: TComponent);
  48. begin
  49. inherited;
  50.  
  51. // We'll have 15 positions which should account for the following values
  52. // 125 250 - 500 - - - 1000 2000 3000 4000 5000 6000 7000 8000
  53. // positions 3,5..7 will be skipped when tracking
  54. Min := 1;
  55. Max := 15;
  56. LineSize := 1;
  57. PageSize := 1;
  58. end;
  59.  
  60. procedure TTrackBar.CreateParams(var Params: TCreateParams);
  61. begin
  62. inherited;
  63. // remove automatic ticks so that we don't have ticks at 3 and 5..7
  64. Params.Style := Params.Style and not TBS_AUTOTICKS;
  65. end;
  66.  
  67. procedure TTrackBar.CreateWnd;
  68. begin
  69. inherited;
  70. // first and last tick not required
  71. SetTick(2); // 250
  72. SetTick(4); // 500
  73. SetTick(8); // 1000
  74. SetTick(9); // 2000
  75. SetTick(10);
  76. SetTick(11);
  77. SetTick(12);
  78. SetTick(13);
  79. SetTick(14); // 7000
  80. end;
  81.  
  82. procedure TTrackBar.CNHscroll(var Message: TCNHScroll);
  83. var
  84. Pos: Integer;
  85. begin
  86. // prevent jumping back and forth while thumb tracking,do not slide to the
  87. // next tick until a threshold is passed
  88. if Message.ScrollCode = SB_THUMBTRACK then begin
  89. case Message.Pos of
  90. 5: SendMessage(Handle,TBM_SETPOS,4);
  91. 6,7: SendMessage(Handle,8);
  92. end;
  93. end;
  94.  
  95. // for line and page and rest of the scrolling,skip certain ticks
  96. Pos := SendMessage(Handle,TBM_GETPOS,0);
  97. if Pos > Position then // compare with prevIoUs position
  98. case Pos of
  99. 3: SendMessage(Handle,4);
  100. 5..7: SendMessage(Handle,8);
  101. end;
  102. if Pos < Position then
  103. case Pos of
  104. 3: SendMessage(Handle,2);
  105. 5..7: SendMessage(Handle,4);
  106. end;
  107.  
  108. inherited;
  109. end;
  110.  
  111. end.

如果需要,垂直实施将是相似的.这不是一个完整的产品,只是一个模仿上述控件的行为的审判.

猜你在找的Delphi相关文章