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

(Ebook - Pdf) Kick Ass Delphi Programming

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

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!

-----------

Deleted records are treated differently by Paradox and dBASE. When dBASE “deletes” a record, it doesn’t physically remove it. It just marks the record for deletion, by placing an asterisk in the first byte of the record. The advantage is that records can be easily “undeleted”. The disadvantage is that zero space is recovered when a record is deleted. On the other hand, Paradox really does delete the record, and it reuses the space when new records are added.

The mechanics of packing the two flavors of tables are different, as well. dBASE tables are packed with a DBiPackTable command. In contrast, Paradox tables are packed as part of a restructuring process (a relationship that may explain why the pack option was placed on Paradox’s restructure dialog).

Most of the machinations in PackTable center around capturing the state of the table (so it can be restored on exit) and getting the table in the proper state prior to making the appropriate call to the BDE’s API. The only way PackTable can differentiate between the two table styles is by the value given to the TableType property. When setting the table’s properties, either ttParadox or ttDBase must be selected; the standard ttDefault won’t cut the mustard. And no matter which type of table is being packed, it must be operated on in the Exclusive mode. No one else can be accessing the table while the packing operation is taking place.

The Packing Demo

I needed a simple program to demonstrate the functionality of the PakTable unit. The running version of what I came up with is shown in Figure 12.4. The code for the program can be seen in Listing 12.6.

FIGURE 12.4 The Packing Demo program.

Listing 12.6 Code for the packing demo

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

{

Data Table Packing

Demo

}

{

PackMain.PAS : Main

Form

}

{

By Ace Breakpoint, N.T.P.

}

{

Assisted by Don Taylor

}

{

 

}

{ A program demonstrating the use of the PakTable

}

{ unit for packing Paradox and dBASE tables.

}

{

 

}

{ Written for *Kick-Ass Delphi Programming*

}

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

}

{

Last Updated 3/20/96

}

{———————————————————————————————————————————————————} unit PackMain;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

DB, DBTables, StdCtrls, Grids, DBGrids, PakTable, ExtCtrls;

type

TForm1 = class(TForm) AddBtn: TButton; RemoveBtn: TButton; PackBtn: TButton; QuitBtn: TButton; Table1: TTable; DataSource1: TDataSource; DBGrid1: TDBGrid; Label1: TLabel; TableNameLabel: TLabel; Label2: TLabel; FileSizeLabel: TLabel; Label3: TLabel; NumRecsLabel: TLabel; Bevel1: TBevel;

procedure QuitBtnClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure AddBtnClick(Sender: TObject); procedure RemoveBtnClick(Sender: TObject); procedure PackBtnClick(Sender: TObject); procedure FormActivate(Sender: TObject);

private

TablePathName : ShortString; procedure UpdateFileLabels;

public

{ Public declarations } end;

var

Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.QuitBtnClick(Sender: TObject); begin

Close;

end;

procedure TForm1.FormCreate(Sender: TObject); var

s : ShortString; begin

Table1.Active := True;

s := Application.ExeName;

TablePathName := Copy(s, 1, pos('.', s)) + 'DB'; TableNameLabel.Caption := TablePathName;

end;

procedure TForm1.UpdateFileLabels; var

f : File of Byte; begin

