Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

API to Put text into a window

Is there an API that can put text into a window? SetWindowText is close to what I want but it only puts the text into windows caption of the title bar. I want to put the text into the window.
Right now I'm using SendKeys to do the job but if there's a cleaner way I'd sure like to know it.  Thanks
0
royster
Asked:
royster
1 Solution
 
wpsjr1Commented:
Not sure what kind of window to which you want to set the text.  In any case you will need FindWindow to get the top level window handle (hWnd) and FindWindowEx to the the hWnd of any child controls on the top level.  In most cases you will use SendMessage and some window message to set the text.  If it were a textbox you would use WM_SETTEXT.  For instance:

Dim lTophWnd&, lChildhWnd&, lRet&

lTophWnd = FindWindow("classname", "caption") <- substitute vbNullString for an unknown

lChildhWnd = FindWindowEx(lTophWnd, 0&, "classname", vbNullString)

lRet = SendMessage(lChildhWnd, WM_SETTEXT, 0&, ByVal "foo")

If its some other type of control let me know.  
0
 
roysterAuthor Commented:
It is a text box. I have the handle for it but haven't been able to get this to work.  I don't know the proper value to use for WM_SETTEXT and the order of the SendMessage function I found appears different than the one in your example. Below is the function I have:
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long
0
 
ConniverCommented:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long

Public Const WM_SETTEXT = &HC

Call SendMessageByString(textbox_handle, WM_SETTEXT, 0, "what said")

try that...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
AzraSoundCommented:
you need to declare it as such:

Public Const WM_SETTEXT = &HC
0
 
AzraSoundCommented:
oops...little late on my post
0
 
roysterAuthor Commented:
It is a text box. I have the handle for it but haven't been able to get this to work.  I don't know the proper value to use for WM_SETTEXT and the order of the SendMessage function I found appears different than the one in your example. Below is the function I have:
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long
0
 
roysterAuthor Commented:
When I refreshed this screen it automatically rejected connivers answer and reposted my last comment... sorry. I'll test the answer a bit later and comment again what I find out. Thanks
0
 
AzraSoundCommented:
there is no value for WM_SETEXT, it is a constant value that you declare as pointed out above.  It is the message being sent using the SendMessage api call.  SendMessage using the message WM_SETEXT takes as the paramaters:

hWnd:  handle to the destination window
Msg:  WM_SETTEXT (message to send)
wParam: (not used, set to 0)
lParam:  string that you want to send

notice lParam is declared As Any so you can make it whatever you want.  Declare it as a string

Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As String) As Long



And implement it:


Call SendMessageByString(textbox_handle, WM_SETTEXT, 0, "text to send")



0
 
roysterAuthor Commented:
I had to fool with it some to get it to work. This did the trick:
SendMessage(EditHwnd, WM_SETTEXT, 0, ByVal "mystring")

It needed the ByVal. Thanks everyone for helping and if anyone else would like some points for helping let me know.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now