子类化技术,例,捕获ListBox的WM_VSCROLL消息:
TfrmMain = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
end;
implementation
var
ListBoxOldWProc : TFNWndProc;
function ListBoxNewWndProc(Handle: HWND; Msg: Integer; wParam, lParam: Longint):
Longint; stdcall;
begin
Result := CallWindowProc(ListBoxOldWProc, Handle, Msg, wParam, lParam);
if Msg=WM_VSCROLL then beep;//这里让它发声
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
inherited;
ListBoxOldWProc := TFNWndProc(SetWindowLong(ListBox1.Handle, GWL_WNDPROC,
Longint(@ListBoxNewWndProc)));
end;
procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ListBoxOldWProc<>nil then //还原,别忘了
SetWindowLong(ListBox1.Handle, GWL_WNDPROC,
Longint(ListBoxOldWProc));
inherited;
end;