Reputation: 1568
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?
Upvotes: 9
Views: 12302
Reputation: 186
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;
}
Upvotes: 1
Reputation: 30883
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?
Upvotes: 3