9

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?

Diogo
  • 1,527
  • 7
  • 19
  • 29
  • Check this answer out http://stackoverflow.com/questions/131690/how-can-i-programmatically-manipulate-windows-desktop-icon-locations – Pepe Sep 15 '11 at 19:06
  • Possible duplicate of [How can I programmatically manipulate Windows desktop icon locations?](https://stackoverflow.com/questions/131690/how-can-i-programmatically-manipulate-windows-desktop-icon-locations) – Ben Voigt Sep 27 '19 at 14:11
  • Does this answer your question? [How to move desktop icons using winapi on C?](https://stackoverflow.com/questions/76020228/how-to-move-desktop-icons-using-winapi-on-c) – IInspectable Apr 18 '23 at 11:04

2 Answers2

2

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?

Community
  • 1
  • 1
Giorgi
  • 30,270
  • 13
  • 89
  • 125
  • [Manipulating the positions of desktop icons](https://devblogs.microsoft.com/oldnewthing/20130318-00/?p=4933) explains the *proper* way of doing this, without relying on implementation details (that can, and have, change). – IInspectable Apr 15 '23 at 07:05
1

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;
}
eineComadreja
  • 166
  • 1
  • 7
  • 1
    [Manipulating the positions of desktop icons](https://devblogs.microsoft.com/oldnewthing/20130318-00/?p=4933) explains the *proper* way of doing this, without relying on implementation details (that can, and have, change). – IInspectable Apr 15 '23 at 07:05