[ Pobierz całość w formacie PDF ]
.For examples of the techniques used in subclassing Windows controls, see thecomponents in the StdCtls unit that represent standard Windows controls, such asTEdit.For CLX examples, see QStdCtls.40-4 Devel oper  s Gui de Wh a t g o e s i n t o a c o mp o n e n t ?Creating nonvisual componentsNonvisual components are used as interfaces for elements like databases (TDataSet orTSQLConnection) and system clocks (TTimer), and as placeholders for dialog boxes(TCommonDialog (VCL) or TDialog (CLX) and its descendants).Most of thecomponents you write are likely to be visual controls.Nonvisual components can bederived directly from TComponent, the abstract base class for all components.What goes into a component?To make your components reliable parts of the Delphi environment, you need tofollow certain conventions in their design.This section discusses the following topics:" Removing dependencies" Properties, methods, and events" Graphics encapsulation" RegistrationRemoving dependenciesOne quality that makes components usable is the absence of restrictions on what theycan do at any point in their code.By their nature, components are incorporated intoapplications in varying combinations, orders, and contexts.You should designcomponents that function in any situation, without preconditions.An excellent example of removing dependencies is the Handle property ofTWinControl.If you have written Windows applications before, you know that one ofthe most difficult and error-prone aspects of getting a program running is makingsure that you do not try to access a window or control until you have created it bycalling the CreateWindow API function.Delphi windowed controls relieve users fromthis concern by ensuring that a valid window handle is always available whenneeded.By using a property to represent the window handle, the control can checkwhether the window has been created; if the handle is not valid, the control creates awindow and returns the handle.Thus, whenever an application s code accesses theHandle property, it is assured of getting a valid handle.By removing background tasks like creating the window, Delphi components allowdevelopers to focus on what they really want to do.Before passing a window handleto an API function, there is no need to verify that the handle exists or to create thewindow.The application developer can assume that things will work, instead ofconstantly checking for things that might go wrong.Although it can take time to create components that are free of dependencies, it isgenerally time well spent.It not only spares application developers from repetitionand drudgery, but it reduces your documentation and support burdens.Ov er v i ew of c omponent cr eat i on 40-5 Wh a t g o e s i n t o a c o mp o n e n t ?Properties, methods, and eventsAside from the visible image manipulated in the Form designer, the most obviousattributes of a component are its properties, events, and methods.Each of these has achapter devoted to it in this book, but the discussion that follows explains some ofthe motivation for their use.PropertiesProperties give the application developer the illusion of setting or reading the valueof a variable, while allowing the component writer to hide the underlying datastructure or to implement special processing when the value is accessed.There are several advantages to using properties:" Properties are available at design time.The application developer can set orchange initial values of properties without having to write code." Properties can check values or formats as the application developer assigns them.Validating input at design time prevents errors." The component can construct appropriate values on demand.Perhaps the mostcommon type of error programmers make is to reference a variable that has notbeen initialized.By representing data with a property, you can ensure that a valueis always available on demand." Properties allow you to hide data under a simple, consistent interface.You canalter the way information is structured in a property without making the changevisible to application developers.Chapter 42,  Creating properties, explains how to add properties to yourcomponents.EventsAn event is a special property that invokes code in response to input or other activityat runtime.Events give the application developer a way to attach specific blocks ofcode to specific runtime occurrences, such as mouse actions and keystrokes.The codethat executes when an event occurs is called an event handler.Events allow application developers to specify responses to different kinds of inputwithout defining new components.Chapter 43,  Creating events, explains how to implement standard events and howto define new ones.MethodsClass methods are procedures and functions that operate on a class rather than onspecific instances of the class.For example, every component s constructor method(Create) is a class method.Component methods are procedures and functions thatoperate on the component instances themselves.Application developers usemethods to direct a component to perform a specific action or return a value notcontained by any property.40-6 Devel oper  s Gui de Wh a t g o e s i n t o a c o mp o n e n t ?Because they require execution of code, methods can be called only at runtime.Methods are useful for several reasons:" Methods encapsulate the functionality of a component in the same object wherethe data resides." Methods can hide complicated procedures under a simple, consistent interface [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • funlifepok.htw.pl
  •