To give an example of OLE/COM technologies in action, we will now create a compound document and give a very high-level overview of what is going on as we do so. Compound documents are the simplest OLE/COM technology to demonstrate, since all basic Windows 95 and NT 4 setups include some applications that can be used as compound document containers and servers. If you have created a compound document before, you may wish to skip this section.
To begin, simply start the WordPad program. It should be available from the Start menu as Programs > Accessories > WordPad. Type some text into the empty document window. We can embed a picture into this document, thereby creating a compound document.
Choose Insert > Object in WordPad. This command brings up the Insert Object dialog, which shows a list of COM objects you can embed in a WordPad document.
This is actually a list of new objects; by changing the option button selection from Create New to Create from File we can also embed an existing file into the document and activate it using the application that created it.
For this example, we want to create a new Paintbrush picture. Select Paintbrush Picture in the Object Type list and click OK.
Figure 2.2 WordPad's Insert Object dialog.
Having chosen the Paintbrush picture object, the WordPad window shows a drawing region bounded on two sides by scroll bars. This is our embedded Paintbrush picture object. Notice too that the horizontal toolbar has gone away, to be replaced by a vertical toolbar and a pane at the bottom of the window containing a color palette. In addition, the menu bar has changed.
The new toolbar, pane, and menu bar are actually part of the user interface for the bundled Windows application Paint (formerly Paintbrush). We can confirm this by going to the Help menu; the "About" option is now About Paint.
The Paint toolbar, pane, and menus remain available as long as the picture object is active. The object is active when we can draw in the region; activation ends when we click outside the region or press Esc. Because the picture object is active inside the WordPad document, this sort of activation is known as in-place activation. The opposite of an in-place activation would be one where the picture object started up in a separate Paint window instead of in the container, WordPad.
Figure 2.3 An activated embedded Paintbrush picture object in WordPad.
If we click outside the drawing region, the activation is ended and the Paintbrush tools go away. The WordPad toolbar returns. However, the picture we have drawn remains embedded. We can save the document and, when we open it again in WordPad, the embedded Paintbrush picture will still be available for viewing and editing.
Thus we have created a compound document comprised of data from WordPad and Paint. The document remains principally a WordPad document--WordPad will be the application it will open in by default--but clearly it is not just a WordPad document.
We can continue working with this document to demonstrate the difference between linking and embedding, and between in-place and out-of-place activation. We will now link a file containing a picture object into the document.
Start Paint up from the Start menu with Programs > Accessories > Paint. Now draw something in the empty picture, and save it in a new file.
Return to the WordPad document. We are now going to link our new picture file into the WordPad document. Make sure the existing picture object is not activated, because we cannot link a picture object into a picture object. (Notice there is no Insert > Object command in WordPad while a picture object is activated--we can conclude from this that Paint can only play the role of a server in a compound document, and not that of a container.)
Choose Insert > Object in WordPad and select the Create from File option button. Select your Paint picture file in the File box.
If we clicked OK at this stage, the object in the Paint picture file would be embedded in the WordPad compound document. Future changes to the Paint picture file would not be reflected in WordPad document. However, if we select the Link box before clicking OK, the picture object would be stored as a link to the Paint picture file, so future changes to the file would be reflected in the WordPad document.
Select the Link box and click OK. If we double-click on the inserted picture to active it, the activation occurs in a new Paint window. Linked objects can never be edited in place. To end this sort of activation, we have to exit the Paint window.