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

(Ebook - Pdf) Kick Ass Delphi Programming

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

interface

uses

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

type

TForm1 = class(TForm) ExitBtn: TButton; Label1: TLabel;

procedure ExitBtnClick(Sender: TObject); private

{Private declarations } public

{Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ExitBtnClick(Sender: TObject); begin

Close;

end;

end.

Listing 14.6 No Run Demo project file

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

{

The "No Run" Demo

}

{

NORUN.DPR : Project File

}

{

By Ace Breakpoint, N.T.P.

}

{

Assisted by Don Taylor

}

{

 

}

{ An application that demonstrates how to prevent

}

{ execution if Delphi 2.0 isn't running.

}

{

 

}

{ Written for *Kick-Ass Delphi Programming*

}

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

}

{

Last Updated 4/2/96

}

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

program NoRun;

uses

Forms, Dialogs,

NRunMain in 'NRunMain.pas' {Form1}, WalkStuf in 'WalkStuf.pas';

{$R *.RES}

begin Application.Initialize;

{If there isn't a copy of Delphi 2.0 running, display an error message and kill the program. Otherwise, let this application

execute. }

if ModuleSysInstCount('DELPHI32.EXE') < 1

then MessageDlg('Delphi 2.0 must be running to execute this program', mtError, [mbOK], 0)

else begin

Application.CreateForm(TForm1, Form1); Application.Run;

end;

end.

The goal is to kill the application without the user even seeing the main form. So once again, code is added to the project file to accomplish the task. This time the ModuleSysInstCount function from the WalkStuf unit is used to make sure at least once instance of Delphi 2.x (DELPHI32.EXE) is running. If so, the program continues. If not, an error message is displayed.

One note: Because WalkStuf relies on ToolHelp32, this technique will only work with programs running under Win95.

End of entry, April 2.

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!

-----------

Floating Toolbars

Casebook No. 16, April 3: I’ve always liked programs with toolbars that can be moved around on the screen. They’re especially handy in artwork and page makeup programs, because you can have a tool palette right next to where you’re working.

I sifted through the various components provided with Delphi, looking for something that could be used as the basis for a “floating” toolbar. I suppose I could have used an additional form, but I didn’t need anything fancy. Something that would be limited to the main form’s client area would be just fine.

It seemed that Panels would do the trick nicely, except for one thing: they aren’t movable at runtime. But a little investigation revealed they have the provision for mouse event handling. After a few bumps and blind alleys, I came up with the demo program documented in Listing 14.7.

Listing 14.7 Code for the Floating Toolbar demo

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

{

The "Floating Toolbar" Demo

}

{

TOOLMAIN.PAS : Main Form

}

{

By Ace Breakpoint, N.T.P.

}

{

Assisted by Don Taylor

}

{

 

}

{ An application that demonstrates the use of

}

{ moveable TPanel objects as floating toolbars.

}

{

 

}

{ Written for *Kick-Ass Delphi Programming*

}

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

}

{

Last Updated 4/3/96

}

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

unit ToolMain;

interface

uses

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

type

TDirection = (otHorizontal, otVertical);

TForm1 = class(TForm) Toolbar: TPanel; ExitSB: TSpeedButton; ZoomInSB: TSpeedButton; ZoomOutSB: TSpeedButton; ControlPanel: TPanel;

GranRBGroup: TRadioGroup;

MarginRBGroup: TRadioGroup;

OrientRBGroup: TRadioGroup; ExitBtn: TButton;

LEDSB: TSpeedButton;

procedure ExitBtnClick(Sender: TObject);

procedure ToolbarMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

procedure ToolbarMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

procedure ToolbarMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);

procedure FormCreate(Sender: TObject); procedure GranRBGroupClick(Sender: TObject); procedure MarginRBGroupClick(Sender: TObject); procedure ExitSBClick(Sender: TObject); procedure OrientRBGroupClick(Sender: TObject);

private

DraggingPanel : Boolean; DragStartX : Integer; DragStartY : Integer; GridSize : Integer; MarginSize : Integer;

procedure OrientToolBar(Direction : TDirection); public

{ Public declarations } end;

var

Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ExitBtnClick(Sender: TObject); begin

Close;

end;

procedure TForm1.ToolbarMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

begin

if Button = mbLeft then begin

DraggingPanel := True; DragStartX := X; DragStartY := Y;

end;

end;

procedure TForm1.ToolbarMouseUp(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer); begin

