2013-03-31 32 views
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 

我怎麼能檢查foucsed控制活動窗口文本是passwordcahr

回答

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 
+0

大概應該是'GetWindowLongPtr'這些天。我想這個用法並不重要,但是使用'GetWindowLongPtr'可以讓你不必考慮64位> 32位截斷 –