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

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

Prestwood eMagazine

August 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 18 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...
First Comment
Comment 1 of 8

Hello ! I am Alex - professionally writing texts and essays! I have been doing this for many years, I have a huge experience! You can contact: essay writer uk

Posted 5 months ago

Comment 2 of 8

amazing information provide thank you so much dear you have made some nice information provide i really like your blog Mens Leather Jacket

Posted 5 months ago

Comment 3 of 8
  1. n turn, I want to share my story- once when I was still at university I got a little sick and of course I had a whole bunch of tasks that were not completed in the list of which of course there was work on writing an essay and of course I was very upset because I realized that the deadlines expire very quickly and the work had to be written immediately. and here they helped me a lot by advising this wonderful site for writing essays and cheers, I was saved. I hasten to share this wonderful essay writing site with you

Posted 4 months ago

Comment 4 of 8

This is the first time that I visit here. I found so many exciting matters in this particular blog, One thing I would like to request you that pls keep posting such type of informative blog. Star Wars Jacket

Posted 119 days ago

Comment 5 of 8

I appreciate this blog your blog is vert help full for me i really enjoyed this stuff dude. Apoc Jacket

Posted 119 days ago

Comment 6 of 8
The post is very informative many people want to read this type of post I am a fashion designer and also designed the Michael Jackson Jacket and after the job, my first choice is reading this type of post.
Posted 112 days ago

Comment 7 of 8

Good post. Thank you for sharing this with us. Drive Jacket

Posted 53 days ago

Latest Comment
Comment 8 of 8

Australian assignment help experts at MAHO are here to provide you the best thesis writing service at cheap rates. The best part about our thesis writing help service here you can ask from experts to write my thesis round the clock and all of our specialists are well-versed in drafting the thesis writing an academic paper for you.

Posted 53 days ago
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 = P1102A1
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 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
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site:

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