/// <summary>
 /// Private helper to raise a ShowContextMenu event.
 /// </summary>
 /// <param name="eventKey">The event key of the event to raise.</param>
 /// <param name="e">A ShowContextMenuEventArgs that contains the event data.</param>
 private void RaiseEvent(object eventKey, ShowContextMenuEventArgs e)
 {
     ShowContextMenuEventHandler showContextEventHandler = (ShowContextMenuEventHandler)Events[eventKey];
     if (showContextEventHandler != null)
         showContextEventHandler(this, e);
 }
 /// <summary>
 /// Raises the ShowContextMenu event.
 /// </summary>
 /// <param name="e">A ShowContextMenuEventArgs that contains the event data.</param>
 internal void RaiseShowContextMenu(ShowContextMenuEventArgs e)
 {
     OnShowContextMenu(e);
 }
 /// <summary>
 /// Raises the ShowContextMenu event.
 /// </summary>
 /// <param name="e">A ShowContextMenuEventArgs that contains the event data.</param>
 protected virtual void OnShowContextMenu(ShowContextMenuEventArgs e)
 {
     RaiseEvent(ShowContextMenuEventKey, e);
 }
        /// <summary>
        /// Processes Windows messages.
        /// </summary>
        /// <param name="m">The Windows Message to process.</param>
        protected override void WndProc(ref Message m)
        {
            //	Dispatch the message.
            switch (m.Msg)
            {
                //	The WM_CONTEXTMENU message notifies a window that the user clicked the right
                //	mouse button (right clicked) in the window (or performed some other action that
                //	will display the context menu).
                case (int)WM.CONTEXTMENU:
                    {
                        //	Crack out x and y position.  This is in screen coordinates.
                        int x = MessageHelper.LOWORDToInt32(m.LParam);
                        int y = MessageHelper.HIWORDToInt32(m.LParam);

                        //	Remember the old ContextMenu so we can restore it later on should we change
                        //	it below.
                        ContextMenu oldContextMenu = ContextMenu;

                        //	Raise the ShowContextMenu event.
                        ShowContextMenuEventArgs showContextMenuEventArgs;
                        if (x == -1 && y == -1)
                        {
                            //	The position is -1, -1.  This indicates that the context menu is being
                            //	shown without a mouse position.  Raise the ShowContextMenu event on
                            //	this control.
                            showContextMenuEventArgs = new ShowContextMenuEventArgs(x, y, ContextMenu);
                            OnShowContextMenu(showContextMenuEventArgs);
                        }
                        else
                        {
                            //	The position is not -1, -1.  This indicates that the context menu is
                            //	being shown via a mouse gesture.  Attempt to locate the lightweight
                            //	control at the position.
                            Point point = PointToClient(new Point(x, y));
                            LightweightControl lightweightControl = GetMouseLightweightControlAtClientPoint(point);

                            //	If the lightweight control is non-null, and has a ContextMenu, raise
                            //	the ShowContextMenu event on it.
                            if (lightweightControl != null)
                            {
                                //	Instantiate the ShowContextMenuEventArgs.
                                showContextMenuEventArgs = new ShowContextMenuEventArgs(x, y, lightweightControl.ContextMenu);

                                //	Raise the ShowContextMenu event.
                                lightweightControl.RaiseShowContextMenu(showContextMenuEventArgs);
                            }
                            else
                            {
                                //	Raise the ShowContextMenu event.
                                showContextMenuEventArgs = new ShowContextMenuEventArgs(x, y, ContextMenu);
                                OnShowContextMenu(showContextMenuEventArgs);
                            }
                        }

                        //	If the event was not handled, handle it.
                        if (!showContextMenuEventArgs.Handled)
                        {
                            //	If we have a context menu to show, show it.  Otherwise, pass the message
                            //	on to the base class.
                            if (showContextMenuEventArgs.ContextMenu != null)
                            {
                                //	Make the context menu we're showing the context menu of this
                                //	control.  Doing so allows mnemonic processing to work.
                                ContextMenu = showContextMenuEventArgs.ContextMenu;

                                //	Show the context menu.
                                showContextMenuEventArgs.ContextMenu.Show(this, new Point(showContextMenuEventArgs.X, showContextMenuEventArgs.Y));
                            }
                            else
                                base.WndProc(ref m);
                        }

                        //	If we changed the context menu of this control, restore it.
                        if (ContextMenu != oldContextMenu)
                            ContextMenu = oldContextMenu;

                        //	Done!
                        return;
                    }
                case (int)WM.SETFOCUS:
                    {
                        if (_focusAndAccessibilityController != null && ActiveControl == null)
                        {
                            //eat focus messages so we can override all focus handling for this control!
                            OnGotFocus(EventArgs.Empty);
                            return;
                        }
                        break;
                    }
            }

            //	Call the base class's method.

            try
            {
                base.WndProc(ref m);
            }
            catch (Exception e)
            {
                Trace.Fail("LightweightControlContainerControl WndProc Exception", e.ToString());
            }
        }