根据X,Y左标判断. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)End Subx, y 返回一个指定鼠标指针当前位置的数。x 和 y 的值所表示的总是通过该对象 ScaleHeight, ScaleWidth, ScaleLeft, 和ScaleTop 属性所建立的坐标系统的方式。
我没有说清楚,如何单击左键来确定x,y的位置,x,y的初始值为(0,0) 谢谢了!
给以个好用的类 class1Option Explicit DefLng A-ZPrivate Type POINTAPI X As Long Y As Long End Type Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private CurVisible As Boolean Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long Private Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long Private Declare Function ClipCursor Lib "user32" (lpRect As RECT) As Long Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As LongPublic Property Get X() As LongDim tmpPoint As POINTAPI Call GetCursorPos(tmpPoint) X = tmpPoint.XEnd PropertyPublic Property Let X(ByVal vNewValue As Long)Call SetCursorPos(vNewValue, Y)End PropertyPublic Property Get Y() As LongDim tmpPoint As POINTAPI Call GetCursorPos(tmpPoint) Y = tmpPoint.YEnd PropertyPublic Property Let Y(ByVal vNewValue As Long)Call SetCursorPos(X, vNewValue)End PropertyPublic Sub SnapTo(ctl As Control)'Snaps the cursor to the center of 'a given control.Dim pnt As POINTAPI Dim xx As Long Dim yy As Longpnt.X = pnt.Y = 0 'Get Left-Top corner of control Call ClientToScreen(ctl.hWnd, pnt) xx = pnt.X + (ctl.Width \ 2) yy = pnt.Y + (ctl.Height \ 2) 'xx = pnt.X + ctl.Width / (2 * (Screen.ActiveForm.Left + ctl.Left) / pnt.X) 'yy = pnt.Y + ctl.Height / (2 * (Screen.ActiveForm.Top + ctl.Top) / pnt.Y) Call SetCursorPos(xx, yy)End SubPublic Sub ClipTo(ToCtl As Object)On Error Resume NextDim tmpRect As RECT Dim pt As POINTAPIWith ToCtl If TypeOf ToCtl Is Form Then tmpRect.Left = (.Left \ Screen.TwipsPerPixelX) tmpRect.Top = (.Top \ Screen.TwipsPerPixelY) tmpRect.Right = (.Left + .Width) \ Screen.TwipsPerPixelX tmpRect.Bottom = (.Top + .Height) \ Screen.TwipsPerPixelY ElseIf TypeOf ToCtl Is Screen Then tmpRect.Left = 0 tmpRect.Top = 0 tmpRect.Right = (.Width \ Screen.TwipsPerPixelX) tmpRect.Bottom = (.Height \ Screen.TwipsPerPixelY) Else pt.X = 0 pt.Y = 0 Call ClientToScreen(.hWnd, pt) tmpRect.Left = pt.X tmpRect.Top = pt.Y pt.X = .Width pt.Y = .Height Call ClientToScreen(.hWnd, pt) tmpRect.Bottom = pt.Y tmpRect.Right = pt.X End If
Call ClipCursor(tmpRect)End WithEnd SubPrivate Sub Class_Initialize() CurVisible = True End SubPublic Property Get Visible() As BooleanVisible = CurVisibleEnd PropertyPublic Property Let Visible(ByVal vNewValue As Boolean)CurVisible = vNewValue Call ShowCursor(CurVisible)End Property在窗体: Dim Cursor As cCursor Cursor.X = 你要的x坐标位置 Cursor.Y = 你要的y坐标位置
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)End Subx, y 返回一个指定鼠标指针当前位置的数。x 和 y 的值所表示的总是通过该对象 ScaleHeight, ScaleWidth, ScaleLeft, 和ScaleTop 属性所建立的坐标系统的方式。
谢谢了!
class1Option Explicit
DefLng A-ZPrivate Type POINTAPI
X As Long
Y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private CurVisible As Boolean
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function ClipCursor Lib "user32" (lpRect As RECT) As Long
Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As LongPublic Property Get X() As LongDim tmpPoint As POINTAPI
Call GetCursorPos(tmpPoint)
X = tmpPoint.XEnd PropertyPublic Property Let X(ByVal vNewValue As Long)Call SetCursorPos(vNewValue, Y)End PropertyPublic Property Get Y() As LongDim tmpPoint As POINTAPI
Call GetCursorPos(tmpPoint)
Y = tmpPoint.YEnd PropertyPublic Property Let Y(ByVal vNewValue As Long)Call SetCursorPos(X, vNewValue)End PropertyPublic Sub SnapTo(ctl As Control)'Snaps the cursor to the center of
'a given control.Dim pnt As POINTAPI
Dim xx As Long
Dim yy As Longpnt.X = pnt.Y = 0
'Get Left-Top corner of control
Call ClientToScreen(ctl.hWnd, pnt)
xx = pnt.X + (ctl.Width \ 2)
yy = pnt.Y + (ctl.Height \ 2)
'xx = pnt.X + ctl.Width / (2 * (Screen.ActiveForm.Left + ctl.Left) / pnt.X)
'yy = pnt.Y + ctl.Height / (2 * (Screen.ActiveForm.Top + ctl.Top) / pnt.Y)
Call SetCursorPos(xx, yy)End SubPublic Sub ClipTo(ToCtl As Object)On Error Resume NextDim tmpRect As RECT
Dim pt As POINTAPIWith ToCtl If TypeOf ToCtl Is Form Then
tmpRect.Left = (.Left \ Screen.TwipsPerPixelX)
tmpRect.Top = (.Top \ Screen.TwipsPerPixelY)
tmpRect.Right = (.Left + .Width) \ Screen.TwipsPerPixelX
tmpRect.Bottom = (.Top + .Height) \ Screen.TwipsPerPixelY
ElseIf TypeOf ToCtl Is Screen Then
tmpRect.Left = 0
tmpRect.Top = 0
tmpRect.Right = (.Width \ Screen.TwipsPerPixelX)
tmpRect.Bottom = (.Height \ Screen.TwipsPerPixelY)
Else
pt.X = 0
pt.Y = 0
Call ClientToScreen(.hWnd, pt)
tmpRect.Left = pt.X
tmpRect.Top = pt.Y
pt.X = .Width
pt.Y = .Height
Call ClientToScreen(.hWnd, pt)
tmpRect.Bottom = pt.Y
tmpRect.Right = pt.X
End If
Call ClipCursor(tmpRect)End WithEnd SubPrivate Sub Class_Initialize()
CurVisible = True
End SubPublic Property Get Visible() As BooleanVisible = CurVisibleEnd PropertyPublic Property Let Visible(ByVal vNewValue As Boolean)CurVisible = vNewValue
Call ShowCursor(CurVisible)End Property在窗体:
Dim Cursor As cCursor
Cursor.X = 你要的x坐标位置
Cursor.Y = 你要的y坐标位置
强!!!!!