Link to home
Start Free TrialLog in
Avatar of rjef
rjefFlag for United States of America

asked on

sendmessage to close window does not work

Why doesn't the below code work.  
"C:\closewin\WdsToClose1.txt" contains screen titles to close.
"c:\closewin\allwin.txt" will contain all windows that have taken focus.
 
 
 
'set timer1 interval to 100
' form code
Private Sub Form_Load()
xx = 0
Open "C:\closewin\WdsToClose1.txt" For Input As #1
Do While Not EOF(1)
xx = xx + 1
Line Input #1, wdstoclose(xx)
Loop
Close #1
End Sub
 
Function foreGWin() As String
Dim textlen As Long
Dim wintext As String
Dim slength As Long
Dim fgHwnd As Long
fgHwnd = GetForegroundWindow()
textlen = GetWindowTextLength(fgHwnd) + 1
wintext = Space(textlen)
slength = GetWindowText(fgHwnd, wintext, textlen)
wintext = Left(wintext, slength)
foreGWin = wintext
hand = fgHwnd
End Function
Public Sub WaitForWindow(WinHead As String)
  DoEvents
    If Left(foreGWin(), Len(WinHead)) = WinHead Then
    SendMessage hWnd, WM_CLOSE, 0&, ByVal 0&
    End If
  If curwin <> foreGWin() Then
    Open "c:\closewin\allwin.txt" For Append As #2
        If foreGWin() <> "" Then
            Print #2, Format(Date, "MM/DD/YY") & " "; Format(Time, "HH:MM") & " " & foreGWin()
        End If
    curwin = foreGWin()
    Close #2
  Else
  End If
  End Sub
 
Private Sub Timer1_Timer()
For hh = 1 To xx
    Call WaitForWindow(wdstoclose(hh))
Next hh
End Sub

' module
Public Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
 
Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal Y As Long) As Long
 
Declare Function GetForegroundWindow Lib "user32.dll" () As Long
 
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
 
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Public Const WM_CLOSE = &H10
Public Declare Function SendMessage Lib _
    "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, _
     ByVal wmsg As Long, _
     ByVal wparam As Long, _
     lparam As Any) As Long
 
 
 
Public curwin As String
 
Public xx As Double
Public wdstoclose(10000) As String
Public hh As Double

 
 
Avatar of vinnyd79
vinnyd79

The hWnd in the sendmessage call is for your forms window,not the form you are trying to close.

SendMessage hWnd, WM_CLOSE, 0&, 0&
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rjef

ASKER


Can you make it cycle through all running processes and then close any that are in the WdsToClose1.txt file.  I need this because sometimes the window i want closed is not the active window