Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

(Ebook - Pdf) Kick Ass Delphi Programming

.pdf
Скачиваний:
284
Добавлен:
17.08.2013
Размер:
5.02 Mб
Скачать

the splash screen is used. } TSplashForm = class(TForm)

CloseBtn: TButton; Image: TImage; DelayTimer: TTimer;

procedure CloseBtnClick(Sender: TObject); procedure Button1Click(Sender: TObject); procedure DelayTimerTimer(Sender: TObject);

private

{Private declarations } public

{Private declarations }

end;

{TSplashDialog is the wrapper that surrounds the TSplashForm.

The form is owned by the TSplashDialog, so it can "automatically" be created, set up, executed and destroyed at any time. The TSplashDialog makes available only those properties used for the splash dialog, then passes them on to the TSplashForm when it

is created. }

TSplashDialog = class(TComponent) private

FAlign : TImageAlign;

FAutoSize : Boolean; FButtonCaption : String; FCaption : String; FDelay : Word; FHasButton : Boolean; FHasDelay : Boolean; FHeight : Word; FPicture : TPicture; FStretch : Boolean; FWidth : Word;

procedure SetCaption(Value : String); procedure SetDelay(Value : Word); procedure SetHasButton(Value : Boolean); procedure SetHasDelay(Value : Boolean); procedure SetHeight(Value : Word); procedure SetPicture(Value : TPicture); procedure SetWidth(Value : Word);

public

constructor Create(AOwner : TComponent); override; destructor Destroy;

function Execute : Boolean; virtual; published

property Align : TImageAlign read FAlign write FAlign; property AutoSize : Boolean read FAutoSize write FAutoSize; property ButtonCaption : String read FButtonCaption

write FButtonCaption; property Caption : String read FCaption write SetCaption; property Delay : Word read FDelay write SetDelay;

property HasButton : Boolean read FHasButton write SetHasButton; property HasDelay : Boolean read FHasDelay write SetHasDelay; property Height : Word read FHeight write SetHeight;

property Picture : TPicture read FPicture write SetPicture; property Stretch : Boolean read FStretch write FStretch;

property Width : Word read FWidth write SetWidth; end;

procedure Register;

implementation

{$R *.DFM}

var

SplashDlg: TSplashDialog;

procedure TSplashDialog.SetCaption(Value : String); begin

if Value <> FCaption then FCaption := Value;

end;

{ Set the value of FHasButton. If the user has specified he wants no button and no delay timer, raise an exception -- without both, the screen has no way to be cleared! }

procedure TSplashDialog.SetHasButton(Value : Boolean); begin

if not Value and not FHasDelay then raise

ESplashConflict.Create('Must have either a button or a delay!') else FHasButton := Value;

end;

{Set the value of FHasDelay, protecting against the anomaly cited above. }

procedure TSplashDialog.SetHasDelay(Value : Boolean); begin

if not Value and not FHasButton then raise

ESplashConflict.Create('Must have either a button or a delay!') else FHasDelay := Value;

end;

procedure TSplashDialog.SetHeight(Value : Word); begin

if (Value <> FHeight) and (Value > 10) then FHeight := Value;

end;

procedure TSplashDialog.SetWidth(Value : Word); begin

if (Value <> FWidth) and (Value > 20) then FWidth := Value;

end;

procedure TSplashDialog.SetDelay(Value : Word); begin

if (Value <> FDelay) and (Value > 0) then FDelay := Value;

end;

procedure TSplashDialog.SetPicture(Value : TPicture); begin

if Value <> nil then FPicture.Assign(Value); end;

constructor TSplashDialog.Create(AOwner : TComponent); begin

inherited Create(AOwner);

{ Set defaults }

FAlign := iaAllAboveButton; FAutoSize := False; FStretch := False; FButtonCaption := 'OK';

FCaption := copy(ClassName, 2, Length(ClassName) - 1); FDelay := 3500;

FHasButton := True;

FHasDelay := True; FHeight := 200; FWidth := 300;

FPicture := TPicture.Create;

{$ifdef Test } FPicture.LoadFromFile('splash.bmp'); FHasDelay := False;

{$endif }

end;

destructor TSplashDialog.Destroy; begin

FPicture.Free; inherited Destroy; end;

{The Execute method is the primary focus. It's the method called by the owner of the TSplashDialog when the splash screen is desired. Execute creates a SplashForm and modifies it according to the parameters given to the SplashDialog.

The SplashForm is destroyed when it has closed. } function TSplashDialog.Execute : Boolean;

var

SplashForm : TSplashForm; begin

try

SplashForm := TSplashForm.Create(Application); except

on E:Exception do begin

MessageBeep(MB_ICONERROR); Exit;

end;

end; { try }

with SplashForm do

begin

Position := poScreenCenter; Caption := FCaption;

