桌面右鍵加入[關閉顯示器]菜單項


起因

購了個ThinkPad T450S,沒發現其組合鍵有關閉顯示器選項。而關閉顯示器,在徹夜下載連續劇及大資料時,非常有用,於是就尋找方法。

終於找到了個工具,Close LCD,27K大小,一點就關,易用之極。

之前裝ThinkPad電源管理器v4.0,其一有選項是在桌面右鍵菜單中加入[關閉顯示器]選項,甚為懷念。現裝v6版,沒有其選項,好吧,想辦法!

於是在51nb上找到一方案,其用批處理加入注冊表菜單項,可以解決問題。但問題來了,操作不直觀,沒有圖標及位置。

研究v4.0方案,提取更多信息,以c#寫一小工具,實現簡易安裝與去除。記錄過程,並分享制作的小工具,文章底部附有下載連接

只是,它不僅加入桌面右鍵菜單,在所有文件夾右鍵菜單中,都存在。

 

實現

c#操作注冊表而已,無奇,實現代碼如下:

namespace CloseLCD
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            UpdateUIState();
        }

        private void UpdateUIState()
        {
            btnSet.Text = MenuItemExists() ? "從右鍵菜單移除" : "加入右鍵菜單";
            txtMenuText.Enabled = !MenuItemExists();
        }

        private bool MenuItemExists()
        {
            var path = Registry.GetValue(@"HKEY_CLASSES_ROOT\Directory\Background\shell\TurnoffMonitor\command", "", "");
            if (path == null)
                return false;
            return File.Exists(path.ToString().Replace("\"", ""));
        }

        private void InstallMenuItem()
        {
            try
            {
                var key = Registry.ClassesRoot.CreateSubKey(@"Directory\Background\shell\TurnoffMonitor");
                if (key != null)
                {
                    string text = !string.IsNullOrEmpty(txtMenuText.Text) ? txtMenuText.Text : "關閉顯示器(&M)";
                    key.SetValue("", text, RegistryValueKind.String);
                    string clPath = Path.Combine(Application.StartupPath, "CloseLCD.exe");
                    clPath = string.Format("\"{0}\"", clPath);
                    key.SetValue("Icon", clPath, RegistryValueKind.String);
                    key.SetValue("Position", "Bottom", RegistryValueKind.String);
                    key = key.CreateSubKey("command");
                    key.SetValue("", clPath, RegistryValueKind.String);
                    key.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "加入菜單項失敗,請確認是否有殺毒軟件等工具防護注冊表。\r\n信息為:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnSet_Click(object sender, System.EventArgs e)
        {
            //加入
            if (!MenuItemExists())
                InstallMenuItem();
            //移除
            else
            {
                try
                {
                    using (var key = Registry.ClassesRoot.OpenSubKey(@"Directory\Background\shell\TurnoffMonitor", true))
                    {
                        if (key != null)
                            key.DeleteSubKeyTree("");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "移除菜單項失敗,請確認是否有殺毒軟件等工具防護注冊表。\r\n信息為:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

            UpdateUIState();
        }
    }
}

 

效果

在筆記本及單位LCD顯示器上驗證,工作正常。

其界面及顯示效果如下圖:


 

集成

找到了關閉顯示器的api函數,干脆置關閉與菜單設置於一體算了,省事。

 

 

參考資料:

為你的桌面右鍵添加一個“關閉顯示器”的菜單!

文件打包下載

單文件版下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM