/// <summary>
        /// Called to query the context menu.
        /// </summary>
        /// <param name="hMenu">The handle to the parent menu.</param>
        /// <param name="indexMenu">The index of the menu.</param>
        /// <param name="idCmdFirst">The first command ID.</param>
        /// <param name="idCmdLast">The last command ID.</param>
        /// <param name="uFlags">The flags.</param>
        /// <returns>An HRESULT indicating success.</returns>
        int IContextMenu.QueryContextMenu(IntPtr hMenu, uint indexMenu, int idCmdFirst, int idCmdLast, CMF uFlags)
        {
            //  Log this key event.
            Log(string.Format("Query Context Menu for items {0}", string.Join(", ", SelectedItemPaths)));
            
            //  If we've got the defaultonly flag, we're done.
            if (uFlags.HasFlag(CMF.CMF_DEFAULTONLY))
                return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0);

            //  Set the first item id.
            var firstItemId = (uint)idCmdFirst;
            
            //  Use the native context menu wrapper to build the context menu.
            uint lastItemId = 0;
            try
            {
                nativeContextMenuWrapper.ResetNativeContextMenu();
                lastItemId = nativeContextMenuWrapper.BuildNativeContextMenu(hMenu, firstItemId, contextMenuStrip.Value.Items);
            }
            catch (Exception exception)
            {
                //  Log the exception.
                LogError("An exception occured building the context menu.", exception);

                //  Return the failure.
                return WinError.E_FAIL;
            }

            //  Return success, passing the the last item ID plus one (which will be the next command id).
            //  MSDN documentation is flakey here - to be explicit we need to return the count of the items added plus one.
            return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, (lastItemId - firstItemId) + 1);
        }
        /// <summary>
        /// Called to query the context menu.
        /// </summary>
        /// <param name="hMenu">The handle to the parent menu.</param>
        /// <param name="indexMenu">The index of the menu.</param>
        /// <param name="idCmdFirst">The first command ID.</param>
        /// <param name="idCmdLast">The last command ID.</param>
        /// <param name="uFlags">The flags.</param>
        /// <returns>An HRESULT indicating success.</returns>
        int IContextMenu.QueryContextMenu(IntPtr hMenu, uint indexMenu, int idCmdFirst, int idCmdLast, CMF uFlags)
        {
            //  Log this key event.
            Log(string.Format("Query Context Menu for items {0}", string.Join(", ", SelectedItemPaths)));

            //  If we've got the defaultonly flag, we're done.
            if (uFlags.HasFlag(CMF.CMF_DEFAULTONLY))
            {
                return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0));
            }

            //  Set the first item id.
            var firstItemId = (uint)idCmdFirst;

            //  Use the native context menu wrapper to build the context menu.
            uint lastItemId = 0;

            try
            {
                nativeContextMenuWrapper.ResetNativeContextMenu();
                lastItemId = nativeContextMenuWrapper.BuildNativeContextMenu(hMenu, firstItemId, contextMenuStrip.Value.Items);
            }
            catch (Exception exception)
            {
                //  Log the exception.
                LogError("An exception occured building the context menu.", exception);

                //  Return the failure.
                return(WinError.E_FAIL);
            }

            //  Return success, passing the the last item ID plus one (which will be the next command id).
            //  MSDN documentation is flakey here - to be explicit we need to return the count of the items added plus one.
            return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, (lastItemId - firstItemId) + 1));
        }
Пример #3
0
        /// <summary>
        /// Called to query the context menu.
        /// </summary>
        /// <param name="hMenu">The handle to the parent menu.</param>
        /// <param name="indexMenu">The index of the menu.</param>
        /// <param name="idCmdFirst">The first command ID.</param>
        /// <param name="idCmdLast">The last command ID.</param>
        /// <param name="uFlags">The flags.</param>
        /// <returns>An HRESULT indicating success.</returns>
        HResult IContextMenu.QueryContextMenu(IntPtr hMenu, uint indexMenu, int idCmdFirst, int idCmdLast, CMF uFlags)
        {
            //  Log this key event.
            Log(string.Format("Query Context Menu for items: {0}{1}", Environment.NewLine, string.Join(Environment.NewLine, SelectedItemPaths)));

            //  If we've got the defaultonly flag, we're done.
            if (uFlags.HasFlag(CMF.CMF_DEFAULTONLY))
            {
                return(WinError.MAKE_HResult(WinError.SEVERITY_SUCCESS, 0, 0));
            }

            //  Before we build the context menu, determine whether we need to show it.
            try
            {
                //  If we can't show the menu, return false.
                if (!CanShowMenu())
                {
                    return(HResult.S_FALSE);
                }
            }
            catch (Exception exception)
            {
                //  We can't show the menu.
                LogError("An exception ocurred determining whether the context menu should be shown.", exception);

                //  Return the failure.
                return(HResult.E_FAIL);
            }

            //  Set the first item id.
            var firstItemId = (uint)idCmdFirst;

            //  Use the native context menu wrapper to build the context menu.
            uint lastItemId = 0;

            try
            {
                nativeContextMenuWrapper.ResetNativeContextMenu();
                lastItemId = nativeContextMenuWrapper.BuildNativeContextMenu(hMenu, firstItemId, contextMenuStrip.Value.Items);
            }
            catch (Exception exception)
            {
                //  DebugLog the exception.
                LogError("An exception occured building the context menu.", exception);

                //  Return the failure.
                return(HResult.E_FAIL);
            }

            //  Return success, passing the the last item ID plus one (which will be the next command id).
            //  MSDN documentation is flakey here - to be explicit we need to return the count of the items added plus one.
            return(WinError.MAKE_HResult(WinError.SEVERITY_SUCCESS, 0, (lastItemId - firstItemId) + 1));
        }