1
我有一個窗口,我需要激活並且窗口名稱在AppActivate(「WindowName」)中不起作用,因爲這不適用於部分字幕等。窗口名稱將根據用戶而有所不同。這就是說我能夠使用「GetwindowhandlefromPartialCaption」來檢索窗口名稱或句柄的#值。有沒有辦法將其轉換或從句柄ID中提取名稱以與AppActivate一起使用?嘗試轉換窗口句柄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。只是不知道該怎麼做。
謝謝!