vb 获取鼠标在窗体内的相对坐标
发布网友
发布时间:2024-10-21 03:43
我来回答
共2个回答
热心网友
时间:2024-11-30 19:15
Private Type POINTAPI
X As Long
Y As Long
End Type
Dim MousePos As POINTAPI
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
GetCursorPos MousePos
newx = MousePos.X * Screen.TwipsPerPixelX
newy = MousePos.Y * Screen.TwipsPerPixelY - 300
Label1 = newx
Label2 = newy
'窗体x,y
labctx = Form1.Left
labcty = Form1.Top
If Not (newx >= Form1.Left And newx <= Form1.Left + Form1.Width And newy >= Form1.Top And newy <= Form1.Top + Form1.Height) Then
MsgBox "你移出窗口了"
Timer1.Enabled = False
End If
End Sub
Form1 窗体上添加
lable 控件 lable1 ,lable2 (鼠标x,y)
labctx,labcty (窗体x,y)
timer 控件 timer1
command 控件 command1
热心网友
时间:2024-11-30 19:11
有个笨方法,试试:(注意表单整大点)
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If X < 100 Or Y < 100 Then
MsgBox "out"
End If
If X > Me.ScaleWidth - 100 Then
MsgBox "out"
End If
If Y > Me.ScaleHeight - 100 Then
MsgBox "out"
End If
End Sub