如何将特定文件的资源管理器window设置为TopMost形式的childwindow?

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

我做了一些研究,但找不到真正“有趣”的东西。我尽力找到与我的情况最接近的任何类型的文档或问题,如下所示:

How to find main window title name of application

how to get the window title of a process

How to get the Title Bar Text by its Process Id

getting the name of a process

How do I get list of Process Names running

Check to see if process is running

How To Get Process Owner ID

How to get the title/name of the last active window?

Get Process ID from Window Title

还有 Process.GetProcessesByName Method

我用来打开进程的代码window

Private Async Function ParentMethod() As Task
       Dim filePath As String = Await Task.Run(
       Function()

           Return Directory.EnumerateFiles(My.Settings.Cartellasalvataggio, titolo & ".mp3",
                         SearchOption.AllDirectories).FirstOrDefault()
       End Function)
       If Not String.IsNullOrEmpty(filePath) Then
           LinkLabel1.Text = "File exist already"
           LinkLabel1.Visible = True
           PictureBox7.Visible = True
       Else
           MsgBox("it doesn't exist")

       End If

   End Function

和助手class

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

然后用

调用它
NativeMethods.OpenFolderAndSelectFile(filepath,filename & "extension"))

由于我以这种方式打开流程,而不是使用流程 class,几乎所有这些都不适合我的情况,因为其中许多都涉及记事本,而我认为资源管理器 window 每个文件的标题和 ID 都会发生变化(很明显),而“记事本”进程则保持“记事本”。

我也尝试了 BringToFront,但是后者将一个控件移动到其他控件的前面,但在这种情况下,Explorer 不是控件,对吗?

我至少想做的是 Get a list of active windows & their process names 因为它会无缘无故地浪费内存和时间,因为我需要“过滤”进程来找到我的进程。

希望我们能找到解决方案,在此先感谢。 马蒂亚

这是使用 FindWindowW e SetWindowPos Api 的解决方案。 它在最顶部的表单顶部显示资源管理器文件夹。

   <DllImport("user32.dll", EntryPoint:="FindWindowW")>
   Public Shared Function FindWindowW(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpClassName As String, <MarshalAs(UnmanagedType.LPTStr)> ByVal lpWindowName As String) As IntPtr
   End Function
   <DllImport("user32.dll")>
   Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As Boolean
   End Function
Shared ReadOnly HWND_TOPMOST As IntPtr = New IntPtr(-1)
   Const SWP_NOSIZE As UInt32 = &H1
   Const SWP_NOMOVE As UInt32 = &H2
   Const SWP_SHOWWINDOW As UInt32 = &H40
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


       Dim inptr = FindWindowW("CabinetWClass", Nothing)
       SetWindowPos(inptr, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
   End Sub