Send contents of another program's textbox to my vb app
Hi,
Trying to interface an off-shelf label printing program by logging the last part number entered by the operator.
The label program has a textbox where the operator scans in the part number, he presses Print and the program prints the label. That part number stays in the textbox until a new one is scanned in.
I need to write an app that will grab this part number every ??? seconds so I know at all times what was the last part number that they entered.
This label printing program's window may be minimized or not have focus.
I should also mention that I KNOW the window's title of the program.
As always any help appreciated.
Re: Send contents of another program's textbox to my vb app
Do you know how to find the hWnd of the textbox? You will need to.
Since the user can type into it, it very well may be control with an hWnd, but maybe not. I think that is the 1st step.
The next step would be to test to ensure you can get the text out of the box after you know the hWnd. This would include one of the APIs: GetWindowText or SendMessage using both WM_GetTextLength & WM_GetText.
Re: Send contents of another program's textbox to my vb app
The following will get the text from the Edit box of Calculator, you can modify it to suit your needs.
VB Code:
Option Explicit
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As Any, ByVal lpszWindow As Any) As Long
Private Const WM_GETTEXT As Long = &HD
Private Const WM_GETTEXTLENGTH As Long = &HE
Private Sub Command1_Click()
Dim wintext As String
Dim slength As Long
Dim retval As Long
Dim hwndDialog As Long
Dim hwndButton As Long
hwndDialog = FindWindow("SciCalc", vbNullString)
If hwndDialog = 0 Then Exit Sub
hwndButton = FindWindowEx(hwndDialog, 0, "Edit", vbNullString)
slength = SendMessage(hwndButton, WM_GETTEXTLENGTH, ByVal CLng(0), ByVal CLng(0)) + 1
wintext = Space$(slength)
retval = SendMessage(hwndButton, WM_GETTEXT, ByVal slength, ByVal wintext)
wintext = Left$(wintext, retval)
MsgBox "The text is: " & wintext
End Sub
Re: Send contents of another program's textbox to my vb app
Thanks for the code, I'll try it on monday.
TGIF!
Re: Send contents of another program's textbox to my vb app
Just tried it and it worked!
Question:
Where did you get the name of the calculator "SciCalc" and the name of the Textbox "Edit". The calculator's caption is "Calculator".
I guess it comes down to finding the form's name and Textbox's name. Where can I get them?
Re: Send contents of another program's textbox to my vb app
Do you have Spy++ that came with VB's installation CDs?
SciCalc is not the title, it is the class name of the window. All windows are created from registered classes. SciCalc is the class name of the calculator's window. Edit is a standard textbox class/window. Spy++ can help identify these for you which is easier than writing your own code to do the "spying". By the way, VB forms have classes too, when compiled: ThunderRT6FormDC and uncompiled: ThunderFormDC
Re: Send contents of another program's textbox to my vb app
Almost there
Got SPY++ on the VB CD and tried it on a window with two textboxes.
Both of them have the same class (Edit) so I guess they have be distinguished by their handles? I need to specify which text box am I getting the text from if there's more than one. Class name is the same for all textboxes (Edit), but the FindWindowEx() in the DEE-U's example works on class name.
Re: Send contents of another program's textbox to my vb app
You can try getting its ID so that the next time you have to set it then you can determine the ID before setting it.
Code:
Option Explicit
Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Const GWL_ID As Long = -12
Private Sub Command1_Click()
Dim ID As Long
ID = GetWindowLong(Text1.hWnd, GWL_ID)
MsgBox ID
End Sub
Re: Send contents of another program's textbox to my vb app
To piggyback on Dee-U's suggestion. Try identifying the IDs multiple times to see if they change. Some apps will create those textboxes dynamically and assign dynamic IDs. Note: Spy++ gives you the ID also, it's the Control ID when you select to view a selected window's properties. Regarding handles, no you cannot use those. They will change a vast majority of the time.
Re: Send contents of another program's textbox to my vb app
Code:
Option Explicit
Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Const GWL_ID As Long = -12
Private Sub Command1_Click()
Dim ID As Long
ID = GetWindowLong(Text1.hWnd, GWL_ID)
MsgBox ID
End Sub
hi
i need ID but
when i use this code for find my own class result is only hwnd of my class!
(my class is:TsMemo)
whats problem?
Re: Send contents of another program's textbox to my vb app
Class? May we see how you are doing it?
Re: Send contents of another program's textbox to my vb app
when i use this code for get id of richedit its give me only hwnd of richedit?!
how can i get only id?
Re: Send contents of another program's textbox to my vb app
Are you using the code you posted in post #10?
Re: Send contents of another program's textbox to my vb app
yes, its work on some object but
not work on a text edit with class name:TsMemo
why?
Re: Send contents of another program's textbox to my vb app
Haven't seen such class type previously. how do you know that it is returning the handle instead of the ID?
Re: Send contents of another program's textbox to my vb app
because i use this and work with vb progRAM AND VC++(ex:1050)
but not work for this class(TsMemo)
Re: Send contents of another program's textbox to my vb app
Do you have Spy++ or something similar (think there is one called WinInspector)? If you do, use that program to get the hWnd of the textbox. It is possible the textbox is windowless and is drawn on the control; meaning it does not have an hWnd, nor ID. I think you have to positively verify whether or not it has an hWnd.
Re: Send contents of another program's textbox to my vb app
Quote:
Originally Posted by LaVolpe
Do you have Spy++ or something similar (think there is one called WinInspector)? If you do, use that program to get the hWnd of the textbox. It is possible the textbox is windowless and is drawn on the control; meaning it does not have an hWnd, nor ID. I think you have to positively verify whether or not it has an hWnd.
yes i have
but dont show to me id of this control!
i think i can find DIFFERENCE betwen this classes.
with size or position of object.
can u tell me how can i get size of window of a richedit?
Re: Send contents of another program's textbox to my vb app
You need the hWnd. Does Spy++ show you an hWnd? If so, use the API GetWindowRect to get size/position. hWnds are not static, they cannot be hardcoded in your app, so you still need to locate the hWnd via the FindWindow or FindWindowEx APIs.
P.S. Spy++ does have an ID entry on the main tab when you opt to view the window's properties.
Re: Send contents of another program's textbox to my vb app