Article ID: 138411 - Last Review: November 21, 2006 - Revision: 3.3 Deriving an OLE Control from a Base ControlThis article was previously published under Q138411 This is the Visual C++ 1.5x and 2.x version of this sample.
There is an equivalent Visual C++ 4.x 32-bit sample available under the name
SHAPES32. On This PageSUMMARY The SHAPES sample illustrates how to derive an OLE control
from a base control thus allowing the derived control to take advantage of the
base control's methods, properties, and events. The following files are available for download from the Microsoft Download Center: Shapes2.exe (http://download.microsoft.com/download/vc10/sample/1/win98/en-us/shapes2.exe) For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base: 119591
(http://support.microsoft.com/kb/119591/EN-US/
)
How to Obtain Microsoft Support Files from Online Services
Microsoft scanned this file for viruses. Microsoft used the most
current virus-detection software that was available on the date that the file
was posted. The file is stored on security-enhanced servers that help to
prevent any unauthorized changes to the file.
MORE INFORMATION The Shapes2 sample implements a base control class called
CBaseShapeCtrl and two derived control classes called CCircleCtrl and
CRectangleCtrl. CCircleCtrl and CRectangleCtrl draw themselves using the
properties provided by the base CBaseShapeCtrl class. CCircleCtrl and
CRectangleCtrl also allow access to the methods and events implemented in the
base CBaseShapeCtrl class. Properties, Methods, and EventsThe CBaseShapeCtrl class provides the base functionality of a simple shape control. It implements the following properties, events, and methods:
Name Type Use
FillColor Property An OLE_COLOR value that represents the color
used to fill the shape.
LineColor Property An OLE_COLOR value that represents the color
used for the shape's outline.
LineWidth Property A short value that represents the line width
in pixels of the shape's outline.
BaseMethod1 Method A test method that, when invoked, fires the
BaseEvent1 event. Takes a single parameter of
type long.
BaseMethod2 Method A test method that, when invoked, fires the
BaseEvent2 event. Takes a single parameter of
type BSTR.
BaseEvent1 Event A test event. Returns a long.
BaseEvent2 Event A test event. Returns a BSTR.
Name Type Use
CircleShape Property A boolean value. If TRUE, the control draws
itself as a circle. If FALSE, it draws itself
as an ellipse.
CircleOffset Property A short value that represents the number of
pixels offset from the center of the bounding
rectangle where the control will draw itself.
CircleMethod1 Method A test method that, when invoked, fires the
CircleEvent1 event. Takes a single parameter
of type long.
CircleMethod2 Method A test method that, when invoked, fires the
CircleEvent2 event. Takes a single parameter
of type long.
CircleEvent1 Event A test event. Returns a long.
CircleEvent2 Event A test event. Returns a long.
Name Type Use
RoundedCorners Property A boolean value. If TRUE, the control draws
itself with rounded corners. If FALSE, it
draws itself with square corners.
RectangleInset Property A short value that represents the number of
pixels inside the control's bounding
rectangle where the control will inset itself.
RectMethod1 Method A test method that, when invoked, fires the
RectEvent1 event. Takes no parameters.
RectMethod2 Method A test method that, when invoked, fires the
RectEvent2 event. Takes no parameters.
RectEvent1 Event A test event. Returns void.
RectEvent2 Event A test event. Returns void.
Modifying the Base Control ClassSeveral changes need be made to the code generated by ControlWizard to allow a derived control to cleanly inherit the functionality provided by a base control. The following changes were made to the files generated by ClassWizard:
Manually Updating the Project's .Odl FileClassWizard does not provide any support for developing OLE controls that are derived from another control. As a result, the property, method, and event dispatch ID's used by a derived control class need to be manually updated. MFC Technical Note #39 discusses the dispatch ID numbering scheme used by MFC. Basically, MFC divides a 32-bit dispatch ID (DISPID) into two parts. The LOWORD of the DISPID contains the distance from the top of the dispatch map (1 relative). The HIWORD contains the distance of the dispatch map from the most derived class (0 relative).The CCircleCtrl and CRectangleControl classes use this technique with their DISPIDs. For example, the CCircleCtrl derived control inherits the FillColor, LineColor, and LineWidth properties from CBaseShapeCtrl. The DISPIDs for these properties need to be manually added to the CCircleCtrl properties section of the project's .odl file and the HIWORD of the DISPIDs needs to be adjusted: The DISPIDs for events are handled differently from those for properties and methods. Event DISPIDs do not have their HIWORD portion adjusted. Instead, they are sequential. For example, CCircleCtrl inherits the BaseEvent1 and BaseEvent2 events from the base control. The DISPIDs for the base events need to be manually added to the CCircleCtrl events section of the project's .odl file. Also, the DISPIDs assigned by ClassWizard for the events provided by CCircleCtrl (CircleEvent1 and CircleEvent2) need to have their DISPIDs manually updated: ClassWizard also maintains DISPID values for properties, methods, and events, in an enum member of the COleControl derived class. Again, because ClassWizard does not support deriving an OLE control from another control, it may generate conflicting values for the different DISPIDs. If this happens, manually edit the values so that they are correct, and match the values as specified in the .odl file. Following is the portion of the CCircleCtrl class that shows the DISPIDs used for both the base class and derived class properties, events, and methods: Shapes.odl - Shows the modified DISPIDs used by the derived control classes. Basectl.cpp - Provides the implementation of the base CBaseShapeCtrl class. Circctl.cpp - Provides the implementation of the derived CCircleCtrl class. Rectctl.cpp - Provides the implementation of the derived CRectangleCtrl class. REFERENCES MFC Technical Note #39. APPLIES TO
| Article Translations
|

Back to the top
