To find the location and size of desktop icons on Windows 10 using Python, you can use the win32gui module from the pywin32 library. Here's an example code snippet that demonstrates how to retrieve the location and size of desktop icons:
python
import win32gui
def get_desktop_icon_info():
# Get the handle of the desktop window
desktop_handle = win32gui.GetDesktopWindow()
# Get the handle of the child window (desktop icons)
child_handle = win32gui.FindWindowEx(desktop_handle, None, "SysListView32", None)
# Get the number of desktop icons
num_icons = win32gui.SendMessage(child_handle, 0x1004, 0, 0)
# Iterate over the desktop icons
for i in range(num_icons):
# Get the icon's location and size
icon_info = win32gui.GetWindowPlacement(child_handle)
icon_rect = icon_info[4]
left, top, right, bottom = icon_rect
# Calculate the width and height of the icon
width = right - left
height = bottom - top
# Print the icon's location and size
print(f"Icon {i + 1}:")
print(f" Location: ({left}, {top})")
print(f" Size: {width} x {height}")
# Call the function to get desktop icon information
get_desktop_icon_info()
Here's how the code works:
We import the win32gui module from the pywin32 library, which provides access to Windows GUI functions.
We define a function called get_desktop_icon_info() to retrieve the location and size of desktop icons.
Inside the function, we first get the handle of the desktop window using win32gui.GetDesktopWindow().
We then find the handle of the child window that represents the desktop icons using win32gui.FindWindowEx(). The class name of the desktop icons window is "SysListView32".
We retrieve the number of desktop icons using win32gui.SendMessage() with the message code 0x1004 (which represents LVM_GETITEMCOUNT).
We iterate over the desktop icons using a for loop from 0 to num_icons - 1.
For each icon, we retrieve its location and size using win32gui.GetWindowPlacement(). The returned icon_info tuple contains the icon's placement information, and we extract the icon's rectangle coordinates from icon_info[4].
We calculate the width and height of the icon by subtracting the left coordinate from the right coordinate and the top coordinate from the bottom coordinate, respectively.
Finally, we print the icon's location and size for each desktop icon.
Note: Make sure you have the pywin32 library installed before running this code. You can install it using pip install pywin32.
Keep in mind that this code relies on the specific window class name "SysListView32" for desktop icons, which may vary depending on the Windows version or system configuration. Additionally, this code retrieves the location and size of the icons, but it doesn't provide information about the icon's file name or other properties.