不能切换“对齐图标网格”中的桌面

问题描述:

这里的是我正在使用的代码:当我运行它不能切换“对齐图标网格”中的桌面

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

什么也没有发生。代码是从https://www.codeproject.com/Messages/1168961/Re-Auto-Arrange-desktop-icons.aspx

尝试了不同的Windows版本。

+0

这是用户设置。你不能从代码中修改它。 –

+0

@DavidHeffernan我不这么认为。它当然可以修改,它上面有文档。 – Ebanashka

+0

哦。文档在哪里?例如 –

此代码适用于我。请注意,它似乎没有更改选项,因此虽然它为我排列网格,但View-> Align Icons To Grid仍未选中。这个问题在这里概述:How do I get the window handle of the desktop?

您的问题,结果来自什么 桌面窗口实际上是一个相当普遍的困惑。 GetDesktopWindow函数的功能确实如此:它返回 桌面窗口的句柄。但是,这与包含 桌面图标的窗口不同。

所以我用了答案在这里:Get handle to desktop/shell window但更换或添加方法和P /直到我有必要的,因为该链接只给方法调用,而不是DLL进口一切调用电话(我可能已经留下了一些多余的东西在这里)。

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; 
} 
+0

谢谢!我感谢帮助!但它似乎并不适合我。我正在运行3台显示器,Windows 10 x64(如果有的话)。另外,我尝试启用'对齐网格'并通过应用程序禁用它 - 结果相同。 – Ebanashka

+0

我也试过在背景上禁用幻灯片 – Ebanashka

+0

@Ebanashka对不起,我不知道如何解决这些问题。真的,这是一个不好的答案,因为我做了你不应该做的事:从3个不同的答案复制代码,而没有写或理解它几乎没有。但你似乎比我更了解,所以我觉得你应该继续努力。 – Quantic