protected override void WndProc(ref Message m) { if (m.Msg == WM_CONTEXTMENU) { if (ContextMenu != null) { int ilParam = WinApi.ToInt(m.LParam); int y = ilParam >> 16; int x = (short)(ilParam & 0xFFFF); IntPtr hWnd = m.WParam; if (hWnd == IntPtr.Zero) hWnd = m.HWnd; bool context = true; if (m.HWnd != m.WParam) context = false; WmContextEventArgs e = new WmContextEventArgs(hWnd, x, y, ((x == -1 && y == -1) ? MouseButtons.None : MouseButtons.Right), context, m.HWnd); ContextMenu(this, e); if (e.Handled) return; } } // This case was taken out becouse the message was not generated for the listview control and possibly for // treview control so this code was moved to the MouseUp event of the Control see ContextExMouseUp // else if(m.Msg==WM_RBUTTONUP || m.Msg==WM_NCRBUTTONUP) // { // if(ContextMenu!=null) // { // int ilParam=m.LParam.ToInt32(); // int y=ilParam>>16; // int x=ilParam & 0xFFFF; // Point p=ParentControl.PointToScreen(new Point(x,y)); // WmContextEventArgs e=new WmContextEventArgs(m.HWnd,p.X,p.Y,MouseButtons.Right,false); // ContextMenu(this,e); // } // } base.WndProc(ref m); }
private void OnContextMenu(object sender, WmContextEventArgs e) { ContextMessageHandler h = sender as ContextMessageHandler; if (h == null) return; BaseItem contextItem = GetContextExItem(h.ParentControl); if (contextItem == null) return; // Check whether context menu originated elsewhere in particular a Panel which handles MouseUp if (e.Handle != e.SourceHandle && e.SourceHandle != IntPtr.Zero) { Control ctrl = Control.FromChildHandle(e.Handle); if (ctrl != null && ctrl is Panel && this.GetContextExItem(ctrl) != null) return; // WM_CONTEXT bubbles up and Panel handles MouseUp } // Find it in pop-ups PopupItem popup = contextItem as PopupItem; popup.Style = this.Style; if (e.Button == MouseButtons.None) { // Get the control with focus Control ctrl = Control.FromChildHandle(e.Handle); if (ctrl != null && ctrl.Handle != e.Handle) { ctrl = FindControl(ctrl, e.Handle); } popup.SetSourceControl(h.ParentControl); if (ctrl != null) { if (ctrl.ClientRectangle.Contains(ctrl.PointToClient(Control.MousePosition))) popup.Popup(Control.MousePosition); else popup.Popup(ctrl.PointToScreen(Point.Empty)); } else popup.Popup(Control.MousePosition); // We need to eat the message in OnSysKeyUp for Shift+F10 case if (this.IgnoreSysKeyUp) { this.IgnoreSysKeyUp = false; this.EatSysKeyUp = true; } } else { // This is handled by the WM_RBUTTONUP just eat it if (!e.WmContext) { popup.SetSourceControl(h.ParentControl); popup.Popup(e.X, e.Y); } } e.Handled = true; }