Search code examples
c#mousewheelnotifyicon

How to use MouseWheel in NotifyIcon


I'm building a tiny program for controlling master volume with the following requirements

  1. Sits in the Task Bar (next to the clock)

  2. On single click it will Mute/Unmute the master volume

  3. While the mouse is hovering over the icon the mouse wheel controls volume Up/Increase Down/Decrease.

I have got the first two working so far by combining these two projects http://www.codeproject.com/Articles/290013/Formless-System-Tray-Application http://www.codeproject.com/Articles/18520/Vista-Core-Audio-API-Master-Volume-Control

The trouble I'm having is with no.3 which I'm guessing is the most complicated part of my tiny program.

Error: 'System.Windows.Forms.NotifyIcon' does not contain a definition for 'MouseWheel'

I'm running Windows 8.1 x64 .NET 4.5 / Visual Studio Express 2013

Personal Background

  1. I am not a programmer.

  2. I did do basic java in a computer course more than a decade ago.

  3. I'm teaching myself C# from microsoftvirtualacademy.com


Solution

  • That happens, because NotifyIcon is not a control, but a component (it's derived from Component class). MouseWheel event is a member of Control class, not Component. So, NotifyIcon doesn't have MouseWheel event.

    I'm afraid, there is no official solution for this problem, as public API (Shell_NotifyIcon) doesn't expose wheel information.

    UPD: As requirements changed, there is my step-by-step guide

    First, you need to add MouseClick handler for your NotifyIcon

    notifyIcon.MouseClick += new MouseEventHandler(notifyIcon_MouseDown);
    

    Then, add this event handler to your code-behind

    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
        }
    }