我想知道如何在trackbar1.position的相反方向上制作我的第二个trackbar.position镜像。 例如。 范围从1到100。
所以当TrackBar1.Position := 2
时,trackbar2.Position := 99
无论轨道轨道走哪条路,我都想以相反的方向镜像。
到目前为止我的代码:(对使用密钥不感兴趣),只是鼠标交互。
Direction : string;
Skip : boolean;
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
if TrackBar1.Position = TrackBar2.Position then
begin
if Direction = 'up' then TrackBar2.Position := TrackBar2.Position + 1;
if Direction = 'down' then TrackBar2.Position := TrackBar2.Position - 1;
skip := true;
end;
if TrackBar1.Position < TrackBar2.Position then
begin
if skip = false then
begin
TrackBar2.Position := TrackBar2.Position - 1;
Direction := 'down';
end;
end
else
begin
if skip = false then
begin
TrackBar2.Position := TrackBar2.Position + 1;
Direction := 'up';
end;
end;
end;
我可能过度了。也许有一种更简单的方法。我更喜欢简单的方法。 谢谢,
本
答案 0 :(得分:5)
2个跟踪栏OnChange
事件与此代码相关联:
procedure TForm1.TrackBarChange(Sender: TObject);
var
tbSource, tbTarget: TTrackBar;
begin
if Sender = TrackBar1 then // Check the Trackbar which triggers the event
begin
tbSource := TrackBar1;
tbTarget := TrackBar2;
end
else
begin
tbSource := TrackBar2;
tbTarget := TrackBar1;
end;
tbTarget.OnChange := nil; // disable the event on the other trackbar
tbTarget.Position := tbSource.Max + tbSource.Min - tbSource.Position; // set the position on the other trackbar
tbTarget.OnChange := TrackBarChange; // define the event back to the other trackbar
// Call a function or whatever after this line if you need to do something when it changes
// lbl1.Caption := IntToStr(TrackBar1.Position);
// lbl2.Caption := IntToStr(TrackBar2.Position);
end;
替代开始(Ken White建议和我的评论; o)):
procedure TForm1.TrackBarChange(Sender: TObject);
var
tbSource, tbTarget: TTrackBar;
begin
// if Sender is TTrackBar then // is it called 'from' a trackbar?
// begin
tbSource := TTrackBar(Sender); // Set the source
if tbSource = TrackBar1 then // Check the Trackbar which triggers the event
tbTarget := TrackBar2
else
tbTarget := TrackBar1;
tbTarget.OnChange := nil; // disable the event on the other trackbar
tbTarget.Position := tbSource.Max + tbSource.Min - tbSource.Position; // set the position on the other trackbar
tbTarget.OnChange := TrackBarChange; // define the event back to the other trackbar
// Call a function or whatever after this line if you need to do something when it changes
// lbl1.Caption := IntToStr(TrackBar1.Position);
// lbl2.Caption := IntToStr(TrackBar2.Position);
// end;
end;