Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to close Selected folder

Posted on 2009-12-30
3
Medium Priority
?
307 Views
Last Modified: 2012-05-08
Hello experts
I got this code from egl1044's post from EE..But this code is closing all open folder.
I want to close selected folder.

i am looking code for closing folder which is selected from my list box.

Please help
Thanks

''''''''''''''''''''''''''''
egl1044:

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Const WM_CLOSE = &H10

Private Sub Command1_Click()

Dim I As Integer
For I = 1 To 100 ' I doubt you will have more than 100 folders open so this will work
Dim cabinetwclass As Long
cabinetwclass = FindWindow("cabinetwclass", vbNullString)
Call PostMessage(cabinetwclass, WM_CLOSE, 0&, 0&)
Sleep 100
DoEvents
Next I

End Sub
0
Comment
Question by:crystalsoft
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 26147163
This should work
Add 1 module
Add 1 command button to form
'//
'// Module1.bas
'//



  Option Explicit
  
  Private Const EXPLORER_CLASS As String = "CabinetWClass"
  Private Const WM_CLOSE As Long = &H10
  
  Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lpParam As Long) As Long
  Private Declare Function GetClassNameW Lib "user32" (ByVal hWnd As Long, ByVal lpClassName As Long, ByVal nMaxCount As Long) As Long
  Private Declare Function GetWindowTextW Lib "user32" (ByVal hWnd As Long, ByVal lpString As Long, ByVal nMaxCount As Long) As Long
  Private Declare Function PostMessageW Lib "user32" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  
  Public ExplorerWindows() As Long
  Public ExplorerCount As Long
  
  
  Private Function GetClassObjectName(ByVal lphWnd As Long) As String
  
  ' Returns the classname associated with hWnd handle.
  
    Dim Buffer(512 - 1) As Byte
    Dim BufferLength As Long
    
    ' Retrieves the name of the class to which the specified window belongs.
    BufferLength = GetClassNameW(lphWnd, VarPtr(Buffer(0)), 512)
    
    If BufferLength <> 0 Then
      ' Return this as TCHAR string length.
      GetClassObjectName = Left$(Buffer, BufferLength)
      
    Else
      
      GetClassObjectName = vbNullString
      
    End If
    
    ' Free memory
    Erase Buffer
    
  End Function
  
  
  Public Function GetObjectWindowText(ByVal lphWnd As Long) As String
  
  ' Returns the window text associated with hWnd handle.
  
    Dim Buffer(4096 - 1) As Byte
    Dim BufferLength As Long
    
    ' Retrieves the name of the class to which the specified window belongs.
    BufferLength = GetWindowTextW(lphWnd, VarPtr(Buffer(0)), 4096)
    
    If BufferLength <> 0 Then
      ' Return this as TCHAR string length.
      GetObjectWindowText = Left$(Buffer, BufferLength)
      
    Else
      
      GetObjectWindowText = vbNullString
      
    End If
    
    ' Free memory
    Erase Buffer
  
  End Function
  
  Public Function CloseWindowGentle(ByVal lphWnd As Long) As Long
    
    ' Close a window
    CloseWindowGentle = PostMessageW(lphWnd, WM_CLOSE, 0, 0)
  
  End Function
  
  
  Public Function EnumExplorerWindows() As Long
    
    '
    ExplorerCount = 0
    Erase ExplorerWindows
    EnumWindows AddressOf EnumWindowProc, 0
    
    If ExplorerCount = 0 Then
      EnumExplorerWindows = (-1)
    Else
      EnumExplorerWindows = ExplorerCount
    End If
  
  End Function
  
  
  Public Function EnumWindowProc(ByVal hWnd As Long, ByVal lpParam As Long) As Long
    
    If StrComp(GetClassObjectName(hWnd), EXPLORER_CLASS, vbTextCompare) = 0 Then
      ' We found an instance of "CabinetWClass" class.
      ReDim Preserve ExplorerWindows(ExplorerCount) As Long
      ExplorerWindows(ExplorerCount) = hWnd
      ExplorerCount = (ExplorerCount + 1)
    End If
  
    EnumWindowProc = 1
    
  End Function
 










'//
'// Form1.frm
'//

Option Explicit
 
Private Sub Command1_Click()
 
Dim i As Long
Dim szTitle As String
 
' Populate ExplorerWindow handles.
' Returns (-1) if none exists.
If EnumExplorerWindows <> (-1) Then
 
For i = LBound(ExplorerWindows) To UBound(ExplorerWindows)
  szTitle = GetObjectWindowText(ExplorerWindows(i))
  ' TODO:// Add the title match here.
  If szTitle Like List1.List(List1.ListIndex) Then
    Call CloseWindowGentle(ExplorerWindows(i))
    Exit For
  End If
Next i
 
Else
 
  Debug.Print "No explorer windows opened"
  
End If
 
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:crystalsoft
ID: 31671215
Thanks it helps me great.
Related to  to this problem i have an question
how can i use this application from server to client .
like if i install on server and client
then i want to see what client doing
so how can i see this list box and if i found something wrong than i can close..
if you will provide me solution i will give you 500 points with A grade
Thanks and hello..
Happy New Year..:-)
0
 
LVL 1

Author Comment

by:crystalsoft
ID: 26164867
Hi
Can you please check following question
i am facing problem some where to kill process from server to client

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_25014770.html

Thanks in advance
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question