gpt4 book ai didi

delphi - 两个 TrackBar 镜像

转载 作者:行者123 更新时间:2023-12-03 15:00:58 26 4
gpt4 key购买 nike

我想知道如何使我的第二个 trackbar.position 镜像与 trackbar1.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;

我可能做得太过分了。也许有更简单的方法。我更喜欢更简单的方法。谢谢,

最佳答案

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;

 

替代开始(肯·怀特建议和我的评论;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;

关于delphi - 两个 TrackBar 镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10615796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com