Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Paste" text directly to notepad 2

Status
Not open for further replies.

Niv3k

Programmer
Jul 11, 2001
350
US
Okay, here is the hack, but I'm really not happy with it written this way:

Code:
Clipboard.SetText Text1
Shell "Notepad.exe", vbNormalFocus
SendKeys Clipboard.GetText

What I would like to do is use the SetWindowText function to put certain text directly into notepad, instead of sendkeys. Can I do this somehow? I was thinking that using createprocess might be good, but I can't find the handle of the notepad text box window from the processid. Keep in mind, that I will be opening upwards of 15 different instances of notepad, so I really don't want to enum through them...


Thanks,
Kevin
 

Check out the sendmessage API for placing text into a window.

Good Luck

 
Yeah, that would work if knew the handle of the window. That's what I can't find.
 

Well, I thought I could look through some code to find how I got the handle to a window with the PID but from looking at it I had to enumerate through the windows and match up the PID to get the handle. But once you have these it should be quite simple to switch between.

Good Luck

 
This method launches a couple of instances of Notepad ans obtains a handle to their window, without enumering:



Private Sub DoLogFiles(ByRef rarrFiles() As String)
Dim lngFile As Integer
Dim rcClient As RECT, rcWnd As RECT
Dim si As STARTUPINFO, pi As PROCESS_INFORMATION, guii As GUITHREADINFO

'//Check if the array is not empty:
Err.Clear
On Error Resume Next
lngFile = UBound(rarrFiles) '//This will result in an error if the array is empty.
If Err.Number > 0 Then Exit Sub

'//Ok, so it's not empty:
On Error GoTo ErrHandler

'//First ask if the user is interested in viewing the logfiles:
If g_clsApp.Application.Message(hWnd, g_clsApp.Term.String(TERM_MSG_VIEWLOGS, CVar("Would you like to see the log files now ?")), IDS_HDR_MSGBOX, edlgQueryYesNo) = edlgRetNo Then Exit Sub

'//Create new processes for the logfiles to be shown in Notepad:
If Not (GetWindowRect(hWnd, rcWnd) > 0 And GetClientRect(hWnd, rcClient) > 0) Then Exit Sub

'//Initialize startup information:
si.cb = Len(si)
si.lpDesktop = vbNullString
si.lpReserved = vbNullString
si.lpTitle = vbNullString

'//Set size of GUI info structure:
guii.cbSize = Len(guii)

For lngFile = LBound(rarrFiles) To UBound(rarrFiles)
'//Set size and position of the new window:
si.dwXSize = rcClient.right - rcClient.left
si.dwYSize = rcClient.bottom - rcClient.top
si.dwX = rcWnd.left + ((rcWnd.right - rcWnd.left - rcClient.right - rcClient.left) / 2)
si.dwY = rcWnd.bottom - si.dwYSize - ((rcWnd.right - rcWnd.left - rcClient.right - rcClient.left) / 2)

If CreateProcess(vbNullString, "Notepad.exe " & Chr(34) & rarrFiles(lngFile) & Chr(34), 0, 0, 0, NORMAL_PRIORITY_CLASS, 0, vbNullString, si, pi) > 0 Then
'//Wait until the window has been created:
WaitForInputIdle pi.hProcess, INFINITE
'//Set the size and position of the new window:
If GetGUIThreadInfo(pi.dwThreadId, guii) > 0 Then
MoveWindow guii.hwndActive, rcWnd.left + ((rcWnd.right - rcWnd.left - rcClient.right - rcClient.left) / 2), _
rcWnd.bottom - si.dwYSize - ((rcWnd.right - rcWnd.left - rcClient.right - rcClient.left) / 2), _
rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, 1
GetWindowRect guii.hwndActive, rcWnd
GetClientRect guii.hwndActive, rcClient
End If
End If
Next lngFile

On Error GoTo 0
Exit Sub

ErrHandler:
HandleError g_clsApp.Application, CreateErrObject(Err)
End Sub


Greetings,
Rick
 
aaaaarrrgh! I always forget to do something simple, such as: WaitForInputIdle pi.hProcess, INFINITE

Once I did that, I was able to get the handles with GetGUIThreadInfo! Thanks, LazyMe.

Kevin
 
Also, thanks, vb5prgrmr, because SetWindowText does not work under other applications edit windows, thus I have to rely on messages...

Kevin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top