Create ActiveX control using ATL (ActiveX Template Library)

Source: Internet
Author: User

 

  ATL can be used as a complete control development framework. Its main goal is to provide convenient development methods and generate smaller target files. Because ATL can be removed from MFC, its execution speed and loading speed are faster than those developed by MFC. Of course, you can also use MFC in ATL, but this will lose the advantages of ATL, because the Development Control Using MFC is more convenient and convenient than ATL at present. Because MFC is not used in ATL, all function calls are completed through WIN32 API functions and C Runtime library functions. However, by using ATL, we can easily develop COM objects, which is weak in MFC, but this problem is beyond the content of this chapter.

In this section, we use an example to see how to useATL creates a project and adds relevant functions. First, it creates an ATL Project Using Class Wizard. There are almost no options when creating the project. All ATL interfaces must be added after being created. In the Insert menu, select New ATL Object ..., in this case, you need to select the Full Control item in the Control class and enter the object name. To add fault tolerance and event Support, you need to select Support ISupportErrorInfo and Support Connection Points in the Attributes option.

Next we willAdd a method to the ATL interface, and enter the method name and parameters in the dialog box. You can see that the parameter attributes can be defined using brackets before parameter definitions. Available Commands include:

  • Parameters passed by the in caller
  • Value returned by the out clause to the caller
  • In and out are both input parameters and return values.


Next, we will add two methods:SetCaption ([in] BSTR pszCaption) and GetCaptionLength ([out] long * lLen) are used to set the title and obtain the string length of the title. The related code is as follows:

STDMETHODIMP CATLSam: SetCaption (BSTR pszCaption)
{
USES_CONVERSION; // conversion is required here because UNICODE is used when the COM character is transferred. unicode-> ansi
LPTSTR pTemp = W2A (pszCaption );
Delete m_pszCaption; // delete the original data
M_pszCaption = new char [strlen (pTemp) + 1];
Strcpy (m_pszCaption, pTemp );
Return S_ OK;
}
STDMETHODIMP CATLSam: GetCaptionLength (long * lLen)
{
* LLen = strlen (m_pszCaption );
Return S_ OK;
}

Next, to display the changes on the interface, let's rewrite them.The OnDraw function has the following code:

HRESUL

Related Article

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.