Height := FHeight; Width := FWidth;

if FAlign = iaAllAboveButton then begin

if FHasButton then begin

Image.Align := alTop;

Image.Height := ClientHeight - CloseBtn.Height - 15; end

else Image.Align := alClient; end

else Image.Align := TAlign(Ord(FAlign)); Image.AutoSize := FAutoSize; Image.Stretch := FStretch;

if Image.Picture <> nil

then Image.Picture.Assign(FPicture);

if FHasButton then begin

CloseBtn.Caption := FButtonCaption;

CloseBtn.Left := (ClientWidth - CloseBtn.Width) div 2; CloseBtn.Top := ClientHeight - CloseBtn.Height - 10;

end

else CloseBtn.Visible := False;

if FHasDelay then begin

DelayTimer.Interval := FDelay; DelayTimer.Enabled := True;

end;

try ShowModal; finally Free;

end; { try } end; { with }

end;

procedure TSplashForm.CloseBtnClick(Sender: TObject); begin

Close;

end;

procedure Register; begin

RegisterComponents('Ace''s Stuff', [TSplashDialog]); end;

procedure TSplashForm.Button1Click(Sender: TObject); begin

Close;

end;

procedure TSplashForm.DelayTimerTimer(Sender: TObject); begin

Enabled := False; Close;

end;

end.

Products | Contact Us | About Us | Privacy | Ad Info | Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.

All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.

To access the contents, click the chapter and section titles.

Kick Ass Delphi Programming

Go!

Keyword

(Publisher: The Coriolis Group)

Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin

ISBN: 1576100448

Publication Date: 09/01/96

Search this book:

Go!

-----------

Several comments need to be made about this code. Here again, I used a conditional directive to give the component two modes. When in the test mode, it automatically loads a special test bitmap and disables the timer.

I added some code to prevent the condition where neither a button nor a timer was selected. (This would mean there would be no way to end the modal dialog!) I also declared an enumerated type (TImageAlign) to functionally extend the TAlign type by adding a value called iaAllAboveButton. This new value would indicate the user wanted to use the form’s entire client area, but only that portion above the button. Oh, yes—I also declared a special exception class as a catch-all for problems detected when the programmer was setting properties.

By far, the most interesting part of this project was selecting the TPicture and placing it in the TImage. After triggering several exceptions for system access violations, I finally found myself combing through the VCL source code, looking for instances of selecting and assigning bitmaps. Once the answers were found, I realized how easy the process had been made, thanks to the foresight of Delphi’s designers. When you declare a property of type TPicture, Delphi’s IDE already knows how to work with it. You create an instance of a TPicture, and the IDE will call up the Picture Editor. Once a bitmap is selected by the Picture Editor, it will automatically be stored in the stream when the form file is closed, which means the bitmap will be there the next time the file is opened.

True to its design goals, the TSplashDialog wrapper manages the critical properties of the form. When its Execute method is called, TSplashDialog creates an instance of the form, sets its properties accordingly, and then calls ShowModal to halt all other processing in the program. When execution resumes, the form is destroyed. The test splash screen is shown in Figure 13.5.

FIGURE 13.5 The splash screen making a splash at runtime.

End of entry, March 26.

Ace Gets an Answer

“Hello, Helen? Yeah, Baby. Just calling to let you know there’s nothing new. It’s a brick wall. I’ve gone over everything a hundred more times, and I’m right back where I started. No clues whatsoever. I feel absolutely helpless. Some detective I am!...”

“Ace, you know you’re still a good P.I.— one of the best. It just so happens you can’t do this one on your own,” Helen said.

“I suppose you’re right,” he admitted. “I could use a little help.”

“Why don’t you talk to The Author,” Helen suggested. “Remember how he helped you before?”

“Yeah, I suppose you’re right. Guess it’s worth a try. Thanks, Baby. Love you.”

“Love you, too,” she replied.

Ace hung up the phone and grabbed the phone directory. Running his finger down the page, he found the number he was after. He quickly dialed, and the line was as quickly answered.

“Hello, Ace,” said the voice.

“Uh, hello,” Ace replied. “I guess you already know why I’m calling.”

“You’re looking for some answers regarding the theft of your Casebook.”

“Yeah, I’m feeling totally helpless here. I decided to give you a call.”

“I haven’t heard from you in quite some time, Ace,” said the voice. “Not since The Case of the Duplicitous Demo. I gave you some help that night, didn’t I?”

“Oh, yes,” Ace replied. “You reminded me how special I was, and you gave me the courage to keep going. I don’t think I would have made it without your help.”

“I wonder why you never thanked me.”

“Uh, I suppose it was a combination of things,” Ace replied, stalling for time. “Once I had the problem solved, I guess I thought I didn’t need any help from anyone else.” He paused momentarily. “And I didn’t want to bug you. After all, I’m just one of your many characters.”

