如何在NotifyIcon中使用MouseWheel

时间:2014-08-31 18:33:56

标签: c# mousewheel notifyicon

我正在构建一个用于控制主卷的小程序,具有以下要求

  1. 坐在任务栏中(时钟旁边)

  2. 单击它将使主音量静音/取消静音

  3. 当鼠标悬停在图标上方时,鼠标滚轮会控制音量上升/下降/下降。

  4. 到目前为止,通过合并这两个项目,我得到了前两个项目http://www.codeproject.com/Articles/290013/Formless-System-Tray-Application http://www.codeproject.com/Articles/18520/Vista-Core-Audio-API-Master-Volume-Control

    我遇到的麻烦就是3号,我猜这是我小程序中最复杂的部分。

    错误:' System.Windows.Forms.NotifyIcon'不包含' MouseWheel'

    的定义

    我正在运行Windows 8.1 x64 .NET 4.5 / Visual Studio Express 2013

    个人背景

    1. 我不是程序员。

    2. 十多年前,我在计算机课程中做过基础java。

    3. 我从microsoftvirtualacademy.com自学C#

1 个答案:

答案 0 :(得分:1)

这种情况发生了,因为NotifyIcon不是一个控件,而是一个组件(它是从Component类派生的)。 MouseWheel事件是Control类的成员,而不是Component。 因此,NotifyIcon没有MouseWheel事件。

我担心,这个问题没有官方解决方案,因为公共API(Shell_NotifyIcon)不会公开轮子信息。

UPD:随着要求的变化,我的分步指南

首先,您需要为NotifyIcon添加MouseClick处理程序

notifyIcon.MouseClick += new MouseEventHandler(notifyIcon_MouseDown);

然后,将此事件处理程序添加到您的代码隐藏

void notifyIcon_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        // Increase volume here
    }
    else if (e.Button == MouseButtons.Right)
    {
        // Decrease volume here
    }
    else if (e.Button == MouseButtons.Middle)
    {
        // Mute here
    }
}