如何以编程方式将 Windows 桌面图标的位置移动?

9
我希望创建一个启动项,在每次 Windows 启动时,将桌面上的某些快捷方式图标重新排列到另一个位置,如右下角等。是否可以使用 VBScript、PowerShell、批处理脚本或甚至是 C\C++\C#\Java 来实现呢?

请查看这个答案:https://dev59.com/BXVC5IYBdhLWcg3w-WSs - Pepe
可能是如何以编程方式操作Windows桌面图标位置?的重复问题。 - Ben Voigt
这个回答解决了你的问题吗?如何使用C语言中的WinAPI移动桌面图标? - IInspectable
2个回答

2

“操作桌面图标的位置”(Manipulating the positions of desktop icons)介绍了正确的方法,而不是依赖于实现细节(这些细节可能会发生变化)。 - IInspectable

1

虽然我来晚了,但是这段代码对我很有效,希望能帮到其他人。它使用了c++17

#include <windows.h>
#include <commctrl.h>
#include <ShlObj.h>

int desktop_shuffle() {
    // You must get the handle of desktop's listview and then you can reorder that listview (which contains the icons).
    HWND progman = FindWindow(L"progman", NULL);
    HWND shell = FindWindowEx(progman, NULL, L"shelldll_defview", NULL);
    HWND hwndListView = FindWindowEx(shell, NULL, L"syslistview32", NULL);
    int nIcons = ListView_GetItemCount(hwndListView);

    POINT* icon_positions = new POINT[nIcons];

    // READ THE CURRENT ICONS'S POSITIONS
    if (nIcons > 0) {
        // We must use desktop's virtual memory to get the icons positions, otherwise you won't be able to
        // read their x, y positions.
        DWORD desktop_proc_id = 0;
        GetWindowThreadProcessId(hwndListView, &desktop_proc_id);
        HANDLE h_process = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, desktop_proc_id);
        if (!h_process)
        {
            printf("OpenProcess: Error while opening desktop UI process\n");
            return -1;
        }

        LPPOINT pt = (LPPOINT)VirtualAllocEx(h_process, NULL, sizeof(POINT), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
        if (!pt)
        {
            CloseHandle(h_process);
            printf("VirtualAllocEx: Error while allocating memory in desktop UI process\n");
            return -1;
        }

        for (int i = 0; i < nIcons; i++)
        {
            if (!ListView_GetItemPosition(hwndListView, i, pt))
            {
                printf("GetItemPosition: Error while retrieving desktop icon (%d) position\n", i);
                continue;
            }

            if (!ReadProcessMemory(h_process, pt, &icon_positions[i], sizeof(POINT), nullptr))
            {
                printf("ReadProcessMemory: Error while reading desktop icon (%d) positions\n", i);
                continue;
            }
        }

        VirtualFreeEx(h_process, pt, 0, MEM_RELEASE);
        CloseHandle(h_process);
    }

    // UPDATE THE ICONS'S POSITIONS
    for (int i = 0; i < nIcons; i++) {
        ListView_SetItemPosition(hwndListView, i, rand(), rand());
    }

    return 0;
}

1
“操作桌面图标位置”的文章解释了如何正确地进行此操作,而不依赖于实现细节(这些细节可能已经发生了变化)。 - IInspectable

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接