添加“打开程序目录”
优化右键菜单指向错误
This commit is contained in:
parent
c4bffa500d
commit
9a66260b2a
61
MainForm.cs
61
MainForm.cs
@ -8,7 +8,8 @@ using System.Linq;
|
||||
using System.Runtime.InteropServices; // 添加P/Invoke支持
|
||||
using System.Drawing.Drawing2D; // 添加GDI+支持
|
||||
using System.Drawing.Imaging; // 添加图像处理支持
|
||||
using Microsoft.Win32; // 添加Registry访问支持
|
||||
using Microsoft.Win32;
|
||||
using System.Diagnostics; // 添加Registry访问支持
|
||||
|
||||
namespace QuickLauncher
|
||||
{
|
||||
@ -480,32 +481,39 @@ namespace QuickLauncher
|
||||
shortcutItemContextMenu = new ContextMenuStrip();
|
||||
shortcutItemContextMenu.Items.Add("启动", null, (s, e) =>
|
||||
{
|
||||
if (s is ToolStripMenuItem menuItem && menuItem.Tag is ShortcutItem item)
|
||||
if (shortcutItemContextMenu.Tag is ShortcutItem item)
|
||||
{
|
||||
LaunchApplication(item.Path);
|
||||
}
|
||||
});
|
||||
shortcutItemContextMenu.Items.Add("删除", null, (s, e) =>
|
||||
{
|
||||
if (s is ToolStripMenuItem menuItem && menuItem.Tag is ShortcutItem item)
|
||||
if (shortcutItemContextMenu.Tag is ShortcutItem item)
|
||||
{
|
||||
DeleteShortcut(item);
|
||||
}
|
||||
});
|
||||
shortcutItemContextMenu.Items.Add("重命名", null, (s, e) =>
|
||||
{
|
||||
if (s is ToolStripMenuItem menuItem && menuItem.Tag is ShortcutItem item)
|
||||
if (shortcutItemContextMenu.Tag is ShortcutItem item)
|
||||
{
|
||||
RenameShortcut(item);
|
||||
}
|
||||
});
|
||||
shortcutItemContextMenu.Items.Add("属性", null, (s, e) =>
|
||||
{
|
||||
if (s is ToolStripMenuItem menuItem && menuItem.Tag is ShortcutItem item)
|
||||
if (shortcutItemContextMenu.Tag is ShortcutItem item)
|
||||
{
|
||||
ShowShortcutProperties(item);
|
||||
}
|
||||
});
|
||||
shortcutItemContextMenu.Items.Add("打开程序目录", null, (s, e) =>
|
||||
{
|
||||
if (shortcutItemContextMenu.Tag is ShortcutItem item)
|
||||
{
|
||||
OpenProgramDirectory(item.Path);
|
||||
}
|
||||
});
|
||||
|
||||
// 应用主题颜色
|
||||
shortcutItemContextMenu.Renderer = new ModernMenuRenderer(isDarkMode);
|
||||
@ -821,6 +829,27 @@ namespace QuickLauncher
|
||||
}
|
||||
}
|
||||
|
||||
// 打开程序目录
|
||||
private void OpenProgramDirectory(string programPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(programPath))
|
||||
{
|
||||
// 使用explorer.exe /select, 来打开文件所在目录并高亮显示该文件
|
||||
Process.Start("explorer.exe", $"/select,\"{programPath}\"");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("文件不存在,无法打开所在目录", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"打开程序目录时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
// 选择默认分类
|
||||
private void SelectDefaultCategory()
|
||||
{
|
||||
@ -1348,13 +1377,27 @@ namespace QuickLauncher
|
||||
label.ContextMenuStrip = shortcutItemContextMenu;
|
||||
|
||||
// 为右键菜单项设置Tag
|
||||
foreach (ToolStripItem menuItem in shortcutItemContextMenu.Items)
|
||||
//foreach (ToolStripItem menuItem in shortcutItemContextMenu.Items)
|
||||
//{
|
||||
// if (menuItem is ToolStripMenuItem)
|
||||
// {
|
||||
// menuItem.Tag = item;
|
||||
// }
|
||||
//}
|
||||
|
||||
// 添加鼠标右键点击事件,在显示菜单前绑定当前选中的 ShortcutItem
|
||||
EventHandler rightClickHandler = (s, e) =>
|
||||
{
|
||||
if (menuItem is ToolStripMenuItem)
|
||||
if (s is Control control && control.Parent is RoundedPanel panel && panel.Tag is ShortcutItem item)
|
||||
{
|
||||
menuItem.Tag = item;
|
||||
shortcutItemContextMenu.Tag = item; // 绑定当前选中的快捷方式
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 绑定到所有相关控件
|
||||
panel.MouseDown += (s, e) => { if (e.Button == MouseButtons.Right) rightClickHandler(s, e); };
|
||||
icon.MouseDown += (s, e) => { if (e.Button == MouseButtons.Right) rightClickHandler(s, e); };
|
||||
label.MouseDown += (s, e) => { if (e.Button == MouseButtons.Right) rightClickHandler(s, e); };
|
||||
|
||||
// 双击事件 - 启动应用程序或预览图片
|
||||
EventHandler doubleClickHandler = (s, e) =>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user