PDA

Click to See Complete Forum and Search --> : Sendmessage API


Da_HerB
Mar 12th, 2001, 06:53 PM
How do I use the sendmessage function to copy text from a textbox to another windows program?

Mar 12th, 2001, 08:02 PM
This will copy the selected text from another program. Open notepad, type some text and then select it, then add this code to a form with a command button, click the command button, you will see that it is copied to the clipboard.


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 SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal _
wMsg As Long, ByVal wParam As Long, lParam As Any) _
As Long

Private Const WM_COPY = &H301


Private Sub Command1_Click()

Dim notepad As Long, editx As Long
notepad = FindWindowEx(0, 0, "notepad", vbNullString)
editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
SendMessage editx, WM_COPY, 0, 0

End Sub

Mar 12th, 2001, 09:36 PM
If you want to copy all the text, use the EM_SETSEL constant also used with the SendMessage API function.


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 SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal _
wMsg As Long, ByVal wParam As Long, lParam As Any) _
As Long

Private Const WM_COPY = &H301
Private Const EM_SETSEL = &HB1


Private Sub Command1_Click()

Dim notepad As Long, editx As Long
notepad = FindWindowEx(0, 0, "notepad", vbNullString)
editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
SendMessage editx, EM_SETSEL, 0, 0
SendMessage editx, WM_COPY, 0, 0

End Sub



And thanks to trr042, Lee M. for sharing with me the constants of the SendMessage API function, ...and to Chris, for being interested in the same topic as well :rolleyes:.