请问滚动条同步问题(50分)

K

karnor

Unregistered / Unconfirmed
GUEST, unregistred user!
请问我现在有两个ListView,其中的内容一一对应,现在要求其中一个拉动垂直滚动条时
另外一个也同步地将滚动条定位到相应的位置,请问具体怎样实现.
 
以下代码win2k+delphi6上测试通过,应该符合你的要求:)
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ImgList,commctrl;

type
TForm1 = class(TForm)
ListView1: TListView;
ListView2: TListView;
procedure FormCreate(Sender: TObject);
private
FClientInstance, FPrevClientProc : TFarProc;
procedure ClientWndProc(var Message: TMessage);

public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.ClientWndProc(var Message: TMessage);
var
i:integer;
begin
if (Message.Msg=WM_VSCROLL) then //垂直滚动同步
begin
i:=GetScrollPos(ListView1.Handle,SB_VERT);
SetScrollPos(ListView2.Handle,SB_VERT, i,true);
end;
{
if (Message.Msg=WM_HSCROLL) then //// 水平滚动同步
begin
i:=GetScrollPos(ListView1.Handle,SB_HORZ);
SetScrollPos(ListView2.Handle,SB_HORZ, i,true);
end;
}
Message.Result := CallWindowProc(FPrevClientProc,ListView1.Handle, Message.Msg, Message.wParam, Message.lParam);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
FClientInstance := MakeObjectInstance(ClientWndProc);
FPrevClientProc := Pointer(GetWindowLong(ListView1.Handle, GWL_WNDPROC));
SetWindowLong(ListView1.Handle, GWL_WNDPROC, LongInt(FClientInstance));
end;

end.
 
但是这样的话另外一个ListView中的内容不跟滚动条变动
 
将ClientWndProc修改如下:
procedure TForm1.ClientWndProc(var Message: TMessage);
var
begin
if (Message.Msg=WM_VSCROLL) then //垂直滚动同步
begin
SendMessage(ListView2.Handle,Message.Msg,Message.WParam,Message.LParam);
end;
Message.Result := CallWindowProc(FPrevClientProc,ListView1.Handle, Message.Msg, Message.wParam, Message.lParam);
end;
 
顶部