열려 있는 폴더 종료 open folder CLOSE

VNote 2022.04.26 09:04:14 *:

열려 있는 폴더 종료

 

 

Imports System.Runtime.InteropServices

Imports System.Text

 

Public Class Form1

    Public Const WM_CLOSE As Integer = &H10

 

    <DllImport("user32.dll", EntryPoint:="FindWindowExW")> _

    Private Shared Function FindWindowExW(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszClass As String, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszWindow As String) As IntPtr

    End Function

 

    <DllImport("user32.dll", EntryPoint:="PostMessageW")> _

    Private Shared Function PostMessageW(<InAttribute()> ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean

    End Function

 

    <DllImport("user32.dll", EntryPoint:="GetWindowTextW")> _

    Private Shared Function GetWindowTextW(<InAttribute()> ByVal hWnd As IntPtr, <OutAttribute(), MarshalAs(UnmanagedType.LPWStr)> ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer

    End Function

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Close all explorer windows that have (My Special Documents) in the title text

        CloseExplorer("C:\\새 폴더") 'This is not case sensitive

    End Sub

 

    Private Sub CloseExplorer(ByVal strTitle As String)

        Dim hWnd As IntPtr = FindWindowExW(IntPtr.Zero, IntPtr.Zero, "CabinetWClass", Nothing)

        While Not hWnd.Equals(IntPtr.Zero)

            Dim sb As New StringBuilder(255)

            GetWindowTextW(hWnd, sb, 255)

            If sb.ToString.ToLower.Contains(strTitle.ToLower) Then PostMessageW(hWnd, WM_CLOSE, 0, 0)

            hWnd = FindWindowExW(IntPtr.Zero, hWnd, "CabinetWClass", Nothing)

        End While

    End Sub

End Class