Toggle the desktop's autoarrange

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Ive been trying to toggle desktop icons autoarrange feature programatically on the Windows Vista, but I fail to understand why its not having any effect. I dont know whether is working on XP, i didnt test it. Can someone please take a look at the code and try to find what is wrong with it:<br/><br/>
<pre lang="x-c# using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const int GWL_STYLE = (-16);
private const int LVS_AUTOARRANGE = 0x100;
private const int WM_COMMAND = 0x111;
private const int IDM_TOGGLEAUTOARRANGE = 0x7041;
private const int IDM_TOGGLEAUTOALIGN = 0x7054;
public const string nullS = null;

[DllImport("user32", EntryPoint = "GetWindowLongA", CharSet = CharSet.Ansi,
SetLastError = true, ExactSpelling = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32", EntryPoint = "FindWindowA", CharSet = CharSet.Ansi,
SetLastError = true, ExactSpelling = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32", EntryPoint = "FindWindowExA", CharSet = CharSet.Ansi,
SetLastError = true, ExactSpelling = true)]
private static extern IntPtr FindWindowEx(IntPtr hWndParent,
int hWndChildAfter, string lpClassName, string lpWindowName);
[DllImport("user32", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi,
SetLastError = true, ExactSpelling = true)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam,
int lParam);


public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

IntPtr handleDesktop = default(IntPtr);
int styleHandle = 0;

// Get a handle to the desktop listview
handleDesktop = GetSysLVHwnd();

if (handleDesktop.Equals(IntPtr.Zero))
{
return;
}

// Get the style of the listview
styleHandle = GetWindowLong(handleDesktop, GWL_STYLE);

// If the list view is set for auto-arrange turn it off
if ((styleHandle & LVS_AUTOARRANGE) == LVS_AUTOARRANGE)
{
IntPtr handleParent = default(IntPtr);
handleParent = GetParent(handleDesktop);
int dd = SendMessage(handleParent, WM_COMMAND, IDM_TOGGLEAUTOARRANGE, 0);
//MessageBox.Show(dd.ToString());
}

}


private IntPtr GetSysLVHwnd()
{
IntPtr lHandle = default(IntPtr);
lHandle = FindWindow("Progman", nullS);
lHandle = FindWindowEx(lHandle, 0, "SHELLDLL_defVIEW", nullS);
return FindWindowEx(lHandle, 0, "SysListView32", nullS);
}


}
}
[/code]

View the full article
 
Back
Top