⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectvc7.cpp

📁 一个统计文件大小和程序信息的插件程序(vc或vc.net下使用)
💻 CPP
字号:
/***************************************************************************/
/* NOTE:                                                                   */
/* This document is copyright (c) by Oz Solomonovich, and is bound by the  */
/* MIT open source license (www.opensource.org/licenses/mit-license.html). */
/* See License.txt for more information.                                   */
/***************************************************************************/

// Connect.cpp : Implementation of CConnect
#include "stdafx.h"

#ifdef TARGET_VC7

#include "LineCount.h"
#include "ConnectVC7.h"
#include "WorkspaceInfo.h"
#include "ResultsDlg.h"
#include "Config.h"

const wchar_t * const TOOLBAR_NAME      = L"PLC (Project Line Counter)";

const wchar_t * const COMMAND_NAME_FULL = L"LineCount.CountLines";
const wchar_t * const COMMAND_NAME      = L"CountLines";
const wchar_t * const COMMAND_TEXT      = L"Count Lines";
const wchar_t * const COMMAND_TIP       = L"Count lines of code in your projects";

// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
//   1) You moved this project to a computer other than which is was originally created on.
//   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
//   3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup project 
// by right clicking the project in the Solution Explorer, then choosing install.


// CConnect
STDMETHODIMP CConnect::OnConnection(IDispatch *pApplication, AddInDesignerObjects::ext_ConnectMode ConnectMode, IDispatch *pAddInInst, SAFEARRAY ** /*custom*/ )
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    HRESULT hr = S_OK;
    pApplication->QueryInterface(__uuidof(EnvDTE::_DTE), (LPVOID*)&m_pDTE);
    pAddInInst->QueryInterface(__uuidof(EnvDTE::AddIn), (LPVOID*)&m_pAddInInstance);

    OnAddinConnect();

    if(ConnectMode == 5 /* AddInDesignerObjects::ext_cm_UISetup */)
    {
        CComPtr<EnvDTE::Commands> pCommands;
        CComPtr<EnvDTE::Command> pCreatedCommand;

        hr = m_pDTE->get_Commands(&pCommands);

        if (SUCCEEDED(hr))
        {
            hr = pCommands->Item(CComVariant(COMMAND_NAME_FULL), 0, 
                &pCreatedCommand);

            // remove command if it's already there
            if (SUCCEEDED(hr)  &&  pCreatedCommand)
            {
                pCreatedCommand->Delete();
                pCreatedCommand = NULL;
            }

            // add our command
            hr = pCommands->AddNamedCommand(
                m_pAddInInstance, 
                CComBSTR(COMMAND_NAME), 
                CComBSTR(COMMAND_TEXT), 
                CComBSTR(COMMAND_TIP), 
                VARIANT_FALSE, 
                IDB_LCGO,  // MSO: 127
                NULL, 
                EnvDTE::vsCommandStatusSupported+EnvDTE::vsCommandStatusEnabled, 
                &pCreatedCommand);

            if (SUCCEEDED(hr)  &&  pCreatedCommand)
            {
                CComPtr<Office::_CommandBars>      pCommandBars;
                CComPtr<Office::CommandBarControl> pCommandBarControl;
                CComQIPtr<Office::CommandBar>      pToolbar;

                hr = m_pDTE->get_CommandBars(&pCommandBars);
                if (SUCCEEDED(hr))
                {
                    // Get our toolbar
                    hr = pCommandBars->get_Item(CComVariant(TOOLBAR_NAME), 
                        &pToolbar);
                    if (!SUCCEEDED(hr))
                    {
                        // haven't created the toolbar yet
                        CComPtr<IDispatch> pDisp;
                        hr = pCommands->AddCommandBar((LPWSTR)TOOLBAR_NAME,
                            EnvDTE::vsCommandBarTypeToolbar, NULL, 0, 
                            &pDisp);        
                        if (SUCCEEDED(hr))
                        {
                            pToolbar = pDisp;
                        }
                        if (pToolbar == NULL)
                        {
                            hr = E_FAIL;
                        }
                    }
                    else
                    {
                        // make sure visible
                        pToolbar->put_Visible(VARIANT_TRUE);
                    }
                }
                if (SUCCEEDED(hr))
                {
                    // this causes a memory leak -- don't know why
                    hr = pCreatedCommand->AddControl(pToolbar, 1, 
                        &pCommandBarControl);
                    if (SUCCEEDED(hr))
                    {
                        // make sure it's image only
                        CComQIPtr<Office::_CommandBarButton> 
                            pButton(pCommandBarControl);
                        if (pButton) pButton->put_Style(Office::msoButtonIcon);
                    }
                }
            }
        }
        
        return hr;
    }

    if (g_pWorkspaceInfo == NULL)
    {
        InitializeWorkspaceInfo_VC7(m_pDTE);
    }

    return hr;
}

STDMETHODIMP CConnect::OnDisconnection(AddInDesignerObjects::ext_DisconnectMode /*RemoveMode*/, SAFEARRAY ** /*custom*/ )
{
    OnAddinDisconnect();
    
    m_pDTE = NULL;
    return S_OK;
}

STDMETHODIMP CConnect::OnAddInsUpdate (SAFEARRAY ** /*custom*/ )
{
    return S_OK;
}

STDMETHODIMP CConnect::OnStartupComplete (SAFEARRAY ** /*custom*/ )
{
    return S_OK;
}

STDMETHODIMP CConnect::OnBeginShutdown (SAFEARRAY ** /*custom*/ )
{
    return S_OK;
}

STDMETHODIMP CConnect::QueryStatus(BSTR bstrCmdName, EnvDTE::vsCommandStatusTextWanted NeededText, EnvDTE::vsCommandStatus *pStatusOption, VARIANT *pvarCommandText)
{
  if(NeededText == EnvDTE::vsCommandStatusTextWantedNone)
    {
      if(!_wcsicmp(bstrCmdName, COMMAND_NAME_FULL))
      {
          *pStatusOption = (EnvDTE::vsCommandStatus)
              (EnvDTE::vsCommandStatusEnabled |
               EnvDTE::vsCommandStatusSupported);
      }
  }
    return S_OK;
}

STDMETHODIMP CConnect::Exec(BSTR bstrCmdName, EnvDTE::vsCommandExecOption ExecuteOption, VARIANT * /*pvarVariantIn*/, VARIANT * /*pvarVariantOut*/, VARIANT_BOOL *pvbHandled)
{
    *pvbHandled = VARIANT_FALSE;
    if(ExecuteOption == EnvDTE::vsCommandExecOptionDoDefault)
    {
        if(!_wcsicmp(bstrCmdName, COMMAND_NAME_FULL))
        {
            AFX_MANAGE_STATE(AfxGetStaticModuleState());
            try
            {
                CResultsDlg dlg(g_pWorkspaceInfo);
                dlg.DoModal();
            }
            catch (...)
            {
                AfxMessageBox("Exception caught in LineCounter", 
                    MB_OK | MB_ICONEXCLAMATION);
            }
            *pvbHandled = VARIANT_TRUE;
            return S_OK;
        }
    }
    return S_OK;
}

#endif // TARGET_VC7

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -