问题描述
我必须在滚动框的TScrollBox上使用TScrollbar(是的,这似乎是个坏主意,但我必须)。
I have to use TScrollbar on a TScrollBox(Yes,it seems bad idea,but I have to) that scrolls the box.
这就是我所做的:我在框上添加了滚动条,并使用顶部和底部锚点使其保持在正轨,并在ScrollBar的OnScroll事件中添加了以下代码:
This is what I did:I added the scroll on the box ,used Top and Bottom anchors to keep it on track and added this code in the OnScroll event of the ScrollBar:
procedure TForm1.ScrollBar1Scroll(Sender: TObject;
ScrollCode: TScrollCode;
var ScrollPos: Integer);
begin
ScrollBox1.ScrollBy(0,-scrollPos);
end;
它不能正常工作,总是向下滚动。如何使其正常滚动?
It's not working properly.Always scrolls down.How to make it scroll normally?
编辑:请不要建议使用TScrollBox中的条,我必须使用TScrollBar。
EDIT: Please do not suggest using the bars from TScrollBox,I have to use a TScrollBar.
谢谢!
推荐答案
问题是您要给ScrollBox ScrollBar的绝对滚动位置,该位置的预期参数是
The problem is that you're giving the ScrollBox the ScrollBar's absolute scroll position, where the expected parameter is a "delta" or change in position
这是一个快速而又简单的演示解决方案。您可以根据需要通过创建其他后代等来使此清理器更清洁。
Here's a quick-and-dirty demonstration solution. You could make this cleaner by creating a different descendant, etc, depending on your needs.
放入ScrollBox和ScrollBar后,在 interface
部分的表单类型声明上方添加此类型声明:
After dropping in your ScrollBox and ScrollBar, add this type declaration above your form's type declaration in the interface
section:
type
TScrollBar = class(StdCtrls.TScrollBar)
private
OldPos : integer;
end;
现在将其用于ScrollBar的OnScroll事件:
Now use this for the OnScroll event for the ScrollBar:
procedure TForm1.ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode;
var ScrollPos: Integer);
var
Delta : integer;
begin
Delta := ScrollPos - ScrollBar1.OldPos;
ScrollBox1.ScrollBy(0, -Delta);
ScrollBar1.OldPos := ScrollPos;
end;
这对我有用(在WinXP下为D2006)。
This works for me (D2006 under WinXP).
这篇关于如何在滚动条上使用滚动条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!