在ListBox中有个WindowProc过程,是个处理消息的方法,本程序就是基于此过程实现的。首先在程序运行时保存WindowProc入口地址,然后用自己的过程处理方法覆盖原来的入口地址,再在程序中调用原来的处理过程,判断ListBox的滚动消息,在其中加上自己的事件处理,完成。
以下是源码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
lbl1: TLabel;
lbl2: TLabel;
procedure FormCreate(Sender: TObject);
private
procedure myProc(var Message: TMessage);
procedure myWndProc(var message: TMessage);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
OldProc: TWndMethod;
p: TPoint;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
OldProc := ListBox1.WindowProc;
ListBox1.WindowProc := myProc;
for i:= 0 to 200 do
ListBox1.Items.Add(IntToStr(i));
end;
procedure TForm1.myProc(var Message: TMessage);
begin
OldProc(Message);
if ((Message.Msg = WM_VScroll) and (Message.WParamLo = SB_EndScroll))
then lbl1.Caption := IntToStr(ListBox1.ItemAtPos(p, True));
end;
end.