用c#排列桌面图标

本文关键字:图标 桌面 排列 | 更新日期: 2023-09-27 18:15:01

好了,伙计们,这就是我想要达到的效果:

我希望所有杂乱的桌面图标都能在点击按钮时排列在桌面的左上角。

下面是我使用的代码:
[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
    IntPtr hwnd = GetDesktopWindow(); //hwnd for desktop
    SendMessage(hwnd, LVM_ARRANGE, LVA_ALIGNLEFT, 0);   
}  

我相信LVM_ARRANGELVA_ALIGNLEFT有未赋值的整数,谁能告诉我它们是什么?我不熟悉使用dll,所以如果我问了一个愚蠢的问题,请原谅我。

谢谢!

帮帮我,伙计们,我正在使用以下代码,它仍然不工作:

  private void button1_Click(object sender, EventArgs e)
    {

        IntPtr hanle = GetHandle();
        IntPtr done;
        done = SendMessage(hanle, LVM_ARRANGE, LVA_ALIGNLEFT, IntPtr.Zero);
    }
    public IntPtr GetHandle()
    {
        hShellWnd = GetShellWindow();
        hDefView = FindWindowEx(hShellWnd, IntPtr.Zero, "SHELLDLL_DefView", null);
        folderView = FindWindowEx(hDefView, IntPtr.Zero,"SysListView32", null);
        return folderView;
    }

    public const int LVM_FIRST =  0x1000;
    public const uint LVM_ARRANGE = LVM_FIRST + 22; 
    //public const IntPtr LVA_SNAPTOGRID = 5;
     IntPtr LVA_ALIGNLEFT = new IntPtr(0x0001);
     IntPtr hShellWnd;
     IntPtr hDefView;
     IntPtr folderView;

    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetDesktopWindow();
    [DllImport("user32.dll")]
    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
    [DllImport("user32.dll")]
    static extern IntPtr GetShellWindow();

用c#排列桌面图标

我四处看了看,这个样本似乎可以工作。

private void button1_Click(object sender, System.EventArgs e)
{
SendMessage(GetDesktopWindow(), LVM_ARRANGE, LVA_SNAPTOGRID , 0);
}

public const int LVM_ARRANGE = 4118;
public const int LVA_SNAPTOGRID = 5;

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

它来自以下链接:http://www.codeproject.com/Messages/1168661/Auto-Arrange-desktop-icons.aspx

注意,从头文件

#define LVA_DEFAULT             0x0000
#define LVA_ALIGNLEFT           0x0001
#define LVA_ALIGNTOP            0x0002
#define LVA_SNAPTOGRID          0x0005

因此,要向左对齐,您需要使用int