locked
Show a Explorer window process by the text in the title bar on top of a top most Form RRS feed

Answers

  • Hi Mattia Fanti,

    Thanks for your feedback.

    You can refer to the following code to set explorer window topmost.

        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
    
        '...
        Dim inptr = FindWindow("CabinetWClass", Nothing)
        SetWindowPos(inptr, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mattia Fanti Friday, October 23, 2020 1:05 PM
    Friday, October 23, 2020 6:07 AM

All replies

  • Hi

    I am confused!  Have you abandoned the other thread?

    In that other thread, it DOES show a means to open an Explorer window with a particular file name selected, with the Explorer Window minimized (all as per your request)

    *

    If you could say that the file is  UNIQUE and only occurs once in the search path then here is an amended version. This version doesn't minimise the Explorer view, and will NOT open on top of your main application Form (assumming you have it set to TopMost = True.

    *

    It will also show the Process Handle in a MessageBox (assuming the file exists)

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim rootFolder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
      Dim file As String = "This Is A Unique File Name.txt"
      Dim WithEvents bgw As New ComponentModel.BackgroundWorker
      Dim ProcID As IntPtr
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TopMost = True
        bgw.RunWorkerAsync()
      End Sub
      Function GetFiles(filename As String) As String()
        Return IO.Directory.GetFiles(rootFolder, IO.Path.GetFileName(filename), IO.SearchOption.AllDirectories)
      End Function
      Private Sub bgw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        For Each filefound As String In GetFiles(file)
          Dim proc As New Process()
          With proc
            With .StartInfo
              .FileName = "explorer.exe"
              .Arguments = "/select," & filefound
              ' .WindowStyle = ProcessWindowStyle.Minimized
            End With
            .Start()
            ProcID = .Handle
          End With
        Next
      End Sub
      Private Sub bgw_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
        MessageBox.Show("Finished, Process Handle = " & ProcID.ToString)
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, October 21, 2020 8:12 PM
  • "and will NOT open on top of your main application Form" exactly, I'm looking the opposite as title says...  

    I still want to keep the code I am using in that discussion, avoiding process class for the same reasons I explained in there. To me, process class is freezing the explorer window ( be careful : I am not saying freezeing the form), that's why I want to use Windows api more reliable to do this task.

    Thanks

    Wednesday, October 21, 2020 9:15 PM
  • Hi Mattia Fanti,

    Thanks for your feedback.

    You can refer to the following code to set explorer window topmost.

        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
    
        '...
        Dim inptr = FindWindow("CabinetWClass", Nothing)
        SetWindowPos(inptr, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mattia Fanti Friday, October 23, 2020 1:05 PM
    Friday, October 23, 2020 6:07 AM
  • Hi, I really appreciated you solve my problem!!
    Thanks a lot, this is the completed code:

       <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

    Friday, October 23, 2020 12:36 PM