尝试转换窗口句柄ID以在AppActivate函数中使用

时间:2011-10-06 14:14:50

标签: vb.net dllimport

我有一个需要激活的窗口,窗口名称在AppActivate(“WindowName”)中不起作用,因为这不适用于部分字幕等...并且窗口名称将因用户而异。话虽如此,我可以使用“GetwindowhandlefromPartialCaption”来检索窗口名称或句柄的#值。有没有办法转换它或从句柄ID中提取名称以用于AppActivate?

我用来获取句柄ID的代码如下:

 Public Class Form1



<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SetWindowText(ByVal hwnd As IntPtr, ByVal lpString As String) As Boolean
End Function
Declare Auto Function GetWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal uCmd As UInt32) As IntPtr


Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
    Dim lhWndP As Long
    GetHandleFromPartialCaption = False

    lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
    Do While lhWndP <> 0
        Dim length As Integer = GetWindowTextLength(lhWndP)
        If length > 0 Then
            Dim sStr As New StringBuilder("", length + 1)
            GetWindowText(lhWndP, sStr, sStr.Capacity)
            If sStr.ToString.Contains(sCaption) Then
                GetHandleFromPartialCaption = True
                lWnd = lhWndP
                Exit Do
            End If
        End If

        lhWndP = GetWindow(lhWndP, GetWindow_Cmd.GW_HWNDNEXT)
    Loop
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    Dim lhWndP As Long
    If GetHandleFromPartialCaption(lhWndP, "Navilink") = True Then
        MsgBox("Found Window Handle: " & lhWndP, vbOKOnly + vbInformation)
    Else
        MsgBox("Window 'Target App -'", vbOKOnly + vbExclamation)
    End If



End Sub

Private Function GetAllHandleCaptions(ByRef lWnd As Long) As Boolean
    Dim lhWndP As Long

    lhWndP = GetWindow(lWnd, GetWindow_Cmd.GW_CHILD)
    Do While lhWndP <> 0
        Dim length As Integer = GetWindowTextLength(lhWndP)
        If length > 0 Then
            Dim sStr As New StringBuilder("", length + 1)
            GetWindowText(lhWndP, sStr, sStr.Capacity)
            TextBox1.Text = TextBox1.Text + sStr.ToString() + " - " + lhWndP.ToString(+System.Environment.NewLine)
        End If

        lhWndP = GetWindow(lhWndP, GetWindow_Cmd.GW_HWNDNEXT)
    Loop
End Function



 End Class

我希望工作的简单代码如下:

 ' Grab the text highlighted in the other program.
Private Sub Command1_Click()
' Activate the other program.
AppActivate ("Applicationname")

' Clear the clipboard.
Clipboard.Clear

' Press Control.
keybd_event VK_CONTROL, 0, 0, 0
DoEvents

' Press C.
keybd_event VK_C, 1, 0, 0
DoEvents

' Release Control.
keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
DoEvents

' Get the text from the clipboard.
Text1.Text = Clipboard.GetText

我认为如果我可以以某种方式使用代码再次获取窗口文本并通过它应该工作 那个AppActivate。只是不知道该怎么做。

谢谢!

1 个答案:

答案 0 :(得分:0)

AppActivate调用SetForegroundWindow ,因此您可以尝试自己调用它。