Search code examples
c#wpfmouseeventnotifyicon

wpf - notifyIcon - mouse event - identify mouse button


I use the notifyIcon from Winforms in a WPF application. Bellow is part of my event handler:

private void notifyIcon_Logger_MouseDown( object sender, EventArgs e )
{
        var st = e.ToString();
...

I may not make e parameter a MouseEventArgs because compiler says it does not match. But even so, I see that st is "System.Windows.Forms.MouseEventArgs". How is that?!

I have pinned e on IDE surface to watch it for debug purposes and I see it has a member Button. I see something like

Button = Right

but if I try e.Button I get error CS1061: 'EventArgs' does not contain a definition for 'Button' How is all these possible? More importantly, how to identify mouse button?


Solution

  • mixing WPF and Winforms can sometimes be tricky...

    There are 2 types called MouseEventArgs. One is the WPF version in the System.Windows.Input namespace and the other is the Winforms version in the System.Windows.Forms namespace.

    By simply casting it as MouseEventArgs the compiler uses the WPF form since this is WPF app but you need the Winforms version since this particular callback is for a Winforms control. So simply qualify it with the correct namespace in the callback definition...

    private void notifyIcon_Logger_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e )
    {
            var st = e.ToString();
    ...