■No33984に返信(猫まっしぐらさんの記事)
> 環境/言語:[Windows7 VB.net2005] > ポインタ下のアイテムの取得
手元に 2005 が無いので、2017 で書いたコードですが、参考までに。
※High DPI 環境では、マニフェストで <dpiAware>True</dpiAware> を指定。
'参照設定:Accessibility
Imports System.Runtime.InteropServices
Partial Public Class Form1
Private Declare Auto Function AccessibleObjectFromPoint Lib "oleacc" (ByVal pt As Point, <Out, MarshalAs(UnmanagedType.Interface)> ByRef accObj As Accessibility.IAccessible, <Out> ByRef childId As Object) As UInt32
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Timer1.Interval = 1
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Dim xy As Point = Cursor.Position
ListBox1.BeginUpdate()
ListBox1.Items.Clear()
ListBox1.Items.Add("Pos=" & xy.ToString())
Dim accObj As Accessibility.IAccessible = Nothing
Dim varChildId As Object = Nothing
Dim result As UInt32 = AccessibleObjectFromPoint(xy, accObj, varChildId)
If result <> 0UI Then
ListBox1.EndUpdate()
Return
End If
For Each prop As String In New String() {"Name", "Role", "Description", "Help", "State", "Value"}
Dim strItem As String = prop & "="
Try
strItem &= CStr(CallByName(accObj, "acc" & prop, CallType.Get, 0))
Catch ex As Exception
'strItem &= ex.Message
End Try
ListBox1.Items.Add(strItem)
Next
ListBox1.EndUpdate()
If Marshal.IsComObject(accObj) Then
Marshal.ReleaseComObject(accObj)
End If
End Sub
End Class
'参照設定:WindowsBase '参照設定:UIAutomationClient '参照設定:UIAutomationTypes Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick Dim xy As System.Drawing.Point = System.Windows.Forms.Cursor.Position ListBox1.BeginUpdate() ListBox1.Items.Clear() ListBox1.Items.Add("Pos=" & xy.ToString()) Dim xy2 As New System.Windows.Point(CDbl(xy.X), CDbl(xy.Y)) Try Dim elm As System.Windows.Automation.AutomationElement = System.Windows.Automation.AutomationElement.FromPoint(xy2) If elm IsNot Nothing Then ListBox1.Items.Add("Name=" & elm.Current.Name)
Dim ptnObj As Object = Nothing If elm.TryGetCurrentPattern(System.Windows.Automation.ValuePattern.Pattern, ptnObj) Then Dim ptnValue As System.Windows.Automation.ValuePattern = DirectCast(ptnObj, System.Windows.Automation.ValuePattern) ListBox1.Items.Add("Value=" & ptnValue.Current.Value) End If End If Catch End Try ListBox1.EndUpdate() End Sub