home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / FORM_UTL / SCRLFM10 / SCRLFORM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-10-29  |  9.9 KB  |  275 lines

  1. unit Scrlform;
  2.  
  3.   Scrolling Form Gallery Component
  4.   John Baumbach                         Delphi 1.0
  5.   email: mantis@vcnet.com               Copyright (c) 10-20-96
  6.   http://www.vcnet.com/mantis              by John Baumbach
  7.  
  8.   This component is 100% free and you are free to use it any
  9.   way you wish.  If you have a use for this component, all I
  10.   ask is that you e-mail me at the above address and let me 
  11.   know.  Thanks!
  12.  
  13.  
  14.   Instructions for using the Scrolling Background template:
  15.  
  16.   Saving in the Form Gallery:
  17.  
  18.   To save this form in the gallery that pops up when you add a new form
  19.   to your project, follow the instructions under Delphi help:  search for
  20.  
  21.            templates ->  Saving a Form as a Template.
  22.  
  23.   This basically tells you to open up this file, then display it's form.  Then,
  24.   right click on the form to bring up the pop-up menu and select "Save as Template".
  25.   For the title, type in "Scrolling Background" and for the description you
  26.   can type in "New Form With a Scrolling Bitmap Background".  I have included
  27.   an icon bitmap called "template.bmp" to use as the thumbnail image.
  28.  
  29.   Using the Scrolling Background form:
  30.  
  31.   This form gets the background image from a resource file, which I've included.
  32.   When you select this form from the gallery, this resource file is not copied
  33.   into your project directory.  You need to do this manually.  The resource
  34.   file is called "IMAGES.RES".
  35.  
  36.   Note that this is a seperate file than the resource file "xx.RES" ("xx" is the
  37.   name of your project), which is the project resource file used by Delphi.
  38.   This file is overwritten during the compilation of the program, so you cannot
  39.   store your bitmaps in it.
  40.  
  41.   The file is linked to your executable by the compiler resource directives:
  42.  
  43.      Existing line -->        {$R *.DFM}                                                    {
  44.      Added line -->           {$R IMAGES.RES}                                               {
  45.  
  46.   This will link your resource file to the project during compilation.  The
  47.   resource file has 9 bitmaps in it.  You can edit these bitmaps with the
  48.   Delphi Image Editor or create your own resource file with other images.
  49.  
  50.   Changing the sign of "xmovement" and "ymovement" changes the scrolling
  51.   direction of the bitmap.  The absolute value of these varibles is the number
  52.   of pixels the bitmap moves per timer firing.  The form has not been debugged
  53.   at values greater than one, so increase these at your own risk!
  54.  
  55.   Change the line:
  56.  
  57.     SetImage('BITMAP_1'); 
  58.  
  59.   in the OnCreate method to load other bitmaps.  There are 19 bitmaps in the resource file,
  60.   ordered sequentially from BITMAP_1 to BITMAP_19.
  61.  
  62.   Other features:
  63.  
  64.   The form has an exception handling procedure built in to handle any errors
  65.   during the form's run.  You can take this out without affecting the form.
  66.  
  67.   The background bitmap name and scrolling direction are declared as constants.
  68.   You should make these variables if you wish to change the background during
  69.   run time.
  70.  
  71.   If you just want a background without it scrolling, you can remove all the
  72.   timer code.  Be sure to leave the resizing and painting code intact.
  73.  
  74.   Possible improvements:
  75.  
  76.   Some components don't look too good on the form itself, such as labels.  If you
  77.   need to use a label on your form, I would recommend putting it on (in??) a
  78.   panel component so you can see it before it's overwritten.
  79.  
  80.   My demo version of this form allows you to load a bitmap from a file during
  81.   runtime for use as a background.  It also allows you to include a bitmap file
  82.   name on the command line and it starts up with that bitmap as a background.
  83.  
  84.   Performance:
  85.  
  86.   The background scrolls smoothly with a full screen form on a 486-100mhz
  87.   running Windows95 w/32 meg RAM using the default settings.  Decreasing the
  88.   timer interval can speed up the scrolling, but may degrade performance when
  89.   the form is full screen.
  90.  
  91.   Disclaimer:
  92.  
  93.   This component is provided free of charge, and you are free to do anything
  94.   with the code presented here.  There is no warranty on this product and the
  95.   author accepts no liability for any damage that may be caused to the user's
  96.   system by this product.  In other words, use at your own risk.
  97.  
  98.   If you have any problems or have any comments you can reach me at:
  99.  
  100.        mantis@vcnet.com     -and-   http://www.vcnet.com/mantis
  101.  
  102.  }
  103.  
  104. interface
  105.  
  106. uses
  107.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  108.   Forms, Dialogs, ExtCtrls, Menus, StdCtrls;
  109.  
  110. type
  111.   Tscrollform = class(TForm)
  112.     backtimer: TTimer;
  113.     procedure FormCreate(Sender: TObject);
  114.     procedure FormDestroy(Sender: TObject);
  115.     procedure GetDIBBMP(Name: string);
  116.     procedure FormPaint(Sender: TObject);
  117.     procedure backtimerTimer(Sender: TObject);
  118.     procedure FormResize(Sender: TObject);
  119.   private
  120.     { Private declarations }
  121.     procedure HandleException(Sender: TObject; E: Exception);
  122.     procedure SetImage(Name: string);
  123.   public
  124.     { Public declarations }
  125.   end;
  126.  
  127. const
  128.   xmovement = 1;            { Scrolls horizontally x pixels per movement}
  129.   ymovement = -1;           { Scrolls vertically x pixels per movement}
  130.   TimerInterval = 100;      { Scroll interval in milliseconds }
  131.  
  132. var
  133.   scrollform: Tscrollform;
  134.   BackImage, FormBitmap: TBitmap;
  135.   xoffset, yoffset: integer;
  136.   DoingResize, NeedToQuit, AmQuitting: boolean;
  137.   BitmapName: string;             { Name of bitmap in resource file }
  138.  
  139. implementation
  140.  
  141. {$R *.DFM}
  142. {$R images.res}
  143.  
  144. procedure Tscrollform.SetImage(Name: string);
  145. begin
  146.     BitmapName:=Name;
  147.     GetDIBBMP(Name);       {Load the bitmap from RES file into 'Backimage'}
  148.     FormResize(Self);      {Create the form background image}
  149. end;
  150.  
  151. procedure Tscrollform.FormCreate(Sender: TObject);
  152. begin
  153.     Application.OnException:=HandleException; {Procedure to handle any exceptions}
  154.     BackImage:= TBitmap.Create;               {Create background image from resource file}
  155.     FormBitmap:= TBitmap.Create;              {Create bitmap to copy to form on repaint calls}
  156.     xoffset:=0; yoffset:=0;                   {Init "FormBitmap" coordinates}
  157.     SetImage('BITMAP_1');                     {Set "Backimage" }
  158. end;
  159.  
  160. procedure Tscrollform.HandleException(Sender: TObject; E: Exception);
  161. begin
  162.     backtimer.enabled:=false;
  163.     MessageDlg('Oops... An exception: ' + E.Message, mtError,
  164.       [mbOk], 0);
  165. end;
  166.  
  167. procedure Tscrollform.FormDestroy(Sender: TObject);
  168. begin
  169.     backtimer.enabled:=false;
  170.     BackImage.Free;
  171.     FormBitmap.Free;
  172. end;
  173.  
  174. procedure Tscrollform.GetDIBBMP(Name: string);
  175. { Code to load DIB from a resource file without palette shift (hopefully).  This
  176.   was obtained from the Borland Delphi Technical Support page at:
  177.  
  178.       http://www.borland.com   }
  179.  
  180. const
  181.   BM = $4D42;  {Bitmap type identifier}
  182. var
  183.   BMF: TBitmapFileHeader;
  184.   HResInfo: THandle;
  185.   MemHandle: THandle;
  186.   Stream: TMemoryStream;
  187.   ResPtr: PByte;
  188.   ResSize: Longint;
  189.   TempName: PChar;
  190. begin
  191.   BMF.bfType := BM;
  192.   {Find, Load, and Lock the Resource containing BITMAP_1}
  193.   TempName:=StrAlloc(Length(Name));
  194.   StrPCopy(TempName, Name);
  195.   HResInfo := FindResource(HInstance, TempName, RT_Bitmap);
  196.   StrDispose(TempName);
  197.   MemHandle := LoadResource(HInstance, HResInfo);
  198.   ResPtr := LockResource(MemHandle);
  199.  
  200.   {Create a Memory stream, set its size, write out the bitmap
  201.    header, and finally write out the Bitmap                  }
  202.   Stream := TMemoryStream.Create;
  203.   ResSize := SizeofResource(HInstance, HResInfo);
  204.   Stream.SetSize(ResSize + SizeOf(BMF));
  205.   Stream.Write(BMF, SizeOf(BMF));
  206.   Stream.Write(ResPtr^, ResSize);
  207.  
  208.   {Free the resource and reset the stream to offset 0}
  209.   FreeResource(MemHandle);
  210.   Stream.Seek(0, 0);
  211.  
  212.   {Create the TBitmap and load the image from the MemoryStream}
  213.   Backimage.LoadFromStream(Stream);
  214.   Stream.Free;
  215. end;
  216.  
  217. procedure Tscrollform.FormPaint(Sender: TObject);
  218. begin
  219.     Canvas.Draw(0 - xoffset, 0 - yoffset, FormBitmap);
  220. end;
  221.  
  222. procedure Tscrollform.backtimerTimer(Sender: TObject);
  223. begin
  224.     { This procedure runs each time the timer inverval arrives.               }
  225.     { It is used to calculate the position of the main bitmap for painting    }
  226.     { on the form.                                                            }
  227.  
  228.     xoffset:=xoffset + xmovement;
  229.     if xmovement > 0 then begin           { if scrolling right to left }
  230.         if xoffset >= BackImage.Width then xoffset:=0;
  231.     end
  232.     else if xmovement < 0 then            { if scrolling left to right }
  233.         if xoffset <= 0 then xoffset:=BackImage.Width;
  234.  
  235.     yoffset:=yoffset + ymovement;
  236.     if ymovement > 0 then begin           { if scrolling bottom to top }
  237.         if yoffset >= BackImage.Height then yoffset:=0;
  238.     end
  239.     else if ymovement < 0 then            { if scrolling top to bottom }
  240.         if yoffset <= 0 then yoffset:=BackImage.Height;
  241.  
  242.     Paint;                                {Repaint the screen}
  243. end;
  244.  
  245. procedure Tscrollform.FormResize(Sender: TObject);
  246. var x, y: integer;
  247. begin
  248.     {Don't want two resizers running at same time}
  249.     if DoingResize then exit;
  250.     DoingResize:=true;
  251.  
  252.     {Set size of "FormBitmap" to size of form, and add size of image
  253.      so the image will be slightly larger than the form canvas.  That
  254.      way "FormBitmap" won't leave any white edges around the form
  255.      when it's scrolled.}
  256.  
  257.     try
  258.        FormBitmap.Width:=Width + BackImage.Width;
  259.        FormBitmap.Height:=Height + BackImage.Height;
  260.     except
  261.        {Bitmaps have been freed, program was trying to exit then timer expired!!!}
  262.        exit;
  263.     end;
  264.  
  265.     {Copy "Backimage" to fill up "FormBitmap" }
  266.     for x:=0 to ((Width div BackImage.Width) + 1) do
  267.        for y:=0 to ((Height div BackImage.Height) + 1) do
  268.           FormBitmap.Canvas.Draw(x * BackImage.Width,
  269.                  y * BackImage.Height, BackImage);
  270.     DoingResize:=false;
  271. end;
  272.  
  273. end.
  274.