Newsgroup: it.comp.lang.visual-basic
Titolo: massimizzare / chiudere app lanciata da shell

fermi con le parolacce: ho GIA' provato vari esempio trovati qua sul
NG, ma non me ne funziona uno! :-(
dunque:
uso WinXP sp2 e VB6 sp5
dovrei lanciare una applicazione "esterna" (esempio shell
"notepad.exe")
poi dovrei farla massimizzare
poi dovrei chiuderla
gli esempio che ho visto usano la sendmessage, ma NON mi funzionano.
 
sarà XP sp2? sarò io che ho fatto qualche fesseria?
Grazie.

 ha scritto nel messaggio
news:1121086927....@g14g2000cwa.googlegroups.com...

Per la massimizzazione: hai provato con il parametro WindowStyle della funzione
Shell impostandolo a vbMaximizedFocus?
Per la chiusura: hai provato con PostMessage e WM_QUIT?
http://support.microsoft.com/default.aspx?scid=KB;en-us;q153463
Bye
Raf

ne sto uscendo...
una fesseria è che devo conoscere la caption della form... se ho due
"notepad.exe" per esempio non so capire quale è quella giusta, e idem
per la ver italiana/ingllese di win...

cippalippa wrote:

Dovresti cercare la finestra non per nome, ma per thread ID:
vedi GetWindowThreadProcessId().
-- 
ciao, hal

ero proprio partito da qua (codice trovato in giro su internet):
questa cosa dovrebbe aprire un notepad e poi scriverci dentro.
... ma non funziona, per lo meno mi trova il threadid ma poi non fa
nulla.
comunque continuo a provare.
Option Explicit
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Const WM_SETTEXT = &HC
Private Declare Function GetWindow Lib "user32" _
  (ByVal hwnd As Long, _
   ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" _
  (ByVal hwnd As Long, _
   lpdwProcessId As Long) As Long
Private Declare Function BringWindowToTop Lib "user32" _
  (ByVal hwnd As Long) As Long
Private Declare Function SetWindowText Lib "user32" _
   Alias "SetWindowTextA" _
  (ByVal hwnd As Long, ByVal _
   lpString As String) As Long
Private Declare Function GetClassName Lib "user32" _
   Alias "GetClassNameA" _
  (ByVal hwnd As Long, _
   ByVal lpClassName As String, _
   ByVal nMaxCount 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 Any) As Long
Private Sub Command1_Click()
   Dim hWndApp As Long
  'call the wrapper function to start shell,
  'and return the handle to the shelled app.
   hWndApp = StartShell("notepad.exe")
  'if found, prove it!
   If hWndApp Then
     'change its caption
      Call SetWindowText(hWndApp, "The handle to Notepad is " &
CStr(hWndApp))
     'bring it to the top
      Call BringWindowToTop(hWndApp)
     'and add some text to its edit window
      Call PutTextIntoNotepad(hWndApp)
   End If
End Sub
Private Function GethWndFromProcessID(hProcessIDToFind As Long) As Long
    Dim hWndDesktop As Long
    Dim hWndChild As Long
    Dim hWndChildProcessID As Long
    On Local Error GoTo GethWndFromProcessID_Error
   'get the handle to the desktop
    hWndDesktop = GetDesktopWindow()
   'get the first child under the desktop
    hWndChild = GetWindow(hWndDesktop, GW_CHILD)
   'hwndchild will = 0 when no more child windows are found
    Do While hWndChild <> 0
       'get the ThreadProcessID of the window
        Call GetWindowThreadProcessId(hWndChild, hWndChildProcessID)
       'if it matches the target, exit returning that value
        If hWndChildProcessID = hProcessIDToFind Then
            GethWndFromProcessID = hWndChild
            Exit Do
        End If
       'not found, so get the next hwnd
        hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
    Loop
Exit Function
GethWndFromProcessID_Error:
    GethWndFromProcessID = 0
    Exit Function
End Function
Private Function StartShell(sApplication As String) As Long
   Dim hProcessID As Long
  'start using shell, and get the process ID
   hProcessID = Shell(sApplication, vbNormalFocus)
  'return the hwnd to the shelled process
   StartShell = GethWndFromProcessID(hProcessID)
End Function
Private Sub PutTextIntoNotepad(hWndApp As Long)
  'This adds text to notepad, by locating its
  ''Edit' class window. This is similar to the
  'method used to locate the hwnd itself, but
  'here we know the parent (hWndApp) so only
  'have to search its child windows.
   Dim hWndChild  As Long
   Dim sMsg As String
   Dim sBuffer As String * 32
   Dim nSize As Long
  'this string is split only to fit the browser window
   sMsg = "This method demonstrates using Shell() "
   sMsg = sMsg & "to launch notepad, obtain its hwnd using "
   sMsg = sMsg & "GetWindowThreadProcessId, and then use that"
   sMsg = sMsg & "hwnd to change its caption and place text into "
   sMsg = sMsg & "its Edit window using SetWindowText and SendMessage."
  'get the first child window in Notepad
   hWndChild = GetWindow(hWndApp, GW_CHILD)
  'hwndchild will = 0 when no more child windows are found
   Do While hWndChild <> 0
     'get the Class Name of the window
      nSize = GetClassName(hWndChild, sBuffer, 32)
     'if nSize > 0, it contains the length
     'of the class name retrieved
      If nSize Then
        'if the class name is "Edit",
        'set some text and exit
         If Left$(sBuffer, nSize) = "Edit" Then
            Call SendMessage(hWndChild, WM_SETTEXT, 0&, ByVal sMsg)
            Exit Sub
         End If
      End If
     'not found, so get the next hwnd
      hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
 
   Loop
End Sub