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

October 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 22 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

1 Comments.
Share a thought or comment...
First Comment
Comment 1 of 11

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 55 months ago

Comment 2 of 11

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

Posted 55 months ago

Comment 3 of 11
  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 https://aussieessaywriter.com.au/

Posted 54 months ago

Comment 4 of 11

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 54 months ago

Comment 5 of 11

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

Posted 54 months ago

Comment 6 of 11
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 54 months ago

Comment 7 of 11

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

Posted 52 months ago

Comment 8 of 11

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 52 months ago

Comment 9 of 11

Thank you for this great contribution, I find it very interesting and well thought out and put together. I hope to read your work in the future.  Arrow Oliver Queen Jacket

Posted 42 months ago

Comment 10 of 11

amazon kindle supportamazon kindle customer servicekindle supportkindle helptwitter supportcontact twittertwitter helptwitter customer servicematch customer servicematch phone numbermatch.com customer service numbermatch.com customer service phone numberarlo supportarlo customer servicearlo setuparlo support numberasus customer serviceasus supportasus support numberasus laptop supportgoogle voice numbergoogle voice setupgoogle voice phone numbergoogle voice customer serviceaol customer serviceaol customer service phone numberaol helpaol supportlinksys router setuplinksys setuplinksys customer servicelinksys supportpanasonic customer servicepanasonic supportpanasonic phone numberpanasonic customer service numbertinder supporttinder customer servicetinder contacttinder phone numberyoutube customer serviceyoutube phone numbercontact youtubeyoutube helphotmail supporthotmail customer servicecontact hotmailhotmail helpbrother customer servicebrother printer customer servicebrother printer setupbrother printer supportkaspersky supportkaspersky helpkaspersky support numberkaspersky customer service

att email supportat&t customer service emailatt email support phone numberalexa setupcall alexaamazon alexa customer servicealexa supportarlo supportarlo customer servicearlo setuparlo support numberasus customer serviceasus supportasus support numberasus laptop supporthotmail supporthotmail customer servicecontact hotmailhotmail helpfirefox supportfirefox helpmozilla thunderbird helpmozilla supportskype phone numberskype numberskype customer serviceskype phonetwitter supportcontact twittertwitter helptwitter customer servicepinterest helpcontact pinterestpinterest phone numberpinterest customer servicemeetme chatmeetme supportmeetme contactmeetme customer servicesnapchat supportsnapchat helpsnapchat contactsnapchat number

hp supportcontact supportmicrosoft customer serviceavast customer servicelinksys router setupasus customer servicehewlett packard customer servicecarbonite phone numberdropbox supporthotmail supportsetup rokunetgear supporttp link supporthp support numberhp customer servicehp printer supportcontact support numbercontact support phone numbermicrosoft support numbermicrosoft supportmicrosoft helpavast supportavast customer service numberavast helplinksys setuplinksys customer servicelinksys supportasus customer service numberasus laptop supportasus supporthewlett packard supporthewlett packard phone numberhewlett packard printer support phone numbercarbonite supportcarbonite customer service numbercarbonite customer servicedropbox helpdropbox customer servicedropbox phone numberhotmail customer servicecontact hotmailroku customer service phone numberroku support phone numbernetgear setupnetgear tech supportnetgear customer servicetp link customer servicetp link router setup

cisco supporthewlett packard customer servicesetup rokuepson supportnorton customer servicesamsung customer servicemcafee supportskype phone numbergoogle voice numbercontact instagramcisco support numbercisco technical support numbercisco technical supportcisco tech supporthewlett packard supporthewlett packard phone numberhewlett packard printer support phone numberroku customer service phone numberroku support phone numberroku tv setupepson customer serviceepson support numberepson printer supportnorton security phone numbernorton phone numbernorton supportsamsung supportsamsung phone numbersamsung contactmcafee customer servicemcafee phone numbermcafee support numbermcafee customer service numberskype numberskype customer serviceskype phonegoogle voice setupgoogle voice phone numbergoogle voice customer serviceinstagram supportinstagram helpinstagram customer serviceinstagram phone number

Posted 35 months ago

Latest Comment
Comment 11 of 11

healthhealthynaturalnuts and seeds Onlinebuy nutsdry fruits onlinebuy dry fruitsBuy Solely Naturalz Kashmiri SaffronSolely Naturalz Kashmiri Saffron costSolely Naturalz Green CardamomSolely Naturalz Green Cardamom costSolely Naturalz Indian Round RaisinsSolely Naturalz Indian Round Raisins costSolely Naturalz Indian Long RaisinsSolely Naturalz Indian Long Raisins costSolely Naturalz W320 Cashew NutsSolely Naturalz W320 Cashew Nuts costSolely Naturalz W240 Cashew NutsSolely Naturalz W240 Cashew Nuts costSolely Naturalz California PistachiosSolely Naturalz California Pistachios costSolely Naturalz W180 Cashew NutsSolely Naturalz W180 Cashew Nuts costSolely Naturalz Jumbo MakhanaSolely Naturalz Jumbo Makhana costSolely Naturalz Walnut HalvesSolely Naturalz Walnut Halves costSolely Naturalz Jumbo Inshell WalnutsSolely Naturalz Jumbo Inshell Walnuts costSolely Naturalz Munakka RaisinsSolely Naturalz Munakka Raisins costSolely Naturalz Afghani Round RaisinsSolely Naturalz Afghani Round Raisins costSolely Naturalz Afghani Long Green RaisinsSolely Naturalz Afghani Long Green Raisins costSolely Naturalz Gurbandi AlmondsSolely Naturalz Gurbandi Almonds costSolely Naturalz Mamra AlmondsSolely Naturalz Mamra Almonds costSolely Naturalz California AlmondsSolely Naturalz California Almonds costSolely Naturalz Sonora AlmondsSolely Naturalz Sonora Almonds cost,

Posted 33 months 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 = P157A1
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
11645
Since 4/2/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


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