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