DraggingPanel := False; end;

procedure TForm1.ToolbarMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);

var

DeltaX : Integer;

DeltaY : Integer; SafetyMargin : Integer; begin

if DraggingPanel then with Toolbar do

begin

DeltaX := X - DragStartX;

DeltaY := Y - DragStartY; if GridSize > MarginSize

then SafetyMargin := GridSize else SafetyMargin := MarginSize;

if (abs(DeltaX) > GridSize - 1) then if DeltaX > 0

then begin

if (ControlPanel.Left - Left) > SafetyMargin then Left := Left + DeltaX

else Left := ControlPanel.Left - SafetyMargin; end

else begin

if (Left + Width) > SafetyMargin then Left := Left + DeltaX

else Left := SafetyMargin - Width; end;

if (abs(DeltaY) > GridSize - 1) then if DeltaY > 0

then begin

if (Form1.ClientHeight - Top) > SafetyMargin then Top := Top + DeltaY

else Top := Form1.ClientHeight - SafetyMargin; end

else begin

if Top + Height > SafetyMargin then Top := Top + DeltaY

else Top := SafetyMargin - Height; end;

end; { with } end;

procedure TForm1.FormCreate(Sender: TObject); begin

GranRBGroup.ItemIndex := 0;

MarginRBGroup.ItemIndex :=0;

OrientRBGroup.ItemIndex := 0; end;

procedure TForm1.GranRBGroupClick(Sender: TObject); begin

case GranRBGroup.ItemIndex of 0 : GridSize := 1;

1 : GridSize := 10;

2 : GridSize := 20; end; { case }

end;

procedure TForm1.MarginRBGroupClick(Sender: TObject); begin

case MarginRBGroup.ItemIndex of 0 : MarginSize := 5;

1 : MarginSize := 10;

2 : MarginSize := 15; end; { case }

end;

procedure TForm1.ExitSBClick(Sender: TObject); begin

Close;

end;

procedure TForm1.OrientRBGroupClick(Sender: TObject); begin

case OrientRBGroup.ItemIndex of 0 : OrientToolBar(otHorizontal); 1 : OrientToolBar(otVertical); end; { case }

end;

procedure TForm1.OrientToolbar(Direction : TDirection); begin

with Toolbar do begin

Left := 20;

Top := 20;

case Direction of otHorizontal : begin

Width := (4 * ExitSB.Width) + 20; Height := ExitSB.Height + 10; ExitSB.Top := 6;

ZoomInSB.Top := 6;

ZoomOutSB.Top := 6; LEDSB.Top := 6;

ExitSB.Left := 11;

ZoomInSB.Left := ExitSB.Left + ExitSB.Width; ZoomOutSB.Left := ZoomInSB.Left + ZoomInSB.Width; LEDSB.Left := ZoomOutSB.Left + ZoomOutSB.Width; end;

otVertical : begin

Width := ExitSB.Width + 10;

Height := (4 * ExitSB.Height) + 20; ExitSB.Left := 6;

ZoomInSB.Left := 6;

ZoomOutSB.Left := 6;

LEDSB.Left := 6;

ExitSB.Top := 11;

ZoomInSB.Top := ExitSB.Top + ExitSB.Height; ZoomOutSB.Top := ZoomInSB.Top + ZoomInSB.Height; LEDSB.Top := ZoomOutSB.Top + ZoomOutSB.Height; end;

end; { case } end; { with }

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!

-----------

As can be seen, the panel (Toolbar) must handle three mouse events—OnMouseDown, OnMouseMove and OnMouseUp. The OnMouseDown handler simply makes sure it’s the left mouse button that has been pressed. If so, it documents the initial position of the mouse and it sets a status variable that indicates a dragging process is underway.

The OnMouseMove handler is quite a bit more complex, mainly because it is making sure the panel doesn’t move outside the client area of the form, where it might get lost. Basically, the ToolbarMouseMove handler computes the difference between the original mouse position and the current mouse position, and then it adds the differential to the original Left and Top properties of Toolbar to move it. A provision has been made to move Toolbar by jumps of 1, 10 or 20 pixels, giving it the same feeling as moving it in the “lock to grid” mode when designing with Delphi. I also added a margining capability, to make sure a piece of the toolbar can always be grabbed with the mouse, in those cases when the toolbar gets shoved off in a corner.

The OnMouseUp handler is obvious: All it has to do is turn off the status variable.

