private void ShowContextMenu() { if (ContextMenu == null && ContextMenuStrip == null) { return; } NativeMethods.Point p = new NativeMethods.Point(); NativeMethods.GetCursorPos(ref p); NativeMethods.SetForegroundWindow(new HandleRef(_window, _window.Handle)); if (ContextMenu != null) { ContextMenu.GetType().InvokeMember("OnPopup", BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null, ContextMenu, new object[] { EventArgs.Empty }); NativeMethods.TrackPopupMenuEx(new HandleRef(ContextMenu, ContextMenu.Handle), 72, p.X, p.Y, new HandleRef(_window, _window.Handle), IntPtr.Zero); NativeMethods.PostMessage(new HandleRef(_window, _window.Handle), WM_NULL, 0, 0); return; } ContextMenuStrip?.GetType().InvokeMember("ShowInTaskbar", BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null, ContextMenuStrip, new object[] { p.X, p.Y }); }
protected virtual void OnMouseUp(object o, MouseEventArgs e) { if (MouseUp != null) { MouseUp(this, e); } if (e.Button == MouseButtons.Right && _contextMenu != null) { Win32.NativeMethods.POINT p = new Win32.NativeMethods.POINT(); Win32.NativeMethods.GetCursorPos(out p); // See Q135788 in the Microsoft Knowledge Base for more info. Win32.NativeMethods.SetForegroundWindow(new HandleRef(_messages, _messages.Handle)); // we cannot call _contextMenu.Show(control, point): we don't have a control reference Type ctxType = _contextMenu.GetType(); try { ctxType.InvokeMember("OnPopup", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, _contextMenu, new object[] { EventArgs.Empty }); Win32.NativeMethods.TrackPopupMenuEx(new HandleRef(ContextMenu, ContextMenu.Handle), 0, p.x, p.y, new HandleRef(_messages, _messages.Handle), IntPtr.Zero); Win32.NativeMethods.PostMessage(new HandleRef(_messages, _messages.Handle), (uint)Win32.NativeMethods.Message.WM_NULL, IntPtr.Zero, IntPtr.Zero); // clicks handled in this.OnPerformContextMenuClick()... } catch { } } }
private void ProcessInitMenuPopup(ref Message message) { if (ContextMenuStrip != null && (bool)ContextMenuStrip.GetType().InvokeMember("ProcessInitMenuPopup", BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null, ContextMenuStrip, new object[] { message.WParam })) { return; } _window.DefWndProc(ref message); }
private void ShowContextMenu() { if (ContextMenuStrip == null) { return; } NativeMethods.Point p = new NativeMethods.Point(); NativeMethods.GetCursorPos(ref p); NativeMethods.SetForegroundWindow(new HandleRef(_window, _window.Handle)); ContextMenuStrip?.GetType().InvokeMember("ShowInTaskbar", BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null, ContextMenuStrip, new object[] { p.X, p.Y }); }
private void mi_Add_Click(object sender, EventArgs e) { MessageBox.Show(contextMenu.GetType().ToString()); btn_Add_Click(sender, e); }