IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

March Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Language Det...   Print This     
  From the November 2015 Issue of Prestwood eMag
 
Delphi Language Details:
A New Look At Delphi Exceptions
 
Posted 17 years ago on 12/26/2002 and updated 12/26/2002
Take Away:

When you want the exception to be the rule. A technique for handling exceptions in Borland Delphi's Object Pascal.

KB100087



When writing applications that potentially require the user to save modifications, it is possible for the user to make changes and then attempt to close the application without saving those modifications. Sometimes, this is what they want; however, more often than not they have simply forgotten to save. The nice thing for us to do is to detect that changes have not been saved, then when the user clicks Exit, ask them if they wist to save the changes or not.

Customarily, when displaying the message enabling the user to save or not on exit, a Cancel button is provided. This is for those times that the mouse jumps and you click Exit instead of Print in the menu. If the user clicks Cancel, the changes are not saved and the application does not close.

The question before us then, is how do we gracefully handle this in our code? There are a number of options, but the one presented here today is somewhat unique and presents possibilities for uses in other situations as well.

TException
Delphi does an excellent job of implementing exception handling. It is possible, not only for your application to handle application and system errors, but it can generate it's own errors (exceptions) and handle them as well. Today, we are going to use a custom exception to handle our Save on Exit message.
To start we have to define an Exception to use, to do this, move to the Interface section of your form and under Type, add the following line of code:

EEditorError = class(Exception)

When I was done, Mike filled the rest of the time by discussing secrets of the Delphi IDE (primarily timesaving keyboard shortcuts). In a future issue I will be listing the keyboard shortcuts for all four versions of Delphi. It should, hopefully, serve as a handy quick reference to some of those more obscure, yet extremely useful keyboard tricks.

 

Delphi Apprentice
Delphi 3 included the ability to display JPEG images as well as BMPs by simply including the JPEG unit in your uses clause, the only problem is you have to load them at run-time. Delphi doesn't store them in the form like BMPs.

It is possible, however, to include the JPEG file in your executable, so as not to require external image files for your application. The nice thing is it only requires five steps to include and use JPEGs.

First Step
The first step is to create a resource script file (*.rc). You can create this with any text editor (an excellent editor to use it YTSEditor, which was written by our very own Ken Hale. YTSEditor is available at the DC al Coda Web-Site.)

For each image you want to include in your executable, add a line like the following to your file:

1 RCDATA "image1.jpg"


The "1" is simply an index for the resource. The number can be any number you want as long as it is unique in the file.

The "RCDATA" tells the Resource Compiler that this is a user-defined resource.

The "image1.jpg" is the filename of the image to include. Replace this with the name of the file to include.

Save this file. You can name the file anything you want, as long as it has the extension ".rc" and the filename without the extension is not the same as any unit or project filename. This is important, because Delphi will create some resource files of its own.

Second Step
The next step is to compile the resource script file. Delphi comes with a command line resource compiler located in the \Delphi 3\Bin directory and is called BRCC32.EXE.

To compile the resource file, change to the \Bin directory and issue the following command:

BRCC32 images.rc



Make sure that the .RC file and the .JPG file(s) are in the same directory. The result will be a file with the same name, but with a .RES extension.

Third Step
The next thing to do is to tell Delphi to link our resource file in with our application. This is accomplished by adding a compiler directive to the source code. This directive needs to immediately follow the form directive, like the following:

{$R *.DFM}
{$R images.res}

Now when you build your application, the JPGs will be included in the .EXE file.

Fourth Step
At this point we have the JPGs in our EXE and now need some way to extract the image and display it. The following function allows us to specify the index of the image to display and returns the image.

function TForm1.LoadJPEG(

                Index: Integer)

                : TJPegImage;

var

  MyImage   : TJPEGImage;

  ResStream : TResourceStream;



begin

  try

    MyImage   := TJPEGImage.Create;

    ResStream := 

    TResourceStream.CreateFromID(

    hInstance, Index, RT_RCDATA_);



    MyImage.LoadFromStream(ResStream);

    Result := MyImage;

  finally

    ResStream.Free;

  end;

end;

           


Fifth (and final) Step
The last thing we need to do is to call this function and display our image. We have two choices, we can either assign the image to a TImage or we can draw it onto a TCanvas.

 

First, let's assign it to a TImage, for this use the following code:

procedure TForm1.Button1Click(

          Sender: TObject);

begin

  Image1.Picture.Graphic := LoadJPEG(1);

end;

         

Or, if instead we want to draw the image onto a TCanvas (i.e. a TForm) use the following code:

procedure TForm1.Button1Click(

          Sender: TObject);

var

  MyJpeg : TJPEGImage;



begin

  MyJpeg := LoadJPEG(1);

  Form1.Canvas.Draw(10, 10, MyJpeg);

end;

         

That's all there is too it. We now can include JPEG images in our executables and display them whenever we want.

 


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1182A1
Enter key:
Article Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #100087 Counter
8612
Since 4/2/2008
-
   Contact Us!
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


Go ahead!   Use Us! Call: 916-726-5675 


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]