createtion_zy, 我当初做了一个小控件。开始也是按你这么想。只是效果不好
鼠标被管死了很难受。后来才改用钩子。
Public
Constructor Create(AOwner : TComponent); Override;
destructor destroy;Override;
Procedure DragOver(Source : TObject; X, Y : Integer;
State : TDragState; Var Accept : Boolean); Override;
Procedure DragDrop(Source : TObject; X, Y : Integer); Override;
procedure StartDrag(Sender: TObject; var DragObject: TDragObject);
procedure EndDrag(Sender, Target: TObject; X, Y: Integer);
Function CallBackProc(Code:integer;wParam:wParam;Lparam:Lparam):Longint; {$IFDEF Win32} stdCall; {$ELSE}Export;{$ENDIF}
var
DragImage : THiStarImage;
Hwd : THandle;
Implementation
Function CallBackProc(Code:integer;wParam:wParam;Lparam:Lparam):Longint;
var
CursorPos : TPoint;
WndRect : TRect;
Begin
if DragImage = Nil then
begin
Result := 0;
Exit;
end;
If DragImage.Dragging then
Begin
// 向上滚动
Windows.GetCursorPos(CursorPos);
GetWindowRect(DragImage.Parent.Handle, Wndrect);
if (CursorPos.y <= WndRect.Top+20) then
if TScrollBox(DragImage.Owner).VertScrollBar.Position > 0 then
TScrollBox(DragImage.Owner).VertScrollBar.Position := TScrollBox(DragImage.Owner).VertScrollBar.Position - 20;
// 向下滚动
if (CursorPos.y >= WndRect.Bottom-20) then
if TScrollBox(DragImage.Owner).VertScrollBar.Position < TScrollBox(DragImage.Owner).VertScrollBar.Range then
TScrollBox(DragImage.Owner).VertScrollBar.Position := TScrollBox(DragImage.Owner).VertScrollBar.Position + 20;
End;
Result := CallNextHookEx(Hwd, Code, wParam, lParam);
End;
procedure THiStarImage.StartDrag(Sender: TObject; var DragObject: TDragObject);
Begin
DragImage := Self;
Hwd:=SetWindowsHookEx(WH_Mouse,CallBackProc,hInstance,0);
End;
procedure THiStarImage.EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
unHookWindowsHookEx(hwd);
DragImage := Nil;
end;
这个是很久以前写的了(Delphi2.0, 3.0). 大致就是这个意思,应该没问题。