Hosting Windows Explorer in your application

Started by José Roca, March 26, 2012, 10:47:38 PM

Previous topic - Next topic

José Roca

Requires Vista or Windows 7.

The Explorer Browser object allows developers to host Windows Explorer in their applications.

This is a minimal example. You can also sink to an event class and receive the following events: OnNavigationPending, OnViewCreated, OnNavigationComplete, OnNavigationFailed.

You can also add a toolbar or menu and, in the %WM_COMMAND message, perform one of these actions:


CASE %IDC_PREVIOUSFOLDER
   peb.BrowseToIDList(NULL, %SBSP_PARENT)
CASE %IDC_BACK
   peb.BrowseToIDList(NULL, %SBSP_NAVIGATEBACK)
CASE %IDC_FORWARD
   peb.BrowseToIDList(NULL, %SBSP_NAVIGATEFORWARD)


There are some more options that I haven't yet explored. See:
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

The example:

This example fills the entire client area of the dialog with Explorer, but you can choose the area where to display it passing the wanted position and size with peb.SetRect.


#COMPILE EXE
#DIM ALL
%UNICODE = 1

' // Include files for external files
#INCLUDE ONCE "CWindow.inc"   ' // CWindow class
#INCLUDE ONCE "ShlObj.inc"
#INCLUDE ONCE "ShObjIdl.inc"

GLOBAL peb AS IExplorerBrowser

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Set process DPI aware
   SetProcessDPIAware

   ' // Create an instance of the class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   ' // Note: CW_USEDEFAULT is used as the default value When passing 0's as the width and height
   pWindow.CreateWindow(%NULL, "IExplorerBrowser interface test", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   ' // Set the client size
   pWindow.SetClientSize 500, 320
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of IExplorerBrowser
   peb = NEWCOM CLSID $CLSID_ExplorerBrowser
   IF ISOBJECT(peb) THEN
      peb.SetOptions(%EBO_SHOWFRAMES)
      LOCAL fs AS FOLDERSETTINGS
      fs.ViewMode = %FVM_DETAILS
      LOCAL rc AS RECT
      GetClientRect pWindow.hwnd, rc
      peb.Initialize(pWindow.hwnd, rc, fs)
      ' // Navigate to the Profile folder
      LOCAL pidlBrowse AS DWORD
      IF SUCCEEDED(SHGetFolderLocation(%NULL, %CSIDL_PROFILE, %NULL, 0, pidlBrowse)) THEN
         peb.BrowseToIDList(pidlBrowse, 0)
         ILFree(pidlBrowse)
      END IF
   END IF

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   ' // Process window mesages
   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
         END SELECT

      CASE %WM_SIZE
         ' // If the window isn't minimized, resize it
         IF wParam <> %SIZE_MINIMIZED THEN
            ' // Resize the explorer browser
            LOCAL rc AS RECT
            GetClientRect hwnd, rc
            IF ISOBJECT(peb) THEN peb.SetRect(BYVAL %NULL, rc)
         END IF

      CASE %WM_DESTROY
         ' // Destroy the instance of the Explorer browser
         IF ISOBJECT(peb) THEN peb.Destroy
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================


Maybe a good candidate for another dedicated control?