使用C#排列桌面图标

时间:2014-07-25 22:56:05

标签: c# winapi

好吧,伙计们,所以这就是我想要实现的目标:

我希望所有无序的桌面图标在点击按钮时排列在桌面的左上角。

以下是我正在使用的代码:

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

1 个答案:

答案 0 :(得分:1)

我环顾四周,这个样本似乎有效。

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 1。