1

I need to know how I can send a message to any text box of windows.

If a focus the google chrome url textbox, then I will "auto paste" the message, or if I focus a Word Document string, or notepad, or anything!

I got a code ho sends by setting the iHwnd, findwindow and findwindowex, but I need to set any time I want to change the final program, and thats why I need an automatic program "focus based".

Here is what I have so far...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim iHwnd As IntPtr = FindWindow("notepad", vbNullString) 
    Dim iHwndChild As IntPtr = FindWindowEx(iHwnd, IntPtr.Zero, "Edit", vbNullString) 
    SendMessage(iHwndChild, WM_SETTEXT, 0, "Hello World!") 
End Sub

Sorry for my bad english!

admdrew
  • 3,790
  • 4
  • 27
  • 39
Renan Macedo
  • 23
  • 1
  • 4
  • 1
    SO isn't a code writing service. Please show us what you have attempted to do before we can help. Thanks! – Trevor Nov 10 '14 at 19:02
  • I got this by far: `Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim iHwnd As IntPtr = FindWindow("notepad", vbNullString) Dim iHwndChild As IntPtr = FindWindowEx(iHwnd, IntPtr.Zero, "Edit", vbNullString) SendMessage(iHwndChild, WM_SETTEXT, 0, "Hello World!") End Sub` And works fine, but every time i change the final program, like wordpad, i need to change the classes to work, i just need a program who work on focus. – Renan Macedo Nov 10 '14 at 19:20
  • You will need to post the code you've tried, but I can tell you this is going to be difficult. There's a means to find the ActiveWindow, but you're not going to get around having to use the specific window handle and a handle for the textbox. – Jimmy Smith Nov 10 '14 at 19:37
  • The code is up there, (yeah im suck on commenting), i just have no idea what to do... =Z – Renan Macedo Nov 10 '14 at 19:40
  • 1
    @RenanMacedo I edited your post to include your commented code. When it's in the comment section it's hard for people to see what you have. – Trevor Nov 10 '14 at 20:31

1 Answers1

1

SendMessage is always going to require a specific window handle, or broadcast to all top level windows. To continue with your current code, you could first try to retrieve the active window's handle with GetActiveWindow or similar function.

Alternately, you could experiment with the SendKeys class to send your text. SendKeys always targets the currently active control (as if the user were typing directly on the keyboard), so you don't need to concern yourself with finding window handles or titles.

Justin Ryan
  • 1,409
  • 1
  • 12
  • 25
  • Well, i used GetActiveWindows like this : `TextBox1.Text = GetActiveWindow()` when is on the program, works fine, but when on another window, i always get 0, don't know why... – Renan Macedo Nov 11 '14 at 16:09
  • That may be the expected behavior of `GetActiveWindow`. The documentation I linked to is somewhat unclear. You might have to to some experimentation with similar functions, like `GetForegroundWindow`. Working directly with the Windows API can be a pain sometimes. – Justin Ryan Nov 11 '14 at 16:31
  • So im trying now the SendKeys, and is working fine, but i got an unexpected problem: I got a timer ,and the timer send a code every second, i putted this code on it `Dim iHwnd As IntPtr = FindWindow(GetActiveWindow(), vbNullString) SendKeys.Send(CodTagProd.Text)` And worked fine if the sendkey is a normal text, but if a put a TEXTBOX.Text , the first pulse of the timer the program just close, dont know why, dont know how, just closes, but if is a normal text, works fine...wtf – Renan Macedo Nov 11 '14 at 16:58
  • Fixed! Ty for all guys!, ITS ALIVE! – Renan Macedo Nov 11 '14 at 18:49
  • `GetActiveWindow` returns the active window attached to the calling thread's message queue. So it will find only the active window in your app (if you are call it from the GUI thread). Use `GetForegroundWindow` to find the currently forwarded window in the system. – nrofis Oct 12 '21 at 14:17