分类: C/C++
2008-03-18 15:07:45

interface ICommands : IDispatch
{
// methods
[id(1)] //在Vtable中的函数索引号
HRESULT GetCurDirCommandMethod(); //得到VC当前工作目录
[id(2)] //在Vtable中的函数索引号
HRESULT QuitCommandMethod (); //退出VC编辑器
};在接口ICommands添加接口函数,那么相应的我们也要在类CCommands中声明和实现ICommands接口函数,函数的内部代码和普通工程代码没什么区别。//Implement(CCommands类内部接口函数的声明)
public:
STDMETHOD(GetCurDirCommandMethod)(THIS);
STDMETHOD(QuitCommandMethod)(THIS);
//Function Code(Ccommands类内部接口函数的实现)
//得到当前VC开发环境的工作目录[您也可以让它成为你想要实现的功能代码]
STDMETHODIMP CCommands::GetCurDirCommandMethod()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
BSTR bstrCurDir;
m_pApplication->get_CurrentDirectory(&bstrCurDir);
CString str(bstrCurDir);
::MessageBox(NULL, str, "VC工作目录", MB_OK | MB_ICONINFORMATION);
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
return S_OK;
}
//退出VC开发环境
STDMETHODIMP CCommands::QuitCommandMethod()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
if(::MessageBox(NULL,
"您想退出VC++编辑器吗(Y/N)?",
"询问信息...",
MB_YESNO | MB_ICONQUESTION) == IDYES)
m_pApplication->Quit();
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
return S_OK;
}LPCTSTR szCommand = _T("GetCurDirCommand");
VARIANT_BOOL bRet;
CString strCmdString;
strCmdString.LoadString(IDS_CMD_STRING);
strCmdString = szCommand + strCmdString;
CComBSTR bszCmdString(strCmdString);
CComBSTR bszMethod(_T("GetCurDirCommandMethod"));
CComBSTR bszCmdName(szCommand); //和下面添加工具栏按钮对应
VERIFY_OK(pApplication->AddCommand(bszCmdString,bszMethod,0,dwCookie,&bRet));
//AddCommand 参数含义:
//bszCmdString:命令字符串。
//bszMethod:Icommands接口函数名。
//第三个参数代表位图偏移量。
//第四和第五个参数分贝为系统参数和返回值(参照MSDN的IApplication介绍)
if (bRet == VARIANT_FALSE)
{
*OnConnection = VARIANT_FALSE;
return S_OK;
}
//添加工具栏按钮
if (bFirstTime == VARIANT_TRUE)
{
VERIFY_OK(pApplication->AddCommandBarButton(dsGlyph, bszCmdName, m_dwCookie));
}


