在这些示例中,我想打印ID tab_form_1 中的内容。尝试这些内容时 样本打印 tab_form_1 内容,lpOleCommandTarget在查询界面使用CComptr时变为NULL。
如何解决这个问题?
SAMMPLE
IHTMLDocument2 *pDoc;
GetDHtmlDocument(&pDoc);
CComPtr<IHTMLElement2> spControl;
if(GetElementInterface(_T("tab_form_1") ,&spControl) != S_OK)
return;
//ole command target
LPOLECOMMANDTARGET lpOleCommandTarget = NULL;
spControl->QueryInterface(IID_IOleCommandTarget,(void**)&lpOleCommandTarget);ASSERT(lpOleCommandTarget);
//lpDispatch->Release();
if(lpOleCommandTarget == NULL) return;
//prepare header
CString header = _T("Project Path: &w");
VARIANT header_variant;
VariantInit(&header_variant);
V_VT(&header_variant) = VT_BSTR;
V_BSTR(&header_variant) = CString(header).AllocSysString();
//prepare footer
CString footer = _T("&d &t&b Page&p of &P");
VARIANT footer_variant;
VariantInit(&footer_variant);
V_VT(&footer_variant) = VT_BSTR;
V_BSTR(&footer_variant) = CString(footer).AllocSysString();
//prepare header footer safe arrray
SAFEARRAYBOUND parameter_array_bound[1];
SAFEARRAY *parameter_array = NULL;
parameter_array_bound[0].cElements = 2;
parameter_array_bound[0].lLbound = 0;
parameter_array = SafeArrayCreate(VT_VARIANT,1,parameter_array_bound);
//HRESULT hr;
long index;
index = 0;
HRESULT hr = SafeArrayPutElement(parameter_array,&index,&header_variant);
index = 1;
hr = SafeArrayPutElement(parameter_array,&index,&footer_variant);
VARIANT parameter;
VariantInit(¶meter);
V_VT(¶meter) = VT_ARRAY | VT_BYREF;
V_ARRAY(¶meter) = parameter_array;
// print contents of web browser control
lpOleCommandTarget->Exec(NULL, OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT, ¶meter,NULL);
//clear all variants
VariantClear(&header_variant);
VariantClear(&footer_variant);
if (parameter_array != NULL) {
SafeArrayDestroy(parameter_array);
}
lpOleCommandTarget->Release();
答案 0 :(得分:0)
尝试此示例。这将消除您的问题。此代码会将特定内容打印到新创建的文档中。
// create DHtmlDocument for Newly Created Dialog
HRESULT hDispatch = printdlg->m_pBrowserApp->get_Document((IDispatch**)&pDisp);
if(hDispatch != S_OK) return;
HRESULT hResult = pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pNewDoc);
if(hResult != S_OK) return;
//GetDHtmlDocument for new document
GetDHtmlDocument(&pDoc);
BSTR strSummaryText = GetElementHtml(L"tab_form_1");
// Creates a new one-dimensional array
SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
if (psaStrings == NULL) {
return;
}
VARIANT *param;
HRESULT hresult = SafeArrayAccessData(psaStrings, (LPVOID*)¶m);
param->vt = VT_BSTR;
param->bstrVal = strSummaryText;
hresult = SafeArrayUnaccessData(psaStrings);
//it will write the div part of tab_form_1 into new document to pass the summary contents alone to the printer
hresult = pNewDoc->write(psaStrings);
// SafeArrayDestroy calls SysFreeString for each strtemp
if (psaStrings != NULL) {
SafeArrayDestroy(psaStrings);
}