检查活动窗口句柄是否为密码框

时间:2013-03-31 20:16:53

标签: vb.net winforms passwords pinvoke

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

此函数返回活动窗口的活动控件句柄:

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

1 个答案:

答案 0 :(得分:0)

如果您想检查Windows编辑控件是否具有ES_PASSWORD style,请按以下步骤操作:

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