def testShellFolder(self):
sf = shell.SHGetDesktopFolder()
names_1 = []
for i in sf: # Magically calls EnumObjects
name = sf.GetDisplayNameOf(i, SHGDN_NORMAL)
names_1.append(name)
# And get the enumerator manually
enum = sf.EnumObjects(0, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN)
names_2 = []
for i in enum:
name = sf.GetDisplayNameOf(i, SHGDN_NORMAL)
names_2.append(name)
names_1.sort()
names_2.sort()
self.assertEqual(names_1, names_2)
python类SHGetDesktopFolder()的实例源码
def testShellFolder(self):
sf = shell.SHGetDesktopFolder()
names_1 = []
for i in sf: # Magically calls EnumObjects
name = sf.GetDisplayNameOf(i, SHGDN_NORMAL)
names_1.append(name)
# And get the enumerator manually
enum = sf.EnumObjects(0, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN)
names_2 = []
for i in enum:
name = sf.GetDisplayNameOf(i, SHGDN_NORMAL)
names_2.append(name)
names_1.sort()
names_2.sort()
self.assertEqual(names_1, names_2)
def __windows_get_dir(win_dir):
from win32com.shell import shell
df = shell.SHGetDesktopFolder()
pidl = df.ParseDisplayName(0, None,
'::%s' % __WINDOWS_FOLDER_GUIDS[win_dir])[1]
return shell.SHGetPathFromIDList(pidl)
def test_parsing_name(self):
sf = shell.SHGetDesktopFolder()
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
children = sf.EnumObjects(0, flags)
child_pidl = children.next()
name = sf.GetDisplayNameOf(child_pidl, shellcon.SHGDN_FORPARSING)
item = shell.SHCreateItemFromParsingName(name, None, shell.IID_IShellItem)
# test the name we get from the item is the same as from the folder.
self.assertEqual(name, item.GetDisplayName(shellcon.SHGDN_FORPARSING))
def test_parsing_relative(self):
desktop_pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
desktop_item = shell.SHCreateItemFromIDList(desktop_pidl, shell.IID_IShellItem)
sf = shell.SHGetDesktopFolder()
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
children = sf.EnumObjects(0, flags)
child_pidl = children.next()
name_flags = shellcon.SHGDN_FORPARSING | shellcon.SHGDN_INFOLDER
name = sf.GetDisplayNameOf(child_pidl, name_flags)
item = shell.SHCreateItemFromRelativeName(desktop_item, name, None,
shell.IID_IShellItem)
# test the name we get from the item is the same as from the folder.
self.assertEqual(name, item.GetDisplayName(name_flags))
def test_create_item_with_parent(self):
desktop_pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
desktop_item = shell.SHCreateItemFromIDList(desktop_pidl, shell.IID_IShellItem)
sf = shell.SHGetDesktopFolder()
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
children = sf.EnumObjects(0, flags)
child_pidl = children.next()
item1 = shell.SHCreateItemWithParent(desktop_pidl, None, child_pidl, shell.IID_IShellItem)
item2 = shell.SHCreateItemWithParent(None, sf, child_pidl, shell.IID_IShellItem)
self.assertShellItemsEqual(item1, item2)
def GetFolderAndPIDLForPath(filename):
desktop = shell.SHGetDesktopFolder()
info = desktop.ParseDisplayName(0, None, os.path.abspath(filename))
cchEaten, pidl, attr = info
# We must walk the ID list, looking for one child at a time.
folder = desktop
while len(pidl) > 1:
this = pidl.pop(0)
folder = folder.BindToObject([this], None, shell.IID_IShellFolder)
# We are left with the pidl for the specific item. Leave it as
# a list, so it remains a valid PIDL.
return folder, pidl
# A cache of pyclbr module objects, so we only parse a given filename once.
def test_parsing_name(self):
sf = shell.SHGetDesktopFolder()
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
children = sf.EnumObjects(0, flags)
child_pidl = children.next()
name = sf.GetDisplayNameOf(child_pidl, shellcon.SHGDN_FORPARSING)
item = shell.SHCreateItemFromParsingName(name, None, shell.IID_IShellItem)
# test the name we get from the item is the same as from the folder.
self.assertEqual(name, item.GetDisplayName(shellcon.SHGDN_FORPARSING))
def test_parsing_relative(self):
desktop_pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
desktop_item = shell.SHCreateItemFromIDList(desktop_pidl, shell.IID_IShellItem)
sf = shell.SHGetDesktopFolder()
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
children = sf.EnumObjects(0, flags)
child_pidl = children.next()
name_flags = shellcon.SHGDN_FORPARSING | shellcon.SHGDN_INFOLDER
name = sf.GetDisplayNameOf(child_pidl, name_flags)
item = shell.SHCreateItemFromRelativeName(desktop_item, name, None,
shell.IID_IShellItem)
# test the name we get from the item is the same as from the folder.
self.assertEqual(name, item.GetDisplayName(name_flags))
def test_create_item_with_parent(self):
desktop_pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
desktop_item = shell.SHCreateItemFromIDList(desktop_pidl, shell.IID_IShellItem)
sf = shell.SHGetDesktopFolder()
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
children = sf.EnumObjects(0, flags)
child_pidl = children.next()
item1 = shell.SHCreateItemWithParent(desktop_pidl, None, child_pidl, shell.IID_IShellItem)
item2 = shell.SHCreateItemWithParent(None, sf, child_pidl, shell.IID_IShellItem)
self.assertShellItemsEqual(item1, item2)
def GetFolderAndPIDLForPath(filename):
desktop = shell.SHGetDesktopFolder()
info = desktop.ParseDisplayName(0, None, os.path.abspath(filename))
cchEaten, pidl, attr = info
# We must walk the ID list, looking for one child at a time.
folder = desktop
while len(pidl) > 1:
this = pidl.pop(0)
folder = folder.BindToObject([this], None, shell.IID_IShellFolder)
# We are left with the pidl for the specific item. Leave it as
# a list, so it remains a valid PIDL.
return folder, pidl
# A cache of pyclbr module objects, so we only parse a given filename once.
def test_parsing_name(self):
sf = shell.SHGetDesktopFolder()
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
children = sf.EnumObjects(0, flags)
child_pidl = next(children)
name = sf.GetDisplayNameOf(child_pidl, shellcon.SHGDN_FORPARSING)
item = shell.SHCreateItemFromParsingName(name, None, shell.IID_IShellItem)
# test the name we get from the item is the same as from the folder.
self.assertEqual(name, item.GetDisplayName(shellcon.SHGDN_FORPARSING))
def test_parsing_relative(self):
desktop_pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
desktop_item = shell.SHCreateItemFromIDList(desktop_pidl, shell.IID_IShellItem)
sf = shell.SHGetDesktopFolder()
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
children = sf.EnumObjects(0, flags)
child_pidl = next(children)
name_flags = shellcon.SHGDN_FORPARSING | shellcon.SHGDN_INFOLDER
name = sf.GetDisplayNameOf(child_pidl, name_flags)
item = shell.SHCreateItemFromRelativeName(desktop_item, name, None,
shell.IID_IShellItem)
# test the name we get from the item is the same as from the folder.
self.assertEqual(name, item.GetDisplayName(name_flags))
def test_create_item_with_parent(self):
desktop_pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
desktop_item = shell.SHCreateItemFromIDList(desktop_pidl, shell.IID_IShellItem)
sf = shell.SHGetDesktopFolder()
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
children = sf.EnumObjects(0, flags)
child_pidl = next(children)
item1 = shell.SHCreateItemWithParent(desktop_pidl, None, child_pidl, shell.IID_IShellItem)
item2 = shell.SHCreateItemWithParent(None, sf, child_pidl, shell.IID_IShellItem)
self.assertShellItemsEqual(item1, item2)
def GetFolderAndPIDLForPath(filename):
desktop = shell.SHGetDesktopFolder()
info = desktop.ParseDisplayName(0, None, os.path.abspath(filename))
cchEaten, pidl, attr = info
# We must walk the ID list, looking for one child at a time.
folder = desktop
while len(pidl) > 1:
this = pidl.pop(0)
folder = folder.BindToObject([this], None, shell.IID_IShellFolder)
# We are left with the pidl for the specific item. Leave it as
# a list, so it remains a valid PIDL.
return folder, pidl
# A cache of pyclbr module objects, so we only parse a given filename once.
def __init__(self):
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
win32con.WM_COMMAND: self.OnCommand,
win32con.WM_SIZE: self.OnSize,
}
# Register the Window class.
wc = win32gui.WNDCLASS()
hinst = wc.hInstance = win32api.GetModuleHandle(None)
wc.lpszClassName = "test_explorer_browser"
wc.lpfnWndProc = message_map # could also specify a wndproc.
classAtom = win32gui.RegisterClass(wc)
# Create the Window.
style = win32con.WS_OVERLAPPEDWINDOW | win32con.WS_VISIBLE
self.hwnd = win32gui.CreateWindow( classAtom, "Python IExplorerBrowser demo", style, \
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
0, 0, hinst, None)
eb = pythoncom.CoCreateInstance(shellcon.CLSID_ExplorerBrowser, None, pythoncom.CLSCTX_ALL, shell.IID_IExplorerBrowser)
# as per MSDN docs, hook up events early
self.event_cookie = eb.Advise(wrap(EventHandler()))
eb.SetOptions(shellcon.EBO_SHOWFRAMES)
rect = win32gui.GetClientRect(self.hwnd)
# Set the flags such that the folders autoarrange and non web view is presented
flags = (shellcon.FVM_LIST, shellcon.FWF_AUTOARRANGE | shellcon.FWF_NOWEBVIEW)
eb.Initialize(self.hwnd, rect, (0, shellcon.FVM_DETAILS))
if len(sys.argv)==2:
# If an arg was specified, ask the desktop parse it.
# You can pass anything explorer accepts as its '/e' argument -
# eg, "::{guid}\::{guid}" etc.
# "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" is "My Computer"
pidl = shell.SHGetDesktopFolder().ParseDisplayName(0, None, sys.argv[1])[1]
else:
# And start browsing at the root of the namespace.
pidl = []
eb.BrowseToIDList(pidl, shellcon.SBSP_ABSOLUTE)
# and for some reason the "Folder" view in the navigator pane doesn't
# magically synchronize itself - so let's do that ourself.
# Get the tree control.
sp = eb.QueryInterface(pythoncom.IID_IServiceProvider)
try:
tree = sp.QueryService(shell.IID_INameSpaceTreeControl,
shell.IID_INameSpaceTreeControl)
except pythoncom.com_error, exc:
# this should really only fail if no "nav" frame exists...
print "Strange - failed to get the tree control even though " \
"we asked for a EBO_SHOWFRAMES"
print exc
else:
# get the IShellItem for the selection.
si = shell.SHCreateItemFromIDList(pidl, shell.IID_IShellItem)
# set it to selected.
tree.SetItemState(si, shellcon.NSTCIS_SELECTED, shellcon.NSTCIS_SELECTED)
#eb.FillFromObject(None, shellcon.EBF_NODROPTARGET);
#eb.SetEmptyText("No known folders yet...");
self.eb = eb
def __init__(self):
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
win32con.WM_COMMAND: self.OnCommand,
win32con.WM_SIZE: self.OnSize,
}
# Register the Window class.
wc = win32gui.WNDCLASS()
hinst = wc.hInstance = win32api.GetModuleHandle(None)
wc.lpszClassName = "test_explorer_browser"
wc.lpfnWndProc = message_map # could also specify a wndproc.
classAtom = win32gui.RegisterClass(wc)
# Create the Window.
style = win32con.WS_OVERLAPPEDWINDOW | win32con.WS_VISIBLE
self.hwnd = win32gui.CreateWindow( classAtom, "Python IExplorerBrowser demo", style, \
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
0, 0, hinst, None)
eb = pythoncom.CoCreateInstance(shellcon.CLSID_ExplorerBrowser, None, pythoncom.CLSCTX_ALL, shell.IID_IExplorerBrowser)
# as per MSDN docs, hook up events early
self.event_cookie = eb.Advise(wrap(EventHandler()))
eb.SetOptions(shellcon.EBO_SHOWFRAMES)
rect = win32gui.GetClientRect(self.hwnd)
# Set the flags such that the folders autoarrange and non web view is presented
flags = (shellcon.FVM_LIST, shellcon.FWF_AUTOARRANGE | shellcon.FWF_NOWEBVIEW)
eb.Initialize(self.hwnd, rect, (0, shellcon.FVM_DETAILS))
if len(sys.argv)==2:
# If an arg was specified, ask the desktop parse it.
# You can pass anything explorer accepts as its '/e' argument -
# eg, "::{guid}\::{guid}" etc.
# "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" is "My Computer"
pidl = shell.SHGetDesktopFolder().ParseDisplayName(0, None, sys.argv[1])[1]
else:
# And start browsing at the root of the namespace.
pidl = []
eb.BrowseToIDList(pidl, shellcon.SBSP_ABSOLUTE)
# and for some reason the "Folder" view in the navigator pane doesn't
# magically synchronize itself - so let's do that ourself.
# Get the tree control.
sp = eb.QueryInterface(pythoncom.IID_IServiceProvider)
try:
tree = sp.QueryService(shell.IID_INameSpaceTreeControl,
shell.IID_INameSpaceTreeControl)
except pythoncom.com_error, exc:
# this should really only fail if no "nav" frame exists...
print "Strange - failed to get the tree control even though " \
"we asked for a EBO_SHOWFRAMES"
print exc
else:
# get the IShellItem for the selection.
si = shell.SHCreateItemFromIDList(pidl, shell.IID_IShellItem)
# set it to selected.
tree.SetItemState(si, shellcon.NSTCIS_SELECTED, shellcon.NSTCIS_SELECTED)
#eb.FillFromObject(None, shellcon.EBF_NODROPTARGET);
#eb.SetEmptyText("No known folders yet...");
self.eb = eb
def __init__(self):
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
win32con.WM_COMMAND: self.OnCommand,
win32con.WM_SIZE: self.OnSize,
}
# Register the Window class.
wc = win32gui.WNDCLASS()
hinst = wc.hInstance = win32api.GetModuleHandle(None)
wc.lpszClassName = "test_explorer_browser"
wc.lpfnWndProc = message_map # could also specify a wndproc.
classAtom = win32gui.RegisterClass(wc)
# Create the Window.
style = win32con.WS_OVERLAPPEDWINDOW | win32con.WS_VISIBLE
self.hwnd = win32gui.CreateWindow( classAtom, "Python IExplorerBrowser demo", style, \
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
0, 0, hinst, None)
eb = pythoncom.CoCreateInstance(shellcon.CLSID_ExplorerBrowser, None, pythoncom.CLSCTX_ALL, shell.IID_IExplorerBrowser)
# as per MSDN docs, hook up events early
self.event_cookie = eb.Advise(wrap(EventHandler()))
eb.SetOptions(shellcon.EBO_SHOWFRAMES)
rect = win32gui.GetClientRect(self.hwnd)
# Set the flags such that the folders autoarrange and non web view is presented
flags = (shellcon.FVM_LIST, shellcon.FWF_AUTOARRANGE | shellcon.FWF_NOWEBVIEW)
eb.Initialize(self.hwnd, rect, (0, shellcon.FVM_DETAILS))
if len(sys.argv)==2:
# If an arg was specified, ask the desktop parse it.
# You can pass anything explorer accepts as its '/e' argument -
# eg, "::{guid}\::{guid}" etc.
# "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" is "My Computer"
pidl = shell.SHGetDesktopFolder().ParseDisplayName(0, None, sys.argv[1])[1]
else:
# And start browsing at the root of the namespace.
pidl = []
eb.BrowseToIDList(pidl, shellcon.SBSP_ABSOLUTE)
# and for some reason the "Folder" view in the navigator pane doesn't
# magically synchronize itself - so let's do that ourself.
# Get the tree control.
sp = eb.QueryInterface(pythoncom.IID_IServiceProvider)
try:
tree = sp.QueryService(shell.IID_INameSpaceTreeControl,
shell.IID_INameSpaceTreeControl)
except pythoncom.com_error as exc:
# this should really only fail if no "nav" frame exists...
print("Strange - failed to get the tree control even though " \
"we asked for a EBO_SHOWFRAMES")
print(exc)
else:
# get the IShellItem for the selection.
si = shell.SHCreateItemFromIDList(pidl, shell.IID_IShellItem)
# set it to selected.
tree.SetItemState(si, shellcon.NSTCIS_SELECTED, shellcon.NSTCIS_SELECTED)
#eb.FillFromObject(None, shellcon.EBF_NODROPTARGET);
#eb.SetEmptyText("No known folders yet...");
self.eb = eb