constructor ***.Create() begin // ListView的消息钩子 FNewProc := MakeObjectInstance(SelfListViewProc); FOldProc := Pointer(GetWindowLong(FListView.Handle, GWL_WNDPROC)); SetWindowLong(FListView.Handle, GWL_WNDPROC, LongInt(FNewProc)); end; procedure ***.SelfListViewProc(var Msg: TMessage); begin if Assigned(FOnListViewMsg) then FOnListViewMsg(msg); Msg.Result := CallWindowProc(FOldProc, FListView.Handle, Msg.Msg, Msg.WParam, Msg.LParam); end; destructor ****.Destroy; var i: Integer; begin SetWindowLong(FListView.Handle, GWL_WNDPROC, LongInt(FOldProc)); FreeObjectInstance(FNewProc); end;
但是需要注意的是ListView_SetItemPosition之类的函数可能导致设置的消息回调无效,原因不详,需要在调用这些函数后再次设置
SetWindowLong(FListView.Handle, GWL_WNDPROC, LongInt(FNewProc));