[ Pobierz całość w formacie PDF ] .htm (65 of 79) [10/10/2000 1:14:54 AM]Ch 22 -- Creating Descendants of Existing Components///////////////////////////////////////// Clock2.h// Clock Component// Copyright (c) 1997 by Charlie Calvert////------------------------------------#ifndef Clock2H#define Clock2H#include#include#include#include#include#includeclass TClockAttribute2: public TPersistent{private:TColor FColor;TShapeType FShape;TComponent *FOwner;void __fastcall SetColor(TColor AColor);void __fastcall SetShape(TShapeType AShape);public:virtual __fastcall TClockAttribute2(TComponent *AOwner): TPersistent() { FOwner = AOwner; }__published:__property TColor Color={read=FColor, write=SetColor};__property TShapeType Shape={read=FShape, write=SetShape};};file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch22.htm (66 of 79) [10/10/2000 1:14:54 AM]Ch 22 -- Creating Descendants of Existing Components////////////////////////////////////////// TMyClock ////////////////////////////////////////////////////////////////////class TMyClock2: public TCustomControl{private:int FTimer;Boolean FRunning;TClockAttribute2 *FClockAttributes;void __fastcall SetRunning(Boolean ARun);protected:virtual void __fastcall Paint(void);void __fastcall WMTimer(TMessage &Message);void __fastcall WMDestroy(TMessage &Message);public:virtual __fastcall TMyClock2(TComponent* Owner);__published:__property Boolean Running={read=FRunning, write=SetRunning};__property Align;__property TClockAttribute2 *ClockAttributes={read=FClockAttributes, write=FClockAttributes };__property Font;BEGIN_MESSAGE_MAPMESSAGE_HANDLER(WM_TIMER, TMessage, WMTimer);MESSAGE_HANDLER(WM_DESTROY, TMessage, WMDestroy);END_MESSAGE_MAP(TCustomControl);};////////////////////////////////////////file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch22.htm (67 of 79) [10/10/2000 1:14:54 AM]Ch 22 -- Creating Descendants of Existing Components// TColorClock /////////////////////////////////////////////////////////////////class TColorClock2: public TMyClock2{private:TColor FFaceColor;void __fastcall SetColor(TColor Color);protected:void virtual __fastcall Paint(void);public:virtual __fastcall TColorClock2(TComponent *Owner):TMyClock2(Owner) { FFaceColor = clGreen; }__published:__property TColor FaceColor={read=FFaceColor, write=SetColor, nodefault};};////////////////////////////////////////// TClockEditor ////////////////////////////////////////////////////////////////class TClockEditor2: public TComponentEditor{protected:virtual __fastcall void Edit(void);public:virtual __fastcall TClockEditor2(TComponent *AOwner, TFormDesigner *Designer): TComponentEditor(AOwner, Designer) {}};////////////////////////////////////////file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch22.htm (68 of 79) [10/10/2000 1:14:54 AM]Ch 22 -- Creating Descendants of Existing Components// TColorNameProperty /////////////////////////////////////////////////////////class TClockAttributeProperty: public TClassProperty{public:TClockAttributeProperty(): TClassProperty() {}TPropertyAttributes __fastcall GetAttributes(void);void virtual __fastcall Edit(void);};#endifListing 22.19.The main source file for the new version of the clock components.///////////////////////////////////////// Clock2.cpp// Clock Component with Component Editor// Copyright (c) 1997 by Charlie Calvert//#include#pragma hdrstop#include "Clock2.h"#include "ClockEditor.h"#include "ClockAttributes.h"#pragma link "ClockEditor.obj"#pragma link "ClockAttributes.obj"///////////////////////////////////////// ValidControlCheck///////////////////////////////////////static inline TMyClock2 *ValidCtrCheck(){file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch22.htm (69 of 79) [10/10/2000 1:14:54 AM]Ch 22 -- Creating Descendants of Existing Componentsreturn new TMyClock2(NULL);}///////////////////////////////////////// TClockAttributes///////////////////////////////////////void __fastcall TClockAttribute2::SetColor(TColor AColor){FColor = AColor;((TControl *)(FOwner))->Invalidate();};void __fastcall TClockAttribute2::SetShape(TShapeType AShape){FShape = AShape;((TControl *)(FOwner))->Invalidate();};///////////////////////////////////////// Constructor///////////////////////////////////////__fastcall TMyClock2::TMyClock2(TComponent* Owner): TCustomControl(Owner){Width = 100;Height = 100;FTimer = 1;FClockAttributes = new TClockAttribute2(this);FClockAttributes->Color = clBtnFace;FClockAttributes->Shape = stEllipse;file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch22.htm (70 of 79) [10/10/2000 1:14:54 AM]Ch 22 -- Creating Descendants of Existing Components}///////////////////////////////////////// SetRunning///////////////////////////////////////void __fastcall TMyClock2::SetRunning(Boolean Run){if (Run){SetTimer(Handle, FTimer, 50, NULL);FRunning = True;}else{KillTimer(Handle, FTimer);FRunning = False;}}///////////////////////////////////////// Paint///////////////////////////////////////void __fastcall TMyClock2::Paint(void){Color = ClockAttributes->Color;switch (ClockAttributes->Shape){int X;case stEllipse: Canvas->Ellipse(0, 0, Width, Height); break;file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch22.htm (71 of 79) [10/10/2000 1:14:54 AM]Ch 22 -- Creating Descendants of Existing Componentscase stRectangle: Canvas->Rectangle(0, 0, Width, Height); break;case stRoundRect:Canvas->RoundRect(0, 0, Width, Height, Width - 100, Height);break;case stSquare:{if (Width Rectangle(Width - X, 0, Width + X, Width);}else{X = Height / 2;Canvas->Rectangle((Width / 2) - X, 0, (Width / 2) + X, Height);}break;}case stCircle:{if (Width Ellipse(Width - X, 0, Width + X, Width);}else{X = Height / 2;file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch22.htm (72 of 79) [10/10/2000 1:14:54 AM]Ch 22 -- Creating Descendants of Existing ComponentsCanvas->Ellipse((Width / 2) - X, 0, (Width / 2) + X, Height);}break;}}}///////////////////////////////////////// WM_TIMER///////////////////////////////////////void __fastcall TMyClock2::WMTimer(TMessage &Message){AnsiString S = TimeToStr(Time());Canvas->Font = Font;Canvas->TextOut(Width / 2 - Canvas->TextWidth(S) / 2,Height / 2 - Canvas->TextHeight(S) / 2, S);}///////////////////////////////////////// WM_DESTROY///////////////////////////////////////void __fastcall TMyClock2::WMDestroy(TMessage &Message){KillTimer(Handle, FTimer);FTimer = 0;TCustomControl::Dispatch(&Message);}//------------------------------------//-- TColorClock2 --------------------file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch22
[ Pobierz całość w formacie PDF ] zanotowane.pldoc.pisz.plpdf.pisz.plmikr.xlx.pl
|