Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

viewstate.py 2.3KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. Demonstrates how to propagate a folder's view state to all its subfolders
  3. The format of the ColInfo stream is apparently undocumented, but
  4. it can be read raw from one folder and copied to another's view state.
  5. """
  6. import os
  7. import sys
  8. import pythoncom
  9. from win32com.shell import shell, shellcon
  10. template_folder = os.path.split(sys.executable)[0]
  11. print("Template folder:", template_folder)
  12. template_pidl = shell.SHILCreateFromPath(template_folder, 0)[0]
  13. template_pb = shell.SHGetViewStatePropertyBag(
  14. template_pidl,
  15. "Shell",
  16. shellcon.SHGVSPB_FOLDERNODEFAULTS,
  17. pythoncom.IID_IPropertyBag,
  18. )
  19. # Column info has to be read as a stream
  20. # This may blow up if folder has never been opened in Explorer and has no ColInfo yet
  21. template_iunk = template_pb.Read("ColInfo", pythoncom.VT_UNKNOWN)
  22. template_stream = template_iunk.QueryInterface(pythoncom.IID_IStream)
  23. streamsize = template_stream.Stat()[2]
  24. template_colinfo = template_stream.Read(streamsize)
  25. def update_colinfo(not_used, dir_name, fnames):
  26. for fname in fnames:
  27. full_fname = os.path.join(dir_name, fname)
  28. if os.path.isdir(full_fname):
  29. print(full_fname)
  30. pidl = shell.SHILCreateFromPath(full_fname, 0)[0]
  31. pb = shell.SHGetViewStatePropertyBag(
  32. pidl,
  33. "Shell",
  34. shellcon.SHGVSPB_FOLDERNODEFAULTS,
  35. pythoncom.IID_IPropertyBag,
  36. )
  37. ## not all folders already have column info, and we're replacing it anyway
  38. pb.Write("ColInfo", template_stream)
  39. iunk = pb.Read("ColInfo", pythoncom.VT_UNKNOWN)
  40. s = iunk.QueryInterface(pythoncom.IID_IStream)
  41. s.Write(template_colinfo)
  42. s = None
  43. ## attribute names read from registry, can't find any way to enumerate IPropertyBag
  44. for attr in (
  45. "Address",
  46. "Buttons",
  47. "Col",
  48. "Vid",
  49. "WFlags",
  50. "FFlags",
  51. "Sort",
  52. "SortDir",
  53. "ShowCmd",
  54. "FolderType",
  55. "Mode",
  56. "Rev",
  57. ):
  58. pb.Write(attr, template_pb.Read(attr))
  59. pb = None
  60. os.path.walk(template_folder, update_colinfo, None)