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

(Ebook - Pdf) Kick Ass Delphi Programming

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

ProductTableCost.Value);

end;

end.

I had the data. Now it was time for the demo. I decided to play with two forms. The first form would simply access the data through ProductSource on the data module. This form would display the product’s name and the calculated field, and I would give it a Navigator so I could browse through the data.

The other form (which would be the application’s main form) would have its own Table and DataSource, plus a DBGrid and a Navigator. In addition, I would provide a set of radio buttons that would permit dynamic switching between the data module and the local source. By selecting the local source, the Navigators on the two forms would work independently, since they would be working with different table objects.

Figure 13.7 shows the two forms up and running. Listing 13.5 illustrates the code for the demo’s main form, while Listing 13.6 contains the code for the secondary form.

FIGURE 13.7 The Data Module Demo at runtime.

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 13.5 Code for the Data Module Demo’s main form

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

{

Data Module Demo

}

{

PIZAMAIN.PAS : Main Form

}

{

By Ace Breakpoint, N.T.P.

}

{

Assisted by Don Taylor

}

{

 

}

{ This demo shows how a form can be connected to

}

{ a data module constructed for a project. This

}

{ form provides a switch to select the source of

}

{ its data -- the module or a local Table/Datasource}

{ combination.

 

}

{

 

}

{ Written for *Kick-Ass Delphi Programming*

}

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

}

{

Last Updated 3/27/96

}

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

unit PizaMain;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids, DBGrids, ExtCtrls, DBCtrls, DBTables, DB, StdCtrls;

type

TForm1 = class(TForm) DBGrid: TDBGrid; Navigator: TDBNavigator;

DataSourceRBGroup: TRadioGroup; QuitBtn: TButton;

LocalTable: TTable; LocalDataSource: TDataSource; LocalTableName: TStringField; LocalTablePrice: TCurrencyField;

LocalTableCost: TCurrencyField; Bevel1: TBevel;

procedure FormShow(Sender: TObject); procedure FormCreate(Sender: TObject);

procedure DataSourceRBGroupClick(Sender: TObject); procedure QuitBtnClick(Sender: TObject);

private

{Private declarations } public

{Public declarations }

end;

var

Form1: TForm1;

implementation

uses

PizaDat, PizaFrm2;

{$R *.DFM}

procedure TForm1.FormShow(Sender: TObject); begin

Form2.Show;

end;

procedure TForm1.FormCreate(Sender: TObject); begin

DataSourceRBGroup.ItemIndex := 0; end;

procedure TForm1.DataSourceRBGroupClick(Sender: TObject); begin

if Tag > 0

then case DataSourceRBGroup.ItemIndex of 0 : begin

DBGrid.DataSource := PizzaData.ProductSource; Navigator.DataSource := PizzaData.ProductSource; end;

1 : begin

DBGrid.DataSource := LocalDataSource; Navigator.DataSource := LocalDataSource; end;

end { case } else Tag := 1; end;

procedure TForm1.QuitBtnClick(Sender: TObject); begin

Close;

end;

end.

Listing 13.6 Code for the Data Module Demo's secondary form

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

{

Data Module Demo

}

{

PIZAFRM2.PAS : Secondary Form

}

{

By Ace Breakpoint, N.T.P.

}

{

Assisted by Don Taylor

}

{

 

}

{ This demo shows how a form can be connected to

}

{ a data module constructed for a project. This

}

{ form receives its data from the project's data

}

{ module.

 

}

{

 

}

{ Written for *Kick-Ass Delphi Programming*

}

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

}

{

Last Updated 3/27/96

}

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

unit PizaFrm2;

interface

uses

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

type

TForm2 = class(TForm) NameDBText: TDBText; PctDBText: TDBText; Label1: TLabel; Label2: TLabel;

Navigator: TDBNavigator; Bevel1: TBevel;

private

{Private declarations } public

{Public declarations }

end;

var

Form2: TForm2;

implementation

uses PizaDat;

{$R *.DFM}

end.

Before compilation, I set the Active property for all the table objects to True. I guess that’s just force of habit.

Both forms declare PizaDat (the data module’s unit name) in their implementation section. Once that is done, the fields from the data module become selectable through the Object Inspector for any data-aware component.

As can be seen, it was a straightforward exercise to make the switch between the two data sources with

the radio buttons. Well, almost...

It seems that the OnClick event handler for radio buttons gets called during the creation of a form. In this case, the data sources weren’t fully constructed and connected, which generated an exception. I chose to avert this situation by using the form’s Tag property. On initialization, it is zero and there is no attempt to connect the sources. The next time through is a different story, because Tag has been set equal to one.

