| |||||
|
|
Hi all, Today we will have a look at ActiveX controls. An ActiveX control is a reusable component based on the COM technology that supports a wide variety of OLE functionality. ActiveX controls can be used in normal applications (ActiveX containers) or in Web pages over the internet.Most ActiveX controls basically provide three types of interfaces (not COM related). These are Methods, Properties and Events. Properties are specific features of ActiveX control which it exposes. The properties can be Read Only or Read Write.These can be accessed directly by the containers and if they are Read Write, a container can also change the values of a property. A property of an ActiveX class is analogous to the data member of a simple C++ class.ActiveX controls can also have methods which are simple functions exposed by the control which can be called by the container of the control. These methods may be a way to get/set the value of a property, or they may be independent functions doing some specific jobs. The methods of an ActiveX control are analogous to the member functions of a C++ class.An event is a callback function which is fired by the ActiveX control when it needs to indicate something to the container. The event firing can also pass on some relevant data as chosen by the control.Now, there are many ways in which an ActiveX control can be created (MFC/ATL etc), also a lot of languages (C++, VB etc.) support ActiveX control development. Earlier an ActiveX control used to be an OCX file which can be registered and used by other applications. Now a days, the controls are DLLs or Exes which need to be registered to be used.VC++ supports ATL and MFC ActiveX control development and the controls can be OCX, DLLs or EXE's. There are wizards which can guide you through while creating an MFC or ATL ActiveX control. ActiveX controls can have visible windows or they can be hidden when they are instantiated. A single DLL can contain multiple ActiveX controls. When creating an ActiveX control through MFC wizard, you are presented with an option to select the number of controls.Let's see how we can create a simple activeX control using MFC and test it using ActiveX Test Container that comes with VC++.1. Create an ActiveX control project using MFC ActiveX wizard, select the number of controls as 1. Leave other wizard values at their default.2. Now to add a method to the control, open class wizard and go to Automation Page. Click on Add method. Add an external name for the method. An external name is one which will be exposed to the containers of the control. Select an Internal name which typically is the same as the external name. Internal name is the one with which the class wizard will generate the function. Provide a return type and give some parameters which ever you may deem suitable. You can similarly add more methods.3. To add a property, Click Add property in the class wizard->Automation page. Provide an external name for your property. Specify a type for the property. Give a name for the variable which will hold the value of this property. There are two types of implementations for a property. A member function implementation allows access to the property with a .PropertyName convention on your control. The Get/Set methods implementation generates two methods in your control through which the property can be get or set. Similarly, the notification function gets fired when the value of the property changes.4. To add an event, open class wizard and go to ActiveX events page. Click AddEvent and give an external name to the Event. This is the name which will be exposed to the container. The internal name is the name of a function which you can call in your control whenever you need to fire the event you have exposed. You can now provide the Parameters to this event which get passed to the container of your control. 5. A property page is also generated for your control where you can provide a way to give design time modifications of the properties of your control. 6. Now you can add your own code for handling of the properties, methods and events for the control. 7. Once finished, you can build your control. The build process will automatically register the control. 8. To test the control, Click on Execute from Build menu. In the resulting dialog, select (Right arrow) ActiveX Control Test container as the Executable File Name.9. In the test container, go to Edit->Insert Control. You will be presented with a list where you can select your control.10. Now you can invoke methods of your control, you can view the properties, get/set their values, see the events getting fired and also view the property pages of your control in this test container. You should experiment with this to get familiar with the container.So, that's about all that you need to develop a framework ActiveX control and test it. You can now experiment with the properties, methods and events and see their results. You can also customize how the control appears (if it's visible) by providing your own OnDraw() method code.Go ahead and experiment with the control and it's test container. You will learn new things all the way. Exercises: 1. Study ActiveX controls in MSDN. 2. Develop a simple ActiveX control (MFC) with some properties, methods and events and test it in the test container. 3. Find out how you can develop an ActiveX control using ATL. 4. Experiment with multiple controls in one project. This should be sufficient for this week, we will cover some new topic next week. Thanks for your time ! -Farooque
|