C# 如何以编程方式操作 Windows 桌面图标位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/131690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I programmatically manipulate Windows desktop icon locations?
提问by ZeroBugBounce
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?
任何人都知道 Windows 在哪里保存此信息,以及是否有 API 来设置它们?
Thanks, Richard
谢谢,理查德
采纳答案by Davy Landman
If I'm not mistaken the desktop is just a ListView, and you'll have to send the LVM_SETITEMPOSITIONmessage to the handle of the desktop.
如果我没记错的话,桌面只是一个 ListView,你必须将LVM_SETITEMPOSITION消息发送到桌面的句柄。
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#.
我在谷歌上搜索了一些 c# 代码并找不到示例,但我确实找到了以下文章。Torry: ...获取/设置桌面图标的位置?. 它是 delphi 代码,但我发现它非常易读,并且通过一些 P/Invokes,您将能够将其转换为 c#。
回答by Ted
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.
我对 API 一无所知,但我知道 Ultramon ( http://www.realtimesoft.com/ultramon/) 具有用于保留图标位置的功能(尽管我从未使用它来保留图标位置,但它是必不可少的用于多显示器使用)。最新的 beta 版本与 Vista 完美配合(除了在最初通过 RDP 登录我的机器时有时会出现一两个小故障),当然,XP 没有任何问题。我已经用了四年多了。
And did I mention that it's the best utility for multiple monitor usage?
我有没有提到它是多显示器使用的最佳实用程序?
回答by Ferruccio
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.
桌面只是一个 ListView 控件,您可以获取其句柄并向其发送消息以使用 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!
不过,使用 LVMGETITEMPOS 获取图标位置要复杂一些。您必须将指向 POINT 结构的指针作为 LPARAM 传递。如果您尝试这样做,您很可能会导致资源管理器崩溃。问题是您在地址空间中传递了一个指针,控件将其解释为资源管理器地址空间中的指针。哎哟!
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.
我使用的解决方案是将一个 DLL 注入资源管理器进程并从那里发送消息。然后您只需要有一种方法将位置信息返回到您的流程中。
回答by ZeroBugBounce
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:
我仍在研究这个问题,一旦我终于开始工作,就会发布结果。我发布这个是因为,间接感谢 Davy 的帖子,我还找到了一个经典的 VB 实现:
Shuffle Desktop Icons Using Interprocess Memory Communication
and that will probably be the basis for my code.
这可能是我代码的基础。