I added three radiobutton groups to the form, so grid and margin sizes can be set on the fly. I also included a capability for switching the toolbar between horizontal and vertical orientations.

An executing version of the program is shown in Figure 14.5. It’s kind of fun to drag the toolbar around, and the first button on the toolbar is actually functional—it ends the program.

FIGURE 14.5 Floating a Delphi toolbar.

End of entry, April 3.

Ace Gets the Goods

As the Delphi Avenger glanced up from the Casebook, it was obvious that something had gone awry. The room had slowly filled with a thick carpet of fog that now hugged the floor like a Basset Hound on its day off. A brilliant white light emanated from the cloud, illuminating everything in the room with an eerie cast. The Avenger drew in a quick breath of air, a breath that stung the nostrils with the numbing cold of carbon dioxide. Something was very, very wrong. Then suddenly, from across the room there came a violent explosion, as the rickety door blew off its hinges and crashed inward to the floor. In the open doorway, illuminated from behind by the neon bug zapper in the hallway, stood a triumphant Ace Breakpoint!

* * *

The odor that permeated the seedy little apartment was unmistakable, a veritable snack food smorgasbord. I crossed the room powerfully, knowing I had not only solved this case, but that I had wrested complete control of the story from The Third Person. Three giant steps, and I had closed the gap between myself and the pathetic creature that cowered against the opposite wall—the now-defeated entity that once dared to call itself the Delphi Avenger.

“Breakpoint!” Bohacker hissed, dropping my Casebook on the floor. “How did you know?”

“Easy,” I snarled. “From the very beginning of this case, I knew something was amiss. It just took me a while to realize the “miss” was youMiss Bohacker!” I shouted, as I tore the false mustache from her upper lip.

“Cursors!” she spat through clenched teeth. She quickly scrutinized me from head to toe, then back again, like a wild animal trapped in a corner. The look of defeat was gone, and in its place was a twisted smile that quite unnerved me. “Yes, it’s me,” she grinned. “Melvin Bohacker’s evil twin sister, Mevlyn.”

A chill ran down my spine. Suddenly I felt like I was playing a scene in a very bad “B” movie. How did I get myself into these situations? I just shook my head.

“Sure,” she said disdainfully. “Be Mr. High and Mighty. But imagine for just one minute what your life would be like, spending every day of your childhood following in Melvin Bohacker’s footsteps.”

And following his nutritional guidelines, as well, I mused, noticing the mass of Twinkie and HoHo wrappers strewn all over the apartment. But strangely, I did feel a twinge of empathy. I encouraged her to resume her confession.

“He was exactly eight minutes older,” she said. “So he got all the good stuff. I got whatever was left. And when it came time for college, there was only enough money to send one of us. So of course it was Melvin who went off to

school, while I stayed home. I probably would have starved to death, if I hadn’t started the designer clothes business.”

“Wait a second,” I said. “You mean you’re the owner of Bohacker Industries? The people who make Bohacker Blues jeans?”

“You hadn’t made the connection?” she asked wryly. “Then you’re also not aware that my company produces the Purely Prophet designer line for your little friend, Muffy Katz.”

“Why, you must be worth a bundle!” I exclaimed.

“Oh, yes—I have a net worth of several million. Which is fine, I suppose—if you’re merely interested in money. But Melvin is the one who got all the love. He got the accolades. He’s the one who got the college education. And he’s the one who went on to become one of the most esteemed and revered members of the entire scientific community—a Windows 95 programmer.”

Her eyes burned with hatred. “Since childhood, I’ve been a dysfunctional, codependent victim, and I’ve sworn I would get even with Melvin, no matter how long it took. Then a few weeks ago, it became obvious,” she said, staring dreamily off into the distance. “I would steal your Casebook, and armed with the secrets it contains, I would become the best Windows programmer in the world—better even than Melvin. And certainly better than you.”

She stood silently for several seconds, then turned back to continue the confession. “At the same time,” she said thoughtfully, “I could frame Melvin for the robbery. I called him earlier today, to torment him and then lure him out of town tonight, so everyone would think he had taken it on the lam. It was a perfect plan. But something went wrong. Evidently I underestimated you.”

“You forgot I used to be a detective,” I replied. “And you left a trail of clues so transparent that even an MIS manager from Bayport could have followed them.”

“Like the glove?” she asked. “Losing it was an accident. I thought of trying to get it back. But even so, it should have pointed to Melvin, not me.”

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.