⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cpp

📁 电脑屏保程序
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "MAINFORM.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
#pragma resource "PICTURES.res"

const int MouseMoveDistance = 10;

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}

void __fastcall TForm1::CreateParams(TCreateParams &Params)
{
  TForm::CreateParams(Params);     // call base class first
  Params.ExStyle |= WS_EX_TOPMOST; // then modify the style.
}


//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Left = 0;                 // make the form fill
  Top  = 0;                 // the whole screen
  Width = Screen->Width;
  Height= Screen->Height;
  Cursor = crNone;          // hide the cursor.

  POINT WinPoint;           // find the mouse cursor
  GetCursorPos(&WinPoint);  // using api function, store
  AnchorPoint.x=WinPoint.x; // result for measuring
  AnchorPoint.y=WinPoint.y; // mouse movement

  ScreenBitmap    = new Graphics::TBitmap; // memory bitmap for the
  SpaceShipBitmap = new Graphics::TBitmap; // screen and a spaceship.
  ScreenBitmap->Width  = Width;            // size the screenbitmap
  ScreenBitmap->Height = Height;           // load spaceship from resource
  SpaceShipBitmap->LoadFromResourceName((int)HInstance,"SHIP");

  ShipRect = Rect(0,0,      // size of ship never changes, calculate its
                  SpaceShipBitmap->Width,   // size once for use in many
                  SpaceShipBitmap->Height); // functions

  XSpeed     = YSpeed     = 2;     // initialize data members
  XDirection = YDirection = 1;
  Location.x = Location.y = 0;
  OldLocation = Location;
  Timer1->Enabled = true;
  // blackout the memory bitmap.
  ScreenBitmap->Canvas->Brush->Color = clBlack;
  ScreenBitmap->Canvas->FillRect(Rect(0,0,Width,Height));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  Timer1->Enabled = false;
  delete SpaceShipBitmap;
  delete ScreenBitmap;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
	TShiftState Shift)
{
  Close();	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
	TShiftState Shift, int X, int Y)
{
  Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X,
	int Y)
{
  // calculate how far the mouse has been moved
  // from its original point.
  int XDistance = X - AnchorPoint.x;
  int YDistance = Y - AnchorPoint.y;

  if ((XDistance < -MouseMoveDistance) || (XDistance > MouseMoveDistance))
   Close();
  else if ((YDistance < -MouseMoveDistance) || (YDistance > MouseMoveDistance))
    Close();
}

void __fastcall TForm1::WMEraseBkgnd(TWMEraseBkgnd &Msg)
{                     // tell windows to never mind about the
  Msg.Result = false; // background, we've got it covered.
}

void __fastcall TForm1::WMActivate(TWMActivate &Msg)
{
  if(Msg.Active == false) // if we are deactivated for
    Close();              // some reason, close down.
}
void __fastcall TForm1::WMSysCommand(TWMSysCommand &Msg)
{                                  // SC_SCREENSAVE means that
  if(Msg.CmdType == SC_SCREENSAVE) // windows is trying to start the
    Msg.Result = true;             // screensaver twice, don't let it
  else
    TForm::Dispatch(&Msg);                // other system commands can be
}                                  // handled as usual

//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  PatchBackground();      // repair the memory bitmap
  MoveShipLocation();     // calculate new location of ship
  DrawShipOnBackground(); // paint ship on memory bitmap
  MoveBitmapToScreen();   // copy pixels from memory to screen.
}

void TForm1::PatchBackground(void)
{
  // blacken in pixels where the ship is
  // currently located.
  ScreenBitmap->Canvas->Brush->Color = clBlack;
  ScreenBitmap->Canvas->FillRect(LocationRect);
}


void TForm1::MoveShipLocation(void)
{
  // save old location
  OldLocation = Location;
  // calculate new location.
  Location.x+=XSpeed*XDirection; // direction is
  Location.y+=YSpeed*YDirection; // either 1 or -1

  // must determine when the ship hits the walls. easy calculation
  // on the left, on the right, must take into account ship's width
  if (Location.x < 0 || Location.x > ScreenBitmap->Width-ShipRect.Right)
  {
    XDirection *= -1;   // if wall collision detected, change direction
    if(Location.x<0)    // bounce the ship back into the client area
      Location.x *= -1; // bounce the ship back in as far as it would
    else                // have travelled if the wall without the wall
      Location.x=2*ScreenBitmap->Width-2*ShipRect.Right-Location.x;
  }
  // detect up down wall collisions just like left right collisions
  if (Location.y < 0 || Location.y > ScreenBitmap->Height-ShipRect.Bottom)
  {
    YDirection *= -1;
    if(Location.y<0)
      Location.y *= -1;
    else
      Location.y=2*ScreenBitmap ->Height-2*ShipRect.Bottom-Location.y;
  }

  // update the rect variable
  LocationRect = Rect(Location.x,Location.y,
                      Location.x+SpaceShipBitmap->Width,
                      Location.y+SpaceShipBitmap->Height);
}


void TForm1::DrawShipOnBackground(void)
{
  // copy the ship into its new location
  // in the memory bitmap.
  ScreenBitmap->Canvas->CopyMode = cmSrcCopy; // DEST = SRC
  ScreenBitmap->Canvas->CopyRect(LocationRect,
                                 SpaceShipBitmap->Canvas,
                                 ShipRect);
}

void TForm1::MoveBitmapToScreen(void)
{
  // the screen is big. Rather than move all pixels from
  // memory to the screen, its more effecient to only
  // move the regions that have changed.
  //
  // for example, an 800x600 screen has 480,000 pixels
  // this code copies only 2*(57*23) = 2622 pixels rather
  // than all 480,000

  // first copy in the pixels where the ship used to be
  TRect OldRect = Rect(OldLocation.x,OldLocation.y,
                       OldLocation.x+SpaceShipBitmap->Width,
                       OldLocation.y+SpaceShipBitmap->Height);
  Canvas->CopyRect(OldRect,
                   ScreenBitmap->Canvas,
                   OldRect);

  // then copy in pixels where the ship is now.
  Canvas->CopyRect(LocationRect,
                   ScreenBitmap->Canvas,
                   LocationRect);
}

//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
  Canvas->Draw(0,0,ScreenBitmap);	
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -