【问题标题】:How to apply Windows taskbar changes without restarting Explorer in C#?如何在 C# 中应用 Windows 任务栏更改而不重新启动资源管理器?
【发布时间】:2020-05-21 10:15:59
【问题描述】:

这是我当前的代码,它可以工作,但它会重新启动资源管理器进程,这有点奇怪。

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace MyApp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            ShowSmallTaskbarIcons();
            // ShowLargeTaskbarIcons();
            Console.WriteLine("Press a key to exit...");
            Console.ReadKey();
        }

        static void ShowSmallTaskbarIcons()
        {
            Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", "TaskbarSmallIcons", "1", RegistryValueKind.DWord);
            RefreshExplorer();
        }

        static void ShowLargeTaskbarIcons()
        {
            var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", writable: true);
            key.DeleteValue("TaskbarSmallIcons", throwOnMissingValue: false);
            key.Close();
            RefreshExplorer();
        }

        static void RefreshExplorer()
        {
            Process.Start("taskkill.exe", "/f /im explorer.exe");
            Thread.Sleep(2000);
            Process.Start("explorer.exe");
            Thread.Sleep(10000);
        }
    }
}

我想对“设置”中的“使用小任务栏按钮”切换开关做同样的事情。

如何用 C# 做同样的事情?

【问题讨论】:

  • 请问为什么不需要重启windows explorer?
  • 东西突然从屏幕上消失有点令人不安。
  • it restarts the Explorer process and that's kinda weird 你从来没有在你的帖子中描述过kinda weird 是什么。您确实知道您正在自己杀死任务然后重新启动它,请以其他方式说明您的代码正在做什么。
  • 我想模仿 Windows 的做法,似乎有办法重新启动或应用任务栏更改。
  • 找到渲染任务栏的函数并只使用它们。根据需要更改变量。

标签: c# windows .net-core registry


【解决方案1】:

How to change Windows 10 taskbar icon size programmatically

这篇文章有一个关于如何在 c++ 中发送 WM_SETTINGCHANGE 消息的示例。

但是,在 c# 包装函数中没有简单的方法可以做到这一点。你必须通过 p/invoke 来完成

以下帖子将帮助您在 c# 中做到这一点

convert C++ code to C#: SendMessageTimeout()

【讨论】:

  • 你有我可以与 SendMessageTimeout 一起使用的所有 lParam 字符串的列表吗?
  • 我应该使用 SendNotifyMessage() 还是 SendMessageTimeout()?
  • 检查 msdn 以获取 lParam 表。 docs.microsoft.com/en-us/windows/win32/winmsg/…
  • msdn 还指出 SendMessageTimeout 是正确的功能。
  • 关于“lParam”值,是否有一个包含所有可用元素的列表,如“TraySettings”?
猜你喜欢
  • 2016-04-24
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
相关资源
最近更新 更多