home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / chap01 / howto01 / delphi10 / ccsavrsu.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-08-18  |  7.5 KB  |  229 lines

  1. unit Ccsavrsu;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Buttons, ExtCtrls;
  8.  
  9. type
  10.   TSetupDialog = class(TForm)
  11.     AcceptButton: TBitBtn;
  12.     CancelButton: TBitBtn;
  13.     TestButton: TBitBtn;
  14.     Image1: TImage;
  15.     Label1: TLabel;
  16.     Edit1: TEdit;
  17.     Label2: TLabel;
  18.     Button1: TButton;
  19.     ScrollBar1: TScrollBar;
  20.     Label3: TLabel;
  21.     OpenDialog1: TOpenDialog;
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure AcceptButtonClick(Sender: TObject);
  24.     procedure CancelButtonClick(Sender: TObject);
  25.     procedure TestButtonClick(Sender: TObject);
  26.     procedure ScrollBar1Change(Sender: TObject);
  27.     procedure Edit1Exit(Sender: TObject);
  28.     procedure Button1Click(Sender: TObject);
  29.   private
  30.     { Private declarations }
  31.   public
  32.     { Public declarations }
  33.     TheImageFileName : String; { This holds the image filename and path }
  34.     TheImageSpeed : Integer;   { This holds how fast to move the image  }
  35.     procedure ReadTheIniFile;  { This reads in the configuration info   }
  36.     procedure WriteTheIniFile; { This writes out the configuration info }
  37.   end;
  38.  
  39. var
  40.   SetupDialog: TSetupDialog;
  41.  
  42. implementation
  43.  
  44. {$R *.DFM}
  45.  
  46. uses
  47.   Ccscreen , { Actual screen saver unit    }
  48.   CCErrors , { CIUPKC Error dialogs unit   }
  49.   IniFiles;  { INI Files manipulation unit }
  50.  
  51. const
  52.   IniFileName = 'CCSAVER.INI';  { Make this a constant to save stack space }
  53.  
  54. { This procedure reads in the configuration from an INI file }
  55. procedure TSetupDialog.ReadTheIniFile;
  56. var
  57.    { The excellent TIniFile object handles the complexity of Windows INI files! }
  58.    TheIniFile : TIniFile;
  59. begin
  60.   { Combine assignfile and reset into one call with create }
  61.   TheIniFile := TIniFile.Create( IniFileName );
  62.   { Use a try block to make sure the ini file is closed by free call }
  63.   try
  64.     { Use the Tinifile object's read methods to get the configuration data }
  65.     with TheIniFile do
  66.     begin
  67.       { Use readinteger to get the speed to move the image }
  68.       TheImageSpeed := ReadInteger( 'Setup', 'ImageSpeed', 0 );
  69.       { Use readstring to get the image filename }
  70.       TheImageFilename := ReadString( 'Setup', 'ImageFileName', '' );
  71.     end;
  72.   finally
  73.     { Regardless of success or failure, free the TIniFile object }
  74.     TheIniFile.Free;
  75.   end;
  76. end;
  77.  
  78. { This procedure writes out the configuration to an INI file }
  79. procedure TSetupDialog.WriteTheIniFile;
  80. var
  81.    { Again use the very useful TIniFile object to encapsulate Windows INI files }
  82.    TheIniFile : TIniFile;
  83. begin
  84.   { Combine an AssignFile and Reset call into the create method }
  85.   TheIniFile := TIniFile.Create( IniFileName );
  86.   try
  87.     { Use the TIniFile object's methods to write out the data }
  88.     with TheIniFile do
  89.     begin
  90.       { Use writeinteger to send the image movement speed }
  91.       WriteInteger( 'Setup' , 'ImageSpeed' , TheImageSpeed );
  92.       { Use writestring to send the image file name }
  93.       WriteString( 'Setup' , 'ImageFileName' , TheImageFileName );
  94.     end;
  95.   finally
  96.     { Regardless of above calls, free the TIniFile object }
  97.     TheIniFile.Free;
  98.   end;
  99. end;
  100.  
  101. { Delphi wrote this; put in the call to initialize from the setup INI file }
  102. procedure TSetupDialog.FormCreate(Sender: TObject);
  103. begin
  104.   { Load in the setup }
  105.   ReadTheIniFile;
  106.   Scrollbar1.position := TheImageSpeed;
  107.   {$I+}
  108.   { Use a try construct to make sure errors are trapped }
  109.   try
  110.     { Try to load the image }
  111.     Image1.Picture.LoadFromFile( TheImageFileName );
  112.     { If successful reset the stored filename }
  113.   except
  114.     { Since the file exists, only possible error is invalid format }
  115.     on EInvalidGraphic do
  116.     begin
  117.       { Signal error and make no further changes }
  118.       ErrorDialog( 'File ' + TheImageFileName + ' Is Not A Valid Image!' );
  119.     end;
  120.   end;
  121. end;
  122.  
  123. { Delphi wrote this; put in the call to save out the configuration data }
  124. { and close the main form, thereby exiting the configuration program    }
  125. procedure TSetupDialog.AcceptButtonClick(Sender: TObject);
  126. begin
  127.   { Write out the setup }
  128.   WriteTheIniFile;
  129.   { Close and exit }
  130.   Close;
  131. end;
  132.  
  133. { Delphi wrote this; put in call to close form and exit without saving }
  134. procedure TSetupDialog.CancelButtonClick(Sender: TObject);
  135. begin
  136.   { Just close and exit since this is a cancel call }
  137.   Close;
  138. end;
  139.  
  140. { Delphi wrote this; put in the call to display the actual screen saver form }
  141. procedure TSetupDialog.TestButtonClick(Sender: TObject);
  142. begin
  143.   { Show the screen saver form, thereby invoking the screen saver behavior! }
  144.   CCScreenSaverForm.Show;
  145. end;
  146.  
  147. { Delphi wrote this: put in the call to reset the label caption when the }
  148. { scrollbar position changes.                                            }
  149. procedure TSetupDialog.ScrollBar1Change(Sender: TObject);
  150. begin
  151.   { Reset the caption to reflect current scrollbar settings }
  152.   Label3.Caption := 'Image Speed = ' + IntToStr( ScrollBar1.Position );
  153.   TheImagespeed := ScrollBar1.position;
  154. end;
  155.  
  156. { Delphi wrote this; put in the code to check for a valid image filename; }
  157. { if one is not found signal error(s); otherwise put it in the image and  }
  158. { change the filename variable.                                           }
  159. procedure TSetupDialog.Edit1Exit(Sender: TObject);
  160. begin
  161.   { Only do this is the edit control text was modified }
  162.   if Edit1.modified then
  163.   begin
  164.     { If the file does not exist then signal error and exit }
  165.     if not FileExists( Edit1.Text ) then
  166.     begin
  167.       { This is from the CCErrors unit }
  168.       ErrorDialog( 'File ' + Edit1.Text + ' Not Found!' );
  169.     end
  170.     else
  171.     begin
  172.       { Use a try construct to make sure errors are trapped }
  173.       try
  174.         { Try to load the image }
  175.         Image1.Picture.LoadFromFile( Edit1.Text );
  176.         { If successful reset the stored filename }
  177.         TheImageFileName := Edit1.Text;
  178.       except
  179.         { Since the file exists, only possible error is invalid format }
  180.         on EInvalidGraphic do
  181.         begin
  182.           { Signal error and make no further changes }
  183.           ErrorDialog( 'File ' + Edit1.Text + ' Is Not A Valid Image!' );
  184.         end;
  185.       end;
  186.     end;
  187.   end;
  188. end;
  189.  
  190. { Delphi wrote this code; put in call to activate the open dialog box }
  191. { and get a bitmap (GIF's will be added in sw version) and test it    }
  192. procedure TSetupDialog.Button1Click(Sender: TObject);
  193. begin
  194.   { Set the default extension and filename for bitmaps in the open dialog }
  195.   OpenDialog1.DefaultExt := 'BMP';
  196.   OpenDialog1.Filename := '*.BMP';
  197.   { If successfully execute the dialog box then make changes }
  198.   if OpenDialog1.Execute then
  199.   begin
  200.     { Get the filename into the edit control }
  201.     Edit1.Text := OpenDialog1.Filename;
  202.     { If the file does not exist then signal error and exit }
  203.     if not FileExists( Edit1.Text ) then
  204.     begin
  205.       { This is from the CCErrors unit }
  206.       ErrorDialog( 'File ' + Edit1.Text + ' Not Found!' );
  207.     end
  208.     else
  209.     begin
  210.       { Use a try construct to make sure errors are trapped }
  211.       try
  212.         { Try to load the image }
  213.         Image1.Picture.LoadFromFile( Edit1.Text );
  214.         { If successful reset the stored filename }
  215.         TheImageFileName := Edit1.Text;
  216.       except
  217.         { Since the file exists, only possible error is invalid format }
  218.         on EInvalidGraphic do
  219.         begin
  220.           { Signal error and make no further changes }
  221.           ErrorDialog( 'File ' + Edit1.Text + ' Is Not A Valid Image!' );
  222.         end;
  223.       end;
  224.     end;
  225.   end;
  226. end;
  227.  
  228. end.
  229.