Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arranging desktop icons with C#

Tags:

c#

winapi

Alright guys, so here is what I am trying to achieve:

I want all the disordered desktop icons to arrange themselves towards the top-left of the desktop on a button click.

Here is the code I am using:

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

I believe there are unassigned integers associated with LVM_ARRANGE and LVA_ALIGNLEFT, can anyone please guide me what they are. I am unfamiliar with using dlls, so pardon me if I am asking a stupid question.

Thanks!

Help me guys, I am using the following code and its still not working:

  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();
like image 249
user3860157 Avatar asked Jul 25 '14 22:07

user3860157


1 Answers

I looked around and this sample seemed to work.

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

It came from the following link: http://www.codeproject.com/Messages/1168661/Auto-Arrange-desktop-icons.aspx

Note that from the header file

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

Therefore to align left you want to use the int 1.

like image 127
Alfredo Alvarez Avatar answered Oct 06 '22 00:10

Alfredo Alvarez