explorer_browser.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # A sample of using Vista's IExplorerBrowser interfaces...
  2. # Currently doesn't quite work:
  3. # * CPU sits at 100% while running.
  4. import sys
  5. import pythoncom
  6. from win32com.shell import shell, shellcon
  7. import win32gui, win32con, win32api
  8. from win32com.server.util import wrap, unwrap
  9. # event handler for the browser.
  10. IExplorerBrowserEvents_Methods = """OnNavigationComplete OnNavigationFailed
  11. OnNavigationPending OnViewCreated""".split()
  12. class EventHandler:
  13. _com_interfaces_ = [shell.IID_IExplorerBrowserEvents]
  14. _public_methods_ = IExplorerBrowserEvents_Methods
  15. def OnNavigationComplete(self, pidl):
  16. print "OnNavComplete", pidl
  17. def OnNavigationFailed(self, pidl):
  18. print "OnNavigationFailed", pidl
  19. def OnNavigationPending(self, pidl):
  20. print "OnNavigationPending", pidl
  21. def OnViewCreated(self, view):
  22. print "OnViewCreated", view
  23. # And if our demo view has been registered, it may well
  24. # be that view!
  25. try:
  26. pyview = unwrap(view)
  27. print "and look - its a Python implemented view!", pyview
  28. except ValueError:
  29. pass
  30. class MainWindow:
  31. def __init__(self):
  32. message_map = {
  33. win32con.WM_DESTROY: self.OnDestroy,
  34. win32con.WM_COMMAND: self.OnCommand,
  35. win32con.WM_SIZE: self.OnSize,
  36. }
  37. # Register the Window class.
  38. wc = win32gui.WNDCLASS()
  39. hinst = wc.hInstance = win32api.GetModuleHandle(None)
  40. wc.lpszClassName = "test_explorer_browser"
  41. wc.lpfnWndProc = message_map # could also specify a wndproc.
  42. classAtom = win32gui.RegisterClass(wc)
  43. # Create the Window.
  44. style = win32con.WS_OVERLAPPEDWINDOW | win32con.WS_VISIBLE
  45. self.hwnd = win32gui.CreateWindow( classAtom, "Python IExplorerBrowser demo", style, \
  46. 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
  47. 0, 0, hinst, None)
  48. eb = pythoncom.CoCreateInstance(shellcon.CLSID_ExplorerBrowser, None, pythoncom.CLSCTX_ALL, shell.IID_IExplorerBrowser)
  49. # as per MSDN docs, hook up events early
  50. self.event_cookie = eb.Advise(wrap(EventHandler()))
  51. eb.SetOptions(shellcon.EBO_SHOWFRAMES)
  52. rect = win32gui.GetClientRect(self.hwnd)
  53. # Set the flags such that the folders autoarrange and non web view is presented
  54. flags = (shellcon.FVM_LIST, shellcon.FWF_AUTOARRANGE | shellcon.FWF_NOWEBVIEW)
  55. eb.Initialize(self.hwnd, rect, (0, shellcon.FVM_DETAILS))
  56. if len(sys.argv)==2:
  57. # If an arg was specified, ask the desktop parse it.
  58. # You can pass anything explorer accepts as its '/e' argument -
  59. # eg, "::{guid}\::{guid}" etc.
  60. # "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" is "My Computer"
  61. pidl = shell.SHGetDesktopFolder().ParseDisplayName(0, None, sys.argv[1])[1]
  62. else:
  63. # And start browsing at the root of the namespace.
  64. pidl = []
  65. eb.BrowseToIDList(pidl, shellcon.SBSP_ABSOLUTE)
  66. # and for some reason the "Folder" view in the navigator pane doesn't
  67. # magically synchronize itself - so let's do that ourself.
  68. # Get the tree control.
  69. sp = eb.QueryInterface(pythoncom.IID_IServiceProvider)
  70. try:
  71. tree = sp.QueryService(shell.IID_INameSpaceTreeControl,
  72. shell.IID_INameSpaceTreeControl)
  73. except pythoncom.com_error, exc:
  74. # this should really only fail if no "nav" frame exists...
  75. print "Strange - failed to get the tree control even though " \
  76. "we asked for a EBO_SHOWFRAMES"
  77. print exc
  78. else:
  79. # get the IShellItem for the selection.
  80. si = shell.SHCreateItemFromIDList(pidl, shell.IID_IShellItem)
  81. # set it to selected.
  82. tree.SetItemState(si, shellcon.NSTCIS_SELECTED, shellcon.NSTCIS_SELECTED)
  83. #eb.FillFromObject(None, shellcon.EBF_NODROPTARGET);
  84. #eb.SetEmptyText("No known folders yet...");
  85. self.eb = eb
  86. def OnCommand(self, hwnd, msg, wparam, lparam):
  87. pass
  88. def OnDestroy(self, hwnd, msg, wparam, lparam):
  89. print "tearing down ExplorerBrowser..."
  90. self.eb.Unadvise(self.event_cookie)
  91. self.eb.Destroy()
  92. self.eb = None
  93. print "shutting down app..."
  94. win32gui.PostQuitMessage(0)
  95. def OnSize(self, hwnd, msg, wparam, lparam):
  96. x = win32api.LOWORD(lparam)
  97. y = win32api.HIWORD(lparam)
  98. self.eb.SetRect(None, (0, 0, x, y))
  99. def main():
  100. w=MainWindow()
  101. win32gui.PumpMessages()
  102. if __name__=='__main__':
  103. main()