{Can't have the table open during the Reset } Table1.Close;

AssignFile(f, TablePathName); {$I-}

Reset(f); {$I+}

if IOResult = 0 then begin

FileSizeLabel.Caption := IntToStr(FileSize(f)); CloseFile(f);

end

else FileSizeLabel.Caption := 'I/O error!';

{Re-open the table }

Table1.Open;

NumRecsLabel.Caption := IntToStr(Table1.RecordCount); end;

procedure TForm1.AddBtnClick(Sender: TObject); var

i : Integer; begin

with Table1 do begin

for i := 1 to 100 do begin

Append; Table1.FieldByName('MessageString').AsString

:= IntToStr(i) + ': Hello. My name is Mister Ed.'; Post;

end; { for } end; { with }

UpdateFileLabels;

end;

procedure TForm1.RemoveBtnClick(Sender: TObject); begin

with Table1 do begin

First;

while not EOF do begin

Edit;

Delete;

MoveBy(3); end; { while }

end; { with }

UpdateFileLabels;

end;

procedure TForm1.PackBtnClick(Sender: TObject); begin

if not PackTable(Table1)

then MessageDlg('Error packing the table', mtError, [mbOK], 0);

UpdateFileLabels;

end;

procedure TForm1.FormActivate(Sender: TObject); begin

UpdateFileLabels;

end;

end.

This simple application demonstrates the packing of Paradox files. Clicking the Add button adds 100 new records to the table; clicking Remove deletes every third record. By hitting the Add and Remove buttons a few times while monitoring the display, it becomes obvious that not all the space is being recovered by the delete operation. Clicking the Pack button doesn’t change the number of records, but it can certainly reduce the total file size.

End of entry, March 20.

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!

-----------

Back at Ace’s Office

He had been so engrossed with the loss of his Casebook, Ace hadn’t even noticed the morning light streaming in through the venetian blinds. Although he had paced the small room until he had nearly worn a hole in the carpet, he was no closer to a solution to the whereabouts of his Casebook than when he started.

Defeated, he flopped down in his chair. If I’m such a great detective, why can’t I solve this simple mystery? he thought. During the past nine hours, he had gone over the scenario a thousand times, but still there were no answers. Only questions. He had even dusted the room for fingerprints, and found only his own. There didn’t seem to be a single clue.

Ace sat there for nearly an hour, drowning in the gloom. His Casebook, the repository of all technical knowledge he had gained in working with Delphi, was gone. Someone out there was reading his secret aspirations. His innermost thoughts. He felt naked and exposed. And in all likelihood he would never see the Casebook again. If only there were even a glimmer of hope, he thought.

His eyes popped wide open. There was a glimmer. He had seen it last night, as his headlights swept across the shrubbery in the hasty departure from his office. He remembered seeing the two tiny reflections from one of those trees that could have come from the eyes of a human being!

The former detective leaped to his feet. If there was a man, he must have left footprints in the soft, soggy soil! He rushed outside, and was in the midst of painstaking examination of the ground around the shrubbery when he was startled by the sound of a familiar voice.

“Hi, neighbor.”

Ace whirled around. “Uh—Hi, Marge,” he replied. “I didn’t hear you.”

“Looking for anything in particular?” she asked, her squinty eyes peering out from underneath a camouflage-colored sou’wester.

Marge Reynolds was a frumpy, middle-aged widow and a socially responsible member of the Polyester Knit movement. She had been Ace’s neighbor for nearly two years, and her cat, Charmin, was one of Mewnix’s most ardent admirers. Marge was someone who kept her ear to the ground and her eyes fixed in the gap between the drapes she never quite closed. Very little escaped her notice. Ace decided to take her into his confidence, and told her the entire sordid tale. She was particularly interested in the part where Ace thought he saw a man in the bushes.

“You know, it must have been the same man I saw,” she said thoughtfully. “When I opened the door to let Charmin out last night, he was moving about in the bushes near the door. When I asked him what he was doing out there at that time of night, he took off like a shot.”

“What time was that?” Ace queried.

“Oh, I would say it was about ten or ten-thirty,” she replied.

“Can you describe him?”

“He was rather tall and slender,” she said. “He was wearing a long raincoat and a hat that totally hid his face. He was holding something in his hand. Could have been a tool. Maybe even a weapon. You know, someone else might have seen him. I think we should ask the manager.”

“Great idea!”

The two would-be sleuths strode across the parking lot to the manager’s office. But when they got there, they found the door standing wide open and no one in sight! They exchanged questioning glances. Then Marge expressed the concern that weighed heavily on both their minds:

“I wish Frank and Joe Hardy were here right now,” she said grimly.

Different Strokes

Across town, the sounds of the last bus leaving for Bayport drifted in through a broken pane in the only window in the shabby, one-room apartment. Oblivious to the noise from the street below, the mysterious figure reached up to the corner of the hardbound Casebook and turned the page.

Casebook No. 16, March 21: Got a new client today, a man by the name of Barry Mountebank. As the chief Spin Doctor for a prominent politician, Barry wants me to develop a word processor that can create different descriptions of the same events, depending on which way the wind is blowing on a given day.

I thought it might be profitable to create a demo that would show how you can filter and translate the keystrokes arriving at a Delphi application, turning keystrokes received into whatever keystrokes you please. When I proposed this to the client, he got very excited.

The technique is exceedingly simple. The Delphi Application object recognizes an OnMessage event that lets anyone tie directly into the message chain for all components in that application.

I set out to create a simple application that would demonstrate three points:

1.It is an easy process to exchange one keystroke for another;

2.All key swaps performed in this manner are automatically sent to all components within the application, even other forms; and

3.Swapping can be turned on and off on the fly.

Figure 1.5 shows the example I created, with two forms up on the screen. Selecting the appropriate radio button switches filtering off and on. When filtering is on, the capital and lowercase “a” are reversed, the backspace key acts as the delete key, and the delete key and shift-F5 combination both take on the former role of the backspace key. The code is included in KSMAIN.PAS and KSFORM2.PAS, shown as Listings 12.7 and 12.8.

FIGURE 12.5 Filtering Delphi’s keystrokes.

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!

-----------

Listing 12.7 Main form of the key swapping demo

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

{

Key Swapping Demo

}

{

KSMAIN.PAS : Main Form

}

{

By Ace Breakpoint, N.T.P.

}

{

Assisted by Don Taylor

}

{

 

}

{ A simple application that demonstrates the

}

{ selective filtering and substitution of one

}

{ key for another, throughout an application.

}

{

 

}

{ Written for *Kick-Ass Delphi Programming*

}

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

}

