'//API declarations:
Private Const INFINITE As Long = -1
Private Type RECT
left As Long
top As Long
right As Long
bottom As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type GUITHREADINFO
cbSize As Long
flags As Long
hwndActive As Long
hwndFocus As Long
hwndCapture As Long
hwndMenuOwner As Long
hwndMoveSize As Long
hwndCaret As Long
rcCaret As RECT
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, ByRef lpRect As RECT) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, ByRef lpRect As RECT) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function GetGUIThreadInfo Lib "user32.dll" (ByVal idThread As Long, ByRef pGUI As GUITHREADINFO) As Long
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByRef lpEnvironment As Any, _
ByVal lpCurrentDriectory As String, _
ByRef lpStartupInfo As STARTUPINFO, _
ByRef lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Public Function SpawnNotepad(ByRef rstrFile As String, ByVal vhwndParent As Long) As Long
'//Spawns an instance of Notepad to show the specified logfile.
'//Returns a handle to the Notepad window.
Dim intBorder As Integer
Dim rcWnd As RECT, rcClient As RECT, pi As PROCESS_INFORMATION, si As STARTUPINFO, gui As GUITHREADINFO
'//Get size-and position of parent window:
If GetWindowRect(vhwndParent, rcWnd) = 0 Or GetClientRect(vhwndParent, rcClient) = 0 Then Exit Function
si.cb = Len(si)
gui.cbSize = Len(gui)
'//Create the process:
If CreateProcess(vbNullString, "Notepad.exe " & Chr(34) & rstrFile & Chr(34), 0&, 0&, 0, 0, 0, vbNullString, si, pi) > 0 Then
'//Wait until the new thread is ready to receive input:
If WaitForInputIdle(pi.hProcess, INFINITE) = 0 Then
'//Get a handle to the new window:
If GetGUIThreadInfo(pi.dwThreadId, gui) > 0 Then
intBorder = (rcWnd.right - rcWnd.left - rcClient.right) / 2
MoveWindow gui.hwndActive, rcWnd.left + intBorder, rcWnd.bottom - intBorder - rcClient.bottom, rcClient.right, rcClient.bottom, 1
SpawnNotepad = gui.hwndActive
End If
End If
End If
End Function
Greetings,
Rick