Step 7: Putting the Control on a Web Page
Updated: January 2011
Your control is now finished. To see your control work in a real-world situation, put it on a Web page. An HTML file that contains the control was created when you defined your control. Open the PolyCtl.htm file from Solution Explorer, and you can see your control on a Web page.
In this step, you will script the Web page to respond to events. You will also modify the control to let Internet Explorer know that the control is safe for scripting.
The control does not do anything yet, so change the Web page to respond to the events that you send.
To script the Web page
Open PolyCtl.htm and select HTML view. Add the following lines to the HTML code. They should be added after </object> but before </body>.
Save the HTM file.
You can view the Web page with the control in Internet Explorer or, more conveniently, use the Web browser view built into Visual C++. To see your control in the Web browser view, right-click PolyCtl.htm, and click View in Browser.
Based on your current Internet Explorer security settings, you may receive a Security Alert dialog box stating that the control may not be safe to script and could potentially do damage. For example, if you had a control that displayed a file but also had a Delete method that deleted a file, it would be safe if you just viewed it on a page. It would be not safe to script, however, because someone could call the Delete method.
For this tutorial, you can change your security settings in Internet Explorer to run ActiveX controls that are not marked as safe. In Control Panel, click Internet Properties and click Security to change the appropriate settings. When you have completed the tutorial, change your security settings back to their original state.
You can programmatically alert Internet Explorer that it does not need to display the Security Alert dialog box for this particular control. You can do this with the IObjectSafety interface, and ATL supplies an implementation of this interface in the class IObjectSafetyImpl. To add the interface to your control, add IObjectSafetyImpl to your list of inherited classes and add an entry for it in your COM map.
To add IObjectSafetyImpl to the control
Build the control. Once the build has finished, open PolyCtl.htm in browser view again. This time, the Web page should be displayed directly without the Safety Alert dialog box. Click inside the polygon; the number of sides increases by one. Click outside the polygon to reduce the number of sides. If you try to reduce the number of sides below three, you will see the error message that you set.
This concludes the ATL tutorial. For links to more information about ATL, see the ATL start page.