I have a window that I need to activate and the window name does not work in AppActivate(“WindowName”) because this does not work with partial captions etc… and the window name will be different depending on user. That being said I am able to use “GetwindowhandlefromPartialCaption” to retrieve the # value of the window name or handle. Is there a way to convert this or extract the name from the handle ID to use with AppActivate?
The code I’m using to get the handle ID is as follows:
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
The simple code I would love to get working is as follows:
' 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
I think it should work if i can somehow use the code to get the window text again and pass
that to the AppActivate. Just not sure how to do it.
Thanks!
AppActivate calls SetForegroundWindow , so you can try calling it yourself.