用ATL实现VC插件
作者: IUNKNOW 出处: vchelp
如果我增加一条这样的命令AddCommand(CComBSTR(_T("ShowInfoDlg\n显示\n修改用户版权信息\n显示")),CComBSTR(_T("ShowInfoDlg")), 0, dwAddInID, &bRet);在Categroy的All Commands中就能找到ShowInfoDlg这条命令。AddCommandBarButton()方法就是向IDE集成环境增加一个工具条按钮,程序中可以不调用此方法,可以手动拖拉到工具条上。打开菜单Tools/Customize 用鼠标拽拉你需要的的命令到工具条上即可(如图)
在Ccopyright 类NewDocument()方法中的代码,主要就是增加必要的版权信息到文档中,此处主要是说明怎样操作VC环境中文档,大家也得到具体的Windows来修改窗口的形状和属性,怎么修改就要看各自的想象力了。
HRESULT CCopyright::NewDocument(IDispatch* theDocument)
{
CComQIPtr <ITextDocument,&IID_ITextDocument> pTextDoc(theDocument);
HRESULT hr = S_OK;
CComBSTR bstrLang;
if (pTextDoc == NULL) return hr;
hr = pTextDoc->get_Language(&bstrLang);
if (FAILED(hr))
return hr;
//µÃµ½Îļ
þÀàÐÍ£¬²¢
µÃ³ö¶ÔÓ¦µÄ
×¢ÊÍ·û
CComBSTR bstrComment;
if (bstrLang == DS_CPP)
bstrComment = L"//";
else if (bstrLang == DS_HTML_IE3 || bstrLang == DS_HTML_RFC1866)
bstrComment = L"<!--";
else if (bstrLang == DS_IDL)
bstrComment = L"//";
else if (bstrLang == DS_VBS)
bstrComment = L"\'";
else if (bstrLang == DS_JAVA)
bstrComment = L"//";
else
bstrComment = L";";
CComBSTR bstrType;
hr = pTextDoc->get_Type(&bstrType);
if(FAILED(hr))
return hr;
if(bstrType == DS_TEXT_DOCUMENT)
{
CComQIPtr <ITextSelection,&IID_ITextSelection> pTextSel;
CComPtr<IDispatch> pDisp;
hr = pTextDoc->get_Selection(&pDisp);
if(FAILED(hr))
return hr;
pTextSel = pDisp;
hr = pTextSel->StartOfDocument(CComVariant());
if(FAILED(hr))
return hr;
SYSTEMTIME st;
GetLocalTime(&st);
TCHAR strDate[30];
TCHAR strYear[5];
wsprintf(strYear,_T("%ld"),st.wYear);
wsprintf(strDate,_T("%ldÄê%ldÔÂ%ldÈÕ"),st.wYear,st.wMonth,st.wDay);
CComBSTR bstrText;
bstrText = bstrComment;
bstrText += "\n";
if (!(bstrLang == DS_HTML_IE3 || bstrLang == DS_HTML_RFC1866))
bstrText += bstrComment;
bstrText += L" °æȨËùÓÐ(C) Robin Hohai University ";
bstrText += strYear;
bstrText += L"\n";
if (!(bstrLang == DS_HTML_IE3 || bstrLang == DS_HTML_RFC1866))
bstrText += bstrComment;
bstrText += L"\n";
if (!(bstrLang == DS_HTML_IE3 || bstrLang == DS_HTML_RFC1866))
bstrText += bstrComment;
bstrText += L" ×÷ Õߣº";
bstrText += m_bstrName;
bstrText += L" E-mail£º";
bstrText += m_bstrEmail;
bstrText += L"\n";
if (!(bstrLang == DS_HTML_IE3 || bstrLang == DS_HTML_RFC1866))
bstrText += bstrComment;
bstrText += L"\n";
if (!(bstrLang == DS_HTML_IE3 || bstrLang == DS_HTML_RFC1866))
bstrText += bstrComment;
bstrText += L" ´´½¨ÈÕÆÚ£º";
bstrText += strDate;
bstrText += L"\n";
if (!(bstrLang == DS_HTML_IE3 || bstrLang == DS_HTML_RFC1866))
bstrText += bstrComment;
if (bstrLang == DS_HTML_IE3 || bstrLang == DS_HTML_RFC1866)
bstrText += L" -->";
bstrText += L"\n";
hr = pTextSel->put_Text(bstrText);
}
return hr;
}
评论