分类:
2008-10-13 16:31:43
如何在Tool window中使用VS.NET的Core Editor
在做VSIP编程的时候,我们会经常建立一些自己的tool window。但是并没有相关方档说明如果在tool window中使用core editor及相应的language service。其实要做到这点还是很简单的。可以将CodeWindow作为WindowPane同级来看,将CodeWindow建立在Tool window中,并hook上相应的language service。做到下面简单几步即可(代码仅为提示性片断)。
1、 创建CodeWindow
// Create a std code view (text)
hr = _VxModule.QueryService(
SID_SLocalRegistry, IID_ILocalRegistry,
(void **)&srpLocalRegistry);
if(FAILED(hr)) return hr;
hr = srpLocalRegistry->CreateInstance(
CLSID_VsCodeWindow, NULL, IID_IVsCodeWindow,
CLSCTX_INPROC_SERVER,
(void **)&srpCodeWin );
if(FAILED(hr)) return hr;
IfFailRet(srpLocalRegistry->CreateInstance(CLSID_VsTextBuffer, NULL, IID_IVsTextLines, CLSCTX_INPROC_SERVER, (LPVOID*)&srpTextLines));
IfFailRet(srpTextLines->InitializeContent(csText, csText.GetLength()));
IfFailRet(srpCodeWin->SetBuffer(srpTextLines));
IfFailRet(srpCodeWin->SetViewClassID(CLSID_VsTextView));
BSTR bstrCaption = SysAllocString(_T("[Code]"));
hr = srpCodeWin->SetBaseEditorCaption((LPCOLESTR*)&bstrCaption);
if(FAILED(hr)) return hr;
SysFreeString(bstrCaption);
// Set site of textbuffer, and therefore set language service can work.
CComPtr
hr = srpTextLines->QueryInterface( IID_IObjectWithSite, (void **)&srpObjWSite );
if(FAILED(hr))
return E_FAIL;
hr = srpObjWSite->SetSite( srpEditorSiteSP );
if(FAILED(hr))
return E_FAIL;
// Set the specific language service for the text buffer
hr = srpTextLines->SetLanguageServiceID(g_languageCLSID); //g_languageCLSID is the language service’s CLSID
hr = srpCodeWin->SetViewClassID(CLSID_VsTextView);
IfFailRet(hr);
2、 建立包含CodeWindow的tool window
// Create Tool window using code window build in.
hr = srpVsUIShell->CreateToolWindow(
dwCreateToolFlags,
0,
srpCodeWin,
CLSID_NULL,
guidToolWindow,
GUID_NULL,
NULL,
csCaption.GetBuffer(),
NULL,
&srpWindowFrame);
IfFailRet(hr);
//ExpectedExprGo(srpWindowFrame != NULL);
// Give code window all functions of core editor.
hr = srpWindowFrame->SetGuidProperty(VSFPROPID_InheritKeyBindings, CMDUIGUID_TextEditor);