|
TMacroRecorder’s Dual Life
A component designer has a more complex
task than the application programmer. Components live dual lives--they have split
personalities. They live on the component palette as design components during application
design, then they live within the application as object factories during run-time.
As a design component, they can be dropped on forms, interact with the form designer
and property inspector, and can contain a variety of rich, design-time functionality.
At run-time their mild-mannered behavior changes, and they silently perform services
on behalf of their clients. Many of these silently operating components remain completely
unknown to end users.
The TMacroRecorder will live such
a dual life. During design time, it will not need to know which controls are its
clients. During run-time, however, it will need to construct a list of its clients.
The ComponentState property helps tell the difference. Component designers use the
ComponentState property to ascertain whether a component is living its design life
or its run-time life. TMacroRecorder employs this technique in its constructor.
TMacroRecorder’s constructor first
initializes FMacro, FRecording and FEventList. If the component has simply been dropped
on a form during design, these are all the services that are required. When the form
is running, additional functionality is needed. If the component is not being designed
and its owner is a form, then the component must override the form’s OnCreate event
handler and install a new OnCreate handler.
The new handler, DoCreate, scans
all the components on the form, looking for potential client controls. In our case
(pun intended), TMacroRecorder is seeking buttons and checkboxes only. When it finds
one, it adds the control’s window handle and original event handler to the event
list, overrides the event handler with its RecordEvent method, and indexes the new
entry with the control’s name. The last step is to call the form’s original OnCreate
handler if one was assigned by the application programmer or another component.
|