如何在C#中排列或排序桌面图标? [英] How do I arrange or sort the desktop icons in c#?

查看:228
本文介绍了如何在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.

我在这里看到了关于stackoverflow的类似问题,但是它很旧,并且代码对我不起作用.问题的链接是:使用C#排列桌面图标.我正在尝试在Windows 10中实现这一目标.

I saw a similar question being asked here on stackoverflow but it's old and the code didn't work for me. The link to the question is: Arranging desktop icons with C# . I'm trying to achieve this in Windows 10.

有一条评论说LVM_ *和LVA_ *值存储在SDK随附的commctrl.h文件中.由于某种原因,我找不到该文件.

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. I couldn't find that file for some reason.

这就是我正在使用的:

  //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);

   }

我一直在寻找有关此主题的信息或某种方向,特别是有关Windows 10的信息,但我找不到很多东西.请帮忙吗?

I've been digging for info or some sort of direction about this topic, especially regarding windows 10, but I can't find much. Please help?

推荐答案

在Windows 10中,桌面(不是平铺世界!)仍然是SysListView32,但是GetDesktopWindow API调用将返回其祖父母的句柄,即Progman窗口-让人联想起Windows 3.0的古老程序管理器".然后有一个SHELLDLL_DefView类的垫片,在它的下面,您可以找到要隐藏的列表视图.

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. Then there is a shim of the SHELLDLL_DefView class, and below that you find buried the listview you're after.

使用此答案中的信息从外壳程序窗口向下移至文件夹视图,您最终可以将其发送LVM_ARRANGE消息.

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.

这是一种脆弱的方法,因为它依赖于操作系统的未记录属性,该属性可能随时随更新或新版本而变化.当用户使用幻灯片放映作为桌面背景时,它也可能会中断,因为Windows随后会重新排列桌面窗口堆栈.请在此处处理.

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. Hack to deal with this here.

有文献记载的另一种方法是在外壳自动化中使用IFolderView,这涉及到COM和C#的噩梦,这在以后的版本中不太可能打破,这涉及两个相关的发现此处.

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.

这篇关于如何在C#中排列或排序桌面图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