VB如何判断鼠标是否在窗体内?

来源:百度知道 编辑:UC知道 时间:2024/05/21 15:55:44
要编这么一个程序:
鼠标移入窗体内,窗体变大,当鼠标移出窗体内,窗体变小

怎么判断鼠标是否在窗体内呢?

如果用Mousemove事件的话,离开窗体就无法判断了。而且应该是鼠标进入窗体时,窗体是变小的(应该是这么理解的吧。。。),如果用Mousemove的话,鼠标在窗体内时,窗体就一直变小了= =

最好方法能简单些,因为是很初级的题目来着。。。
谢谢。不过,有没有不用API的方法呢= =

判断鼠标位置要用api

在窗体上创建一个timer,一个label复制以下代码:

Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Dim x1, y1, x2, y2
Private Sub Form_Load()
Show
x1 = ScaleX(Me.Left, vbTwips, vbPixels)
y1 = ScaleY(Me.Top, vbTwips, vbPixels)
x2 = ScaleX(Me.Width, vbTwips, vbPixels)
y2 = ScaleY(Me.Height, vbTwips, vbPixels)
End Sub

Private Sub Timer1_Timer()
Me.Cls
Dim lpPoint As POINTAPI
GetCursorPos lpPoint
Label1.Caption = "X=" & lpPoint.x & vbNewLine & "Y=" & lpPoint.y
If lpPoint.x > x1 And lpPoint.x < x1 + x2 And lpPoint.y > y1 And lpPoint.y < y1 + y2 Then
Print "鼠标在窗体内"
Else
Print "鼠标在窗体外"
End If
End Sub

不用api恐怕不行,期待能有人帮忙解决!!

先看下你原来画的窗口左上角的顶点坐标是多少,然后就用MOUSE