“One of my favorite characters. I’ve missed hearing from you. Sometimes it

makes me sad when you try to do everything on your own. Remember, you can call on me anytime, day or night. It doesn’t matter how big the problem is—or how small. But as to your question at the moment. You want to know who stole your Casebook.”

“Right. Helen thinks it was Melvin Bohacker and a female accomplice. At first I didn’t think so, but now I’m not so sure. Was it Bohacker who stole the Casebook?”

“There are three possible answers to that question: ‘Yes,’ ‘No,’ and the answer I have for you, ‘Not right now.’”

“But I’ve got to know who took my most valuable possession. I don’t know how I’m going to go on without it. And my reputation with Helen as a P.I. is on the line, as well.”

“Ace, in your case, life is definitely not like a box of chocolates. For you, it’s more like event-driven programming: You see, it’s not only important that an event occurs, it’s equally as important when it occurs. Life is a mystery. And in any mystery, the timing of events is critical. Besides, sometimes you learn more by discovering things than by just being given the answer.”

“So what should I do?” Ace asked plaintively.

“Go to the parking lot and thoroughly search the area near where your car is parked. You’ll find the key that will unlock this whole mystery.”

“Thanks,” Ace replied excitedly. “I won’t forget this.” He was barely able to get the phone back in the cradle before he was out the door.

Products | Contact Us | About Us | Privacy | Ad Info | Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.

All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.

To access the contents, click the chapter and section titles.

Kick Ass Delphi Programming

Go!

Keyword

(Publisher: The Coriolis Group)

Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin

ISBN: 1576100448

Publication Date: 09/01/96

Search this book:

Go!

-----------

Making Data Global to an Application

In the shabby little apartment, the Avenger stuffed down a whole Twinkie and continued to read Ace’s purloined Casebook.

Casebook No. 16, March 27: With Delphi 1.0, sharing tables between forms was always bothersome. Although there was an alternative to placing tables and datasources on every form that needed data from a table, it was a clumsy process that involved temporarily creating extra datasources and then later deleting them. I had on many occasions wished for an easier way. When Delphi 2.0 was released, I heard that it included objects called Data Modules that greatly simplified the process. I decided to investigate.

A Data Module, as it turns out, is basically a specialized form that will accept only the standard items on Delphi’s Data Access palette. You can build the entire database for an application, including tables, sources, queries and whatever—all in a data module. To make the whole kit and caboodle available to a form, you just include the data module in the form’s implementation section (not the interface section). Components and fields on the data module then become available to data aware components on the form (and to the Object Inspector as well).

I decided to try a simple example using some data for a client, The Chichen Itza Pizza Company. The data is stored in a Paradox table in a file named PIZADATA.DB. The table contains three fields: the name, retail price and actual cost of the company’s best-selling products. To this mix, I wanted to add a calculated field to display the percent profit generated by each item.

I first created a DatabaseName alias called “Pizza” to point to the table’s directory. Then I created a new data module and gave it the name PizzaData. Onto that module, I dropped a Table and a DataSource, giving them the names ProductTable and ProductSource. I connected ProductSource to ProductTable, and set AutoEdit to False. I opened the Fields Editor in the table and added all the available fields. Next, I added a new calculated field to hold the percent profit, and then wrote a handler for ProductTable’s OnCalc event. The data module can be seen in Figure 13.6. The code for PizzaData is shown in Listing 13.4.

FIGURE 13.6 A data module at design time.

Listing 13.4 Code for the Data Module Demo

{———————————————————————————————————————————————————}

{

Data Module Demo

}

{

PIZADAT.PAS : Data Module

}

{

By Ace Breakpoint, N.T.P.

}

{

Assisted by Don Taylor

}

{

 

}

{ This data module contains a simple Table/

}

{ Datasource combination that links to a Paradox

}

{ data table. A calculated field is provided to

}

{ users of this module.

}

{

 

}

{ Written for *Kick-Ass Delphi Programming*

}

{ Copyright (c) 1996 The Coriolis Group, Inc.

}

{

Last Updated 3/27/96

}

{———————————————————————————————————————————————————}

unit PizaDat;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DB, DBTables;

type

TPizzaData = class(TDataModule) ProductTable: TTable; ProductSource: TDataSource; ProductTableName: TStringField; ProductTablePrice: TCurrencyField; ProductTableCost: TCurrencyField; ProductTablePctProfit: TFloatField;

procedure ProductTableCalcFields(DataSet: TDataSet); private

{Private declarations } public

{Public declarations }

end;

var

PizzaData: TPizzaData;

implementation

{$R *.DFM}

procedure TPizzaData.ProductTableCalcFields(DataSet: TDataSet); begin

ProductTablePctProfit.Value := 100.0 * ((ProductTablePrice.Value - ProductTableCost.Value) /