33

Several years back, I innocently tried to write a little app to save my tactically placed desktop icons because I was sick of dragging them back to their locations when some event reset them. I gave up after buring WAY too much time having failed to find a way to query, much less save and reset, my icons' desktop position.

Anyone know where Windows persists this info and if there's an API to set them?

Thanks, Richard

ZeroBugBounce
  • 3,652
  • 3
  • 31
  • 40

2 Answers2

18

If I'm not mistaken the desktop is just a ListView, and you'll have to send the LVM_SETITEMPOSITION message to the handle of the desktop.

I googled a bit for some c# code and couldn't find a example, but I did found the following article. Torry: ...get/set the positions of desktop icons?. It's delphi code, but I find it very readable and with some P/Invokes you'll be able to translate it to c#.

Davy Landman
  • 15,109
  • 6
  • 49
  • 73
  • I started using that code as the basis, but then while googling something from it, found the link I posted as an answer, so I'll credit you with the answer, thanks. – ZeroBugBounce Sep 26 '08 at 16:38
  • Hi, I don´t quite understand how to send the "message"! Furthermore not sure how to implement it - the given example was no help for me, because the written code was not explained. Are you able to give a short example how to get the location of the icons and rearrange them? Thanks for helping – JanF Sep 12 '21 at 02:40
  • [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:06
2

The desktop is just a ListView control and you can get its handle and send messages to it to move icons around using LVM_SETITEMPOSITION.

Getting icon positions using LVMGETITEMPOS is a bit more complicated, though. You have to pass a pointer to a POINT structure as your LPARAM. If you try to do that, you will likely crash Explorer. The problem is you passed it a pointer in your address space, which the control interpreted as a pointer in Explorer's address space. Ouch!

The solution I've used is to inject a DLL into the Explorer process and send the message from there. Then you just have to have a way to get the position info back to your process.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • 3
    Note that you may not need to inject a DLL: you can send the message from out of proc, and use VirtualAllocEx and Read/WriteProcessMemory to set up the memory in explorer's process space, so that you have a valid LPARAM pointer (from explorer's point of view) to use. This way avoids having to communicate back to your own process or deal with a separate DLL. – BrendanMcK Jul 28 '11 at 23:16
  • [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:06