{

Last Updated 3/21/96

}

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

unit KsMain;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, KSForm2, ExtCtrls;

type

TForm1 = class(TForm) ExitBtn: TButton; ShowBtn: TButton; Form1Memo: TMemo; Bevel1: TBevel;

KeyHandlerRBGroup: TRadioGroup; procedure FormCreate(Sender: TObject); procedure ExitBtnClick(Sender: TObject); procedure ShowBtnClick(Sender: TObject);

private

procedure OnAppMessage(var Msg : TMsg; var Handled : Boolean); public

{ Public declarations } end;

const

Shifted : Boolean = False;

var

Form1: TForm1; implementation

{$R *.DFM}

procedure TForm1.OnAppMessage(var Msg : TMsg; var Handled : Boolean); begin

if KeyHandlerRBGroup.ItemIndex = 1 then with Msg do

begin

case Message of WM_KEYDOWN :

begin

case WParam of

VK_SHIFT : Shifted := True;

VK_F5 : if Shifted then WParam := VK_BACK; VK_DELETE : WParam := VK_BACK;

VK_BACK : WParam := VK_DELETE; end; { case }

end;

WM_CHAR : begin

case chr(WParam) of

'a' : WParam := ord('A'); 'A' : WParam := ord('a'); end; { case }

end;

WM_KEYUP : begin

case WParam of

VK_SHIFT : Shifted := False; end; { case }

end;

end; { case } end; { with }

end;

procedure TForm1.FormCreate(Sender: TObject); begin

Application.OnMessage := OnAppMessage; KeyHandlerRBGroup.ItemIndex := 0;

end;

procedure TForm1.ExitBtnClick(Sender: TObject); begin

Close;

end;

procedure TForm1.ShowBtnClick(Sender: TObject); begin

Form2.Show;

end;

end.

Listing 1.8 Secondary entry form for the swapping demo

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

{

Key Swapping Demo

}

{

KSFORM2.PAS : Secondary Entry Form

}

{

By Ace Breakpoint, N.T.P.

}

{

Assisted by Don Taylor

}

{

 

}

{ A simple application that demonstrates the

}

{ selective filtering and substitution of one

}

{ key for another, throughout an application.

}

{

 

}

{ Written for *Kick-Ass Delphi Programming*

}

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

}

{

Last Updated 3/21/96

}

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

unit KsForm2;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;

type

TForm2 = class(TForm) CloseBtn: TButton; Bevel1: TBevel; Form2Memo: TMemo;

procedure CloseBtnClick(Sender: TObject); private

{Private declarations } public

{Public declarations }

end;

var

Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.CloseBtnClick(Sender: TObject); begin

Close;

end;

end.