The following C++ code will find a file named "test.txt" on the desktop and move the icon 100 pixels to the right. Code is heavily based on Manipulating the positions of desktop icons by Raymond Chen.
#include <windows.h>
#include <shlobj.h>
#include <atlbase.h>
// Class to help with object destruction
// https://devblogs.microsoft.com/oldnewthing/20040520-00/?p=39243
class CCoInitialize {
public:
CCoInitialize() : m_hr(CoInitialize(NULL)) { }
~CCoInitialize() { if (SUCCEEDED(m_hr)) CoUninitialize(); }
operator HRESULT() const { return m_hr; }
HRESULT m_hr;
};
// Get shell view for the desktop
// https://devblogs.microsoft.com/oldnewthing/20130318-00/?p=4933
void FindDesktopFolderView(REFIID riid, void** ppv)
{
CComPtr<IShellWindows> spShellWindows;
spShellWindows.CoCreateInstance(CLSID_ShellWindows);
CComVariant vtLoc(CSIDL_DESKTOP);
CComVariant vtEmpty;
long lhwnd;
CComPtr<IDispatch> spdisp;
spShellWindows->FindWindowSW(
&vtLoc, &vtEmpty,
SWC_DESKTOP, &lhwnd, SWFO_NEEDDISPATCH, &spdisp);
CComPtr<IShellBrowser> spBrowser;
CComQIPtr<IServiceProvider>(spdisp)->
QueryService(SID_STopLevelBrowser,
IID_PPV_ARGS(&spBrowser));
CComPtr<IShellView> spView;
spBrowser->QueryActiveShellView(&spView);
spView->QueryInterface(riid, ppv);
}
int main()
{
CCoInitialize init;
CComPtr<IFolderView> spView;
FindDesktopFolderView(IID_PPV_ARGS(&spView));
CComPtr<IShellFolder> spFolder;
spView->GetFolder(IID_PPV_ARGS(&spFolder));
CComPtr<IEnumIDList> spEnum;
spView->Items(SVGIO_ALLVIEW, IID_PPV_ARGS(&spEnum));
// Iterate through desktop icons
for (CComHeapPtr<ITEMID_CHILD> spidl;
spEnum->Next(1, &spidl, nullptr) == S_OK;
spidl.Free()) {
STRRET str;
// Check icon display name
spFolder->GetDisplayNameOf(spidl, SHGDN_NORMAL, &str);
CComHeapPtr<wchar_t> spszName;
StrRetToStr(&str, spidl, &spszName);
if (wcscmp(spszName, L"test.txt") == 0)
{
// Get icon position
POINT pt;
spView->GetItemPosition(spidl, &pt);
// Move icon 100 pixels right
pt.x += 100;
PCITEMID_CHILD apidl[1] = { spidl };
spView->SelectAndPositionItems(1, apidl, &pt, SVSI_POSITIONITEM);
break;
};
}
return 0;
}
Computer
,Recycle Bin
, etc. show as icons but are not present in the folder – Hipparch