首先,TLDR:
我不认为你想要的在批处理文件中是可能的,所以我编译了一个小程序,每次运行时都会打开和关闭桌面图标,并且不会影响位置。https://github.com/smithmart/ToggleIcons/raw/main/ToggleIcons.exe
这要归功于堆栈溢出的另一个答案,但从你所说的来看,我认为你没有能力自己使用这个答案......所以我为你做了它。
ELI5:
我相信你的图标移动的原因是因为你正在使用taskkill来结束explorer.exe。
资源管理器是 Windows 中的程序,它运行您看到的大部分内容,例如开始栏和桌面,以及浏览驱动程序和文件夹时。
So,
taskkill /f /im explorer.exe
使 explorer.exe 停止运行,这就是为什么当您运行批处理时您会看到开始栏瞬间消失。
在这样停止它时,它没有机会保存您的图标位置,因此它们总是恢复到最后已知的位置。
您找到的脚本执行此操作的原因是停止 explorer.exe 然后再次启动它会刷新您的桌面,这意味着第一行:
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /V HideIcons /T REG_DWORD /D 1 /F
可以生效。此行将 Windows 中的设置更改为“隐藏我的桌面图标”
解决方案(某种)
我不知道刷新桌面设置的简单批处理文件的方法,但是,我确实在堆栈溢出上看到了这个答案,它是用 c# 编写的如何刷新/重新加载桌面
我知道你可能不具备使用这个答案的知识,所以我为你制作了一个小 exe 来切换你的图标
https://github.com/smithmart/ToggleIcons/raw/main/ToggleIcons.exe
它称为 ToggleIcons.exe
每次运行它时,它所做的只是打开或关闭桌面图标
要使用它,只需将其弹出到批处理文件所在的同一文件夹中并添加以下行:
切换图标.exe
到你的脚本。
您可以删除已有的 3 行,因为这基本上可以完成所有这 3 件事。但是,它不会重新启动 explorer.exe,这意味着您的图标不会移动到任何地方
我还为您提供了源代码,因为从陌生人那里获取糖果通常并不理想,所以如果您觉得使用它不舒服,我不会生气! :D
以防万一其他答案的链接丢失,以下是来源:
class Program
{
//Thanks to https://stackoverflow.com/questions/17503289/how-to-refresh-reload-desktop
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
enum GetWindow_Cmd : uint
{
GW_HWNDFIRST = 0,
GW_HWNDLAST = 1,
GW_HWNDNEXT = 2,
GW_HWNDPREV = 3,
GW_OWNER = 4,
GW_CHILD = 5,
GW_ENABLEDPOPUP = 6
}
private const int WM_COMMAND = 0x111;
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
private 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);
[DllImport("user32.dll")]
private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", SetLastError = false)]
static extern IntPtr GetShellWindow();
public static string GetWindowText(IntPtr hWnd)
{
int size = GetWindowTextLength(hWnd);
if (size++ > 0)
{
var builder = new StringBuilder(size);
GetWindowText(hWnd, builder, builder.Capacity);
return builder.ToString();
}
return String.Empty;
}
public static IEnumerable<IntPtr> FindWindowsWithClass(string className)
{
IntPtr found = IntPtr.Zero;
List<IntPtr> windows = new List<IntPtr>();
EnumWindows(delegate (IntPtr wnd, IntPtr param)
{
StringBuilder cl = new StringBuilder(256);
GetClassName(wnd, cl, cl.Capacity);
if (cl.ToString() == className && (GetWindowText(wnd) == "" || GetWindowText(wnd) == null))
{
windows.Add(wnd);
}
return true;
},
IntPtr.Zero);
return windows;
}
static void ToggleDesktopIcons()
{
var toggleDesktopCommand = new IntPtr(0x7402);
IntPtr hWnd = IntPtr.Zero;
if (Environment.OSVersion.Version.Major < 6 || Environment.OSVersion.Version.Minor < 2) //7 and -
hWnd = GetWindow(FindWindow("Progman", "Program Manager"), GetWindow_Cmd.GW_CHILD);
else
{
IEnumerable<IntPtr> ptrs = FindWindowsWithClass("WorkerW");
int i = 0;
while (hWnd == IntPtr.Zero && i < ptrs.Count())
{
hWnd = FindWindowEx(ptrs.ElementAt(i), IntPtr.Zero, "SHELLDLL_DefView", null);
i++;
}
}
if (hWnd == IntPtr.Zero)
{
//"SHELLDLL_DefView" was not found as a child within WorkerW - Lets check the current ShellWindow
IntPtr desktop = GetShellWindow();
hWnd = FindWindowEx(desktop, IntPtr.Zero, "SHELLDLL_DefView", null);
}
if (hWnd != IntPtr.Zero)
{
SendMessage(hWnd, WM_COMMAND, toggleDesktopCommand, IntPtr.Zero);
}
}
static void Main(string[] args)
{
ToggleDesktopIcons();
}
}
更新:
进程“SHELLDLL_DefView”是我们需要发送命令来切换图标的地方,这实际上是作为 progman 的子进程启动的。用户激活一些其他功能(不确定是哪些功能,但 Windows + Tab 是其中之一)后,它将父级切换到 WorkerW
更改此代码以在 currentShellWindow 内检查