Results 1 to 9 of 9

Thread: How to set the Explorer window of a specific file as a child window of TopMost form?

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    How to set the Explorer window of a specific file as a child window of TopMost form?

    Hi, as title says, How can I set the explorer windows of a specifi file as a child window of my TopMost form?
    I've been reading online and I found out I should call SetParent function https://docs.microsoft.com/en-us/win...user-setparent but I can't find examples for my case, as they are for calculator or notepad.
    I'm opening the explorer window with the answer code in : https://www.vbforums.com/showthread....20#post5497520

    I'm using a helper class:
    Code:
    Imports System.IO
    Imports System.Runtime.InteropServices
    
    Public Class NativeMethods
        <DllImport("shell32.dll", SetLastError:=True)>
        Private Shared Function SHOpenFolderAndSelectItems(
                pidlFolder As IntPtr, cidl As UInteger,
                <[In], MarshalAs(UnmanagedType.LPArray)> apidl As IntPtr(),
                dwFlags As UInteger) As Integer
        End Function
    
        <DllImport("shell32.dll", SetLastError:=True)>
        Private Shared Sub SHParseDisplayName(
                <MarshalAs(UnmanagedType.LPWStr)> name As String,
                bindingContext As IntPtr, <Out> ByRef pidl As IntPtr,
                sfgaoIn As UInteger, <Out> ByRef psfgaoOut As UInteger)
        End Sub
    
        Public Shared Sub OpenFolderAndSelectFile(filePath As String)
            Dim dirPath As String = Path.GetDirectoryName(filePath)
            Dim fileName As String = Path.GetFileName(filePath)
            OpenFolderAndSelectFile(dirPath, fileName)
        End Sub
    
        Public Shared Sub OpenFolderAndSelectFile(dirPath As String, fileName As String)
            Dim nativeFolder As IntPtr
            Dim psfgaoOut As UInteger
            SHParseDisplayName(dirPath, IntPtr.Zero, nativeFolder, 0, psfgaoOut)
    
            If nativeFolder = IntPtr.Zero Then
                ' Log error, can't find folder
                Return
            End If
    
            Dim nativeFile As IntPtr
            SHParseDisplayName(Path.Combine(dirPath, fileName),
                               IntPtr.Zero, nativeFile, 0, psfgaoOut)
    
            Dim fileArray As IntPtr()
            If nativeFile = IntPtr.Zero Then
                ' Open the folder without the file selected if we can't find the file
                fileArray = New IntPtr(-1) {}
            Else
                fileArray = New IntPtr() {nativeFile}
            End If
    
            SHOpenFolderAndSelectItems(nativeFolder, CUInt(fileArray.Length), fileArray, 0)
    
            Marshal.FreeCoTaskMem(nativeFolder)
            If nativeFile <> IntPtr.Zero Then
                Marshal.FreeCoTaskMem(nativeFile)
            End If
        End Sub
    End Class
    On main form:
    Code:
     Private Async Function ParentMethod() As Task
            filePath = Await Task.Run(
            Function()
    
                Return Directory.EnumerateFiles(My.Settings.Cartellasalvataggio, titolo & ".mp3",
                              SearchOption.AllDirectories).FirstOrDefault()
            End Function)
        End Function
    And finally I'm calling it with:
    Code:
    NativeMethods.OpenFolderAndSelectFile(filePath)
    Thanks!!
    Last edited by matty95srk; Oct 20th, 2020 at 11:20 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width