我正在向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