browse_for_folder.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # A couple of samples using SHBrowseForFolder
  2. import sys, os
  3. from win32com.shell import shell, shellcon
  4. import win32gui
  5. # A callback procedure - called by SHBrowseForFolder
  6. def BrowseCallbackProc(hwnd, msg, lp, data):
  7. if msg== shellcon.BFFM_INITIALIZED:
  8. win32gui.SendMessage(hwnd, shellcon.BFFM_SETSELECTION, 1, data)
  9. elif msg == shellcon.BFFM_SELCHANGED:
  10. # Set the status text of the
  11. # For this message, 'lp' is the address of the PIDL.
  12. pidl = shell.AddressAsPIDL(lp)
  13. try:
  14. path = shell.SHGetPathFromIDList(pidl)
  15. win32gui.SendMessage(hwnd, shellcon.BFFM_SETSTATUSTEXT, 0, path)
  16. except shell.error:
  17. # No path for this PIDL
  18. pass
  19. if __name__=='__main__':
  20. # Demonstrate a dialog with the cwd selected as the default - this
  21. # must be done via a callback function.
  22. flags = shellcon.BIF_STATUSTEXT
  23. shell.SHBrowseForFolder(0, # parent HWND
  24. None, # root PIDL.
  25. "Default of %s" % os.getcwd(), # title
  26. flags, # flags
  27. BrowseCallbackProc, # callback function
  28. os.getcwd() # 'data' param for the callback
  29. )
  30. # Browse from this directory down only.
  31. # Get the PIDL for the cwd.
  32. desktop = shell.SHGetDesktopFolder()
  33. cb, pidl, extra = desktop.ParseDisplayName(0, None, os.getcwd())
  34. shell.SHBrowseForFolder(0, # parent HWND
  35. pidl, # root PIDL.
  36. "From %s down only" % os.getcwd(), # title
  37. )