How To Programmatically Copy an IMG Element to the Clipboard

How To Programmatically Copy an IMG Element to the Clipboard

<script type="text/javascript">function loadTOCNode(){}</script>
Article ID:293125
Last Review:May 11, 2006
Revision:3.0
This article was previously published under Q293125

SUMMARY

<script type="text/javascript">loadTOCNode(1, 'summary');</script>
This article illustrates how to programmatically copy an image on a Web page (an IMG element) to the clipboard.

MORE INFORMATION

<script type="text/javascript">loadTOCNode(1, 'moreinformation');</script>
The best way to copy an image on a Web page to the clipboard is to use the execCommand method of the controlRange object. The following code illustrates to do this from within script on the page itself (given the ID of the image to be copied):
   function copyImage(sImgID) 
   {
      var ctrlRange = document.body.createControlRange();
      ctrlRange.add(document.all(sImgID));
      ctrlRange.execCommand("Copy");
   }
				
In Microsoft Visual C++, this method call translates to IHTMLControlRange::execCommand. The following code illustrates how to implement the same technique in Visual C++ given an IDispatch pointer to the document that contains the image and the ID of the IMG element wrapped in a VARIANT structure (with a type VT_BSTR):
   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;
   }
				

REFERENCES

<script type="text/javascript">loadTOCNode(1, 'references');</script>
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:
http://msdn.microsoft.com/ie/ (http://msdn.microsoft.com/ie/)

http://support.microsoft.com/iep (http://support.microsoft.com/iep)
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值