c# winform如何做出桌面右键菜单?

2025-05-08 11:45:49
推荐回答(3个)
回答1:

在工具箱的“菜单和工具栏”中找到ContextMenuStrip控件拖入窗体中,在其中添加你所要实现的效果,如:打开,保存…….

我现在就用总窗体Form来说:前面的步骤完成了,再到Form的属性中找到ContextMenuStrip属性,一般默认的都是“无”,点击这一属性,有一下拉框,其中就有你创建的ContextMenuStrip,选择ContextMenuStrip就OK了;给你图片:

回答2:

这个是通过注册表实现的,跟C#程序本身没啥关系
比如下面两段就能实现在文件的右键菜单中加入“用记事本打开”的效果
[HKEY_CLASSES_ROOT\*\shell\OpenInNotepad]
@="用记事本打开"

[HKEY_CLASSES_ROOT\*\shell\OpenInNotepad\command]
@="\"notepad.exe\" %1"

在制作安装程序时,可以写脚本,实现对注册表的操作
Setup Factory,installshield 都是不错的打包软件
手动编辑好注册表也能看到效果

可以写代码将自己的程序注册到系统右键菜单,主要代码如下:
private void button1_Click(object sender, EventArgs e)
{
string menuName = string.IsNullOrEmpty(this.textBox1.Text.Trim()) ? "真有意思网" : this.textBox1.Text.Trim();

//注册到所有文件
if (chkFile.Checked)
{
RegistryKey shell = Registry.ClassesRoot.OpenSubKey(@"*shell", true);
RegistryKey custom = shell.CreateSubKey(menuName);
RegistryKey cmd = custom.CreateSubKey("command");
cmd.SetValue("", Application.ExecutablePath + " %1");
//Application.ExecutablePath 是本程序自身的路径
//%1 是传入打开的文件路径
cmd.Close();
custom.Close();
shell.Close();
}

//注册到所有目录
if (chkFile.Checked)
{
RegistryKey shell = Registry.ClassesRoot.OpenSubKey(@"directoryshell", true);
RegistryKey custom = shell.CreateSubKey(menuName);
RegistryKey cmd = custom.CreateSubKey("command");
cmd.SetValue("", Application.ExecutablePath + " %1"); //%1 是传入打开的文件路径
cmd.Close();
custom.Close();
shell.Close();
}

MessageBox.Show("注册成功!rn请通过文件或目录的右键菜单来测试结果!");
}

别忘了引用命名空间Microsoft.Win32
using Microsoft.Win32;

详细代码请参考
http://hi.baidu.com/deltacat2006上这篇文章
C#、VB.NET将自己的程序注册到系统的右键菜单(所有文件和目录)

回答3:

采纳答案留QQ