Windows 8 开发者预览版的 Metro 风格切换






3.58/5 (10投票s)
在 Windows 8 开发者预览版中切换 Metro 风格(开始菜单和资源管理器Ribbon)和经典 Windows 7 Shell。
引言

我已经使用 Windows 8 开发者预览版一段时间了。由于我的笔记本电脑不支持触摸,我被困在了新的 Metro 风格开始菜单中。说实话,在触摸设备上它非常棒!但我更喜欢 Windows 7 的经典开始菜单。
我在网上搜索了一段时间,寻找禁用新的 Metro 开始菜单并使用经典的 Windows 7 的方法。许多网站指出,它通过 Windows 注册表中的 HKEY_CURRENT_USER\Software\Microsoft\Windows\ CurrentVersion\Explorer 下的 RPEnabled 值进行控制。Metro 的值为 1,经典 Windows 7 的值为 0。
我决定通过编写一个应用程序(我称之为“Metro 风格切换”)来简化切换,该应用程序会更新 RPEnabled 注册表值。在这篇文章中,我将分享我是如何读取/写入 Windows 注册表值来切换 Shell 风格的。
背景
快速总结代码
- 使用
Microsoft.Win32.Registry.GetValue
读取RPEnabled
注册表值,并通过更新状态标签和禁用当前状态切换按钮在 UI 上反映状态。例如,如果当前状态是 Metro,则应禁用 Metro 切换按钮。 - 当用户切换状态时,我使用
Microsoft.Win32.Registry.SetValue
更新RPEnabled
注册表值。例如,如果用户想切换回 Metro,则应将RPEnabled
更新为1
。 - 会向用户显示一个警报,要求他们重新启动机器以使更改生效。
我编写的代码是在 Windows 8 开发者预览版上运行的,我不知道它是否能在 Beta 或 RTM 版本上运行。此外,您必须以管理员身份运行此应用程序,否则它将无法更新 Windows 注册表(它在我这里无需管理员权限即可工作,也许因为我是管理员 ,但最好还是以管理员身份运行)。请注意,Windows 可能会要求您使用 Windows 功能激活安装 Microsoft .NET Framework 3.5 SP1,如果您尚未激活运行时。
Metro 风格
经典 Windows 7 风格
源代码
对我来说,解释源代码的最简单方法是在代码中添加注释并将其粘贴到这里。所以对于那些对源代码感兴趣的极客们,让我们开始挖掘吧
using System;
using Microsoft.Win32;
//Used for Registry.GetValue() and Registry.SetValue() - Lines 78 and 88
using System.Windows.Forms;
using System.Diagnostics;
//Used for Process.Start() - Line 134
namespace MetroStyleToggle
{
public partial class ToggleControl : Form
{
public ToggleControl()
{
InitializeComponent();
}
enum ShellStatus { Metro, Classic, Unknown }
ShellStatus currentShellStatus;
string registryPath = @"HKEY_CURRENT_USER\Software\Microsoft\
Windows\CurrentVersion\Explorer",
registryValue = "RPEnabled";
string metroStyle = "1", classicStyle = "0", currentKeyValue = "",
restartMessege = "Restart needed for updates to take effect.";
private void ToggleControl_Load(object sender, EventArgs e)
{
SetShellStatusMessege();
}
private void SetShellStatusMessege()
{
//Get the current shell style state from Windows Registry and
// store it inside currentKeyValue
//You will find more explanation about the GetRegistryValue function
// whereabouts below
currentKeyValue = GetRegistryValue(registryPath, registryValue);
//Maintain the current shell style state
SetShellStatus(currentKeyValue);
//Update the shell status label with the current shell style state
ShellStatusMessege.Text = currentShellStatus.ToString();
}
private void SetShellStatus(string status)
{
if (status == metroStyle)
{
//Set the shell style state to Metro
currentShellStatus = ShellStatus.Metro;
//Disable the MetroToggle button
MetroToggle.Enabled = false;
//Enable the ClassicToggle button
ClassicToggle.Enabled = true;
}
else if (status == classicStyle)
{
//Set the shell style state to Classic
currentShellStatus = ShellStatus.Classic;
//Enable the MetroToggle button
MetroToggle.Enabled = true;
//Disable the ClassicToggle button
ClassicToggle.Enabled = false;
}
else
{
//Set the shell style state to Unknown
currentShellStatus = ShellStatus.Unknown;
}
}
public string GetRegistryValue(string path, string value)
{
//Return the Windows Registry value using the Path set in
// registryPath and registryValue
//registryPath value is
// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer
//registryValue value is RPEnabled
//If the key does not exist, the return value will be "Unknown"
return Convert.ToString(Registry.GetValue(path, value, "Unknown"));
}
public void SetRegistryValue(string path, string value, string shell)
{
//Write to Windows Registry using the
//Path set in registryPath and registryValue
//registryPath value is
// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer
//registryValue value is RPEnabled
//shell contains either 0 for Classic Windows 7 Style or 1 for Metro Style
Registry.SetValue(path, value, shell);
}
private void MetroToggle_Click(object sender, EventArgs e)
{
try
{
//Set the shell style state to Classic, classicStyle was set to 1
SetRegistryValue(registryPath, registryValue, metroStyle);
//Update the status label
SetShellStatusMessege();
//Alert the user to restart their machine
MessageBox.Show(restartMessege,this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
//Capture the error and push it to the status label
ShellStatusMessege.Text = "Error: " + ex.Message;
}
}
private void ClassicToggle_Click(object sender, EventArgs e)
{
try
{
//Set the shell style state to Classic, classicStyle was set to 0
SetRegistryValue(registryPath, registryValue, classicStyle);
//Update the status label
SetShellStatusMessege();
//Alert the user to restart their machine
MessageBox.Show(restartMessege, this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
//Capture the error and push it to the status label
ShellStatusMessege.Text = "Error: " + ex.Message;
}
}
private void BlogLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
//Launch the user's default browser and browse to my blog URL.
//BlogLink is a LinkButton and it's Text property
// is set to http://www.sharepointstack.com/
Process.Start(BlogLink.Text);
}
}
}
好了,就这些了。