(Ebook - Pdf) Kick Ass Delphi Programming
.pdfListing 14.2 reflects some additional capability I just couldn’t resist adding to this example: the ability to drag an item from a string grid and copy or move it to one of the other grids by dropping it on the grid’s tab. This required writing a common OnMouseDown handler for the grids, and extending the OnDragOver and OnDragDrop handlers for the PageControl. I also added CopyDrag, a boolean flag that is set whenever the Ctrl key is pressed and a drag is started in any of the grids.
DropGridString performs the lion’s share of the work when items are dragged from a string grid to a tab. If the control key was not held down during the drag, DropGridString executes some extra steps that turn a straight copy into a move, clearing out the selected item from the source grid and then squeezing out the dead space.
An executing version of the demo is shown in Figure 14.2. This little application is really a lot of fun to play with. You can easily drag items back and forth between the pages, copying and moving, zigging and zagging. I think sharing event handlers is a lot more fun than attending a wedding. Especially my own.
FIGURE 14.2 The shared events demo app in action.
End of entry, March 29.
The fax machine in Ace’s office began to whirr. Helen immediately jumped to her feet.
“Ace, there’s a fax coming in,” she said. “Hurry—it must be the test results!”
Breakpoint crossed the room and deftly ripped the page from the machine, just as the beep indicated it had finished printing. “Now we’ll see which one of us is right about this Bohacker accusation,” he said smugly.
He stood there, staring at the page and fidgeting for nearly a minute before Helen couldn’t stand it any longer.
“Well, what does it say?” she demanded. “Was it Bohacker, or wasn’t it?”
“Uh, it isn’t completely clear. You see, these quick tests aren’t always conclusive, and—”
“Let me see that,” she said, grabbing the page from his hand. She quickly skimmed the report and then turned to her companion.
“It says right here—and I quote: ‘The test results indicate a nearly point-for-point match between the two samples, with a congruence in the 95th percentile, perfectly consistent with DNA tests of this type.’ That means the
hair and blood samples match, doesn’t it?”
“Well, yes,” Ace admitted. “But—”
“Then it must be Melvin Bohacker, just as I said. And whereever he is, you can bet Madame X is there with him. Now all we have to do is find out where he has gone.”
“Norton City.”
“What?”
“Norton City,” Ace repeated. “Biff told me earlier this evening that Bohacker was going to Norton City sometime tonight. Trust me on this.”
“But where in Norton City,” she asked. “He could be in any one of a hundred places.”
“I think I have a way to find that out,” Ace said, as he strode across the room and flipped on his computer. “I’m going online—and I’m going to get some answers!”
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.
Listing 14.3.
FIGURE 14.3 The single-instance program at runtime.
Listing 14.3 A simple single-instance program
{———————————————————————————————————————————————————}
{ |
The One Instance Demo |
} |
{ |
INSTMAIN.PAS : Main Form |
} |
{ |
By Ace Breakpoint, N.T.P. |
} |
{ |
Assisted by Don Taylor |
} |
{ |
|
} |
{ Application that demonstrates how to prevent |
} |
|
{ more than one instance of a program from running |
} |
|
{ in the Win95 environment. |
} |
|
{ |
|
} |
{ Written for *The Delphi Programming Explorer* |
} |
|
{ Copyright (c) 1995, 1996 The Coriolis Group, Inc. } |
||
{ |
Last Updated 4/1/96 |
} |
{———————————————————————————————————————————————————}
unit InstMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
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.
Of course, the form does nothing by itself. The idea is for a multiple instance to detect the presence of a previous instance and then shut itself down. And while this could be done in the startup code of the form, it is much more polite to accomplish this work in a way that doesn’t display the new instance at all. That means taking care of it before the application even begins.
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.
MemFileSize = 1024;
MemFileName = 'one_inst_demo_memfile';
var
MemHnd : HWND;
{$R *.RES}
begin
{ Attempt to create a file mapping object } MemHnd := CreateFileMapping(HWND($FFFFFFFF),
nil, PAGE_READWRITE, 0, MemFileSize, MemFileName);
{ If it didn't already exist, then run the app… } if GetLastError <> ERROR_ALREADY_EXISTS
then begin Application.Initialize;
Application.CreateForm(TForm1, Form1); Application.Run;
end;
CloseHandle(MemHnd);
end.
This is a kick. First, an attempt is made to create a file mapping object with the CreateFileMapping API call. Whether the object already exists or is actually created, a handle to the object is returned and assigned to MemHnd. Calling CreateFileMapping with a handle value of $FFFFFFFF causes the operating system to use its own paging file instead of the conventional file system, so the file can be shared between processes; all the process needs to know is the name of the file. Although the file is set up for read/write, a call is not made here to MapViewOfFile, which would give the program access to the file’s contents through a pointer. For the purpose of this example, it’s sufficient just to check for the file’s existence.
If the memory file already existed at the time of the call to CreateFileMapping, a handle to the file is returned to the caller, and an error value of ERROR_ALREADY_EXISTS is sent to the system. If that error isn’t found by GetLastError, it means there isn’t an instance already running, and it’s safe to go ahead.
Because a handle was returned whether or not the file was created, the handle must be closed before the application finally shuts down. The first program to call CreateFileMapping creates the file mapping object; the last program to close a handle to it causes the system to destroy the object. This is the equivalent of deleting the file.
End of entry, April 1.
Ace clicked the Print button on a dialog and his laser printer came alive. He printed four pages of information, then pulled the sheets from the tray and examined them.
“Now there’s no doubt in my mind,” he said with a newly found excitement in his voice.
Helen was tempted to say something about her being right after all, but thought better of it. Besides, Ace was heading for the door.
“I’m coming with you,” she said, picking up her raincoat and purse.
“Sorry, Baby,” Ace replied. “This one might be dangerous, so you’re staying here. Wait by the phone in case something goes wrong.”
“I suppose you’re right,” she said reluctantly. “But be careful, Sweetheart.” Then she tenderly kissed him on the lips.
“I should be back in an hour or so,” he said. “If you haven’t heard from me by then, call the cops. Tell them I’m on a mission to bring in Bohacker!” One more kiss and he was gone.
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.