COM Component Development Practices (VII)---multithreaded ActiveX controls and AutoFit ActiveX control size (top)

Source: Internet
Author: User
Tags goto thread

Disclaimer: This code is based on CodeProject's article, "A Complete ActiveX Web control Tutorial," and therefore also follows code Project Open License (Cpol).

Recently encountered two requirements: 1 use worker threads in an ActiveX control to complete the underlying hardware Device scan task, and to callback the JavaScript function of the external Web page in the worker thread based on the result of the operation; 2 to automatically resize the control based on the different control tasks. But after consulting a lot of data, found that the internet discussion of the multithread development of the article basically did not, finally in the CSDN Forum encountered a master to help, groping for a few days to solve these two problems, the purpose of this article is to record my solution to these two problems of the process, I also hope to help friends who have the same needs in the future.

Simple abstraction of the first task's model: Open a worker thread in the Acitvex control to perform the feature task, and then notify the external Web page's JavaScript based on the execution results of the worker thread. Before going into multithreading, let's look at two ways to invoke the JavaScript functions of an external Web page in ActiveX.

Invoke JavaScript in ActiveX

The first way is to use events, which is the simplest method. In Class View, right-click Cmyactivexctrl and choose Add Event, which is not the way to go.

The second approach is to use the IWebBrowser2 and IHTMLDocument2 two COM components to access all the elements on the external Web page that contains the ActiveX control. The following steps are implemented:

1, add two variables to the Cmyactivexctrl class:

public:
  IWebBrowser2* pWebBrowser; //IE浏览器
  IHTMLDocument2* pHTMLDocument; //包含此控件的web页面

2, overload the Onsetclientsite function.

void Cmyactivexctrl::onsetclientsite ()
{
HRESULT hr = S_OK;
IServiceProvider *isp,*isp2 = NULL;
if (!m_pclientsite)
{
Comrelease (Pwebbrowser);
}
Else
{
hr = M_pclientsite->queryinterface (iid_iserviceprovider,reinterpret_cast<void **> (&isp));
if (FAILED (HR))
{
hr = S_OK;
Goto cleanup;
}
hr = Isp->queryservice (sid_stoplevelbrowser,iid_iserviceprovider,reinterpret_cast<void **> (&AMP;ISP2));
if (FAILED (HR))
{
hr = S_OK;
Goto cleanup;
}
hr = Isp2->queryservice (sid_swebbrowserapp,iid_iwebbrowser2,reinterpret_cast<void **> (&pWebBrowser)) ; Query IE Browser interface
if (FAILED (HR))
{
hr = S_OK;
Goto cleanup;
}
hr = Pwebbrowser->get_document ((idispatch**) &phtmldocument); Query Web page interface
if (FAILED (HR))
{
hr = S_OK;
Goto cleanup;
}
Cleanup
Free resources.
Comrelease (ISP);
Comrelease (ISP2);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.