如何在 Delphi 2010 中的 TTrackBar 上设置自定义刻度?

发布于 2024-08-22 23:18:15 字数 426 浏览 11 评论 0原文

我尝试将刻度样式设置为 tsManual,将最小和最大位置分别设置为 1 和 100,并在 9、19、79 和 89 添加刻度,除了控件自动显示的默认第一个和最后一个刻度之外,根本不显示任何刻度。我尝试了其他值,但没有显示任何值。我的代码是:

TrackBar1.TickStyle := tsManual;
TrackBar1.Min := 1;
TrackBar1.Max := 100;
TrackBar1.SetTick( 9 );
TrackBar1.SetTick( 19 );
TrackBar1.SetTick( 79 );
TrackBar1.SetTick( 89 );

有什么建议吗?我确信我遗漏了一个重要的细节,而且文档也非常稀疏。这是 Delphi 2010 中的一个新的空 VCL Forms 项目,更新为 4。

提前谢谢您。

I tried to set the tick style to tsManual, the min and max position to 1 and 100 respectively and add ticks at 9, 19, 79 and 89 and no ticks are shown at all except the detault first and last which the control automatically shows. I tried other values and none are ever shown. My code is:

TrackBar1.TickStyle := tsManual;
TrackBar1.Min := 1;
TrackBar1.Max := 100;
TrackBar1.SetTick( 9 );
TrackBar1.SetTick( 19 );
TrackBar1.SetTick( 79 );
TrackBar1.SetTick( 89 );

Any suggestions? I'm sure I'm missing an important detail, and the documentation is pretty sparse. This is on a new empty VCL Forms project in Delphi 2010 with update 4.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

林空鹿饮溪 2024-08-29 23:18:15

如果当前未分配 Handle 属性,则 TTrackBar.SetTick() 不会发送 TBM_SETTIC 消息:

procedure TTrackBar.SetTick(Value: Integer);
begin
  if HandleAllocated then // <-- here
    SendMessage(Handle, TBM_SETTIC, 0, Value);
end;

直到第一次读取 Handle 属性(而不是在最初创建组件时)才会分配窗口句柄。因此,在调用 SetTick() 之前调用 HandleNeeded():

TrackBar1.TickStyle := tsManual; 
TrackBar1.Min := 1; 
TrackBar1.Max := 100; 
TrackBar1.HandleNeeded; // <-- here 
TrackBar1.SetTick( 9 ); 
TrackBar1.SetTick( 19 ); 
TrackBar1.SetTick( 79 ); 
TrackBar1.SetTick( 89 );

TTrackBar.SetTick() does not send the TBM_SETTIC message if the Handle property is currently unassigned:

procedure TTrackBar.SetTick(Value: Integer);
begin
  if HandleAllocated then // <-- here
    SendMessage(Handle, TBM_SETTIC, 0, Value);
end;

The window handle does not get allocated until the Handle property is read for the first time, not when the component is initially created. As such, call HandleNeeded() before calling SetTick():

TrackBar1.TickStyle := tsManual; 
TrackBar1.Min := 1; 
TrackBar1.Max := 100; 
TrackBar1.HandleNeeded; // <-- here 
TrackBar1.SetTick( 9 ); 
TrackBar1.SetTick( 19 ); 
TrackBar1.SetTick( 79 ); 
TrackBar1.SetTick( 89 );
如梦 2024-08-29 23:18:15

我不知道为什么过程 TrackBar1.SetTick 不起作用,但如果您以与过程相同的方式发送消息,它将起作用。您需要将单位 CommCtrl 添加到您的 use 子句中以解析 TBM_SETTIC,如图所示...

implementation

Uses CommCtrl;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  TrackBar1.TickStyle := tsManual;
  TrackBar1.Min := 0;
  TrackBar1.Max := 100;
  SendMessage(TrackBar1.Handle, TBM_SETTIC, 0, 9);
  SendMessage(TrackBar1.Handle, TBM_SETTIC, 0, 19);
  SendMessage(TrackBar1.Handle, TBM_SETTIC, 0, 79);
  SendMessage(TrackBar1.Handle, TBM_SETTIC, 0, 89);
end;

希望这会有所帮助!

I don't know why the procedure TrackBar1.SetTick does not work but if you SendMessage the same way the procedure does it will work. You will need to add the unit CommCtrl to your uses clause to resolve TBM_SETTIC as shown...

implementation

Uses CommCtrl;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  TrackBar1.TickStyle := tsManual;
  TrackBar1.Min := 0;
  TrackBar1.Max := 100;
  SendMessage(TrackBar1.Handle, TBM_SETTIC, 0, 9);
  SendMessage(TrackBar1.Handle, TBM_SETTIC, 0, 19);
  SendMessage(TrackBar1.Handle, TBM_SETTIC, 0, 79);
  SendMessage(TrackBar1.Handle, TBM_SETTIC, 0, 89);
end;

Hope this helps!

ぃ弥猫深巷。 2024-08-29 23:18:15

除了 handle 已准备就绪且 TickStyle = tsManual 之外,Frequency 属性还必须设置为多个或,更容易,到 1。

Besides the handle being ready and the TickStyle = tsManual, the frequency property must be set to a multiple or, more easily, to 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文