用ATL实现VC插件
作者: IUNKNOW 出处: vchelp
4) 选择属性对话框的Names标签,在C++组的Short Name编辑框输入Copyright(如图)
5) 选择DevStudio Add-in标签,在Add-in Features中选择Application Events,选择OK按钮确定
上面的步骤生成基本Add-in框架代码
6) 创建IDE工具条
a) 创建两个Bitmap 文件作为工具条图像资源
IDB_TOOLBAR_LARGE_COPYRIGHT 32 x 32大小
IDB_TOOLBAR_MEDIUM_COPYRIGHT 16 x 16 大小
b) 在Ccopyright 类的OnConnection()函数中加入创建工具条按钮的代码, 详细代码:
HRESULT CCopyright::OnConnection(IApplication* pApp,
VARIANT_BOOL bFirstTime, long dwAddInID, VARIANT_BOOL* bOnConnection)
{
HRESULT hr = S_OK;
m_spApplication = pApp;
m_dwAddInID = dwAddInID;
// Connect up to application event sink
AtlAdvise(pApp, GetUnknown(), IID_IApplicationEvents, &m_dwAppEvents);
hr = pApp->SetAddInInfo((long)_Module.GetModuleInstance(),
static_cast<ICopyright*>(this), IDB_TOOLBAR_MEDIUM_COPYRIGHT,
IDB_TOOLBAR_LARGE_COPYRIGHT, dwAddInID);
// LPCTSTR szCommand = _T("ShowInfoDlg");
VARIANT_BOOL bRet;
if (SUCCEEDED(hr))
{
hr = pApp->AddCommand(CComBSTR(_T("ShowInfoDlgÏÔʾ
ÐÞ¸ÄÓûµÄ
æȨÐÅÏ¢
Ð޸İæÈ
ÐÅÏ¢")),CComBSTR(_T("ShowInfoDlg")), 0, dwAddInID, &bRet);
hr = pApp->AddCommand(CComBSTR(_T("MiniWindow\nËõС
\n×îС»¯ËÓеĴ
¿ÚîС»¯´°¿Ú")),
CComBSTR(_T("MiniWindow")), 1, dwAddInID, &bRet);
hr = pApp->AddCommand(CComBSTR(_T("MaxWindow\n·Å´
î´»¯ËÓеĴ
¿Úî´»¯´
¿Ú")),CComBSTR(_T("MaxWindow")), 2, dwAddInID, &bRet);
hr = pApp->AddCommand(CComBSTR(_T("Test\nTest\nTest\nTest")),CComBSTR(_T("Test")), 3, dwAddInID, &bRet);
}
// Add toolbar buttons only if this is the first time the add-in
// is being loaded. Toolbar buttons are automatically remembered
// by Developer Studio from session to session, so we should only
// add the toolbar buttons once.
if (bFirstTime)
{
if (SUCCEEDED(hr))
{
hr = pApp->AddCommandBarButton(dsGlyph, CComBSTR(_T("ShowInfoDlg")), dwAddInID);
hr = pApp->AddCommandBarButton(dsGlyph, CComBSTR(_T("MiniWindow")), dwAddInID);
hr = pApp->AddCommandBarButton(dsGlyph, CComBSTR(_T("MaxWindow")), dwAddInID);
hr = pApp->AddCommandBarButton(dsGlyph, CComBSTR(_T("Test")), dwAddInID);
}
}
//
CRegKey reg;
TCHAR strName[256];
DWORD dwSize = 256,dwRet = 0;
reg.Create(HKEY_CURRENT_USER,_T("Software\\Evesoft\\Addins"));
dwRet = reg.QueryValue(strName,_T("NAME"),&dwSize);
if(dwRet != ERROR_SUCCESS || lstrlen(strName) == 0)
m_bstrName = _T("Evelan");
else
m_bstrName = strName;
dwSize = 100;
dwRet = reg.QueryValue(strName,_T("EMAIL"),&dwSize);
if(dwRet != ERROR_SUCCESS || lstrlen(strName) == 0)
m_bstrEmail = _T("dbw.evelan@263.net");
else
m_bstrEmail = strName;
//
*bOnConnection = SUCCEEDED(hr) ? VARIANT_TRUE :VARIANT_FALSE;
return hr;
}
HRESULT CCopyright::OnDisconnection(VARIANT_BOOL bLastTime)
{
AtlUnadvise(m_spApplication, IID_IApplicationEvents, m_dwAppEvents);
return S_OK;
}
此段代码主要用了Application Object的两个方法AddCommand()和AddCommandBarButton(),AddCommand()方法调用后,在菜单的Tools/Customize 中的Commands标签中的Categroy列表框中就会有对应的Command命令(),
评论