How to close an external windows

Started by degac, December 28, 2017, 19:45:18

Previous topic - Next topic

degac

Hi
I'm trying to 'close' a window 'folder' (ie: d:\temp_result) opened with OpenURL "d:\temp_result" directly from a BlitzMax application.
I've tried to look for ProcessID/HWND etc - but I've found only how to 'close' Explorer.exe - it will be a 'too drastic' solution :D

Any hints?

Thanks!
If there's a problem, there's at least one solution.
www.blitzmax.org

Scaremonger

You need to obtain the window handle, and then close it. Take a look at: EnumWindows(), FindWindow() and DestroyWindow().

There is also an example here that uses FindWindow() but sends an event to close the window which is probably cleaner.




degac

If there's a problem, there's at least one solution.
www.blitzmax.org

degac

Ok
after some reading (thanks again) and testing I found a working way to resolve my problem


SuperStrict

Extern "win32"
Function GetWindowText(hWnd:Int,lpString:Byte Ptr,nMaxCount) = "GetWindowTextA@12"
Function IsWindowVisible(hwnd:Int)
Function GetWindow(hwnd:Int,uCmd:Int)
Function GetDesktopWindow()
Function DestroyWindow(hwnd:Int)
End Extern

Const GW_CHILD:Int = 5
Const GW_HWNDNEXT:Int = 2
'Const WM_DESTROY = $002'already defined


Global CharBuffer:Byte[255]

Function getWindowHandles:Int()
Local hwnd:Int = GetWindow(GetDesktopWindow(),GW_CHILD)
Local wname$
Repeat
If IsWindowVisible(hwnd) Then
GetWindowText(hwnd,CharBuffer,255)
If String.FromCString(CharBuffer) <> "" And String.FromCString(CharBuffer) <> "Program Manager" Then

wname=String.FromCString(CharBuffer)


Print "("+hwnd+") / "+wname
If wname="temp_test" Return Hwnd

End If
End If
hwnd = GetWindow(hwnd,GW_HWNDNEXT)
Until hwnd = 0
End Function

CreateDir "temp_test"
OpenURL "temp_test"
Delay 5000 'give time to the OS to 'collect' the info
Local hhh:Int=getWindowHandles()
Print "HHH: "+hhh
SendMessageA( hhh,WM_CLOSE,0,0)


In this case the example


  • creates a folder (temp_test)
  • open it (OpenURL)
  • wait 5 seconds to allow the OS to collect the info
  • check the window's handles and if found what I'm looking for
  • return the HWND to close (via SetMessageA) that folder


I needed to add a Delay (1000 at the start, but of course I think it depends on the machine) because I noticed the information (wname) was 'strange' (sometimes just D:\)

Thanks again to point me to the right roads.
If there's a problem, there's at least one solution.
www.blitzmax.org