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
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#.
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.
I am still looking into this and will post the result once I finally get something working. I'm posting this because, thanks indirectly to Davy's post, I also found a classic VB implementation:
Shuffle Desktop Icons Using Interprocess Memory Communication
and that will probably be the basis for my code.
I have no idea about the API, but I know Ultramon (http://www.realtimesoft.com/ultramon/) has a feature included for preserving icon placement (although I've never used it for preserving icon location, it is indispensable for multiple monitor usage). The latest beta release works flawlessly with Vista (except for sometimes having a minor glitch or two when initially logging into my machine via RDP), and of course, haven't had any issues with XP. I've used it for over four years now.
And did I mention that it's the best utility for multiple monitor usage?
来源:https://stackoverflow.com/questions/131690/how-can-i-programmatically-manipulate-windows-desktop-icon-locations