ある点が指定領域内かどうかの判断          <TOP>


ある点が指定した領域内にあるかどうかを判断します。

PtInRect ある点が指定領域内にあるかどうかを判断

GetCursorPos マウスポインタの現在位置に相当するスクリーン座標を取得

 

カーソルが設定領域内にある時「範囲内」、設定外にあるとき「範囲外」と、スクリーン座標とともに表示されます。

 

 

'================================================================
'= 指定した点が指定領域内かどうかの判断
'=    (PtInRect.bas)
'================================================================
#include "Windows.bi"

Type POINTAPI
    x As Long
    y As Long
End Type

Type RECT
    Left   As Long
    Top    As Long
    Right  As Long
    Bottom As Long
End Type

' ある点が指定の矩形領域内にあるかどうかを判定
Declare Function Api_PtInRect& Lib "user32" Alias "PtInRect" (lpRect As RECT, ByVal x&, ByVal y&)

' マウスカーソル( マウスポインタ)の現在の位置に相当するスクリーン座標を取得
Declare Function Api_GetCursorPos& Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI)

Var Shared Text(2) As Object
Var Shared Timer1 As Object

For i = 0 To 2
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1))) : Text(i).SetFontSize 14
Next
Timer1.Attach GetDlgItem("Timer1")

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Var txt As String
    Var CrLf As String

    Crlf = Chr$(13, 10)
    txt = txt & "設定範囲" & CrLf
    txt = txt & "rc.Left   = 100" & CrLf
    txt = txt & "rc.Top    = 100" & CrLf
    txt = txt & "rc.Right  = 600" & CrLf
    txt = txt & "rc.Bottom = 400"

    Text(0).SetWindowText txt

    Timer1.SetInterval 10
    Timer1.Enable -1
End Sub

'================================================================
'=
'================================================================
Declare Sub Timer1_Timer edecl ()
Sub Timer1_Timer()
    Var pt As POINTAPI
    Var rc As RECT
    Var Ret As Long

    rc.Left = 100
    rc.Top = 100
    rc.Right = 600
    rc.Bottom = 400

    Ret = Api_GetCursorPos(pt)
    Text(1).SetWindowText Format$(pt.x, "####") & "/" & Format$(pt.y, "####")

    If Api_PtInRect(rc, pt.x, pt.y) = 1 Then
        Text(2).SetFontBold -1
        Text(2).SetWindowText "範囲内"
    Else
        Text(2).SetFontBold 0
        Text(2).SetWindowText "範囲外" 
    End If
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End