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

发布于 2024-12-04 13:32:19 字数 135 浏览 3 评论 0原文

我希望创建一个启动作业,每次我的 Windows 启动时,它都会将一些快捷方式图标从我的桌面重新排列到另一个位置,例如右下角。

我可以使用 VBScript、Powershell、bat 命令脚本甚至 C\C++\C#\Java 来实现吗?

I wish to create a startup job that every time that my Windows starts, it will rearrange some shortcut icons from my desktop to another location, such as right-bottom for example.

Can I make it with VBScript, Powershell, bat command script or even with C\C++\C#\Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坐在坟头思考人生 2024-12-11 13:32:19

桌面是一个普通的列表视图,因此您可以使用 Windows api 将项目移动到不同的位置。看看这个类似的问题:How can I programmatically Operate Windows桌面图标位置?

Desktop is an ordinary listview so you can use windows api to move items to different locations. Have a look at this similar question: How can I programmatically manipulate Windows desktop icon locations?

漫雪独思 2024-12-11 13:32:19

我来晚了,但这段代码对我有用,我希望它可以帮助别人。它是用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;
}

I come late, but this piece of code works for me and I hope it may help somebody. It's in 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文