-1

Here's the code I'm using:

private void Button_Click(object sender, RoutedEventArgs e) {
    SendMessage(GetDesktopWindow(), LVM_ARRANGE, LVA_SNAPTOGRID, 0);
}

public const uint LVM_ARRANGE = 0x1000 + 22;
public const int LVA_SNAPTOGRID = 0x0005;

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

Nothing happens when I run it. Code is borrowed from https://www.codeproject.com/Messages/1168961/Re-Auto-Arrange-desktop-icons.aspx

Tried different Windows versions too.

gkman
  • 108
  • 1
  • 5
dmitry.sergeyev
  • 439
  • 5
  • 11
  • That's a user setting. You don't get to modify it from code. – David Heffernan May 08 '17 at 17:49
  • @DavidHeffernan I don't think so. It can certainly be modified, there's documentation on it. – dmitry.sergeyev May 08 '17 at 19:42
  • Oh. Where is the documentation? – David Heffernan May 08 '17 at 19:51
  • @DavidHeffernan https://msdn.microsoft.com/en-us/library/windows/desktop/bb774884(v=vs.85).aspx for example. I can easily disable auto icon arrangement, but not this – dmitry.sergeyev May 08 '17 at 19:52
  • That's for list view controls that you own. There's no reason to believe that you are entitled to do that to somebody else's list view. You aren't. The user already has a means to do this. It's part of the shell UI. – David Heffernan May 08 '17 at 20:03
  • @DavidHeffernanI How would you explain the fact that I am able to toggle auto arrange? And I can see posts on the web where people do that, not to mention the answer below. – dmitry.sergeyev May 08 '17 at 20:04
  • Because they are hacking at undocumented implementation details. Find some documentation that says you can do this at arbitrary times to a shell control and I will concede. If there was a supported way, then it would be part of the shell API. We get a lot of these questions every day. Feel free to hack but don't kid yourself this is supported. – David Heffernan May 08 '17 at 20:05

1 Answers1

0

This code worked for me. Note that it doesn't appear to change the options, so although it arranges to the grid for me the View->Align Icons To Grid is still unchecked. The issue is outlined here: How do I get the window handle of the desktop?

Your problem results from a fairly widespread confusion over what the desktop window actually is. The GetDesktopWindow function does precisely what it's documented to do: it returns a handle to the desktop window. This, however, is not the same window that contains the desktop icons.

So I used the answer here: Get handle to desktop / shell window but with replacing or adding methods and p/invoke calls until I had everything necessary because that link only gives method calls and not the dll imports (I may have left some superfluous stuff in here).

static void Main()
{
    SendMessage(GetDesktopWindow(DesktopWindow.SysListView32), LVM_ARRANGE, LVA_SNAPTOGRID, 0);
    Console.ReadLine();
}

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

[DllImport("user32.dll")]
static extern IntPtr GetShellWindow();

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);

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

[DllImport("user32.dll")]
private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);


[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetWindowTextLength(IntPtr hWnd);

public enum DesktopWindow
{
    ProgMan,
    SHELLDLL_DefViewParent,
    SHELLDLL_DefView,
    SysListView32
}

public static IntPtr GetDesktopWindow(DesktopWindow desktopWindow)
{
    IntPtr _ProgMan = GetShellWindow();
    IntPtr _SHELLDLL_DefViewParent = _ProgMan;
    IntPtr _SHELLDLL_DefView = FindWindowEx(_ProgMan, IntPtr.Zero, "SHELLDLL_DefView", null);
    IntPtr _SysListView32 = FindWindowEx(_SHELLDLL_DefView, IntPtr.Zero, "SysListView32", "FolderView");

    if (_SHELLDLL_DefView == IntPtr.Zero)
    {
        EnumWindows((hwnd, lParam) =>
        {
            if (GetWindowText(hwnd) == "WorkerW")
            {
                IntPtr child = FindWindowEx(hwnd, IntPtr.Zero, "SHELLDLL_DefView", null);
                if (child != IntPtr.Zero)
                {
                    _SHELLDLL_DefViewParent = hwnd;
                    _SHELLDLL_DefView = child;
                    _SysListView32 = FindWindowEx(child, IntPtr.Zero, "SysListView32", "FolderView"); ;
                    return false;
                }
            }
            return true;
        }, IntPtr.Zero);
    }

    switch (desktopWindow)
    {
        case DesktopWindow.ProgMan:
            return _ProgMan;
        case DesktopWindow.SHELLDLL_DefViewParent:
            return _SHELLDLL_DefViewParent;
        case DesktopWindow.SHELLDLL_DefView:
            return _SHELLDLL_DefView;
        case DesktopWindow.SysListView32:
            return _SysListView32;
        default:
            return IntPtr.Zero;
    }
}

public static string GetWindowText(IntPtr hWnd)
{
    int size = GetWindowTextLength(hWnd);
    if (size > 0)
    {
        var builder = new StringBuilder(size + 1);
        GetWindowText(hWnd, builder, builder.Capacity);
        return builder.ToString();
    }

    return String.Empty;
}
Community
  • 1
  • 1
Quantic
  • 1,779
  • 19
  • 30
  • Thanks! I appreciate the help! It doesn't seem to work for me, though. I'm running 3 monitors, windows 10 x64 if that matters.Additionally, I've tried enabling 'align to grid' and disabling it thru the app--same result. – dmitry.sergeyev May 08 '17 at 19:39
  • I also tried disabling the slideshow on the background – dmitry.sergeyev May 08 '17 at 19:48
  • @Ebanashka Sorry I don't know how to fix those problems. Really this is a poor answer because I did what you aren't supposed to do: copied code from 3 different answers without writing or understanding hardly any of it. But you seem to understand even less than me so I feel like you should move on to other endeavors. – Quantic May 12 '17 at 15:15