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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

API call to control Notepad

Status
Not open for further replies.

stonewal

Technical User
Sep 26, 2000
17
0
0
US
I can make the API call to run Notebook.exe from inside my Access project and display a txt file but I would like more control over notebook when it executes such as window size, font size,locking to prevent changes,etc.

Can anyone give me the API createprocessA constants to accomplish this?

Thanks in advance



 

I belive these may work for you ...
[tt]
Private Type Rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As Rect) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Const WM_KEYDOWN = &H100
Const WM_KEYUP = &H101
Const GW_CHILD = 5
Const SWP_NOMOVE = &H2
Const SWP_NOZORDER = &H4
[/tt]

Good Luck

 
I don't think you can control anything other than changing the window size directly. However, you can't get initial control over its size (so not possible with CreateProcess). You'll have to do that afterwards. This is quite easy though:
CreateProcess provided you with both an id of the new process and its primary thread and handles to them. Use the process handle in a call to WaitForInputIdle, directly after you've created the new process. This provides synchronization between your own thread and the newly created one (you want to wait until the Main window of the new thread has been created, so that you can get a handle to this window and WaitForInputIdle does exactly that...).
After call the GetGUIThreadInfo API, provide it the id of the thread that CreateProcess returned you. The GUITHREADINFO structure will now contain the handle to the window and you can use MoveWindow to set its size and position.

If you don't want to make the file writable, you can make it read-only before opening it with Notepad.

I think this is as far as you can go with remotely controlling Notepad.....
Greetings,
Rick
 
Thanks for the fast information. I do still have a problem with integrating the code you gave me with my code. I may be in over my head on this one. Can you give me a little more info on using it with the CreateProcessA function?

Many Thanks!
 
'//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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top