You need to get the hWnd handle of the textbox in the other application, using FindWindows and FindWindowEx, after which you can send a WM_SETTEXT message.
Well, first you need to know the class name of the window in the other application and the class name of the textbox (which might be "Edit" but it depends on what language the other program was written in). You can use Spy++ to get these.
This example shows how to set the text in Notepad. The class name of the Notepad main window happens to be Notepad, and the first text box (which is also the only textbox) have the class name "Edit".
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String _
) As Long
Private Declare Function SendMessageText Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As String _
) As Long
Private Const WM_SETTEXT As Long = &HC
Private Sub Command1_Click()
Dim hWndParent As Long
Dim hWndText As Long
'Find the Notepad window
hWndParent = FindWindow("Notepad", vbNullString)
If hWndParent Then
'Find the first text box in Notepad
hWndText = FindWindowEx(hWndParent, 0, "Edit", vbNullString)
If hWndText Then
'set the text
Call SendMessageText(hWndText, WM_SETTEXT, 0, Text1.Text)
End If
End If
End Sub
As I mentioned, you need to get the correct class names. You can use either Spy++ for that or my own Finder tool which you can download from here. In that thread you'll also find more information about handles and sendmessage.
ur code works with notepad
bt with That application it dnt work.. i dnt knw why
That's because Notepad is a MS Windows application. None MS applications may or may not work.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
That's because Notepad is a MS Windows application. None MS applications may or may not work.
It has nothing to do with MS per se, what it has to do with is if the textbox is based on the regular Windows edit box or not. I have not runned the attached exe but I did look at the source code manmad used and according to the class name he used it looks like the other application is a .Net app in which case sending WM_SETTEXT should work.
It has to do with MS per se because all MS applications are designed to 'talk' (or communicate) with one another via API's whereas an application that was not made by MS may or may not be able to 'talk' with other applications. Anyway that is what I was told and read. So if that is not so then I was told wrong and I read wrong.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
That is not true I'm afraid, and there is a huge amount of documentation (particularly on MSDN) to disprove it.
It isn't about who made the apps, it is about what types of controls they use - because the messages (and functionality) are specific to the control type rather than the application. If a different type of control is used, you will probably need to use different messages (and may not be able to do the same things).
Several types of controls (such as textboxes, listboxes, and scrollbars) are used by many applications simply because they are readily available - languages like VB (and even non MS ones, probably including Delphi) use them in many cases, simply because they don't need to be re-created.
If you use a tool like Spy++ you can find the Class of the control you want to work with, and can then find the documentation for that class to see what messages to use with it.
If you use a tool like Spy++ you can find the Class of the control you want to work with, and can then find the documentation for that class to see what messages to use with it.
Well, you can't just go on the class name since that can easily be changed when you create the window (it's one of the parameters you pass to CreateWindowEx). For example a TextBox in VB6 has the class name ThunderTextBox (if you run the application from the IDE) or ThunderRT6TextBox (if you run a compiled application). But it still just an implementation of the regular Edit window which means that you can send any of the EM_xxx messages to it (depending a little bit if its a multiline textbox or not). It also response to many of the WM_xxx messages, such as WM_SETTEXT.
In Delphi the class name for a single line textbox is TEdit and for a multiline textbox its called TMemo, but they are still based on the Edit window and as such you may send them the messages that an Edit window expects.
However if you would create a new textbox from scratch and not base it at all on the built in Windows standard control it will of course then not accept any of the standard messages unless you build in support for them. But building a textbox from scratch is a lot of work and why would you want to do that instead of basing it on something that is already there?
I took a chance and ran the EXE file manmad attached and I then noticed that he used the wrong class name and also the calls where slightly wrong. Also the call to FindWindowsEx finds the first window with the specified class name and/or text. Now that might not be the one you expect. On that particular Form there are two frames (or group boxes) which both contains two textboxes each. One editable textbox and one that is marked as read-only (however even though you can't type in it directly it still accepts a WM_SETTEXT message and changes its text.
The Frames both have the same class name, in fact they have the same class names as the window itself has. So to find one of the edit boxes you must first call FindWindow to find the actual Form window. Then call FindWindowEx to find the first frame, now the "first" frame that is returned is actually the one placed on the right hand side of the Form which you might expect to be the second frame. A new FindWindowEx call let you find one of the textboxes inside this frame.
Now another "funny" thing, the first TextBox in the frame to the right is the top-most textbox, but in the other frame the first textbox is the textbox on the bottom (the read-only one).
So it all depends on which textbox you want to change the text for. But I assume you want the top-left-most textbox in which case this is the code (please note that I pass in the text as well as the class name to find the "frame" window).
Well, you can't just go on the class name since that can easily be changed when you create the window...
Very true, but in the vast majority of cases the class name will at least give you a good starting point to find the relevant info - even if it is just to send you back to the standard control types.