📄 ch23.htm
字号:
{
Width = 25;
Height = 25;
Color = clBlue;
FHierarchy = new TListHierarchy();
}
void __fastcall TCustomPallet::Paint(void)
{
Canvas->Brush->Color = Color;
Canvas->Rectangle(0, 0, ClientWidth, ClientHeight);
}
__fastcall TDataPallet::TDataPallet(TComponent *AOwner)
:
TCustomPallet(AOwner)
{
OnDragOver = PalletDragOver;
OnDragDrop = PalletDragDrop;
}
void __fastcall TDataPallet::PalletDragOver(TObject *Sender,
TObject *Source, int X, int Y, TDragState State, bool &Accept)
{
Accept =
dynamic_cast<TWidget *>(Source);
}
void __fastcall TDataPallet::PalletDragDrop(TObject *Sender,
TObject *Source, int X, int Y)
{
if (WidgetsTable == NULL)
ShowMessage("No table assigned to the WidgetsTable property");
else
{
if (dynamic_cast<TWidget *>(Source))
{
TWidget *W = (TWidget *)(Source);
AnsiString S = "Enter number of " + W->Name;
AnsiString NumWidgets;
if (InputQuery("Widget Number
Dialog", S, NumWidgets))
{
EnterWidgets(NumWidgets.ToInt(), W);
}
}
}
}
void TDataPallet::EnterWidgets(int Total, TWidget* W)
{
int i;
for (i = 0; i < Total; i++)
{
FWidgetsTable->Insert();
FWidgetsTable->FieldByName("Name")->AsString = W->Name;
FWidgetsTable->FieldByName("Created")->AsString = W->TimeCreated;
FWidgetsTable->FieldByName("Description")->AsString = W->Description;
FWidgetsTable->FieldByName("Cost")->AsCurrency = W->Cost;
FWidgetsTable->FieldByName("PalletNumber")->AsInteger =
PalletNumber;
FWidgetsTable->Post();
}
}
void __fastcall TDataPallet::QueryPallet(TQuery *Query)
{
AnsiString S = "Select * from Widgets where PalletNumber = " +
AnsiString(PalletNumber);
Query->SQL->Clear();
Query->SQL->Add(S);
Query->Open();
}
float __fastcall TDataPallet::QueryPalletSum(TQuery *Query)
{
AnsiString S = "Select Sum(Cost) from Widgets where PalletNumber = " +
AnsiString(PalletNumber);
Query->SQL->Clear();
Query->SQL->Add(S);
Query->Open();
return Query->Fields[0]->AsFloat;
}
namespace Widgets
{
void __fastcall RegisterShort()
{
TComponentClass classes[4] = {__classid(TWidget),
__classid(TPentium), __classid(TPentiumPro),
__classid(TPallet) };
RegisterClasses(classes, 3);
}
void __fastcall Register()
{
TComponentClass classes[5] = {__classid(TWidget),
__classid(TPentium),
__classid(TPentiumPro),
__classid(TPallet), __classid(TDataPallet) };
RegisterComponents("Unleash", classes, 4);
}
}
</FONT></PRE>
<P><A NAME="Heading18"></A><FONT COLOR="#000077"><B>Listing 23.6. The final take
on the
header for the MyObject unit.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////
// MyObject.h
// Learning how to use objects
// Copyright (c) 1997 by Charlie Calvert
//
#ifndef MyObjectH
#define MyObjectH
#include <conio.h>
#include <vcl\stdctrls.hpp>
class TMyObject :public TObject
{
protected:
virtual void PrintString(AnsiString S);
public:
TMyObject() : TObject() {}
void ShowHierarchy(TObject *AnObject);
};
class
TListHierarchy: public TMyObject
{
private:
TStringList *FList;
protected:
virtual void PrintString(AnsiString S)
{ FList->Add(S); }
public:
TListHierarchy() { FList = new TStringList(); }
__fastcall virtual ~TListHierarchy() {
delete FList; }
TStringList *GetHierarchy(TObject *AnObject)
{ FList->Clear(); ShowHierarchy(AnObject); return FList; }
};
class __declspec(delphiclass) TVCLHierarchy;
class THierarchy: public TMyObject
{
friend TVCLHierarchy;
int FTextColor;
int FBackColor;
protected:
virtual __fastcall void SetTextColor(int Color)
{ FTextColor = Color; textcolor(FTextColor); }
virtual __fastcall void SetBackColor(int Color)
{ FBackColor = Color;
textbackground(FBackColor); }
public:
THierarchy() : TMyObject() {}
virtual void PrintString(AnsiString S);
virtual void ClrScr();
__property int TextColor={read=FTextColor,write=SetTextColor};
__property int
BackColor={read=FBackColor,write=SetBackColor};
};
class TVCLHierarchy : public THierarchy
{
TMemo *FMemo;
protected:
virtual __fastcall void SetTextColor(int Color)
{ FTextColor = Color; FMemo->Font->Color = TColor(FTextColor);
}
virtual __fastcall void SetBackColor(int Color);
public:
TVCLHierarchy(TMemo *AMemo): THierarchy() { FMemo = AMemo; }
virtual void PrintString(AnsiString S)
{ FMemo->Lines->Add(S); }
virtual void ClrScr() {
FMemo->Clear(); }
};
#endif
</FONT></PRE>
<P><A NAME="Heading19"></A><FONT COLOR="#000077"><B>Listing 23.7. The final take
on the main source file for the MyObject unit.</B></FONT></P>
<PRE><FONT
COLOR="#0066FF">///////////////////////////////////////
// MyObject.cpp
// Learning how to use objects
// Copyright (c) 1997 by Charlie Calvert
//
#include <vcl\vcl.h>
#include <conio.h>
#pragma hdrstop
#include
"myobject.h"
void TMyObject::PrintString(AnsiString S)
{
printf("%s\n", S.c_str());
}
void TMyObject::ShowHierarchy(TObject *AnObject)
{
TClass AClass;
AnsiString AClassName =
AnsiString(AnObject->ClassName()).c_str();
PrintString(AClassName);
AClass = AnObject->ClassParent();
while (True)
{
AClassName = AnsiString(AClass->ClassName());
PrintString(AClassName);
if (AClassName ==
"TObject")
break;
AClass = AClass->ClassParent();
}
}
void THierarchy::PrintString(AnsiString S)
{
char Temp[250];
sprintf(Temp, "%s\n\r", S.c_str());
cputs(Temp);
}
void THierarchy::ClrScr()
{
clrscr();
}
void __fastcall TVCLHierarchy::SetBackColor(int Color)
{
FBackColor = Color;
FMemo->Color = TColor(FBackColor);
}
</FONT></PRE>
<P><A NAME="Heading20"></A><FONT COLOR="#000077"><B>Listing 23.8. The header file
for
the programs data module.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////
// DMod1.h
// Warehouse example: learning about objects
// Copyright (c) 1997 by Charlie Calvert
//
#ifndef DMod1H
#define DMod1H
#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include <vcl\DBTables.hpp>
#include <vcl\DB.hpp>
class TDMod : public TDataModule
{
__published:
TTable *WidgetsTable;
TDataSource *WidgetsTableSource;
TQuery *WidgetsQuery;
TDataSource *WidgetsQuerySource;
void __fastcall DModCreate(TObject *Sender);
private:
public:
virtual __fastcall TDMod(TComponent* Owner);
void
__fastcall SumByProduct();
void __fastcall ReportByPallet();
};
extern TDMod *DMod;
#endif
</FONT></PRE>
<P><A NAME="Heading21"></A><FONT COLOR="#000077"><B>Listing 23.9. The main source
file for the data module.</B></FONT></P>
<PRE><FONT
COLOR="#0066FF">///////////////////////////////////////
// DMod1.cpp
// Warehouse example: learning about objects
// Copyright (c) 1997 by Charlie Calvert
//
#include <vcl\vcl.h>
#pragma hdrstop
#include "DMod1.h"
#pragma
resource "*.dfm"
TDMod *DMod;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -