简体   繁体   English

如何在c#中排列或排序桌面图标?

[英]How do I arrange or sort the desktop icons in c#?

You know when you right-click the desktop and there's a "Sort by" option that allows you to sort the icons by "Name", "Size", "Item Type", or "Date Modified"? 您知道在您的桌面上单击鼠标右键时,有一个“排序依据”选项可以让您按“名称”,“大小”,“项目类型”或“修改日期”对图标进行排序吗? Well, I want to find a way to sort the desktop's icons with a push of a button. 好吧,我想找到一种方法,只需按一下按钮即可对桌面的图标进行排序。

I saw a similar question being asked here on stackoverflow but it's old and the code didn't work for me. 我在这里在stackoverflow上看到了类似的问题,但是它已经很老了,代码对我不起作用。 The link to the question is: Arranging desktop icons with C# . 问题的链接是: 用C#排列桌面图标 I'm trying to achieve this in Windows 10. 我正在尝试在Windows 10中实现这一目标。

There was a comment on there that said that the LVM_* and LVA_* values are stored in the commctrl.h file which comes with the SDK. 上面有一条评论说LVM_ *和LVA_ *值存储在SDK随附的commctrl.h文件中。 I couldn't find that file for some reason. 由于某种原因,我找不到该文件。

Here's what i'm using: 这是我正在使用的:

  //sort desktop
     public const int LVM_ARRANGE = 4118;
     public const int LVM_ALIGNLEFT = 1;


     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     public static extern IntPtr GetDesktopWindow(); 


    [DllImport("user32.dll")]
     public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

     //end of sort desktop 

private void organizeBtn_Click(object sender, EventArgs e)
   {
       var DesktopHandle = GetDesktopWindow();
       MessageBox.Show(GetDesktopWindow().ToString());

       SendMessage(GetDesktopWindow(), LVM_ARRANGE, LVM_ALIGNLEFT, 0);

   }

I've been digging for info or some sort of direction about this topic, especially regarding windows 10, but I can't find much. 我一直在寻找有关此主题的信息或某种方向,特别是有关Windows 10的信息,但我找不到太多。 Please help? 请帮忙?

In Windows 10, the desktop (not tile world!) is still a SysListView32, but the GetDesktopWindow API call will return the handle of its grandparent, a Progman window - reminiscence of the ancient "Program Manager" of Windows 3.0. 在Windows 10中,桌面(不是平铺世界!)仍然是SysListView32,但是GetDesktopWindow API调用将返回其祖父母(一个Progman窗口)的句柄-让人联想起Windows 3.0的古老“程序管理器”。 Then there is a shim of the SHELLDLL_DefView class, and below that you find buried the listview you're after. 然后是SHELLDLL_DefView类的填充程序,在该填充程序的下面,您将找到要隐藏的listview。

Use the information from this answer to move down from the shell window to the folder view, which you can eventually send the LVM_ARRANGE message. 使用此答案中的信息从外壳程序窗口向下移至文件夹视图,您最终可以发送该文件夹视图LVM_ARRANGE消息。

This is a brittle approach, as it relies on undocumented properties of the operating system, which may change at any time with updates or new versions. 这是一种脆弱的方法,因为它依赖于操作系统的未记录属性,该属性可能随时随更新或新版本而变化。 It will probably break also when a user uses a slideshow as desktop background, because Windows then rearranges the desktop window stack. 当用户将幻灯片放映用作桌面背景时,它也可能会中断,因为Windows随后会重新排列桌面窗口堆栈。 Hack to deal with this here . 哈克在这里处理这个问题

Another approach which is documented and less likely to break in future versions, with the downside of involving COM and a nightmare from C#, is via the IFolderView of shell automation, two relevant finds here and here . 记录在案的,并且不太可能在将来的版本中打破的另一种方法是,它涉及COM和C#的噩梦,这是通过shell自动化的IFolderView进行的,这是在此处此处的两个相关发现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM