EDN Admin
Well-known member
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 Im using to get the handle ID is as follows:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Class Form1
<DllImport(<span style="color:#A31515; "user32.dll", SetLastError:=<span style="color:Blue; True, CharSet:=CharSet.<span style="color:Blue; Auto)> _
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Function FindWindow(<span style="color:Blue; ByVal lpClassName <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal lpWindowName <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As IntPtr
<span style="color:Blue; End <span style="color:Blue; Function
<DllImport(<span style="color:#A31515; "user32.dll", SetLastError:=<span style="color:Blue; True, CharSet:=CharSet.<span style="color:Blue; Auto)> _
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Function GetWindowText(<span style="color:Blue; ByVal hwnd <span style="color:Blue; As IntPtr, <span style="color:Blue; ByVal lpString <span style="color:Blue; As StringBuilder, <span style="color:Blue; ByVal cch <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; End <span style="color:Blue; Function
<DllImport(<span style="color:#A31515; "user32.dll", SetLastError:=<span style="color:Blue; True, CharSet:=CharSet.<span style="color:Blue; Auto)> _
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Function GetWindowTextLength(<span style="color:Blue; ByVal hwnd <span style="color:Blue; As IntPtr) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; End <span style="color:Blue; Function
<DllImport(<span style="color:#A31515; "user32.dll", SetLastError:=<span style="color:Blue; True, CharSet:=CharSet.<span style="color:Blue; Auto)> _
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Function SetWindowText(<span style="color:Blue; ByVal hwnd <span style="color:Blue; As IntPtr, <span style="color:Blue; ByVal lpString <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Declare <span style="color:Blue; Auto <span style="color:Blue; Function GetWindow <span style="color:Blue; Lib <span style="color:#A31515; "user32.dll" (<span style="color:Blue; ByVal hWnd <span style="color:Blue; As IntPtr, <span style="color:Blue; ByVal uCmd <span style="color:Blue; As UInt32) <span style="color:Blue; As IntPtr
<span style="color:Blue; Private <span style="color:Blue; Function GetHandleFromPartialCaption(<span style="color:Blue; ByRef lWnd <span style="color:Blue; As <span style="color:Blue; Long, <span style="color:Blue; ByVal sCaption <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; Dim lhWndP <span style="color:Blue; As <span style="color:Blue; Long
GetHandleFromPartialCaption = <span style="color:Blue; False
lhWndP = FindWindow(vbNullString, vbNullString) <span style="color:Green; PARENT WINDOW
<span style="color:Blue; Do <span style="color:Blue; While lhWndP <> 0
<span style="color:Blue; Dim length <span style="color:Blue; As <span style="color:Blue; Integer = GetWindowTextLength(lhWndP)
<span style="color:Blue; If length > 0 <span style="color:Blue; Then
<span style="color:Blue; Dim sStr <span style="color:Blue; As <span style="color:Blue; New StringBuilder(<span style="color:#A31515; "", length + 1)
GetWindowText(lhWndP, sStr, sStr.Capacity)
<span style="color:Blue; If sStr.ToString.Contains(sCaption) <span style="color:Blue; Then
GetHandleFromPartialCaption = <span style="color:Blue; True
lWnd = lhWndP
<span style="color:Blue; Exit <span style="color:Blue; Do
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
lhWndP = GetWindow(lhWndP, GetWindow_Cmd.GW_HWNDNEXT)
<span style="color:Blue; Loop
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Private <span style="color:Blue; Sub Button1_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Button1.Click
<span style="color:Blue; Dim lhWndP <span style="color:Blue; As <span style="color:Blue; Long
<span style="color:Blue; If GetHandleFromPartialCaption(lhWndP, <span style="color:#A31515; "Navilink") = <span style="color:Blue; True <span style="color:Blue; Then
MsgBox(<span style="color:#A31515; "Found Window Handle: " & lhWndP, vbOKOnly + vbInformation)
<span style="color:Blue; Else
MsgBox(<span style="color:#A31515; "Window Target App -", vbOKOnly + vbExclamation)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Function GetAllHandleCaptions(<span style="color:Blue; ByRef lWnd <span style="color:Blue; As <span style="color:Blue; Long) <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; Dim lhWndP <span style="color:Blue; As <span style="color:Blue; Long
lhWndP = GetWindow(lWnd, GetWindow_Cmd.GW_CHILD)
<span style="color:Blue; Do <span style="color:Blue; While lhWndP <> 0
<span style="color:Blue; Dim length <span style="color:Blue; As <span style="color:Blue; Integer = GetWindowTextLength(lhWndP)
<span style="color:Blue; If length > 0 <span style="color:Blue; Then
<span style="color:Blue; Dim sStr <span style="color:Blue; As <span style="color:Blue; New StringBuilder(<span style="color:#A31515; "", length + 1)
GetWindowText(lhWndP, sStr, sStr.Capacity)
TextBox1.Text = TextBox1.Text + sStr.ToString() + <span style="color:#A31515; " - " + lhWndP.ToString(+System.Environment.NewLine)
<span style="color:Blue; End <span style="color:Blue; If
lhWndP = GetWindow(lhWndP, GetWindow_Cmd.GW_HWNDNEXT)
<span style="color:Blue; Loop
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
The simple code I would love to get working is as follows:
<pre><span><span style="font-size:x-small <span style="color:#008000 Grab the text highlighted in the other program.
Private Sub Command1_Click()
<span style="color:#008000 Activate the other program.
AppActivate ("Applicationname")
<span style="color:#008000 Clear the clipboard.
Clipboard.Clear
<span style="color:#008000 Press Control.
keybd_event VK_CONTROL, 0, 0, 0
DoEvents
<span style="color:#008000 Press C.
keybd_event VK_C, 1, 0, 0
DoEvents
<span style="color:#008000 Release Control.
keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
DoEvents
<span style="color:#008000 Get the text from the clipboard.
Text1.Text = Clipboard.GetText[/code]
I think it should work if i can somehow use the code to get the window text again and pass<br/>
that to the AppActivate. Just not sure how to do it.
Thanks!
View the full article
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 Im using to get the handle ID is as follows:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Class Form1
<DllImport(<span style="color:#A31515; "user32.dll", SetLastError:=<span style="color:Blue; True, CharSet:=CharSet.<span style="color:Blue; Auto)> _
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Function FindWindow(<span style="color:Blue; ByVal lpClassName <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal lpWindowName <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As IntPtr
<span style="color:Blue; End <span style="color:Blue; Function
<DllImport(<span style="color:#A31515; "user32.dll", SetLastError:=<span style="color:Blue; True, CharSet:=CharSet.<span style="color:Blue; Auto)> _
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Function GetWindowText(<span style="color:Blue; ByVal hwnd <span style="color:Blue; As IntPtr, <span style="color:Blue; ByVal lpString <span style="color:Blue; As StringBuilder, <span style="color:Blue; ByVal cch <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; End <span style="color:Blue; Function
<DllImport(<span style="color:#A31515; "user32.dll", SetLastError:=<span style="color:Blue; True, CharSet:=CharSet.<span style="color:Blue; Auto)> _
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Function GetWindowTextLength(<span style="color:Blue; ByVal hwnd <span style="color:Blue; As IntPtr) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; End <span style="color:Blue; Function
<DllImport(<span style="color:#A31515; "user32.dll", SetLastError:=<span style="color:Blue; True, CharSet:=CharSet.<span style="color:Blue; Auto)> _
<span style="color:Blue; Private <span style="color:Blue; Shared <span style="color:Blue; Function SetWindowText(<span style="color:Blue; ByVal hwnd <span style="color:Blue; As IntPtr, <span style="color:Blue; ByVal lpString <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Declare <span style="color:Blue; Auto <span style="color:Blue; Function GetWindow <span style="color:Blue; Lib <span style="color:#A31515; "user32.dll" (<span style="color:Blue; ByVal hWnd <span style="color:Blue; As IntPtr, <span style="color:Blue; ByVal uCmd <span style="color:Blue; As UInt32) <span style="color:Blue; As IntPtr
<span style="color:Blue; Private <span style="color:Blue; Function GetHandleFromPartialCaption(<span style="color:Blue; ByRef lWnd <span style="color:Blue; As <span style="color:Blue; Long, <span style="color:Blue; ByVal sCaption <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; Dim lhWndP <span style="color:Blue; As <span style="color:Blue; Long
GetHandleFromPartialCaption = <span style="color:Blue; False
lhWndP = FindWindow(vbNullString, vbNullString) <span style="color:Green; PARENT WINDOW
<span style="color:Blue; Do <span style="color:Blue; While lhWndP <> 0
<span style="color:Blue; Dim length <span style="color:Blue; As <span style="color:Blue; Integer = GetWindowTextLength(lhWndP)
<span style="color:Blue; If length > 0 <span style="color:Blue; Then
<span style="color:Blue; Dim sStr <span style="color:Blue; As <span style="color:Blue; New StringBuilder(<span style="color:#A31515; "", length + 1)
GetWindowText(lhWndP, sStr, sStr.Capacity)
<span style="color:Blue; If sStr.ToString.Contains(sCaption) <span style="color:Blue; Then
GetHandleFromPartialCaption = <span style="color:Blue; True
lWnd = lhWndP
<span style="color:Blue; Exit <span style="color:Blue; Do
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
lhWndP = GetWindow(lhWndP, GetWindow_Cmd.GW_HWNDNEXT)
<span style="color:Blue; Loop
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Private <span style="color:Blue; Sub Button1_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Button1.Click
<span style="color:Blue; Dim lhWndP <span style="color:Blue; As <span style="color:Blue; Long
<span style="color:Blue; If GetHandleFromPartialCaption(lhWndP, <span style="color:#A31515; "Navilink") = <span style="color:Blue; True <span style="color:Blue; Then
MsgBox(<span style="color:#A31515; "Found Window Handle: " & lhWndP, vbOKOnly + vbInformation)
<span style="color:Blue; Else
MsgBox(<span style="color:#A31515; "Window Target App -", vbOKOnly + vbExclamation)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Function GetAllHandleCaptions(<span style="color:Blue; ByRef lWnd <span style="color:Blue; As <span style="color:Blue; Long) <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; Dim lhWndP <span style="color:Blue; As <span style="color:Blue; Long
lhWndP = GetWindow(lWnd, GetWindow_Cmd.GW_CHILD)
<span style="color:Blue; Do <span style="color:Blue; While lhWndP <> 0
<span style="color:Blue; Dim length <span style="color:Blue; As <span style="color:Blue; Integer = GetWindowTextLength(lhWndP)
<span style="color:Blue; If length > 0 <span style="color:Blue; Then
<span style="color:Blue; Dim sStr <span style="color:Blue; As <span style="color:Blue; New StringBuilder(<span style="color:#A31515; "", length + 1)
GetWindowText(lhWndP, sStr, sStr.Capacity)
TextBox1.Text = TextBox1.Text + sStr.ToString() + <span style="color:#A31515; " - " + lhWndP.ToString(+System.Environment.NewLine)
<span style="color:Blue; End <span style="color:Blue; If
lhWndP = GetWindow(lhWndP, GetWindow_Cmd.GW_HWNDNEXT)
<span style="color:Blue; Loop
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
The simple code I would love to get working is as follows:
<pre><span><span style="font-size:x-small <span style="color:#008000 Grab the text highlighted in the other program.
Private Sub Command1_Click()
<span style="color:#008000 Activate the other program.
AppActivate ("Applicationname")
<span style="color:#008000 Clear the clipboard.
Clipboard.Clear
<span style="color:#008000 Press Control.
keybd_event VK_CONTROL, 0, 0, 0
DoEvents
<span style="color:#008000 Press C.
keybd_event VK_C, 1, 0, 0
DoEvents
<span style="color:#008000 Release Control.
keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
DoEvents
<span style="color:#008000 Get the text from the clipboard.
Text1.Text = Clipboard.GetText[/code]
I think it should work if i can somehow use the code to get the window text again and pass<br/>
that to the AppActivate. Just not sure how to do it.
Thanks!
View the full article