下面是在网上找到的VB获取一个控件的屏幕位置,怎么用运用它呢
GetControlRECT.Bas '////////////////////////////////////////////////////////////////////////////////////////////////////////// 'ProgrammingBy Kejisoft (Http://Hi.Baidu.com/Kejisoft) //
'Date:2011-02-10 //
'Functions: //
' GetDesktopWindowRect //
' 获取一个控件在屏幕用Rect描述的位置 //
' SetCursorToControl //
' 设置鼠标指针到一个控件 'IDE:Microsoft Visual Basic 6.0企业版 // '////////////////////////////////////////////////////////////////////////////////////////////////////////// Declare FunctionGetDesktopWindow Lib "user32" () As Long
Declare Function GetWindowRect Lib"user32" (ByVal Hwnd As Long, lpRect As RECT) As Long
Type RECT
Left AsLong
Top AsLong
Right AsLong
Bottom AsLong
End Type Declare Function GetCursorPosLib "user32" (lpPoint As POINTAPI) As Long
Type POINTAPI
X As Long
Y As Long
End Type Declare Function SetCursorPosLib "user32" (ByVal X As Long, ByVal Y As Long) As Long '/////////////////////////////////////////Functions//////////////////////////////////////////////////////////////// Public Function GetDesktopWindowRect(HwndAs Long, Rct As RECT, MousePos As POINTAPI) As Boolean
Dimexecute As Integer
execute =GetWindowRect(Hwnd, Rct)
GetDesktopWindowRect = IIf(execute = 0, False, True)
GetCursorPos MousePos
End Function Public FunctionSetCursorToControl(ControlHwnd As Long) As Boolean
Dim Rect2As RECT, MousePos2 As POINTAPI
DimControlX As Long, ControlY As Long
SetCursorToControl = GetDesktopWindowRect(ControlHwnd, Rect2, MousePos2)
'计算控件的中心位置坐标
ControlX= Rect2.Left + ((Rect2.Right - Rect2.Left) / 2)
ControlY= Rect2.Top + ((Rect2.Bottom - Rect2.Top) / 2)
SetCursorPos ControlX, ControlY
End Function
说明:本模块的函数可以获取一个控件的屏幕位置,并可以设置鼠标指针到该控件上,另外利用该函数可以实现控件的鼠标MouseOut事件,因此可以利用其制作漂亮的控件(按钮、CheckBox、Option等)。
|