0

我想检查活动窗口句柄是否是密码框。

这个函数返回给我活动窗口的活动控制句柄:

Imports System.Runtime.InteropServices
Public Class FormMain
    Inherits Form

    Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr

    Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal ProcessId As IntPtr) As IntPtr

    Private Declare Function AttachThreadInput Lib "user32.dll" (ByVal idAttach As IntPtr, ByVal idAttachTo As IntPtr, ByVal fAttach As Boolean) As IntPtr

    Private Declare Function GetFocus Lib "user32.dll" () As IntPtr

    Public Sub New()
        MyBase.New
        InitializeComponent
    End Sub

    Private Sub timerUpdate_Tick(ByVal sender As Object, ByVal e As EventArgs)
        labelHandle.Text = ("hWnd: " + FocusedControlInActiveWindow.ToString)
    End Sub

    Private Function FocusedControlInActiveWindow() As IntPtr
        Dim activeWindowHandle As IntPtr = GetForegroundWindow
        Dim activeWindowThread As IntPtr = GetWindowThreadProcessId(activeWindowHandle, IntPtr.Zero)
        Dim thisWindowThread As IntPtr = GetWindowThreadProcessId(Me.Handle, IntPtr.Zero)
        AttachThreadInput(activeWindowThread, thisWindowThread, true)
        Dim focusedControlHandle As IntPtr = GetFocus
        AttachThreadInput(activeWindowThread, thisWindowThread, false)
        Return focusedControlHandle
    End Function
End Class

现在我想做类似的事情:

if FocusedControlInActiveWindow() <> intptr.zero then
dim IsPass as boolean = isPassword(FocusedControlInActiveWindow())
if IsPass then
msgbox("yes")
else
msgbox ("no")
end if
end if

如何检查活动窗口文本中的焦点控件是否是passwordcahr

4

1 回答 1

0

如果要检查 Windows 编辑控件是否具有ES_PASSWORD 样式,请执行以下操作:

Public Shared Function HasPasswordStyle(ByVal hWnd As IntPtr) As Boolean
    Return ((GetWindowLong(hWnd, GWL_STYLE) And ES_PASSWORD) <> 0)
End Function

<DllImport("user32.dll")> _
Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
End Function

Private Const ES_PASSWORD As Integer = 32
Private Const GWL_STYLE As Integer = -16
于 2013-04-01T06:12:50.570 回答