How do I enable standard Visual Studio commands and accelerator keys for a Tool Window?

   

Alright! Alright! I know you want to be in command here. Just make sure your tool window gets a
good dose of high octane OLE command handling. Using Visual Studio Integration packages (VSPackages)
it is very easy to handle existing command likeEdit.Copy or Edit.Paste in your tool windows. For example
to handle Edit.Copy or Edit.Paste in your tool windows simply handle guidVSStd97.cmdidCopy and
guidVSStd97.cmidPaste commands in tool window’s IOleCommandTarget::QueryStatus and IOleCommandTarget::Exec
methods. To do this you need to implement IOleCommandTarget for the tool window.

 

See code snippets below.

 

IOleCommandTarget::QueryStatus

STDMETHODIMP CMWToolWindow::QueryStatus(

                    /*[in] */ const GUID* pguidCmdGroup,

                    /*[in] */ ULONG cCmds,

                    /*[in,out]*/ OLECMD prgCmds[],

                    /*[in,out]*/ OLECMDTEXT* pCmdText)

{

    HRESULT hr = E_OLECMDERR_E_UNKNOWNGROUP;

    ………// Some other command set handling

………

    if (*pguidCmdGroup == guidVSStd97)

    {

        // indicate supported standard commands

        switch (prgCmds[0].cmdID)

        {

        case cmdidCopy:

        case cmdidPaste:

      cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;

      hr = S_OK;

            break;

    default:

    hr = OLECMDERR_E_NOTSUPPORTED;

          break;

        }

}

return hr

}

 

 

 

IOleCommandTarget::Exec

STDMETHODIMP CMWToolWindow::Exec(

                    /*[in] */ const GUID* pguidCmdGroup,

                    /*[in] */ DWORD nCmdID,

                    /*[in] */ DWORD nCmdexecopt,

                    /*[in] */ VARIANT* pvaIn,

                    /*[out]*/ VARIANT* pvaOut)

{

      HRESULT hr = S_OK;

 ….// Some other command handling

    if (*pguidCmdGroup == guidVSStd97)

    {

        // execute supported standard commands

        switch (nCmdID)

        {

    case cmdidPaste:

    case cmdidCopy:

            ::MessageBox(NULL,L”Edit Commands",L"MitWin",MB_OK);

            break;

   default:

            return OLECMDERR_E_NOTSUPPORTED;

        }

    }

    return hr;

}

Build and register the tool window. Bring up your tool window keep it as the active window and hit Ctrl+v or

Ctrl+C and you would see a MessageBox!! 

See the MitWin Environment SDK sample for more details.

 

Thanks to some kind comments from Douglas Hodges the Doctor was able to revise content posted earlier.

 

Regards

Dr. eX