📄 ch23.htm
字号:
{
Close();
}
void __fastcall
TForm1::DataPallet10MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if (Shift.Contains(ssLeft))
{
if (dynamic_cast<TDataPallet *>(Sender))
{
TDataPallet *D = (TDataPallet *)(Sender);
D->QueryPallet(DMod->WidgetsQuery);
QueryForm->Panel1->Caption =
"List of Items on Pallet: " + AnsiString(D->PalletNumber);
QueryForm->ShowModal();
}
}
}
void __fastcall
TForm1::ItemsOnPallet1Click(TObject *Sender)
{
TDataPallet *D = (TDataPallet *)PopupMenu1->PopupComponent;
DataPallet10MouseDown(D, TMouseButton(), TShiftState() << ssLeft, 0, 0);
}
void __fastcall
TForm1::ValueofItems1Click(TObject *Sender)
{
TDataPallet *D = (TDataPallet *)PopupMenu1->PopupComponent;
D->QueryPalletSum(DMod->WidgetsQuery);
QueryForm->Panel1->Caption =
"Value of Items on Pallet: " +
AnsiString(D->PalletNumber);
QueryForm->ShowModal();
}
void __fastcall TForm1::GetHierarchy1Click(TObject *Sender)
{
TDataPallet *D = (TDataPallet *)PopupMenu1->PopupComponent;
HierarchyDlg->ListBox1->Items =
D->GetHierarchy();
HierarchyDlg->ShowModal();
}
void __fastcall TForm1::ListClick(TObject *Sender)
{
DMod->SumByProduct();
QueryForm->Panel1->Caption = "Sum By Product of Entire Warehouse";
QueryForm->ShowModal();
}
void __fastcall TForm1::ListbyPallet1Click(TObject *Sender)
{
DMod->ReportByPallet();
QueryForm->Panel1->Caption = "Report By Pallet";
QueryForm->ShowModal();
}
</FONT></PRE>
<P><A
NAME="Heading16"></A><FONT COLOR="#000077"><B>Listing 23.4. The header file
for the Widgets unit.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////
// Widgets.h
// Learning how to use objects
// Copyright (c) 1997 by
Charlie Calvert
//
#ifndef WidgetsH
#define WidgetsH
#include <vcl\dbtables.hpp>
#include "myobject.h"
TCustomControl *ReadWidgetFromStream(AnsiString StreamName);
void WriteWidgetToStream(AnsiString StreamName,
TCustomControl *Widget);
class __declspec(delphiclass) TWidget;
namespace Widgets
{
void __fastcall RegisterShort();
void __fastcall Register();
}
class TWidget: public TCustomControl
{
private:
TListHierarchy *Hierarchy;
Currency FCost;
TDateTime FTimeCreated;
AnsiString FDescription;
void __fastcall SetTimeCreated(AnsiString S);
AnsiString __fastcall GetTimeCreated();
protected:
virtual void __fastcall Paint(void);
public:
__fastcall virtual
TWidget(TComponent *AOwner): TCustomControl(AOwner)
{ Hierarchy = new TListHierarchy(); Width = 25; Height = 25; }
__fastcall virtual TWidget(TComponent *AOwner, int ACol, int ARow);
__fastcall virtual ~TWidget() { delete Hierarchy; }
TStringList *GetHierarchy() { return Hierarchy->GetHierarchy(this); }
void __fastcall WidgetMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
__published:
__property Currency
Cost={read=FCost, write=FCost};
__property AnsiString TimeCreated={read=GetTimeCreated, write=SetTimeCreated};
__property AnsiString Description={read=FDescription, write=FDescription};
__property OnDragDrop;
__property OnMouseDown;
};
class TChip: public TWidget
{
public:
virtual __fastcall TChip(TComponent *AOwner): TWidget(AOwner) {}
virtual __fastcall TChip(TComponent *AOwner, int ACol, int ARow)
: TWidget(AOwner, ACol, ARow) {}
};
class TPentium: public TChip
{
protected:
virtual void __fastcall Paint(void);
public:
virtual __fastcall TPentium(TComponent *AOwner): TChip(AOwner) {}
virtual __fastcall TPentium(TComponent *AOwner, int ACol, int ARow)
: TChip(AOwner, ACol, ARow) {}
};
class
TPentiumPro: public TChip
{
protected:
virtual void __fastcall Paint(void);
public:
virtual __fastcall TPentiumPro(TComponent *AOwner): TChip(AOwner) {}
virtual __fastcall TPentiumPro(TComponent *AOwner, int ACol, int ARow)
:
TChip(AOwner, ACol, ARow) {}
};
class TCustomPallet: public TCustomControl
{
private:
int FPalletNumber;
TListHierarchy *FHierarchy;
protected:
virtual void __fastcall Paint(void);
public:
virtual __fastcall
TCustomPallet(TComponent *AOwner);
virtual __fastcall ~TCustomPallet(void)
{ delete FHierarchy; }
TStringList *GetHierarchy() { return FHierarchy->GetHierarchy(this); }
__property int PalletNumber={read=FPalletNumber,
write=FPalletNumber};
};
class TPallet: public TCustomPallet
{
public:
virtual __fastcall TPallet(TComponent *AOwner): TCustomPallet(AOwner)
{ Width = 25; Height = 25; }
__published:
__property PalletNumber;
__property OnDragDrop;
__property OnDragOver;
__property Color;
};
class TDataPallet: public TCustomPallet
{
private:
TTable *FWidgetsTable;
TQuery *FWidgetsQuery;
protected:
void __fastcall PalletDragDrop(TObject *Sender,
TObject *Source, int X, int
Y);
void __fastcall PalletDragOver(TObject *Sender, TObject *Source,
int X, int Y, TDragState State, bool &Accept);
void virtual EnterWidgets(int Total, TWidget *W);
public:
virtual __fastcall TDataPallet(TComponent *AOwner);
void __fastcall QueryPallet(TQuery *Query);
float __fastcall QueryPalletSum(TQuery *Query);
__published:
__property TTable *WidgetsTable={read=FWidgetsTable, write=FWidgetsTable};
__property TQuery *WidgetsQuery={read=FWidgetsQuery,
write=FWidgetsQuery};
__property PalletNumber;
__property Hint;
__property Color;
__property TabOrder;
__property OnMouseDown;
__property PopupMenu;
};
#endif
</FONT></PRE>
<P><A NAME="Heading17"></A><FONT
COLOR="#000077"><B>Listing 23.5. The main source
file for the Widgets unit.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////
// Widgets.cpp
// Learning how to use objects
// Copyright (c) 1997 by Charlie Calvert
//
#include <vcl\vcl.h>
#include <conio.h>
#pragma hdrstop
#include "widgets.h"
#pragma link "MyObject.obj"
void WriteWidgetToStream(AnsiString StreamName, TCustomControl *Widget)
{
TFileStream *Stream = new
TFileStream(StreamName, fmCreate | fmOpenWrite);
Stream->WriteComponent(Widget);
delete Stream;
}
TCustomControl *ReadWidgetFromStream(AnsiString StreamName)
{
Widgets::RegisterShort();
TFileStream *Stream = new
TFileStream(StreamName, fmOpenRead);
TWidget *Widget = (TWidget *)Stream->ReadComponent(NULL);
delete Stream;
return Widget;
}
__fastcall TWidget::TWidget(TComponent *AOwner, int ACol, int ARow)
: TCustomControl(AOwner)
{
Hierarchy = new TListHierarchy();
Left = ACol;
Top = ARow;
Width = 25;
Height = 25;
OnMouseDown = WidgetMouseDown;
}
AnsiString __fastcall TWidget::GetTimeCreated()
{
return FTimeCreated.DateTimeString();
}
void __fastcall
TWidget::SetTimeCreated(AnsiString S)
{
FTimeCreated = TDateTime(S);
}
void __fastcall TWidget::Paint()
{
Canvas->Brush->Color = clBlue;
Canvas->Rectangle(0, 0, ClientWidth, ClientHeight);
Canvas->Brush->Color = clYellow;
Canvas->Ellipse(0, 0, ClientWidth, ClientHeight);
}
void __fastcall TWidget::WidgetMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int
Y)
{
ShowMessage(Format("%m", OPENARRAY(TVarRec, (FCost))));
}
void __fastcall TPentium::Paint()
{
Canvas->Brush->Color = clPurple;
Canvas->Rectangle(0, 0, ClientWidth, ClientHeight);
Canvas->Brush->Color =
clRed;
Canvas->Ellipse(0, 0, ClientWidth, ClientHeight);
}
void __fastcall TPentiumPro::Paint()
{
Canvas->Brush->Color = clGreen;
Canvas->Rectangle(0, 0, ClientWidth, ClientHeight);
Canvas->Brush->Color = clBlue;
Canvas->Ellipse(0, 0, ClientWidth, ClientHeight);
}
__fastcall TCustomPallet::TCustomPallet(TComponent *AOwner)
: TCustomControl(AOwner)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -