Sending a message to any windows textbox just by focus

2.8k views Asked by At

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!

1

There are 1 answers

5
Justin Ryan On BEST ANSWER

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.