现在的位置: 首页 > 综合 > 正文

重建图标存

2018年04月15日 ⁄ 综合 ⁄ 共 6236字 ⁄ 字号 评论关闭

 //refresh the shell icon cache
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        public const int WM_SETTINGCHANGE = 25;
        public const int HWND_BROADCAST = 65536;
        void IconCacheRefresh()
        {
            int value = Convert.ToInt32(Registry.GetValue(@"HKEY_CURRENT_USER/Control              

                                                           Panel/Desktop/WindowMetrics", "Shell Icon Size", -1).ToString());
            //MessageBox.Show(value.ToString());
            Registry.SetValue(@"HKEY_CURRENT_USER/Control Panel/Desktop/WindowMetrics", "Shell Icon Size", value -                                                           1, RegistryValueKind.String);
            SendMessage(new IntPtr(HWND_BROADCAST), WM_SETTINGCHANGE, 0, 0);
            Registry.SetValue(@"HKEY_CURRENT_USER/Control Panel/Desktop/WindowMetrics", "Shell Icon Size",

                                                            value,RegistryValueKind.String);
            SendMessage(new IntPtr(HWND_BROADCAST), WM_SETTINGCHANGE, 0, 0);
        }

//似乎不起作用。

----------------------------------------------------------------------------------

//可能用的一些代码

using System.Runtime.InteropServices;

private const int HWND_BROADCAST = 0xFFFF;
private const int WM_WININICHANGE = 0x001A;

[DllImport("User32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);

/// <summary>
        /// 重建图标缓存
        /// </summary>
        /// <returns>true=成功</returns>
        public bool ReBuildIconCache()
        {
            try
            {
                // HKEY_CURRENT_USER/Control Panel/Desktop/WindowMetrics/Shell Icon Size
                string RegPath = @"HKEY_CURRENT_USER/Control Panel/Desktop/WindowMetrics";
                // 读取原图标大小
                string IconWidth1 = RegistryRW(RegPath, "Shell Icon Size", ""); //这里是从注册表读出的,也可以使用别的方法读取
                int IconWidth2 = int.Parse(IconWidth1) - 1;
               
                RegistryRW(RegPath, "Shell Icon Size", IconWidth2.ToString());
                SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, 0);
               
                RegistryRW(RegPath, "Shell Icon Size", IconWidth1.ToString());
                SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, 0);
               
                return true;
            }
            catch
            {
                return false;
            }
        }

/// <summary>
        /// 读取或写入指定的注册表项目
        /// </summary>
        /// <param name="regPath">完整的注册表路径</param>
        /// <param name="RegItem">要读取或者写入的注册表项</param>
        /// <param name="NewValue">要写入的值,如果为空则表示读取</param>
        /// <returns>string</returns>
        public string RegistryRW(string regPath, string RegItem, string NewValue)
        {
            string myValue = "";
            try
            {
                RegistryKey myRegistry = null;
                if (NewValue == "" || NewValue == null)
                {
                    myRegistry = GetRegistryKey(regPath, false);
                    myValue = myRegistry.GetValue(RegItem).ToString();
                }
                else
                {
                    myRegistry = GetRegistryKey(regPath, true);
                    myRegistry.SetValue(RegItem, NewValue);
                    myRegistry.Flush();
                }
                myRegistry.Close();
            }
            catch
            {
                myValue = "";
            }
            return myValue;
        }

        /// <summary>
        /// 根据注册表路径自动获取RegistryKey对象
        /// </summary>
        /// <param name="regPath">完整的注册表路径</param>
        /// <returns>RegistryKey对象</returns>
        public RegistryKey GetRegistryKey(string regPath, bool CanWrite)
        {
            regPath = regPath.ToUpper();
            try
            {
                RegistryKey myRegistry = null;
                // HKEY_CLASSES_ROOT
                if (regPath.StartsWith(@"HKEY_CLASSES_ROOT/") == true)
                {
                    regPath = regPath.Replace(@"HKEY_CLASSES_ROOT/", "");
                    myRegistry = Registry.ClassesRoot.OpenSubKey(regPath, CanWrite);
                }
                // HKEY_CURRENT_CONFIG
                else if (regPath.StartsWith(@"HKEY_CURRENT_CONFIG/") == true)
                {
                    regPath = regPath.Replace(@"HKEY_CURRENT_CONFIG/", "");
                    myRegistry = Registry.CurrentConfig.OpenSubKey(regPath, CanWrite);
                }
                // HKEY_CURRENT_USER
                else if (regPath.StartsWith(@"HKEY_CURRENT_USER/") == true)
                {
                    regPath = regPath.Replace(@"HKEY_CURRENT_USER/", "");
                    myRegistry = Registry.CurrentUser.OpenSubKey(regPath, CanWrite);
                }
                // HKEY_DYN_DATA
                else if (regPath.StartsWith(@"HKEY_DYN_DATA/") == true)
                {
                    regPath = regPath.Replace(@"HKEY_DYN_DATA/", "");
                    myRegistry = Registry.DynData.OpenSubKey(regPath, CanWrite);
                }
                // HKEY_LOCAL_MACHINE
                else if (regPath.StartsWith(@"HKEY_LOCAL_MACHINE/") == true)
                {
                    regPath = regPath.Replace(@"HKEY_LOCAL_MACHINE/", "");
                    myRegistry = Registry.LocalMachine.OpenSubKey(regPath, CanWrite);
                }
                // HKEY_PERFORMANCE_DATA
                else if (regPath.StartsWith(@"HKEY_PERFORMANCE_DATA/") == true)
                {
                    regPath = regPath.Replace(@"HKEY_PERFORMANCE_DATA/", "");
                    myRegistry = Registry.PerformanceData.OpenSubKey(regPath, CanWrite);
                }
                // HKEY_USERS
                else if (regPath.StartsWith(@"HKEY_USERS/") == true)
                {
                    regPath = regPath.Replace(@"HKEY_USERS/", "");
                    myRegistry = Registry.Users.OpenSubKey(regPath, CanWrite);
                }
                return myRegistry;
            }
            catch
            {
            }
            return null;
        }

---------------------------------------------------------------------------------------------------

没有单独的API可用,可参考的方法

  • 1. open HKEY_CURRENT_USER/Control Panel/Desktop/WindowMetrics
  • 2. get the type of value and its size stored as "Shell Icon Size" (string)
  • 3. get value of Shell Icon Size
  • 4. change this value (i.e. decrement the value by one)
  • 5. write the new value back to the registry
  • 6. call SendMessageTimeout with HWND_BROADCAST
  • 7. return the "Shell Icon Size" value to its original setting
  • 8. call SendMessageTimeout with HWND_BROADCAST again
  • 9. close the key

抱歉!评论已关闭.