How to set custom ticks on a TTrackBar in Delphi 2010?
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 automat开发者_如何学运维ically 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.
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 );
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!
Besides the handle
being ready and the TickStyle
= tsManual
, the frequency
property must be set to a multiple or, more easily, to 1.
精彩评论