我正在向TScrollBox添加對鼠標滾輪移動的支持(使用FormMouseWheel過程),並且需要確定鼠標是否位於組件內部。如何確定鼠標光標是否位於控件中
基本上我需要確定鼠標是否在TScrollBox內,以便我相應地處理滾動代碼。
任何想法如何做到這一點?
編輯:下面的代碼(包括回答這個問題),因爲它可能會幫助別人:
procedure TForm1.FormMouseWheel(Sender: TObject;
Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
var Handled: Boolean);
var
Msg: Cardinal;
Code: Cardinal;
I, ScrollLines: Integer;
ScrollBoxCursosPos: TPoint;
begin
//position of the mouse cursor related to TScrollBox
ScrollBoxCursosPos := ScrollBox1.ScreenToClient(Mouse.CursorPos);
if (PtInRect(ScrollBox1.ClientRect, ScrollBoxCursosPos)) then
begin
Handled := True;
If ssShift In Shift Then
msg := WM_HSCROLL
Else
msg := WM_VSCROLL;
If WheelDelta < 0 Then
code := SB_LINEDOWN
Else
code := SB_LINEUP;
ScrollLines:= Mouse.WheelScrollLines * 3;
for I:= 1 to ScrollLines do
ScrollBox1.Perform(Msg, Code, 0);
ScrollBox1.Perform(Msg, SB_ENDSCROLL, 0);
end;
end;
完美,我已經到達ScreenToClient部分,但不知道我是否獲得了正確的值。謝謝您的幫助。 – smartins 2009-11-12 15:42:25