home *** CD-ROM | disk | FTP | other *** search
- { Project Load.DPR Delphi 2.0 Demos
-
- Description:- Load.Dpr Project:-
-
- Demonstrates the use of:
-
- 1) 'FileName'
- 2) 'ImageNumber'
- 3) 'Imageformat'
- 4) 'Callbacks'
- 5) 'Load'
- 6) 'Load Callbacks'
- 7) Progress Bars
-
- Date of Origin: 16/04/96
- Original Author: Andrew Hutchison
- Modification History:
-
- Date Person Change
- ----------------------------------------------------
- 16/04/96 A Hutchison Created
-
- (c) Copyright Media Architects Inc. 1996.
- All rights reserved. No part of this program may be
- photocopied, reproduced, translated to another programming
- language or transported to any computer system without the
- prior written consent of Media Architects Inc.}
-
- unit ULoad;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Menus, OleCtrls, ImageKnife32, ExtCtrls, Buttons, StdCtrls, Spin,
- ComCtrls;
-
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- File1: TMenuItem;
- LoadImage1: TMenuItem;
- LoadImagewithCallBacks1: TMenuItem;
- N1: TMenuItem;
- Exit1: TMenuItem;
- Bevel1: TBevel;
- Picbuf1: TPicbuf;
- ImageType: TComboBox;
- GroupBox1: TGroupBox;
- ImageNumber: TSpinEdit;
- GroupBox2: TGroupBox;
- LoadOption: TComboBox;
- LoadScale: TComboBox;
- LoadWidth: TSpinEdit;
- LoadButton: TSpeedButton;
- LoadCallbackButton: TSpeedButton;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- OpenDialog: TOpenDialog;
- LineNumber: TLabel;
- Label5: TLabel;
- ProgressBar: TProgressBar;
- Bevel2: TBevel;
- BarMax: TSpinEdit;
- Picbuf2: TPicbuf;
- procedure FormCreate(Sender: TObject);
- procedure ImageTypeClick(Sender: TObject);
- procedure LoadImage1Click(Sender: TObject);
- procedure LoadImagewithCallBacks1Click(Sender: TObject);
- procedure LoadButtonClick(Sender: TObject);
- procedure LoadCallbackButtonClick(Sender: TObject);
- procedure Picbuf1Callback(Sender: TObject; var Lines: Smallint);
- procedure BarMaxChange(Sender: TObject);
- procedure Exit1Click(Sender: TObject);
- procedure FormActivate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- {Function's and Procedures used below - By placing them here you can re-use them
- from any unit with this units name in its USES clause}
-
- {Set a Picbuf's Load Options - Pass the Name of the Picbuf to Set}
- procedure SetLoadModes(SetPicbuf:TPicbuf);
- {Determine the Image Format Number from the given String - Returns Integer 0-10}
- function GetImageFormat(FileName:String):Integer;
- {Determine if the passed index value is a Valid file extension for CallBacks -
- returns a True or False}
- function ValidCallBack(ImageFormatIndex:Integer):Boolean;
- {See Below for Functions}
-
- implementation
-
- {$R *.DFM}
-
- {-------------------------------------------------------------------------------}
- {Set up defaults. Including setting the Combo Boxes to there default locations}
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Application.HintPause := 10;
- Application.HintColor := clAqua;
- ImageType.ItemIndex :=0; {'ImageType' Combo Box}
- LoadOption.ItemIndex:=0; {'LoadOptions' Combo Box}
- LoadScale.ItemIndex:=0; {'LoadScale' Combo Box}
- ProgressBar.Max := BarMax.Value; {Set Progress to user Value}
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Load an Image from disk using a Common Dialog control - No Call Backs}
- procedure TForm1.LoadImage1Click(Sender: TObject);
- begin
- {Display Dialog}
- if OpenDialog.Execute then
- begin
- {Catch Up}
- Application.ProcessMessages;
-
- {Set 'ImageType' & 'Combobox' to reflect the extension of the selected file - this
- uses the procedure called 'GetImageFormat' - See Below. This is, in case the user
- typed in a file extension when the dialog is being displayed.}
- ImageType.ItemIndex := GetImageFormat(OpenDialog.Filename);
-
- {Set OpenDialog Control [Common Dialog Control] to the Same Index as the 'ComboBox'
- - this is just making sure that the ComboBox and the OpenDialog box Index's remain
- in Sync}
- OpenDialog.FilterIndex := ImageType.ItemIndex + 1; {Must add 1}
-
- {Call procedure below to set the various load options based on user selections -
- See Below}
- SetLoadModes(Picbuf1);
-
- {Set the FileName}
- Picbuf1.Filename:=OpenDialog.Filename;
-
- {Load the Image}
- Picbuf1.Load;
- end;
- end;
-
- {-------------------------------------------------------------------------------}
- {This event is similar to the above. With the exception that we use the CallBack
- Method. You will see that we use one extra function called 'ValidCallBack' -
- See Below. This Extra Step is to check that a Valid Call Back file 'type' is being
- loaded. The function returns True/False.}
- procedure TForm1.LoadImagewithCallBacks1Click(Sender: TObject);
- begin
- if OpenDialog.Execute then
- begin
- {Catch up}
- Application.ProcessMessages;
-
- {Set 'ImageType' & 'Combobox' to reflect the extension of the selected file - this
- uses the procedure called 'GetImageFormat' - See Below. This is in case the user
- types in a file extension when the dialog is being displayed.}
- ImageType.ItemIndex := GetImageFormat(OpenDialog.Filename);
-
- {Set OpenDialog Control [Common Dialog Control] to the Same Index as the 'ComboBox'
- - this is just making sure that the ComboBox and the OpenDialog box Index's remain
- in Sync}
- OpenDialog.FilterIndex := GetImageFormat(OpenDialog.Filename) + 1;
-
- {Call Procedure below to set the various load options based on user selections -
- See Below}
- SetLoadModes(Picbuf1);
-
- {Set FileName}
- Picbuf1.Filename:=OpenDialog.Filename;
-
- {Use function below to check and see that the file type is compatible with CallBacks
- - the function evaluates to true if a valid FileFormat is passed to it. Note:-
- You must pass an Integer to the function, we can use the Imageformat of the Picbuf.
- Also note that you must set the Image format prior to using this function}
- if ValidCallBack(Picbuf1.ImageFormat) then
- begin
- {Activate the Load Method - Must pass the Picbuf as an .OLEOBJECT }
- PicBuf1.CallbackLines := 2;
- Picbuf1.LoadCallback(PicBuf1.OLEObject)
- end
- else
- begin
- MessageDlg('Please select a [GIF JPG or PNG] file type for callback functions.', mtConfirmation, [mbOK], 0);
- end;
- BarMaxChange(Sender); {After Completion force Progress bar to Max Position}
- end;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Load Button Simply Calls the FileLoad Menu Item event}
- procedure TForm1.LoadButtonClick(Sender: TObject);
- begin
- LoadImage1Click(Sender);
- end;
-
- {-------------------------------------------------------------------------------}
- {Load With Call Back Buttons Simply Calls the FileLoad Menu Item event}
- procedure TForm1.LoadCallbackButtonClick(Sender: TObject);
- begin
- LoadImagewithCallBacks1Click(Sender);
- end;
-
-
- {-------------------------------------------------------------------------------}
- {When the user picks a filetype, set the OpenDialog default Index to the same value.
- This means that the OpenDialog box appears with the correct file extension selected}
- procedure TForm1.ImageTypeClick(Sender: TObject);
- begin
- with ImageType do {ComboBox}
- OpenDialog.FilterIndex := ItemIndex + 1; {Set FilterIndex to ComboBox Index}
- end;
-
-
- {-------------------------------------------------------------------------------}
- {This Procedure is used to set a 'Picbufs' Load Options. To use it simply call the
- function, passing the name of the Picbuf you wish to set -
-
- - SetloadModes(MyPicbuf);
-
- Notice the use of 'With SETPICBUF do..Begin'. From that point on you need make no
- further reference to the Picbuf. You need just reference the properties you
- wish to set.}
- procedure SetLoadModes(SetPicbuf:TPicbuf);{Refer to this name within the procedure}
- begin
- with SetPicbuf do {Reference Picbuf - this is the Picbuf sent to the Procedure}
- begin
- ImageFormat:=Form1.ImageType.ItemIndex; {SetImage Format}
- ImageNumber:=Form1.ImageNumber.Value; {Set Image Number}
- LoadOptions:=Form1.LoadOption.ItemIndex; {Fif Load}
- LoadScale:=Form1.LoadScale.ItemIndex; {Fif Scale}
- LoadWidth:=Form1.LoadWidth.Value; {Fif Width}
- LoadHeight:=Form1.LoadWidth.Value; {Fif Height}
- end;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {This Function returns the Integer format number of a given file name. Pass any
- file path and name to the function and it will return the ImageFormat Number.
- Note the use of 'Result'. Any function you create by default has an in-built
- 'Result' variable. So when you call a function your result is automatically
- available for use.
-
- For example:-
-
- ImageType.ItemIndex := GetImageFormat(OpenDialog.Filename);
-
- The above sets the ItemIndex to the value as returned by the function. Note how we
- pass the function a string - eg a 'FileName' returned by a Common Dialog Control}
- function GetImageFormat(FileName:String):Integer;
- Var
- Temp:String;
- begin
- Temp := UpperCase(Filename);{Convert FileName to Upper Case}
- Result:=0;{Default result if no recognised match is found - *.*}
- if Pos('.TIF', Temp ) > 0 then Result:= 1;
- if Pos('.TGA', Temp ) > 0 then Result:= 2;
- if Pos('.BMP', Temp ) > 0 then Result:= 3;
- if Pos('.GIF', Temp ) > 0 then Result:= 4;
- if Pos('.DIB', Temp ) > 0 then Result:= 5;
- if Pos('.PCX', Temp ) > 0 then Result:= 6;
- if Pos('.JPG', Temp ) > 0 then Result:= 7;
- if Pos('.MSP', Temp ) > 0 then Result:= 8;
- if Pos('.FIF', Temp ) > 0 then Result:= 9;
- if Pos('.PNG', Temp ) > 0 then Result:= 10;
- end;
-
- {-------------------------------------------------------------------------------}
- {Pass any integer value, and the function returns true if it is a valid file.
- For our demo above pass the Picbufs ImageFormat Property - If the set format
- can be used with 'CallBacks' the function returns true.}
- function ValidCallBack(ImageFormatIndex:Integer):Boolean;
- begin
- Result:=False; {Default is False - Non Valid}
- if ImageFormatIndex = 4 then Result := True; {GIF}
- if ImageFormatIndex = 7 then Result := True; {JPG}
- if ImageFormatIndex = 9 then Result := True; {PNG}
- if ImageFormatIndex = 10 then Result := True; {PNG}
- end;
-
- {-------------------------------------------------------------------------------}
- {This event is triggered by the picbuf control - lines represents the Image line
- number being processed}
- procedure TForm1.Picbuf1Callback(Sender: TObject; var Lines: Smallint);
- begin
- try {catch exceptions}
- LineNumber.Caption:=InttoStr(lines); {Update Label Caption to the Line Number}
- ProgressBar.Position:=Lines; {Update Progress Bar}
- Form1.Update;
- Application.ProcessMessages; {Paint Screen}
- except
- end;
- end;
-
- {-------------------------------------------------------------------------------}
- {For the Purposes of the Demo, you should set the number of lines within the Image
- you are loading using CallBack, if you wish the Status bar to read correctly from
- 0 to 100% Complete. For Normal operation you set the Max Value equal to the
- Y - Resolution of the Image to be Loaded}
- procedure TForm1.BarMaxChange(Sender: TObject);
- begin
- try
- ProgressBar.Max := BarMax.Value; {Set Max or 100% Reading of the progress bar}
- ProgressBar.Position := BarMax.Value; {Set Bar to 100% position}
- except
- end;
- end;
-
- {-------------------------------------------------------------------------------}
- {Exit Application}
- procedure TForm1.Exit1Click(Sender: TObject);
- begin
- Halt;
- end;
-
- procedure TForm1.FormActivate(Sender: TObject);
- begin
- PicBuf1.Scrollbars := SB_Both;
- end;
-
- end.
-