Esempio n. 1
0
            protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                case WM_NOTIFYICONCALLBACK:
                    switch ((int)m.LParam)
                    {
                    case WM_LBUTTONDBLCLK:
                        notifyIcon.OnDoubleClick(new MouseEventArgs(MouseButtons.Left, 2, 0, 0, 0));
                        break;

                    case WM_RBUTTONUP:
                        if (ContextMenu != null)
                        {
                            POINT pt = new POINT();
                            GetCursorPos(out pt);

                            // See Q135788 in the Microsoft Knowledge Base for more info.
                            SetForegroundWindow(new HandleRef(this, this.Handle));

                            ContextMenu.Show();

                            TrackPopupMenuEx(new HandleRef(ContextMenu, ContextMenu.Handle), 0, pt.x, pt.y, new HandleRef(this, this.Handle), IntPtr.Zero);
                            PostMessage(new HandleRef(this, this.Handle), WM_NULL, IntPtr.Zero, IntPtr.Zero);
                        }
                        break;
                    }
                    break;

                case WM_COMMAND:
                    if (IntPtr.Zero == m.LParam)
                    {
                        int item  = LOWORD(m.WParam);
                        int flags = HIWORD(m.WParam);

                        if ((flags & MF_POPUP) == 0)
                        {
                            foreach (MenuItem menuItem in ContextMenu.MenuItems)
                            {
                                if (menuItem as MyMenuItem == null)
                                {
                                    Debug.Assert(false, "The context menu items must all be of type MyMenuItem, but \"" + menuItem.Text + "\" is not.");
                                    throw new ApplicationException("The context menu items must all be of type MyMenuItem, but \"" + menuItem.Text + "\" is not.");
                                }

                                if (item == (menuItem as MyMenuItem).GetMenuID())
                                {
                                    menuItem.PerformClick();
                                    break;
                                }
                            }
                        }
                    }
                    base.WndProc(ref m);
                    break;

                default:
                    if (m.Msg == WM_TASKBARCREATED)
                    {
                        notifyIcon.iconShowing = false;
                        notifyIcon.UpdateIcon();
                    }
                    base.WndProc(ref m);
                    break;
                }
            }