ScrollBox滚动条的问题,ScrollBox怎么支持鼠标的滚轮啊(100分)

  • 主题发起人 主题发起人 芙蓉弟弟
  • 开始时间 开始时间

芙蓉弟弟

Unregistered / Unconfirmed
GUEST, unregistred user!
ScrollBox1怎么支持鼠标的滚轮啊 ,我这个是delphi7,有解决办法吗?
还有就是ScrollBox1的拖动滚动条效果,只有当拖动放开时 才跳到对应的位置,我希望能在拖动滚动条的时候,ScrollBox1里的内容也能让我看到在随滚动条动,知道的高手帮帮忙
 
第一个....需要重新封装一遍ScrollBox,支持WM_MOUSEWHEEL消息
不过,我不明白,如果既有横向滚动又有纵向滚动,你的滚轮应该向哪个方向滚动呢?
在消息里这样判断
procedure xxx.WMMouseWheel(var Message: TWMMouseWheel);
begin
with Messagedo
begin
{只写了纵向滚动,因为无法判断是横向还是纵向}
if WheelDelta>0 then
{向上滚动}
ScrollBy(0,i) {这里i是你一次要滚动的像素,这里只是个象征性的值,具体如何计算要看你重新封装的控件滚动的Position值}
else
if WheelDelta<0 then
{向下滚动}
ScrollBy(0,i);
end;
inherited;
end;

如果只是要纵向滚动....我到是有个自己改过的控件,取消了拖动条的,支持鼠标消息
拖动条很麻烦....又要计算位置...
 
谢谢你 不过我没用你的方法 分给你了
 
哦.......我再补充一下吧,刚翻代码发现了以前写过这样的代码,能够满足你的要求....汗....呵呵,把代码贴出来

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
ScrollBox1: TScrollBox;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
Panel5: TPanel;
ScrollBar1: TScrollBar;
Button3: TButton;
procedure FormShow(Sender: TObject);
procedure ScrollBar1Scroll(Sender: TObject;
ScrollCode: TScrollCode;
var ScrollPos: Integer);
procedure ScrollBox1MouseWheel(Sender: TObject;
Shift: TShiftState;
WheelDelta: Integer;
MousePos: TPoint;
var Handled: Boolean);
procedure ScrollBox1MouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
private
{ Private declarations }
ssmax,sspot:integer;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormShow(Sender: TObject);
var
i,vh,xh,m:integer;
begin
vh:=0;
xh:=0;
for i:=0 to ScrollBox1.ControlCount-1do
begin
m:=ScrollBox1.Controls.BoundsRect.Bottom;
if vh<m then
vh:=m;
if xh>ScrollBox1.Controls.Top then
xh:=ScrollBox1.Controls.Top;
end;
ScrollBar1.Max:=vh;
ScrollBar1.Position:=xh;
ScrollBar1.PageSize:=ScrollBox1.ClientHeight;

ssmax:=vh-ScrollBox1.ClientHeight;
sspot:=xh;
end;

procedure TForm1.ScrollBar1Scroll(Sender: TObject;
ScrollCode: TScrollCode;
var ScrollPos: Integer);
begin
if ScrollPos>=(ScrollBar1.Max-ScrollBar1.PageSize) then
ScrollPos:=ScrollBar1.Max-ScrollBar1.PageSize;
ScrollBox1.ScrollBy(0,ScrollBar1.Position-ScrollPos);
sspot:=ScrollBar1.Position;
Caption:=inttostr(sspot);
end;

procedure TForm1.ScrollBox1MouseWheel(Sender: TObject;
Shift: TShiftState;
WheelDelta: Integer;
MousePos: TPoint;
var Handled: Boolean);
begin
if WheelDelta>0 then
{向上滚动}
begin
if sspot-10<=0 then
begin
ScrollBox1.ScrollBy(0,sspot);
sspot:=0;
end
else
begin
ScrollBox1.ScrollBy(0,10);
dec(sspot,10);
end;
end
else
if WheelDelta<0 then
{向下滚动}
begin
if sspot+10>=ssmax then
begin
ScrollBox1.ScrollBy(0,sspot-ssmax);
sspot:=ssmax;
end
else
begin
ScrollBox1.ScrollBy(0,-10);
inc(sspot,10);
end;
end;
ScrollBar1.Position:=sspot;
end;

procedure TForm1.ScrollBox1MouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
SCROLLBOX1.SetFocus;
end;

end.
 
后退
顶部