Hi guys. I have a form using API and can find a text box in another app, but I dont know how the code to send text to the textbox in the other app.
What is the code to send text to an external textbox?
Printable View
Hi guys. I have a form using API and can find a text box in another app, but I dont know how the code to send text to the textbox in the other app.
What is the code to send text to an external textbox?
This is how you would send some text to a notepade fileQuote:
Originally Posted by Jackrabbit319
So, following this logic, you would lose the references to Notepad, substitute hWnd2 with the handle of the textbox in the other application (which you say you already have), and that should work.VB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, _ ByVal lParam As Any) As Long Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _ ByVal wCmd As Long) As Long Private Const WM_SETTEXT = &HC Private Const GW_CHILD = 5 Private Sub Command1_Click() Dim hWnd1 As Long Dim hWnd2 As Long Dim sText As String Clipboard.Clear Clipboard.SetText Text1.Text Shell "Notepad", vbNormalFocus hWnd1 = FindWindow(vbNullString, "Untitled - Notepad") hWnd2 = GetWindow(hWnd1, GW_CHILD) sText = Clipboard.GetText SendMessage hWnd2, WM_SETTEXT, Len(sText), sText End Sub
Ok thanx I'll give it a try :)
Ok I tried it but Im having trouble now with the API function.
Say I have a chat window open in MSN Messenger......here would be the code for the window :
VB Code:
Dim imwindowclass As Long, directuihwnd As Long imwindowclass = FindWindow("imwindowclass", vbNullString) directuihwnd = FindWindowEx(imwindowclass, 0&, "directuihwnd", vbNullString)
I cant get it to send the text to that window for some reason.
That handle (directuihwnd) is generally for the window, not the textbox part of the window... Using Spy++ I can't see any child windows under that handle, so this might not be possible!
Im not using a different API spy software. If anyone can tell me where I can get spy ++ I would greatly appreciate it :)
It comes with Visual Studio. In the start menu, in programs, in MS Visual Studio, in MS Visual Studio Tools you should see Spy++ :)
Ahh ok. Well that would explain why I dont have it, Im running Visual Basic lol
I used an API Spy program in another chat program that does show the child handle and I modified my code but Im still needing a lil help:
VB Code:
Private Sub Command1_Click() Dim hWnd1 As Long Dim hWnd2 As Long Dim sText As String Dim livveusamic As Long, afxframeorview As Long, afxolecontrol As Long Dim afxmdiframe As Long, richedita As Long livveusamic = FindWindow("livveusa-mic", vbNullString) afxframeorview = FindWindowEx(livveusamic, 0&, "afxframeorview42", vbNullString) afxmdiframe = FindWindowEx(afxframeorview, 0&, "afxmdiframe42", vbNullString) afxframeorview = FindWindowEx(afxmdiframe, 0&, "afxframeorview42", vbNullString) afxframeorview = FindWindowEx(afxmdiframe, afxframeorview, "afxframeorview42", vbNullString) afxolecontrol = FindWindowEx(afxframeorview, 0&, "afxolecontrol42", vbNullString) afxmdiframe = FindWindowEx(afxolecontrol, 0&, "afxmdiframe42", vbNullString) richedita = FindWindowEx(afxmdiframe, 0&, "richedit20a", vbNullString) Clipboard.Clear Clipboard.SetText "TEXT TO SEND" FindWindow "livveusa-mic", vbNormalFocus hWnd1 = FindWindow(vbNullString, "afxmdiframe42") hWnd2 = GetWindow(hWnd1, GW_CHILD) sText = Clipboard.GetText SendMessage hWnd2, WM_SETTEXT, Len(sText), sText End Sub
First thing I notice is that this line :
VB Code:
FindWindow "livveusa-mic", vbNormalFocus
does nothing... It's wrong and you don't save the return value anywhere... I'll check the rest now.
Is this for MSN? I'm confused :ehh:
No I used MSN as an example but messed up because I could'nt get the child window like you said earlier. This is another chat program I use. From what i gather the parent window is "afxmdiframe" and the child window is "richedit20a".
I need to be able to set focus on the "richedit20a" window to send the text but Im having trouble doing it.
Ok I figured out a better way to do this. I've coded it to copy to clipboard but I am still having trouble setting focus to the texbox in the external app based on my code I posted earlier.
You should be able to use the SetFocus API to set the focus to the handle of the richedit you got from the FindWindowEx call.
VB Code:
Declare Function SetFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Why do you need it to set focus though? Can't you set the text with the WM_SETTEXT message or the SetWindowText API?
Thats what Im having trouble with. Im not sure why its not doing it. I thought it was because its not setting focus to the richedit but now I dunno
I finally figured it out !!! I figured out how to send the text in a differnt way:
VB Code:
Call SendMessage(richedita, WM_SETTEXT, 0&, ByVal List1.Text)
tyvm for your help manavo11 !!
Ok I was gonna post "RESOLVE" but unfortunatly I'm not out of the woods yet. Here is how I have the command button coded to send the text from the listbox on my form to the textbox in the external app using API :
VB Code:
Private Sub Command1_Click() Do DoEvents livveusamic = FindWindow("livveusa-mic", vbNullString) afxframeorview = FindWindowEx(livveusamic, 0&, "afxframeorview42", vbNullString) afxmdiframe = FindWindowEx(afxframeorview, 0&, "afxmdiframe42", vbNullString) afxframeorview = FindWindowEx(afxmdiframe, 0&, "afxframeorview42", vbNullString) afxframeorview = FindWindowEx(afxmdiframe, afxframeorview, "afxframeorview42", vbNullString) afxolecontrol = FindWindowEx(afxframeorview, 0&, "afxolecontrol42", vbNullString) afxmdiframe = FindWindowEx(afxolecontrol, 0&, "afxmdiframe42", vbNullString) richedita = FindWindowEx(afxmdiframe, 0&, "richedit20a", vbNullString) Call SendMessage(richedita, WM_SETTEXT, 0&, ByVal List1.Text) Loop Until richedita <> 0 End Sub
Now what line or lines of code do I need so that my send does the ENTER command after it sends the text to the textbox? I've used "Sendkeys" and other API functions but none seem to work. And I'm sure I'm not modifying it right either.
Instead of "sending" the enter key, could you simulate a click on a button? ;)
That would be great but there is no button to push lol :)
Huh? What kind of chat application is that? :) Isn't there a "Send" button? Kinda makes sense to have one :ehh:
No there's no send button. Its not a chat client like Yahoo or MSN, it's more like CamFrog or Eyeball chat where it has its own borwser window, user list in lobbys and rooms...ect. And yes I agree with ya that there should be, it would've made things alot easier. I messed around with it last night and discovered that the sendkey was working but I had to keep focus on that child window where the text is entered. It works great now tyvm for all your help :)
Now before I resolve this thread, is there a way I could code my form so that my timer pauses when focus is lost on the texbox I send the text too, then unpause when focus is back? Reason being is whenever I set focus to another textbox it sends text then enters it to that.
You should be able to use the GetAncestor API to see the parent window of each textbox. Using FindWindow you can find the handle of the window containing the textbox you want. Compare the GetAncestor result for the active control with the FindWindow result (should be this "livveusamic = FindWindow("livveusa-mic", vbNullString)") and see if it's the same, then depending on if it is or not, send the text :)
If you need help with any of this just ask :)
Or even better, send the WM_KEYDOWN, WM_KEYUP messages to the textbox with the VK_RETURN key :)
Would I have to use a code that interrupts the event when focus is lost? If so then I have never done that :ehh:
On every timer event, check the foreground window to see if it is your textbox or not. Use GetForegroundWindow() (I think that will work for a textbox).
Using SendMessage doens't require the textbox to have focus.
penagate, the GetForegroundWindow will get you the window that is in focus, not the control. To get the handle of the control you can use this : http://www.vbforums.com/showpost.php...1&postcount=10 :)
OK, nice :)
Use this to send enter to a richedit textbox:
VB Code:
Public Const WM_CHAR As Long = &H102 SendMessage hWndTextbox, WM_CHAR, (vbKeyReturn), 0
Have you tried sending WM_KEYDOWN and WM_KEYUP messages? (Using PostMessage)Quote:
Originally Posted by Jackrabbit319