5

I'm trying to get the number of items on the desktop using win32gui in python 2.7.

The following code: win32gui.SendMessage(win32gui.GetDesktopWindow(), LVM_GETITEMCOUNT) returns zero and I have no idea why.

I wrote win32api.GetLastError() afterwards and it returned zero either.

Thanks in advance.

EDIT: I need to use this method because the final goal is to get the positions of the icons, and it's done by a similar method. So I just wanted to make sure that I know how to use this method. Also, I think that it can give a different output than listing the content of desktop (can it?). And thirdly, my sources of how to get the positions suggested to do it this way - http://www.codeproject.com/Articles/639486/Save-and-restore-icon-positions-on-desktop for example.

EDIT2:

Full code for getting the count (doesn't work for me):

import win32gui
from commctrl import LVM_GETITEMCOUNT
print win32gui.SendMessage(win32gui.GetDesktopWindow(), LVM_GETITEMCOUNT)

Thanks again!

SOLUTION:

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)
Ella Sharakanski
  • 2,683
  • 3
  • 27
  • 47

1 Answers1

2

You can use os.listdir:

import os

len(os.listdir('path/desktop'))
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • I actually want to get the positions of the icons and before that I need the number of icons. I have to use the same method for the positions anyway. Also I'm not sure that these methods will always return the same output - it's a good question. I primarily use this method because my sources suggested so (http://www.codeproject.com/Articles/639486/Save-and-restore-icon-positions-on-desktop for example) – Ella Sharakanski Dec 27 '14 at 02:47
  • what actual code are you using? listdir returns everything on my desktop, I don't use so windows but I see people using `commctrl.LVM_GETITEMCOUNT`? – Padraic Cunningham Dec 27 '14 at 02:58
  • Listdir doesn't help me to get the positions. And to get the positions I need to understand the method of win32gui and messages. – Ella Sharakanski Dec 27 '14 at 03:00
  • well your question was how to get the count so I did answer your question, editing and changing a question is not the way to go – Padraic Cunningham Dec 27 '14 at 03:01
  • My question was about using win32gui in python 2.7. Also I though my question was clear when I posted it, and I edited it when I realized it wasn't clear enough. – Ella Sharakanski Dec 27 '14 at 03:04
  • might be useful http://superuser.com/questions/625854/where-does-windows-store-icon-positions – Padraic Cunningham Dec 27 '14 at 03:13
  • Thanks for really trying to help! This: http://www.codeproject.com/Articles/639486/Save-and-restore-icon-positions-on-desktop says that "Windows reads this information on LogIn and stores in on LogOut" which is not accurate enough for me. – Ella Sharakanski Dec 27 '14 at 03:17
  • can you add the exact code you use to try get the count including imports – Padraic Cunningham Dec 27 '14 at 03:18
  • See EDIT2, just added. – Ella Sharakanski Dec 27 '14 at 03:22
  • if you print `win32gui.GetDesktopWindow()` what does it return? – Padraic Cunningham Dec 27 '14 at 03:40
  • Something reasonable like 65552 – Ella Sharakanski Dec 27 '14 at 03:54
  • 2
    OK the answer is in the second question here, http://stackoverflow.com/questions/1669111/how-do-i-get-the-window-handle-of-the-desktop `GetDesktopWindow()` returns is the actual desktop but you need `FindWindowEx` to access the Explorer desktop where the icons are stored – Padraic Cunningham Dec 27 '14 at 04:08
  • I understand what you say and I'm still trying to get it to work. No luck yet :( – Ella Sharakanski Dec 27 '14 at 18:05
  • Yay I finally got it! I essentially translated the code from the source above. I'll edit my question and add it. Thank you so much! – Ella Sharakanski Dec 27 '14 at 21:26
  • @EllaShar how did you get the locations of the icons? I am trying to do the same thing now. – feltersnach May 24 '18 at 16:43
  • @feltersnach It's a code I wrote in 2014 so I don't remember much about it. Also I don't know a good site for uploading python code. I uploaded it here: https://trinket.io/python/9b0729bdd4 but it doesn't run there since trinket doesn't support all the builtin python modules. Copy the code and run it on your computer, it should work. Only tested on my windows 7 x64 so it probably won't work on windows 10. I got some helper materials I used. Tell me where to send them if you want. – Ella Sharakanski May 25 '18 at 14:18