【发布时间】:2011-11-18 03:52:04
【问题描述】:
我希望创建一个启动作业,每次我的 Windows 启动时,它都会将一些快捷方式图标从我的桌面重新排列到另一个位置,例如右下角。
我可以使用 VBScript、Powershell、bat 命令脚本甚至 C\C++\C#\Java 来制作它吗?
【问题讨论】:
标签: windows scripting icons shortcuts
我希望创建一个启动作业,每次我的 Windows 启动时,它都会将一些快捷方式图标从我的桌面重新排列到另一个位置,例如右下角。
我可以使用 VBScript、Powershell、bat 命令脚本甚至 C\C++\C#\Java 来制作它吗?
【问题讨论】:
标签: windows scripting icons shortcuts
我来晚了,但是这段代码对我有用,我希望它可以帮助别人。它在 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;
}
【讨论】:
桌面是一个普通的列表视图,因此您可以使用 windows api 将项目移动到不同的位置。看看这个类似的问题:How can I programmatically manipulate Windows desktop icon locations?
【讨论】: