禁止和允许显示任务栏的右键菜单

Posted on 2019-01-29 13:54  努力成长静待花开  阅读(1190)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:

   notraycontextmenu键

实现代码:

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                RegistryKey regkey;
                regkey = Registry.CurrentUser;
                regkey = regkey.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                regkey.SetValue("notraycontextmenu", 1, RegistryValueKind.DWord);
                regkey.Close();
                if (MessageBox.Show("设置完成!") == DialogResult.OK)
                {
                    RefreshSystem();
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                RegistryKey regkey;
                regkey = Registry.CurrentUser;
                regkey = regkey.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                regkey.SetValue("notraycontextmenu", 0, RegistryValueKind.DWord);
                regkey.Close();
                if (MessageBox.Show("设置完成!") == DialogResult.OK)
                {
                    RefreshSystem();
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }