如何以程式設計方式將一個 IMG 元素複製到剪貼簿
阿新 • • 發佈:2019-01-05
STDMETHODIMP CMyBrowser::CopyImage(LPDISPATCH pDispDoc, VARIANT vImageID) { HRESULT hr = E_FAIL; IHTMLDocument2* pDoc = NULL; IHTMLElement* pelmBody = NULL; IHTMLElement2* pelmBodyTwo = NULL; IDispatch* pdispImgElement = NULL; IDispatch* pdispCtrlRange = NULL; IHTMLElementCollection* pColl = NULL; IHTMLControlElement* pCtrlElement = NULL; IHTMLControlRange* pCtrlRange = NULL; BSTR bstrCommand = SysAllocString(L"Copy"); VARIANT_BOOL vbReturn; VARIANT vEmpty; VariantInit(&vEmpty); if (pDispDoc == NULL) goto Cleanup; if (FAILED(pDispDoc->QueryInterface(IID_IHTMLDocument2, (void**) &pDoc))) goto Cleanup; if (FAILED(pDoc->get_all(&pColl))) goto Cleanup; if (FAILED(pColl->item(vImageID, vEmpty, &pdispImgElement)) || pdispImgElement == NULL) goto Cleanup; if (FAILED(pDoc->get_body(&pelmBody)) || pelmBody == NULL) goto Cleanup; if (FAILED(pelmBody->QueryInterface(IID_IHTMLElement2, (void**) &pelmBodyTwo)) || pelmBodyTwo == NULL) goto Cleanup; if (FAILED(pelmBodyTwo->createControlRange(&pdispCtrlRange)) || pdispCtrlRange == NULL) goto Cleanup; if (FAILED(pdispCtrlRange->QueryInterface(IID_IHTMLControlRange, (void**) &pCtrlRange)) || pCtrlRange == NULL) goto Cleanup; if (FAILED(pdispImgElement->QueryInterface(IID_IHTMLControlElement, (void**) &pCtrlElement)) || pCtrlElement == NULL) goto Cleanup; hr = pCtrlRange->add(pCtrlElement); if (SUCCEEDED(hr)) hr = pCtrlRange->execCommand(bstrCommand, VARIANT_FALSE, vEmpty, &vbReturn); pCtrlElement->Release(); hr = S_OK; Cleanup: SysFreeString(bstrCommand); if (pCtrlRange) pCtrlRange->Release(); if (pdispCtrlRange) pdispCtrlRange->Release(); if (pelmBodyTwo) pelmBodyTwo->Release(); if (pelmBody) pelmBody->Release(); if (pdispImgElement) pdispImgElement->Release(); if (pColl) pColl->Release(); if (pDispDoc) pDispDoc->Release(); return hr; }