Sure enough, the demo works as designed. When the main form is connected to the data module, the main form displays the percent profit field, and both forms synchronize on the same record, no matter which form is controlling the browsing. When the main form is switched to the local data source, the two forms operate independently. Switching back to the data module instantly synchronizes the two forms.

Of course, a data module is not limited to something as simple as a calculated field. A data module is, after all, an Object Pascal unit that can contain newly defined objects, methods, and handlers for every event in a Table, DataSource, SQL Query, or whatever. In fact, a programmer can implement an entire set of business rules for a company’s data. Pretty cool. And very, very powerful.

End of entry, March 27.

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!

-----------

An Exciting Discovery!

Ace Breakpoint rapidly dialed Helen’s work number. She picked up on the second ring.

“Hardanger World. May I help you?” she said.

“Helen—I’ve got some really exciting news. I wanted you to be the first to hear it,” he said.

“That’s wonderful, Sweetheart,” Helen replied. “What’s happened?”

“I’ve found an important clue—er, I mean The Author led me to an important piece of evidence.”

“I’m so glad you got in touch with him, Ace. What did you find?”

“It’s a single leather glove. It was lying on the ground in the parking space next to mine, nearly buried in the mud. Only the tip of the thumb sticking up. If I hadn’t known to look there, I never would have found it.”

“That means it was probably dropped by someone who parked next to you,” Helen observed.

“Dang!” he exclaimed. “You know, there was a car parked there last night. I noticed it only because that spot is normally empty. But I was in such a hurry to get out of here, it didn’t really register. In fact, I can’t even remember what make the car was. I only remember it was large and kind of boxy—a dirty white color, I think. By now, there’s not a Snowball’s Chance of getting a tread imprint.”

“How about the glove? Can you tell anything from it?” Helen asked.

Ace examined the glove closely. “Needless to say, it’s covered with mud and a

complete mess. There’s no possibility of lifting any fingerprints from it. Let’s see the inside... Totally soaked with water... No lining... Wait! Wait just a second! Part way down inside, I can see a couple of short hairs. Probably from the back of the thief’s hand.”

“It’s Bohacker, Ace,” Helen said in a forced whisper.

“Sweetheart, it can’t be. For a while I did think it might be him. But I tried to call him several times earlier today at his home, and there was no answer. Besides, now that I have the glove—”

“Call it ‘woman’s intuition,’ or call it anything you like,” Helen interrupted. “I just know that’s Bohacker’s glove,” she insisted.

“If I had another hair or something from his body, we could know for sure by performing a DNA test,” Ace observed.

“I suppose we may never know. It’s just... Ace! Don’t you still have that trenchcoat with some of Melvin Bohacker’s blood on it?”

“Sure I do!” he exclaimed. “It’s still in my closet. I can take it and the glove down to Crime City.”

“Huh? What’s that?” she asked.

“Crime City,” he replied. It’s one of those all-night criminal laboratory chain stores. They can do a DNA match test and fax the results to my office in a couple of hours. I think I may even have a coupon for two dollars off the regular price. Tell you what: I’ll drop off the stuff and then pick you up when you get off work. We can catch a quick burger somewhere. By that time, the test results should be in. That okay with you?”

“Count on it,” she said.

Taking Win95 for a Walk

Ace swallowed the last bite of his hamburger and sat deep in thought, staring unexpressively across the table at his dinner partner. Realizing she wasn’t likely to get any more conversation out of him for awhile, Helen suggested they head back to his office to see if the test results had come in.

“While you’re taking care of the check, I have a stop to make,” she said, gracefully getting up from the table.

“Okay,” he said absent-mindedly. His eyes automatically followed her as she meandered toward the front of the restaurant. The Maison de Mort Rouge Viande was one of those upscale, bovine-centric joints that virtually dripped with class. The kind of place Helen liked best. Of course, she had grown up eating in places like this.

Ace partially snapped out of his trance. If there’s a chance this guy really is Bohacker, he thought, I want to know it now. And with Helen out of earshot for the moment, this might be a good time to check in with my Man on the Street, to see what’s happening.

He pulled the trusty cell phone from his coat pocket and dialed the number for Buck McGawk’s Norwegian Fried Chicken Haus. A place, he remembered, that dripped with something other than class.

“Welcome to Buck’s,” the voice said. “Our special tonight is the Pullet Surprise. May I take your order?”

“Is that you, Biff?” Ace asked.

“Ace—how’s it going, Buddy?”

“I need some information, and I need it fast. Have you seen Melvin Bohacker recently?”

“You know, it’s funny like you should mention him. Something really weird happened today.”

