且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检测何时显示 TextBox 自动完成列表

更新时间:2023-02-05 23:12:42

你可以使用EnumThreadWindows 查找所有自动完成的下拉窗口并检查它们是否可见.自动完成下拉窗口类名称为 Auto-Suggest Dropdown.您可以使用 GetClassName 方法获取枚举窗口的类名,然后使用 IsWindowVisible 方法检查窗口是否可见.

You can use EnumThreadWindows to find all auto complete dropdown windows and check if any of them is visible. The Auto-complete dropdown window class name is Auto-Suggest Dropdown. You can use GetClassName method to get class name of enumerated windows and then using IsWindowVisible method check if the window is visible.

示例

在下面的示例中,我在问题中的代码中使用了一个计时器,在计时器的滴答事件中,我检查了是否打开了一个自动完成窗口,我在标题中显示了打开"表格,否则显示关闭":

In below example, I used a timer like you have in your code in the question, and in tick event of the timer, I've checked if there is an auto-complete window open I showed "Open" in title of form, otherwise showed "close":

Delegate Function EnumThreadDelegate(hWnd As IntPtr, lParam As IntPtr) As Boolean

<System.Runtime.InteropServices.DllImport("user32.dll")> _
Shared Function EnumThreadWindows(dwThreadId As Integer, _
    lpfn As EnumThreadDelegate, lParam As IntPtr) As Boolean
End Function

<System.Runtime.InteropServices.DllImport("user32.dll")> _
Shared Function GetClassName(ByVal hWnd As System.IntPtr,
    lpClassName As System.Text.StringBuilder, _
    nMaxCount As Integer) As Integer
End Function

<System.Runtime.InteropServices.DllImport("kernel32.dll")> _
Shared Function GetCurrentThreadId() As Integer
End Function

<System.Runtime.InteropServices.DllImport("user32.dll")> _
Shared Function IsWindowVisible(hWnd As IntPtr) As Boolean
End Function

Const AutoCompleteClassName As String = "Auto-Suggest Dropdown"
Function EnumThreadCallback(hWnd As IntPtr, lParam As IntPtr) As Boolean
    Dim className As New System.Text.StringBuilder("", 256)
    GetClassName(hWnd, className, 256)
    If className.ToString() = AutoCompleteClassName AndAlso IsWindowVisible(hWnd) Then
        AnAutoCOmpleteIsOpen = True
    End If
    Return True
End Function
Dim AnAutoCOmpleteIsOpen As Boolean = False
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    AnAutoCOmpleteIsOpen = False
    EnumThreadWindows(GetCurrentThreadId(), _
        New EnumThreadDelegate(AddressOf Me.EnumThreadCallback), IntPtr.Zero)
    If (AnAutoCOmpleteIsOpen) Then
        Me.Text = "Open"
    Else
        Me.Text = "Close"
    End If
End Sub