我正在尝试使用python2.7中的win32gui来获取桌面上的项目数。
下面的代码:win32gui.SendMessage(win32gui.GetDesktopWindow(), LVM_GETITEMCOUNT)
返回零,我不知道为什么。
后来我写了win32api.GetLastError()
,它也是零。
提前谢谢。
编辑:我需要使用这个方法,因为最终的目标是获取图标的位置,并且是通过类似的方法完成的。所以我只想确保我知道如何使用这个方法。此外,我认为它可以提供一个不同于列出桌面内容的输出(可以吗?)第三,我的资料来源,如何获得建议的立场,这样做-例如http://www.codeproject.com/Articles/639486/Save-and-restore-icon-positions-on-desktop。
EDIT2:
获取计数的完整代码(对我不起作用):
import win32gui
from commctrl import LVM_GETITEMCOUNT
print win32gui.SendMessage(win32gui.GetDesktopWindow(), LVM_GETITEMCOUNT)
再次感谢!
解决办法:
import ctypes
from commctrl import LVM_GETITEMCOUNT
import pywintypes
import win32gui
GetShellWindow = ctypes.windll.user32.GetShellWindow
def get_desktop():
"""Get the window of the icons, the desktop window contains this window"""
shell_window = GetShellWindow()
shell_dll_defview = win32gui.FindWindowEx(shell_window, 0, "SHELLDLL_DefView", "")
if shell_dll_defview == 0:
sys_listview_container = []
try:
win32gui.EnumWindows(_callback, sys_listview_container)
except pywintypes.error as e:
if e.winerror != 0:
raise
sys_listview = sys_listview_container[0]
else:
sys_listview = win32gui.FindWindowEx(shell_dll_defview, 0, "SysListView32", "FolderView")
return sys_listview
def _callback(hwnd, extra):
class_name = win32gui.GetClassName(hwnd)
if class_name == "WorkerW":
child = win32gui.FindWindowEx(hwnd, 0, "SHELLDLL_DefView", "")
if child != 0:
sys_listview = win32gui.FindWindowEx(child, 0, "SysListView32", "FolderView")
extra.append(sys_listview)
return False
return True
def get_item_count(window):
return win32gui.SendMessage(window, LVM_GETITEMCOUNT)
desktop = get_desktop()
get_item_count(desktop)
发布于 2014-12-27 02:43:40
您可以使用os.listdir
import os
len(os.listdir('path/desktop'))
https://stackoverflow.com/questions/27663875
复制相似问题