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

📄 connect.cpp

📁 语法检查程序
💻 CPP
字号:
/***************************************************************************/
/* NOTE:                                                                   */
/* This document is copyright (c) by Oz Solomon and Yonat Sharon, and is   */
/* bound by the MIT open source license.                                   */ 
/* See License.txt or visit www.opensource.org/licenses/mit-license.html   */
/***************************************************************************/

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

//extern CAddInModule _AtlModule;

// 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.

const wchar_t * const MODULE_NAME  = L"Spelly";
const wchar_t * const TOOLBAR_NAME = L"Spelly";

CStringW CMDNAME_WITHCODE = L"<<dummy>>";
CStringW CMDNAME_NOCODE   = L"<<dummy>>";

struct CommandInfo
{
	int					  ResIDText;
	bool				  IsMSOButton;
	int					  ButtonIndex;
	CStringW *   		  StrToInitialize;
};

CommandInfo Commands[] =
{
	// in reverse order of appearance on toolbar
	{ IDS_CMD_SPELL_NOCODE, false, IDB_COMMAND_NOCODE, &CMDNAME_NOCODE    },	
	{ IDS_CMD_SPELL,		false, IDB_COMMAND,		   &CMDNAME_WITHCODE  },
};


HRESULT CConnect::InitializeCommand(CommandInfo& info, bool bCmdStringOnly) const
{
    CComPtr<EnvDTE::Commands> pCommands;
    CComPtr<EnvDTE::Command> pCreatedCommand;

	CComBSTR bstrName, bstrBtnText, bstrTip, bStrFull;
	CString cStr, cStr2;
	CStringW cStrW;
	cStr.LoadString(info.ResIDText);

	AfxExtractSubString(cStr2, cStr, 0);
	ASSERT(!cStr2.IsEmpty());
	bstrName = cStr2;

	AfxExtractSubString(cStr2, cStr, 1);
	ASSERT(!cStr2.IsEmpty());
	bstrBtnText = cStr2;

	AfxExtractSubString(cStr2, cStr, 2);
	ASSERT(!cStr2.IsEmpty());
	bstrTip = cStr2;

	cStrW.Format(L"%ws.%ws", MODULE_NAME, bstrName);

	if (info.StrToInitialize)
	{
		*info.StrToInitialize = cStrW;
	}

	if (bCmdStringOnly)
	{
		return S_OK;
	}

    HRESULT hr = m_pDTE->get_Commands(&pCommands);
	if (SUCCEEDED(hr))
	{
        hr = pCommands->Item(CComVariant(cStrW), 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, 
			bstrName,
			bstrBtnText,
			bstrTip,
			info.IsMSOButton? VARIANT_TRUE : VARIANT_FALSE, 
            info.ButtonIndex,
            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;
}


// 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);

	const bool bUISetup = (ConnectMode == 5); //5 == AddInDesignerObjects::ext_cm_UISetup)
	for (int i = 0; i < countof(Commands); ++i)
	{
		hr = InitializeCommand(Commands[i], !bUISetup);
		if (!SUCCEEDED(hr)) break;
	}

	return hr;
}

STDMETHODIMP CConnect::OnDisconnection(AddInDesignerObjects::ext_DisconnectMode /*RemoveMode*/, SAFEARRAY ** /*custom*/ )
{
	m_pDTE = NULL;
	delete m_pSpellyer;
	m_pSpellyer = 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, CMDNAME_WITHCODE)  ||
			!_wcsicmp(bstrCmdName, CMDNAME_NOCODE) )
		{
			*pStatusOption = (EnvDTE::vsCommandStatus)(EnvDTE::vsCommandStatusSupported);

			CComPtr<EnvDTE::Document> pDocument;
			m_pDTE->get_ActiveDocument(&pDocument);

			if (pDocument)
			{
				IDispatch *pSel;
				pDocument->get_Selection(&pSel);
				CComQIPtr<EnvDTE::TextSelection, &EnvDTE::IID_TextSelection> pTextSel;
				pTextSel = pSel;
				if (pSel) pSel->Release();
				if (!pTextSel) pDocument = NULL;
			}

			if (pDocument)
			{
				*pStatusOption = (EnvDTE::vsCommandStatus)
					(EnvDTE::vsCommandStatusEnabled + *pStatusOption);
			}
		}
	}
	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, CMDNAME_NOCODE))
		{
			SpellyCommandMethod(false);
			*pvbHandled = VARIANT_TRUE;
			return S_OK;
		}
		if (!_wcsicmp(bstrCmdName, CMDNAME_WITHCODE))
		{
			SpellyCommandMethod(true);
			*pvbHandled = VARIANT_TRUE;
			return S_OK;
		}
	}
	return S_OK;
}

STDMETHODIMP CConnect::SpellyCommandMethod(bool bIncludeCode) 
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
//	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));

	try
	{
		if (m_pSpellyer == NULL)
			m_pSpellyer = new Spellyer;

		CComPtr<EnvDTE::Document> pDocument;
		m_pDTE->get_ActiveDocument(&pDocument);
		ASSERT(pDocument);  // command should be disabled if not

		if (pDocument)
			m_pSpellyer->Spell(pDocument, bIncludeCode);
		else
			MessageBeep(0);
	}
	catch (...)
	{
#ifdef _DEBUG
		AfxMessageBox("Exception thrown in Spelly", MB_OK | MB_ICONEXCLAMATION);
#endif
	}

//  VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
	return S_OK;
}

⌨️ 快捷键说明

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