www.pudn.com > C++Builder6.0To-screensave.rar > screen.cpp


//--------------------------------------------------------------------------- 
#include  
#pragma hdrstop 
 
#include "screen.h" 
#include  
#include  
#include  
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 
TScreenForm *ScreenForm; 
 
//--------------------------------------------------------------------------- 
__fastcall TScreenForm::TScreenForm(TComponent* Owner) 
        : TForm(Owner) 
{ 
} 
//--------------------------------------------------------------------------- 
void __fastcall TScreenForm::WMEraseBkgnd(TWMEraseBkgnd & Msg) 
{ 
     Msg.Result = false;  //禁止重画时擦除背景 
} 
//--------------------------------------------------------------------------- 
void __fastcall TScreenForm::WMActivate(TWMActivate & Msg) 
{ 
     if (Msg.Active==false) Close();   //失去激活时退出 
} 
//--------------------------------------------------------------------------- 
void __fastcall TScreenForm::WMSysCommand(TWMSysCommand & Msg) 
{ 
     if(Msg.CmdType==SC_SCREENSAVE)   //若有屏幕保护程序运行,则不再运行 
          Msg.Result=true; 
     else 
          ScreenForm->Dispatch(&Msg); 
} 
//--------------------------------------------------------------------------- 
void __fastcall TScreenForm::FormDestroy(TObject *Sender) 
{ 
        Timer1->Enabled=false; 
        delete Bit1; 
        delete Bit2; 
        delete tmp; 
        delete PicList; 
        delete SndList; 
        delete Reg; 
} 
//--------------------------------------------------------------------------- 
void __fastcall TScreenForm::FormKeyDown(TObject *Sender, WORD &Key, 
      TShiftState Shift) 
{ 
        Close(); 
} 
//--------------------------------------------------------------------------- 
void __fastcall TScreenForm::FormMouseDown(TObject *Sender, TMouseButton Button, 
      TShiftState Shift, int X, int Y) 
{ 
        Close(); 
} 
//--------------------------------------------------------------------------- 
void __fastcall TScreenForm::FormMouseMove(TObject *Sender, TShiftState Shift, 
      int X, int Y) 
{ 
     int xDistance=X-aPoint.x; 
     int yDistance=Y-aPoint.y; 
 
     if ((xDistance<-MouseMoveDistance)||(xDistance>MouseMoveDistance)) 
          Close(); 
     else if ((yDistance<-MouseMoveDistance)||(yDistance>MouseMoveDistance)) 
          Close(); 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TScreenForm::FormPaint(TObject *Sender) 
{ 
     Canvas->Draw(0,0,tmp); 
} 
//--------------------------------------------------------------------------- 
void __fastcall TScreenForm::PlayPic() 
{ 
     switch (flag){ 
 
 
     case 1:     // 从上方推出 
          if (xCanvas->CopyRect(Rect(0,0,BmpW,x), 
                    Bit2->Canvas, 
                    Rect(0,BmpH-x,BmpW,BmpH)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               x+=2; 
          } 
          else ChangePic=true; 
          break;/**/ 
     case 5: // 从下方推出 
          if (xCanvas->CopyRect(Rect(0,BmpH-x,BmpW,BmpH), 
                    Bit2->Canvas, 
                    Rect(0,0,BmpW,x)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               x+=2; 
          } 
          else ChangePic=true; 
          break; 
     case 10: // 从左边推出 
          if (x<=BmpW){ 
               Bit1->Canvas->CopyRect(Rect(0,0,x,BmpH), 
                    Bit2->Canvas, 
                    Rect(BmpW-x,0,BmpW,BmpH)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               x+=2; 
          } 
          else ChangePic=true; 
          break; 
     case 14: // 从右边推出 
          if (x<=BmpW){ 
               Bit1->Canvas->CopyRect(Rect(BmpW-x,0,BmpW,BmpH), 
                    Bit2->Canvas, 
                    Rect(0,0,x,BmpH)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               x+=2; 
          } 
          else ChangePic=true; 
          break; 
     case 6: // 垂直交错 
          if (x<=BmpH){ 
               y=x; 
               while (y >0){ 
               Bit1->Canvas->CopyRect(Rect(0,y-1,BmpW,y), 
                    Bit2->Canvas, 
                    Rect(0,BmpH-x+y-1,BmpW,BmpH-x+y)); 
               Bit1->Canvas->CopyRect(Rect(0,BmpH-y,BmpW,BmpH-y+1), 
                    Bit2->Canvas, 
                    Rect(0,x-y,BmpW,x-y+1)); 
               y-=2; 
               } 
               Canvas->Draw(Lft,Tp,Bit1); 
               x+=2; 
          } 
          else ChangePic=true; 
          break; 
     case 11:  // 水平交错 
          if (x<=BmpW){ 
               y=x; 
               while (y >0){ 
               Bit1->Canvas->CopyRect(Rect(y-1,0,y,BmpH), 
                    Bit2->Canvas, 
                    Rect(BmpW-x+y-1,0,BmpW-x+y,BmpH)); 
               Bit1->Canvas->CopyRect(Rect(BmpW-y,0,BmpW-y+1,BmpH), 
                    Bit2->Canvas, 
                    Rect(x-y,0,x-y+1,BmpH)); 
               y-=2; 
               } 
               Canvas->Draw(Lft,Tp,Bit1); 
               x+=2; 
          } 
          else ChangePic=true; 
          break; 
     case 2:  // 从中心向四周展开 
          if (x<=BmpW/2){ 
               Bit1->Canvas->CopyRect(Rect(BmpW/2-x,BmpH/2-x*3/4,BmpW/2+x,BmpH/2+x*3/4), 
                    Bit2->Canvas, 
                    Rect(BmpW/2-x,BmpH/2-x*3/4,BmpW/2+x,BmpH/2+x*3/4)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               x++; 
          } 
          else  ChangePic=true; 
          break; 
     case 7:     // 从中心向上下 
          if (x<=BmpH/2){ 
               Bit1->Canvas->CopyRect(Rect(0,BmpH/2-x,BmpW,BmpH/2+x), 
                    Bit2->Canvas, 
                    Rect(0,BmpH/2-x,BmpW,BmpH/2+x)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               /*BitBlt(Canvas->Handle,Lft,Height/2-x,BmpW,2*x, 
                      Bit2->Canvas->Handle,0,BmpH/2-x,SRCCOPY);*/ 
               x++; 
          } 
          else 
          { 
             /*//使用BitBlt时要用此语句避免屏幕闪烁 
             Bit1->Canvas->CopyRect(Rect(0,0,BmpW,BmpH), 
                    Bit2->Canvas, Rect(0,0,BmpW,BmpH)); 
             Canvas->Draw(Lft,Tp,Bit1);  */ 
             ChangePic=true; 
          } 
          break; 
     case 13:     // 从中心向左右 
          if (x<=BmpW/2){ 
               Bit1->Canvas->CopyRect(Rect(BmpW/2-x,0,BmpW/2+x,BmpH), 
                    Bit2->Canvas, 
                    Rect(BmpW/2-x,0,BmpW/2+x,BmpH)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               x++; 
          } 
          else  ChangePic=true; 
          break; 
     case 8: // 从右下角平铺 
          if (x<=BmpW){ 
               Bit1->Canvas->CopyRect(Rect(BmpW-x,BmpH-x*3/4,BmpW,BmpH), 
                    Bit2->Canvas, 
                    Rect(BmpW-x,BmpH-x*3/4,BmpW,BmpH)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               x+=2; 
          } 
          else  ChangePic=true; 
          break; 
     case 4:     // 从左下角平铺 
          if (x<=BmpW) 
          { 
               Bit1->Canvas->CopyRect(Rect(0,BmpH-x*3/4,x,BmpH), 
                    Bit2->Canvas, 
                    Rect(0,BmpH-x*3/4,x,BmpH)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               x+=2; 
          } 
          else  ChangePic=true; 
          break; 
     case 15: 
          HRGN MyRgn; 
          if (x<=sqrt(BmpW*BmpW+BmpH*BmpH)/2+2) 
          { 
          Bit1->Canvas->CopyRect(Rect(0,0,BmpW,BmpH), 
                                  Bit2->Canvas, 
                                  Rect(0,0,BmpW,BmpH)); 
          MyRgn=CreateEllipticRgn(Width/2-x,Height/2-x, 
                                  Width/2+x,Height/2+x); 
          SelectObject(Canvas->Handle,MyRgn); 
          Canvas->Draw(Lft,Tp,Bit1); 
          DeleteObject(MyRgn); 
          x+=2; 
          } 
          else  ChangePic=true;/**/ 
          break; 
     case 12:      // 从左上角平铺 
          if (x<=BmpW) 
          { 
               Bit1->Canvas->CopyRect(Rect(0,0,x,x*3/4), 
                    Bit2->Canvas, 
                    Rect(0,0,x,x*3/4)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               x+=2; 
          } 
          else  ChangePic=true; 
          break; 
     case 0:     // 从右上角平铺 
          if (x<=BmpW) 
          { 
               Bit1->Canvas->CopyRect(Rect(BmpW-x,0,BmpW,x*3/4), 
                    Bit2->Canvas, 
                    Rect(BmpW-x,0,BmpW,x*3/4)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               x+=2; 
          } 
          else  ChangePic=true; 
          break; 
 
     case 9:  // 随机块 
          if (x<300){ 
               int i,j; 
               while (b[i=random(15)][j=random(20)]); //注意此句的含义! 
               Bit1->Canvas->CopyRect(Rect(30*j,30*i,30*j+30,30*i+30), 
                    Bit2->Canvas, 
               Rect(30*j,30*i,30*j+30,30*i+30)); 
               Canvas->Draw(Lft,Tp,Bit1); 
               b[i][j]=true; 
               x++; 
          } 
          else  ChangePic=true; 
          break; 
 
     case 3:  // 百叶窗 
          int w=int((BmpW+19)/20); 
          if (x<=w){ 
               for (y=0;y<20;y++){ 
                    Bit1->Canvas->CopyRect(Rect(y*w+x-1,0,y*w+x,BmpH), 
                         Bit2->Canvas, 
                         Rect(y*w+x-1,0,y*w+x,BmpH)); 
               } 
               Canvas->Draw(Lft,Tp,Bit1); 
               x++; 
          } 
          else  ChangePic=true; 
          break;  /**/ 
     } 
} 
//--------------------------------------------------------------------------- 
void __fastcall TScreenForm::Timer1Timer(TObject *Sender) 
{ 
     switch (Mode){ 
     case 0: 
          RunTime++; 
          if (RunTime%600==0) 
          { 
            RunTime=0; 
            randomize(); 
            Pic=floor(random(PicList->Count)); 
            LoadPic(PicList->Strings[Pic]); 
          } 
          PatchBkgnd(); 
          MoveObject(); 
          DrawObject(); 
          ShowObject(); 
     break; 
     case 1: 
          if (ChangePic){ 
               //randomize(); 
               do { 
                    //flag=random(16);  //随机选用效果 
                    flag=(flag+1)%16;   //顺序选用效果 
               }while (!Effect[flag]); 
               Pic=(Pic+1)%PicList->Count; 
               LoadPic(PicList->Strings[Pic]); 
               ChangePic=false; 
               x=0; 
               if (flag==9) 
                    ZeroMemory(b,sizeof(b)); 
          } 
          PlayPic(); 
     } 
} 
//--------------------------------------------------------------------------- 
void __fastcall TScreenForm::LoadPic(AnsiString FName) 
{ 
     float Ratio,W,H; 
     TImage *tmpImage = new TImage(ScreenForm); 
     tmpImage->AutoSize=true; 
     if (FileExists(FName)){ 
     tmpImage->Picture->LoadFromFile(FName); 
     Ratio = float(tmpImage->Width)/float(tmpImage->Height); 
     W=tmpImage->Width;   H=tmpImage->Height; 
     switch (Mode){ 
     case 0: 
          if (W <=600&&H<= 450) 
               Bit2->Assign(tmpImage->Picture->Graphic); 
          else if (Ratio>4/3){ 
               Bit2->Width=600; 
               Bit2->Height=600/Ratio; 
               Bit2->Canvas->StretchDraw(Rect(0,0,Bit2->Width,Bit2->Height), 
                    tmpImage->Picture->Graphic); 
          } 
          else { 
               Bit2->Height=450; 
               Bit2->Width=450*Ratio; 
               Bit2->Canvas->StretchDraw(Rect(0,0,Bit2->Width,Bit2->Height), 
                    tmpImage->Picture->Graphic); 
          } 
          break; 
     case 1: 
          Bit2->Canvas->Brush->Color=clBlack; 
          Bit2->Canvas->FillRect(Rect(0,0,600,450)); 
          if (W <=600&&H<= 450) 
               Bit2->Canvas->Draw((600-W)/2, 
                    (450-H)/2,tmpImage->Picture->Graphic); 
          else{ 
          if (W>600){ 
               W=600; 
               H=600 / Ratio; 
          } 
          if (H >450){ 
               H = 450; 
               W = H * Ratio; 
          } 
          Bit2->Canvas->StretchDraw(Rect((600-floor(W))/2,(450-floor(H))/2, 
          (600-floor(W))/2+floor(W),(450-floor(H))/2+floor(H)),tmpImage->Picture->Graphic); 
          } 
     } 
     } 
     delete tmpImage; 
} 
//--------------------------------------------------------------------------- 
void TScreenForm::DrawObject(void) 
{ 
     Bit1->Canvas->CopyRect(nRect,Bit2->Canvas,oRect); 
} 
//--------------------------------------------------------------------------- 
void TScreenForm::ShowObject(void) 
{ 
     TRect oRect=Rect(oLocation.x,oLocation.y, 
                    oLocation.x+Bit2->Width, 
                    oLocation.y+Bit2->Height); 
     Canvas->CopyRect(oRect,Bit1->Canvas,oRect); 
     Canvas->CopyRect(nRect,Bit1->Canvas,nRect); 
} 
//--------------------------------------------------------------------------- 
void TScreenForm::MoveObject(void) 
{ 
     oLocation=nLocation; 
     nLocation.x+=xSpeed*xDir; 
     nLocation.y+=ySpeed*yDir; 
     if (nLocation.x<0||nLocation.x>Bit1->Width-oRect.Right) 
     { 
          xDir*=-1; 
          if(nLocation.x<0) 
               nLocation.x*=-1; 
          else 
               nLocation.x=2*Bit1->Width-2*oRect.Right-nLocation.x; 
     } 
     if (nLocation.y<0||nLocation.y>Bit1->Height-oRect.Bottom) 
     { 
          yDir*=-1; 
          if(nLocation.y<0) 
               nLocation.y*=-1; 
          else 
               nLocation.y=2*Bit1->Height-2*oRect.Bottom-nLocation.y; 
     } 
     nRect=Rect( nLocation.x,nLocation.y, 
               nLocation.x+Bit2->Width, 
               nLocation.y+Bit2->Height); 
} 
//--------------------------------------------------------------------------- 
void TScreenForm::PatchBkgnd(void) 
{ 
     Bit1->Canvas->Brush->Color=clBlack; 
     Bit1->Canvas->FillRect(nRect); 
} 
//--------------------------------------------------------------------------- 
void TScreenForm::ReadReg() 
{ 
     Reg=new TRegistry(); 
     Reg->RootKey=HKEY_CURRENT_USER; 
     Reg->OpenKey("\\Software\\ScreenSaverDIY\\", 
          true); 
     if (!Reg->ValueExists("Mode")){ 
          Reg->WriteInteger("Mode",1); 
          Mode=1; 
          Reg->WriteInteger("Speed",3); 
          xSpeed=ySpeed=3; 
          for (int i=0;i<16;i++) 
               Reg->WriteBool("Effect"+IntToStr(i),true); 
     } 
     else { 
          Mode=Reg->ReadInteger("Mode"); 
          xSpeed=ySpeed=Reg->ReadInteger("Speed"); 
          for (int i=0;i<16;i++) 
               Effect[i]=Reg->ReadBool("Effect"+IntToStr(i)); 
     } 
} 
//--------------------------------------------------------------------------- 
void __fastcall TScreenForm::MediaPlayer1Notify(TObject *Sender) 
{ 
     if (MediaPlayer1->NotifyValue==nvSuccessful){ 
          Snd=(Snd+1)%SndList->Count; 
          MediaPlayer1->FileName=SndList->Strings[Snd]; 
          MediaPlayer1->Open(); 
          MediaPlayer1->Play(); 
     } 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TScreenForm::FormCreate(TObject *Sender) 
{ 
     PicList=new TStringList(); 
     SndList=new TStringList(); 
     AnsiString Path=ExtractFilePath(Application->ExeName); 
     PicList->LoadFromFile(Path+"PicList"); 
     SndList->LoadFromFile(Path+"SndList"); 
 
     Bit1=new Graphics::TBitmap; 
     Bit2=new Graphics::TBitmap; 
     tmp=new Graphics::TBitmap; 
     ReadReg(); 
     randomize(); 
     Left=0; Top=0; 
     RunTime=0; 
     Width=Screen->Width; 
     Height=Screen->Height; 
     Cursor=crNone; 
     POINT   WinPoint; 
     GetCursorPos(&WinPoint); 
     //记录鼠标初始位置 
     aPoint.x=WinPoint.x; 
     aPoint.y=WinPoint.y; 
 
     if (Mode==0){ 
          Bit1->Width=Width; 
          Bit1->Height=Height; 
          Bit1->Canvas->Brush->Color=clBlack; 
          Bit1->Canvas->FillRect(Rect(0,0,Width,Height)); 
          Pic=floor(random(PicList->Count));    //floor函数向下取整! 
          LoadPic(PicList->Strings[Pic]); 
          oRect=Rect(0,0,Bit2->Width,Bit2->Height); 
          xDir=yDir=1; 
          nLocation.x=nLocation.y=0; 
          oLocation=nLocation; 
     } 
     else { 
          Bit1->Width=BmpW; 
          Bit1->Height=BmpH; 
          Bit1->Canvas->Brush->Color=clBlack; 
          Bit1->Canvas->FillRect(Rect(0,0,BmpW,BmpH)); 
          Bit2->Assign(Bit1); 
     } 
     tmp->Width=Width; 
     tmp->Height=Height; 
     tmp->Canvas->Brush->Color=clBlack; 
     tmp->Canvas->FillRect(Rect(0,0,Width,Height)); 
     Lft=(Width-600)/2;Tp=(Height-450)/2; 
 
     Timer1->Enabled=true; 
     ChangePic=true; 
     flag=2; Pic=0; x=0; Snd=0; 
     if (SndList->Count>0){ 
          MediaPlayer1->FileName=SndList->Strings[Snd]; 
          MediaPlayer1->Open(); 
          MediaPlayer1->Play(); 
     } 
} 
//---------------------------------------------------------------------------