I.T. Discussion Community!
-Collapse +Expand
Search Delphi Group:

-Collapse +Expand Delphi To/From
-Collapse +Expand Delphi Store

Prestwood eMagazine

February 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.


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.

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;


  MyImage   : TJPEGImage;

  ResStream : TResourceStream;



    MyImage   := TJPEGImage.Create;

    ResStream := 


    hInstance, Index, RT_RCDATA_);


    Result := MyImage;






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);


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



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);


  MyJpeg : TJPEGImage;


  MyJpeg := LoadJPEG(1);

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



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



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 = P1193A1
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
Since 4/2/2008
   Contact Us!
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

2,339 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...

©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech