是PtInRect吧: 【VB声明】 Private Declare Function PtInRect Lib "user32" Alias "PtInRect" (lpRect As RECT, pt As POINTAPI) As Long【说明】 这个函数判断指定的点是否位于矩形lpRect内部 【返回值】 Long,非零表示成功,零表示失败。会设置GetLastError 【备注】 如点位于矩形四边之内,或矩形的顶部或左侧边线上,则认为它在矩形内部。如位于矩形的右侧或底部边线,则不认为它在矩形内部【参数表】 lpRect --------- RECT,欲检查的矩形 pt ------------- POINTAPI,欲判断的点实例:Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Declare Function PtInRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long Private Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long Private Declare Function CreateEllipticRgnIndirect Lib "gdi32" (lpRect As RECT) As Long Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long Private Sub Form_Load() Dim mRGN As Long, R As RECT, x As Long, y As Long 'Set the graphical mode to persistent Me.AutoRedraw = True 'Set the rectangle's values SetRect R, 0, 0, 50, 50 'Create an elliptical region mRGN = CreateEllipticRgnIndirect(R) For x = R.Left To R.Right For y = R.Top To R.Bottom 'If the point is in the region, draw a green pixel If PtInRegion(mRGN, x, y) <> 0 Then 'Draw a green pixel SetPixelV Me.hdc, x, y, vbGreen ElseIf PtInRect(R, x, y) <> 0 Then 'Draw a red pixel SetPixelV Me.hdc, x, y, vbRed End If Next y Next x 'delete our region DeleteObject mRGN End Sub
错了! 不是 Private Declare Function PtInRect Lib "user32" Alias "PtInRect" (lpRect As RECT, pt As POINTAPI) As Long 是 Private Declare Function PtInRect Lib "user32" Alias "PtInRect" (lpRect As RECT, ByVal ptX As Long, ByVal ptY As Long) As Long
【VB声明】
Private Declare Function PtInRect Lib "user32" Alias "PtInRect" (lpRect As RECT, pt As POINTAPI) As Long【说明】
这个函数判断指定的点是否位于矩形lpRect内部 【返回值】
Long,非零表示成功,零表示失败。会设置GetLastError 【备注】
如点位于矩形四边之内,或矩形的顶部或左侧边线上,则认为它在矩形内部。如位于矩形的右侧或底部边线,则不认为它在矩形内部【参数表】
lpRect --------- RECT,欲检查的矩形 pt ------------- POINTAPI,欲判断的点实例:Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function PtInRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function CreateEllipticRgnIndirect Lib "gdi32" (lpRect As RECT) As Long
Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Sub Form_Load()
Dim mRGN As Long, R As RECT, x As Long, y As Long
'Set the graphical mode to persistent
Me.AutoRedraw = True
'Set the rectangle's values
SetRect R, 0, 0, 50, 50
'Create an elliptical region
mRGN = CreateEllipticRgnIndirect(R)
For x = R.Left To R.Right
For y = R.Top To R.Bottom
'If the point is in the region, draw a green pixel
If PtInRegion(mRGN, x, y) <> 0 Then
'Draw a green pixel
SetPixelV Me.hdc, x, y, vbGreen
ElseIf PtInRect(R, x, y) <> 0 Then
'Draw a red pixel
SetPixelV Me.hdc, x, y, vbRed
End If
Next y
Next x
'delete our region
DeleteObject mRGN
End Sub
不是
Private Declare Function PtInRect Lib "user32" Alias "PtInRect" (lpRect As RECT, pt As POINTAPI) As Long
是
Private Declare Function PtInRect Lib "user32" Alias "PtInRect" (lpRect As RECT, ByVal ptX As Long, ByVal ptY As Long) As Long
rect不是region!
你自己定义一个rect,就不会出错了!