You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为NotifyIcon控件的MouseDown事件添加事件处理程序?

问题原因

NotifyIcon.MouseDown 事件要求的委托类型为 System.Windows.Forms.MouseEventHandler,你当前使用的 System.EventHandler 类型不匹配,同时事件处理方法的入参也需要同步替换为 MouseEventArgs 才能正常获取鼠标按键信息。

修正代码

事件绑定部分

// 替换原来的EventHandler绑定
notifyIcon.MouseDown += new System.Windows.Forms.MouseEventHandler(NotifyIcon_MouseDown);

System.Windows.Forms.ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem menuItemExit = new System.Windows.Forms.MenuItem();

contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { menuItemExit });

menuItemExit.Index = 0;
menuItemExit.Text = "E&xit";
menuItemExit.Click += new System.EventHandler(menuItemExit_Click);
notifyIcon.ContextMenu = contextMenu;

事件处理方法部分

private void NotifyIcon_MouseDown(object Sender, System.Windows.Forms.MouseEventArgs e)
{
    // 仅左键点击时唤起应用
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        this.Visibility = Visibility.Visible;
        this.Activate();
    }
    // 右键点击逻辑已通过绑定ContextMenu自动实现,无需额外处理
}

你需要的左键唤起应用、右键弹出菜单的效果可以直接通过上述代码实现,不需要额外补充其他逻辑。

内容的提问来源于stack exchange,提问作者Mogash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 04:36:07