本文介绍了切换桌面的自动安排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直试图在Windows Vista上以编程方式切换桌面图标的自动排列功能,但是我无法理解为什么它没有任何作用.我不知道是否可以在XP上运行,但我没有对其进行测试.有人可以看一下代码,然后尝试找出它的问题吗:
I've been trying to toggle desktop icon's autoarrange feature programatically on the Windows Vista, but I fail to understand why it's not having any effect. I don't know whether is working on XP, i didn't test it. Can someone please take a look at the code and try to find what is wrong with it:
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);
}
}
}
推荐答案
这篇关于切换桌面的自动安排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!