[ Pobierz całość w formacie PDF ]
.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, C++Builder componentsallow developers to focus on what they really want to do.Before passing a windowhandle to an API function, there is no need to verify that the handle exists or to createthe window.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.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.Ov er v i ew of component c r eat i on 39-5 Wh a t g o e s i n t o a c o mp o n e n t ?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 41,  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 42,  Creating events, explains how to implement standard events and howto define new ones.MethodsClass methods are functions that operate on a class rather than on specific instancesof the class.For example, every component s constructor method is a class method.Component methods are functions that operate on the component instancesthemselves.Application developers use methods to direct a component to perform aspecific action or return a value not contained by any property.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.Anapplication developer can call a component s AlignControls method without39-6 Dev el oper  s Gui de Cr e a t i n g a n e w c o mp o n e n tknowing how the method works or how it differs from the AlignControls methodin another component." Methods allow updating of several properties with a single call.Chapter 43,  Creating methods, explains how to add methods to your components.Graphics encapsulationC++Builder simplifies Windows graphics by encapsulating various graphic tools intoa canvas.The canvas represents the drawing surface of a window or control andcontains other classes, such as a pen, a brush, and a font.A canvas is much like aWindows device context, but it takes care of all the bookkeeping for you.If you have written a graphical Windows application, you are familiar with therequirements imposed by Windows graphics device interface (GDI).For example,GDI limits the number of device contexts available and requires that you restoregraphic objects to their initial state before destroying them.With C++Builder, you do not have to worry about these things.To draw on a form orother component, you access the component s Canvas property.If you want tocustomize a pen or brush, you set its color or style.When you finish, C++Builderdisposes of the resources.C++Builder also caches resources to avoid recreating themif your application frequently uses the same kinds of resource.You still have full access to the Windows GDI, but you will often find that your codeis simpler and runs faster if you use the canvas built into C++Builder components.Graphics features are detailed in Chapter 44,  Using graphics in components.RegistrationBefore you can install your components in the C++Builder IDE, you have to registerthem.Registration tells C++Builder where you want your component to appear onthe Component palette.You can also customize the way C++Builder stores yourcomponents in the form file.For information on registering a component, seeChapter 46,  Making components available at design time.Creating a new componentYou can create a new component two ways:" Using the Component wizard" Creating a component manuallyYou can use either of these methods to create a minimally functional componentready to install on the Component palette.After installing, you can add your newcomponent to a form and test it at both design time and runtime.You can then addmore features to the component, update the Component palette, and continuetesting.Ov er v i ew of component c r eat i on 39-7 Cr e a t i n g a n e w c o mp o n e n tThere are several basic steps that you perform whenever you create a newcomponent.These steps are described below; other examples in this documentassume that you know how to perform them.1 Creating a unit for the new component.2 Deriving your component from an existing component type.3 Adding properties, methods, and events.4 Registering your component with C++Builder.5 Creating a Help file for your component and its properties, methods, and events.6 Creating a package (a special dynamic-link library) so that you can install yourcomponent in the C++Builder IDE.When you finish, the complete component includes these files:" A package (.BPL) or package collection (.PCE) file" A library (.LIB) for the package" A Borland import library (.BPI) file for the package" A compiled unit (.OBJ) file" A compiled resource (.RES) file for the palette map" A Help (.HLP) fileCreating a help file to instruct component users on how to use the component isoptional.Including a help file is mandatory only if one is created.The chapters in the rest of Part IV explain all the aspects of building components andprovide several complete examples of writing different kinds of components [ Pobierz całość w formacie PDF ]

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