“Tell me about it,” Ace said.

“Well, I may have told you before that Bohacker has a standing order for a tub of Extra Greasy on Tuesdays and Fridays. So normally he would have come by tonight to pick up his dinner.”

“Uh-huh.”

“Well, he called in earlier this afternoon and canceled,” Biff continued. “Said he was going out of town on an unexpected trip, and didn’t know for sure when he’d be back.”

“Was that all?” Ace prompted.

“It was kind of noisy, with cars pulling through here and all. But it sounded sorta like he said he was meeting some woman over in Norton City. Whaddya think? That little receptionist dumped him?”

“I wouldn’t know, Biff,” Ace replied. “Listen. Gotta go. I’ll talk to you later.” Ace switched off the phone, slipped it in his pocket, and headed for the register.

Meanwhile, across town, the missing Casebook was still being scrutinized....

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!

-----------

Casebook No. 16, March 28: Ever since its initial release, I have heard that one thing that set Delphi apart from other visual programming tools is its ability to make all the difficult Windows stuff easy to work with, while affording programmers the ability to work at the nuts-and-bolts level. I decided to investigate some of the inner workings of Windows 95, and how they could be accessed from a Delphi application.

One of the major differences between Windows 3.1 and Win95 is the changes resulting from the addition of preemptive multitasking. Under Win 3.1, the multitasking was cooperative, which meant only one task could execute at a time, and no other task could get control of the CPU until the current task yielded control in a cooperative manner. Among other things, this meant that one program could always prevent another program from accessing system data structures until it was good and ready to allow that access. But Win95, with its multithreading and preemptive multitasking, creates quite a different scenario, where the operating system is in absolute control, parceling out CPU time slices on a prioritized basis.

Microsoft quietly included a Dynamic Link Library called TOOLHELP.DLL with Win 3.1. Although it received little coverage in books or magazines (and the documentation for it was almost non-existent), Borland did provide an interface unit for it as part of Delphi 1.0. ToolHelp provided several interesting “under-the-hood” routines, including TaskFirst and TaskNext routines that enabled a programmer to “walk” the list of currently active tasks within the system. I was pleased to see a similar interface unit, called TLHELP32, included with Delphi 2.0 for the 32-bit world, and I decided to concentrate today’s investigations in that area.

Just Say “Cheese”

At first, I was rather surprised by the difference between the 16-bit and 32-bit versions of ToolHelp. Several of the routines (including TaskFirst and

TaskNext) were missing in the 32-bit version. Was it an oversight?

As it turns out, it was not. The implementation of preemptive, multithreaded multitasking has created a situation so dynamic, it has established the software equivalent of the Heisenberg Uncertainty Principle: You can’t let a system that is continuously managing tasks and creating, prioritizing, scheduling and destroying threads do its thing and also dynamically report on the system at the same time. By the time you would get a report, the system’s state is quite likely to have changed.

My fondest desire would be that Win95 would hand over control for a few CPU cycles, so I could get an absolutely accurate report of the state of the system. But that would put the entire system at the mercy of one application, and that’s in total disagreement with what preemptive multitasking is all about.

So what’s the solution? It’s to take a “snapshot” of the entire system, scheduled at Win95’s discretion. We can then analyze that snapshot to our hearts’ content without interfering with system operation. It’s certainly not a perfect solution, but at least it’s workable.

The snapshot capability is included in the 32-bit version of ToolHelp, in a function called CreateToolHelp32Snapshot. This function requires two parameters. The first is a mask that determines what kind of information to collect. Table 13.1 details the masks and their values. The second parameter is a handle to a process within the system. This handle—to an object called a process ID—gives us access to a single process; by examining that process, several pieces of information can be obtained. In general, it works like this:

Make a call to get a list of all current processes, using the appropriate mask to collect the desired data. This returns a handle to a memory record maintained by KERNEL32, the source of all knowledge regarding system status.

Use the Process32First and Process32Next routines to walk down the list of processes, examining various aspects of each process. The information about each process will be placed in a variable specified in the call to the walking routines.

TABLE 13.1 Masks for CreateToolHelp32Snapshot

Name

Value

Collects data for

TH32CS_SNAPHEAPLIST

1

Memory heaps within a

process

 

 

TH32CS_SNAPPROCESS

2

All processes, system-wide

TH32CS_SNAPTHREAD

4

Threads within a specified

process

 

 

TH32CS_SNAPMODULE

8

Modules within a specified

process

 

 

TH32CS_SNAPALL

15

All of the above

Even though this was an investigation and not a formal exercise, it was time to make a few design decisions. First, I had to determine my overall goal. I