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

(Ebook - Pdf) Kick Ass Delphi Programming

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

It was helpful to dig into the source code for the TApplication object, to see just how messages were processed by default, and how any OnMessage event handler I might create should participate in the overall process. Specifically, what should I do (if anything) with the Handled variable passed to the event handler? Listing 12.9 reveals the source for the TApplication’s ProcessMessage method, which is called as part of any application’s endless message processing loop.

Listing 12.9 Source code for TApplication’s ProcessMessage method

function TApplication.ProcessMessage: Boolean; var

Handled: Boolean; Msg: TMsg;

begin

Result := False;

if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then begin

Result := True;

if Msg.Message <> WM_QUIT then begin

Handled := False;

if Assigned(FOnMessage) then FOnMessage(Msg, Handled);

if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then

begin TranslateMessage(Msg); DispatchMessage(Msg);

end; end else

FTerminate := True;

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!

-----------

Listing 12.9 reveals the source of the Handled variable. As can be seen, the ProcessMessage method is called to detect and process any message waiting in the queue. If a message is present, it is removed from the queue. If the message is WM_QUIT, the FTerminate field is set True; otherwise, Handled is set to False. and if it is defined, the OnMessage handler is called. If the message comes back with Handled set to False, and it is not one of several message types (a hint message, an MDI-related message, a control notification message or a dialog message), then the standard TranslateMessage and DispatchMessage routines are called to process the message. It is evident that setting Handled to True in the OnMessage event handler would halt any further processing of the message. I just wanted to substitute one keystroke for another, and let processing go on as usual. Therefore, I did not touch the Handled variable.

My OnMessage handler is straightforward. If the Filtered radio button is selected, the case statement picks off selected messages and substitutes key values, using Windows-defined virtual key constants for the control keys. One item of note: Detecting a shifted character is a two-step process. Since this routine only receives single keystrokes, it doesn’t know whether any of the shift keys (Alt, Ctrl and Shift) are being held down when other keys are pressed. I had to detect the shift presses and releases by first looking for a VK_SHIFT as the wParam value passed during WM_KEYDOWN and WM_KEYUP messages, and then on detection of a VK_SHIFT, storing the shift status in a boolean variable.

The OnMessage handler belongs to the application and not the main form, so there is no provision for setting it as a property during program design. Instead, it gets installed at runtime as part of the main form’s OnCreate handler.

End of entry, March 21.

Playing a .WAV File

The sinister character in the long raincoat began to quiver, as at last it looked up from the Casebook. A high-pitched cackle began to emanate from the twisted mouth, daring the mustache to stay in synchronization with the lips below it.

“Now I can absorb all the material in this Casebook—information so powerful, it can only be used for Good or for Evil. I will become the most respected and powerful Windows programmer on the face of the earth. Thanks to Ace Breakpoint, I will no longer be known merely as ‘Bohacker’ or ‘Hey—you.’ Instead, I will become known far and wide by another name: The Delphi Avenger.

With my newly found programming knowledge I’ll rule the world! Ha ha ha ha!....”

The fit of uncontrolled laughter lasted for nearly ten minutes. Then the Avenger opened a fresh pack of HoHos and smugly turned to the next page.

Some Sound Advice

Casebook No. 16, March 22: Today, I discovered how to play a .WAV file from my Delphi applications. Not that it’s at all difficult. But I was thinking how great it would be to have one of my biggest heroes—Humphrey Bogart—speak when I clicked a button in an application.

Figure 12.6 shows an executing version of the form I created to test my experiments. Listing 12.10 contains the code.

FIGURE 12.6 Ace’s WAV form.

Listing 12.10 A demo program that plays .WAV files

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

{

The .WAV File Player Demo

}

{

PLAYMAIN.PAS : Main Unit

}

{

By Ace Breakpoint, N.T.P.

}

{

Assisted by Don Taylor

}

{

 

}

{ Application that demonstrates how to play a .WAV

}

{ file in a Delphi application.

}

{

 

}

{ Written for *Kick-Ass Delphi Programming*

}

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

}

{

Last Updated 3/22/96

}

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

unit playmain;

interface

uses

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

type

TForm1 = class(TForm) BadgeBtn: TButton; ExitBtn: TButton;

procedure BadgeBtnClick(Sender: TObject); procedure ExitBtnClick(Sender: TObject);

private

{Private declarations } public

{Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.BadgeBtnClick(Sender: TObject); begin

if not PlaySound('badges.wav', 0, SND_FILENAME)

then MessageDlg('Problem playing sound file', mtError, [mbOK], 0);

end;

procedure TForm1.ExitBtnClick(Sender: TObject); begin

Close;

end;

end.

At first, I had assumed I would have to use a MediaPlayer component to play a file. I soon discovered an alternative low-level function called PlaySound in the MMSystem unit. I simply gave it the name of the file and the constant SND_FILENAME, which indicated I wanted the routine to play a sound stored in a file. It doesn’t get much easier than that.

Note to myself: The file used for my experiment (BADGES.WAV) is an edited excerpt from the famous “Stinking Badges” dialog in the classic 1948 Bogie movie, “The Treasure of the Sierra Madre.” It’s one of my faves.

End of entry, March 22.

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!

-----------

A Disconcerting Discovery

The Avenger closed the Casebook and leaned back, considering the events of the last several hours. It had been a long night, and one fraught with peril. But the ruse had worked perfectly. Just as expected, Breakpoint had been lured away by the sound of a helpless female voice. As soon as the former private detective had left his office, the Delphi Avenger was waiting in perfect coordination to break in and steal the precious Casebook.

Breakpoint was probably a basket case by now. But what became of him was of little importance. The only thing that mattered was the Casebook and the intelligence it contained. The end most certainly justified the means. If that meant the end of Ace Breakpoint, so much the better.

The Avenger began to pat the leather-covered oracle, then suddenly stopped. It was now apparent that all had not gone perfectly, and that something was missing. A frantic search through the pockets of the raincoat came up empty.

Beads of nervous sweat began to pop out on the Avenger’s forehead. Maybe it had merely fallen out of a pocket, and it was still in the car. Perhaps it had been left on the top of the vehicle an3d had slipped off, and was now floating in a gutter somewhere.

But there remained the likelihood that it was lying somewhere near Breakpoint’s office. And that would mean it was...evidence. Very important evidence.

So important, it would be worth the risk of being caught to get it back.

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!

-----------

CHAPTER 13

A Revelation in the Mud

DON TAYLOR

Setting a minimum size for a resizeable form

Splash screens

Global data modules

Preemptive miltitasking

With Ace hard in pursuit, the nefarious Delphi Avenger soaks up Ace’s hard-won programming expertise in every area from splash screens to preemptive multitasking.

Helen rushed into Ace’s office. “I’m so sorry you lost your Casebook. It’s

gonna be all right, Baby,” she said, slipping her arms around Ace and pressing her cheek against his. “I would have been here sooner, but it’s so slippery out there.”

Helen Highwater had come from a well-to-do family, but insisted on making her own way in the world. To look at her feminine, five-foot-five slender frame and the strawberry blond hair falling just above her shoulders, few would suspect just how determined Helen could be. So far, she had made it through college to a management position at an outlet store at the mall. But her bigger dream—as yet unfulfilled—was to spend the rest of her life as the wife of Ace Breakpoint.

“Not lost, Helen. That Casebook was stolen. It was a con from start to finish, and I fell for it like some rube from Bayport.”

Ace related the entire story of what had happened the night before, and of his

conversation earlier that morning with Marge Reynolds.

“So at least we have a fairly good description of the perpetrator,” he concluded. “I’m just not sure it leads anywhere. We left a note on the Manager’s door, asking him to call when he returns.”

“Don’t you see?” Helen asked incredulously. “It’s got to be Melvin Bohacker. The description fits. I’m sure he is just being vindictive about what happened in The Case of the Duplicitous Demo, and he’s trying to get back at both of us. He probably hired that woman to make the bogus phone call for him. I’ll bet he’s sitting at home right now, gloating over the whole situation.”

“I don’t think it’s that simple, Helen,” Ace replied. “You didn’t see Bohacker’s face when I—”

Ace’s attention had been grabbed by something outside. Through the blinds in the side window, he could make out the shape of a man moving about in the bushes. He was a tallish man, wearing a brimmed hat and a long, khaki-colored raincoat. He moved closer to the window and peered inside.

“That’s him!” Ace shouted. “That’s the man Marge described, and undoubtedly the man who stole my Casebook. He has returned, just as sure as another sequel to Nightmare on Elm Street—and I’ve got some very pointed questions to ask him!”

The intruder’s eyes shot wide open when Ace cried out, but as soon as Ace moved toward the door, the mysterious stranger bolted from the shrubbery, heading diagonally across the parking lot. Ace followed in hot pursuit, hampered greatly by the slippery mud that now completely covered the asphalt.

“Stop!” Ace commanded. But the raincoated figure just scrambled faster. Ace managed to give an extra surge, catching up with the stranger and tackling him in front of the Manager’s office. The tall interloper hit the ground hard, letting out a loud groan. Together, they slid a full six feet in the mud before coming to a stop.

“Hey—what’s goin’ on here?” came a voice from behind. Ace wrenched his head around far enough to see Marvin Gardens, the property manager, walking up behind him.

“I’ve caught the intruder Marge saw last night,” Ace replied, vainly trying to keep the rain out of his eyes. “The guy who lifted my Casebook.” With some effort, Ace managed to stand up, still maintaining an iron grip on the man in the raincoat. Both men were covered with mud from head to toe.

“Yeah, I got yer note,” said Gardens, chewing on the stogie clamped between his crooked, yellow teeth. “I was gonna call ya. But dis is no intruder, Breakpoint. Say ‘Hello’ ta my new groundskeeper, Sergei Stakupopov. He don’t speak much English, but dat much he’ll understand.”

“Wait a second,” Ace protested. “Last night this man was seen holding some kind of weapon. He was confronted twice, and each time he ran away. Gardener or not, that says he’s guilty.”

“Where he’s from, people live in fear of da secret police,” Gardens replied, after taking a puff off the cheap stogie. “In his country, a challenge yelled by someone could be da last thing ya ever hear. He’s here onna Green Card, and he’s deathly afraid he’s gonna lose it, which would mean his family would have ta return to da homeland. That’s why he’s willing ta work long, hard hours. He was trimmin’ da bushes by your apartment last night—Marge probably saw him holdin’ a pairra shears, dat’s all.”

Ace loosened his grip on the immigrant, then sheepishly shook hands with the groundskeeper and apologized profusely. Sergei watched him guardedly, then smiled politely and said “Hello.”

The former P.I. trudged back to his office, where Helen was waiting with a million questions. He related his experiences of the past few minutes, then just stood there, hanging his head.

“Hey, it’s going to be okay,” Helen affirmed once again. “This is just a temporary setback. Now get out of that muddy trenchcoat before you catch a cold.”

Reluctantly, Ace complied. “I wish I could believe it’s only temporary,” he said, choosing a fresh trenchcoat and hat from the row of identical outfits in his closet.

“Of course it is, Sweetheart,” she replied. “Listen, my lunch hour is over, and I’ve got to get back to the shop. But give me a call this afternoon. I’ll stop by after work, to see how things are going.”

She gave him a kiss on the cheek and then slipped out into the pouring rain.

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.

Go!

Keyword

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

Kick Ass Delphi Programming

(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!

-----------

Resizing Forms

The Avenger popped open another pack of HoHos and took a big bite. The decision not to return to Breakpoint’s apartment office had been a gamble, but hopefully it would pay off. After all, the chances of the has-been detective finding any incriminating evidence accidentally left behind were slim to none. It was better to absorb as much information as quickly as possible.

Casebook No. 16, March 25: I had often wondered how some applications were able to limit the minimum size of a resizable window, so I determined to find out how it was done. I didn’t have any idea how easy that task would be. I did suspect the process had something to do with messages, however. By this time, I had realized that nearly everything that takes place within the Windows environment is somehow related to messaging.

I was also intrigued by the ability of some rather complex forms to maintain an integrity of their component parts, even if those forms were resized. That would also be part of today’s independent investigations.

I wanted a form with a minimum of four major components. I created a rough form containing a speedbutton, a memo, and two regular buttons. It’s shown in Figure 13.1. My first goal was to limit the resizing process to a minimum form size specified by the programmer. The answer came in a message called WM_GETMINMAXINFO.

FIGURE 13.1 The Form Resize Demo Form at design time.

WM_GETMINMAXINFO is a Windows message that notifies an application when the system is checking the size of a window, so it has an opportunity to change default values. Among these values are the minimum and maximum “tracking sizes,” which determine the horizontal and vertical ranges for the width and height of a window. The default minimum is the size of an icon; the default maximum is the size of the entire screen.

The actual parameter passed to the message handler for WM_ GETMINMAXINFO is a point that represents the X,Y offset from the upper-left corner of the window, measured in pixels.

The challenge, then, would be to write a replacement message handler, defining points that would describe minimum and maximum window sizes. I first declared variables for the minimum height and