home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / Zdroje.exe / JJMain.cpp < prev    next >
C/C++ Source or Header  |  1997-12-28  |  4KB  |  146 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl\vcl.h>
  3. //
  4. // have to add this include for the PlaySound() function
  5. //
  6. #include <vcl\mmsystem.hpp>
  7. #pragma hdrstop
  8.  
  9. #include "JJMain.h"
  10. #pragma package(smart_init)
  11. #pragma resource "*.dfm"
  12. //
  13. // defines for the string resources
  14. //
  15. #define IDS_UP    101
  16. #define IDS_DOWN  102
  17.  
  18. HINSTANCE hInst;
  19.  
  20. TMainForm *MainForm;
  21. //---------------------------------------------------------------------------
  22. __fastcall TMainForm::TMainForm(TComponent* Owner)
  23.   : TForm(Owner),
  24.   done(false)
  25. {
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall TMainForm::FormCreate(TObject *Sender)
  29. {
  30.   // NOTE: The use of the hInst variable is necessary due to
  31.   // problems with the C++Builder beta I was using at the time
  32.   // this was written. The code listing in Day 9 of the book
  33.   // should work as written.
  34.  
  35.   hInst = (HINSTANCE)HInstance;
  36.   //
  37.   // load and display the first bitmap
  38.   //
  39.   Image->Picture->Bitmap->
  40.     LoadFromResourceName((int)hInst, "ID_BITMAP1");
  41. }
  42. //---------------------------------------------------------------------
  43. void __fastcall TMainForm::StartClick(TObject *Sender)
  44. {
  45.   //
  46.   // When the Start button is clicked the animation
  47.   // loop starts. The bitmap resources are named
  48.   // ID_BITMAP1 through ID_BITMAP5 so we'll start with
  49.   // a string called "ID_BITMAP" and append the last
  50.   // digit when needed.
  51.   //
  52.   String s = "ID_BITMAP";
  53.   //
  54.   // a buffer for the string resources
  55.   //
  56.   char buff[10];
  57.   //
  58.   // a flag to let us know when we're done
  59.   //
  60.   done = false;
  61.   //
  62.   // start the loop and keep looping until the 'Stop'
  63.   // button is pressed
  64.   //
  65.   while (!done) {
  66.     //
  67.     // loop through the five bitmaps starting with
  68.     // 1 and ending with 5
  69.     //
  70.     for (int i=1;i<6;i++) {
  71.       //
  72.       // append the value of 'i' to the end of the string
  73.       // to build a string containing the resource name
  74.       //
  75.       String resName = s + String(i);
  76.       //
  77.       // call a class member function to display the bitmap
  78.       //
  79.       DrawImage(resName);
  80.     }
  81.     //
  82.     // load the "Up" string resource using the WinAPI
  83.     // function LoadString(), display the string,
  84.     // and tell Windows to repaint the Label
  85.     //
  86.     LoadString(hInst, IDS_UP, buff, sizeof(buff));
  87.     Label->Caption = buff;
  88.     Label->Refresh();
  89.     //
  90.     // play the 'up' sound using the WinAPI function
  91.     // PlaySound(), play it asynchronously
  92.     //
  93.     PlaySound("ID_WAVEUP",
  94.       hInst, SND_ASYNC | SND_RESOURCE);
  95.     //
  96.     // pause for a moment at the top of the jump
  97.     //
  98.     Sleep(200);
  99.     //
  100.     // repeat all of the above except in reverse
  101.     //
  102.     for (int i=5;i>0;i--) {
  103.       String resName = s + String(i);
  104.       DrawImage(resName);
  105.     }
  106.     PlaySound("ID_WAVEDOWN",
  107.       hInst, SND_ASYNC | SND_RESOURCE);
  108.     LoadString(hInst, IDS_DOWN, buff, sizeof(buff));
  109.     Label->Caption = buff;
  110.     Label->Refresh();
  111.     Sleep(200);
  112.   }
  113. }
  114. //---------------------------------------------------------------------
  115. void __fastcall TMainForm::StopClick(TObject *Sender)
  116. {
  117.   //
  118.   // Stop button pressed, so tell the loop to stop executing
  119.   //
  120.   done = true;
  121. }
  122. //---------------------------------------------------------------------
  123. //
  124. // a class member function to display the bitmap
  125. //
  126. void
  127. TMainForm::DrawImage(String& name)
  128. {
  129.   //
  130.   // load the bitmap from a resource
  131.   // using the name passed to us
  132.   //
  133.   Image->Picture->Bitmap->
  134.     LoadFromResourceName((int)hInst, name);
  135.   //
  136.   // must pump the message loop so that Windows gets
  137.   // a chance to display the bitmap
  138.   //
  139.   Application->ProcessMessages();
  140.   //
  141.   // take a short nap so the animation doesn't go too fast
  142.   //
  143.   Sleep(20);
  144. }
  145. //---------------------------------------------------------------------
  146.