指定された座標を含むウィンドウのハンドルを取得する(WindowFromPoint)
user32.dll の WindowFromPoint 等を用いて、指定された座標を含むウィンドウのハンドルを取得します。
サンプルのダウンロード
■サンプルの説明

起動すると、こんな画面が表示されます。自アプリケーション・他アプリケーション問わず、マウスポインタの位置にあるハンドルの座標を表示します。
■コード
F0010_WindowFromPoint.vb
Imports System.Runtime.InteropServices ''' <summary> ''' WindowFromPoint(user32.dll) ''' </summary> Partial Public Class F0010_WindowFromPoint ''' <summary> ''' WindowFromPoint(指定された座標を含むウィンドウのハンドルを取得)の宣言 ''' </summary> <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function WindowFromPoint(ByVal lpPoint As POINT) _ As IntPtr End Function ''' <summary> ''' GetCursorPos(マウスの座標を取得)の宣言 ''' </summary> <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function GetCursorPos(ByRef lpPoint As POINT) _ As Boolean End Function ''' <summary> ''' POINT 構造体 ''' </summary> Private Structure POINT Public X As Integer Public Y As Integer End Structure ''' <summary> ''' GetWindowRect(Window の位置等を取得)の宣言 ''' </summary> <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, _ ByRef lpRect As RECT) _ As Boolean End Function ''' <summary> ''' RECT 構造体 ''' </summary> Private Structure RECT Public Left As Integer Public Top As Integer Public Right As Integer Public Bottom As Integer End Structure ''' <summary> ''' ロード時のイベント ''' </summary> Private Sub F0010_WindowFromPoint_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.Text = "F0010_WindowFromPoint" Me.Label1.Text = "" Me.Label1.BackColor = Color.White Me.Label2.Text = "" Me.Label2.BackColor = Color.White Me.Timer1.Interval = 100 Me.Timer1.Start() End Sub ''' <summary> ''' 指定したタイマの間隔が経過した時のイベント ''' </summary> Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim lpPoint As POINT 'マウス座標を取得 GetCursorPos(lpPoint) Me.Label1.Text = "X:" + lpPoint.X.ToString() + " Y:" + lpPoint.Y.ToString() 'マウス座標よりハンドル取得 Dim hwnd As IntPtr hwnd = WindowFromPoint(lpPoint) If hwnd.ToInt32() <= 0 Then 'ハンドル取得失敗 Me.Label2.Text = "" Exit Sub End If '現在のハンドルの座標値を取得 Dim lpRect As RECT GetWindowRect(hwnd, lpRect) Dim buff As System.Text.StringBuilder buff = New System.Text.StringBuilder buff.Append("現在のハンドルの座標値" + ControlChars.NewLine) buff.Append("Top : " + lpRect.Top.ToString() + ControlChars.NewLine) buff.Append("Left : " + lpRect.Left.ToString() + ControlChars.NewLine) buff.Append("Bottom : " + lpRect.Bottom.ToString() + ControlChars.NewLine) buff.Append("Right : " + lpRect.Right.ToString()) Me.Label2.Text = buff.ToString() End Sub End Class