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

📄 unitcontrol.cpp

📁 用C++Builder开发Windows下的屏保
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <stdlib.h>
#include <Registry.hpp>
#include <jpeg.hpp>
#include "Unitcontrol.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
        : TForm(Owner)
{
  bussy=false;
  stoploop=false;
  stopcls=false;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::Fade(Graphics::TBitmap *Pic,TCanvas *dest)
{
 int style,x,y,devide;
 int picwidth=Pic->Width ;
 int picheight=Pic->Height ;
 TRect rect1,rect2;
 const block=10;
 devide=picheight/block;
 randomize();
 style=random(5);
 x=random(this->ClientWidth-Pic->Width);
 y=random(this->ClientHeight-Pic->Height);
 Timer1->Enabled=false;
 switch (style)
 {
   case 0:
    for(int i=0;i<devide;i++)
    {if(!stoploop){
     rect1=Rect(0,i*block,picwidth,block*(i+1));
     rect2=Rect(x,y+block*i,picwidth+x,y+block*(i+1));
     dest->CopyRect(rect2,Pic->Canvas,rect1);
     Application->ProcessMessages();
     Sleep(100);}
     else
     break;
     }
   break;
   case 1:
     for(int i=devide;i>=0;i--)
     {
     if(!stoploop){
     rect1=Rect(0,i*block,picwidth,block*(i+1));
     rect2=Rect(x,y+block*i,picwidth+x,y+block*(i+1));
     dest->CopyRect(rect2,Pic->Canvas,rect1);
     Application->ProcessMessages();
     Sleep(100); }
     else
     break;
     }
   break;
   case 2:
     for(int i=x;i>=0;i--)
       {if(!stoploop){
        dest->Draw(i,y,Pic);
        y--;
        Application->ProcessMessages();
        Sleep(10);}
        else
        break;
       }
   break;
   case 3:
      for(int i=picwidth/block-1;i>=0;i--)
     {if(!stoploop){
     rect1=Rect(i*block,0,(i+1)*block,picheight);
     rect2=Rect(x+i*block,y,x+(i+1)*block,picheight);
     dest->CopyRect(rect2,Pic->Canvas,rect1);
     Application->ProcessMessages();
     Sleep(100);}
     else
     break;
     }
   break;
  default:
   {dest->Draw(x,y,Pic) ;
    Sleep(100);}
 }

   Timer1->Enabled=true;
}

void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
stoploop=false;
if(picdir=="no"){
Graphics::TBitmap *srcimg;
srcimg=new Graphics::TBitmap();
randomize();
try
{
srcimg->LoadFromResourceID((int)HInstance,random(7));
this->Fade(srcimg,this->Canvas);
this->clsscreen(Image1);
}
catch(...)
{
srcimg->Free();
Timer1->Enabled=true;
}
this->Canvas->FillRect(Rect(0,0,this->ClientWidth,this->ClientHeight));
}
else
{int s=0;
 s=random(File1->Items->Count);
 try
  {
  if(this->Height>Image1->Height&&this->Width>Image1->Width)
  {Image1->Top=rand()%(this->Height-Image1->Height);
  Image1->Left=rand()%(this->Width-Image1->Width);}
  else
  {
  Image1->Top =0;
  Image1->Left=0;
  }
  Image1->Picture->LoadFromFile(File1->Items->Strings[s]);
  this->clsscreen(Image1);
 /*  Image1->Canvas->Brush->Color=clBlack;
 for(int i=0;i<100;i++){
  if(!stoploop){
  Image1->Canvas->Ellipse(random(Image1->ClientWidth),random(Image1->ClientHeight) , random(Image1->Width/10), random(Image1->ClientHeight/10));
  Application->ProcessMessages();
  Sleep(100);
  }*/
 }
catch(...)
{

}
}

}
//---------------------------------------------------------------------------


void __fastcall TMainForm::FormCreate(TObject *Sender)
{
    SetWindowPos(this->Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
    //Make the form exactly cover the screen
    this->Width=Screen->Width;
    this->Height=Screen->Height;
    this->Top=0;
    this->Left=0;
    this->Canvas->Brush->Color=clBlack;
    stoploop=false;
    Version=GetVersion();
    TRegistry *Registry = new TRegistry;
  try
  {
    if(Version>0x80000000){
    Registry->RootKey =HKEY_CURRENT_USER;
    Registry->OpenKey("\\Control Panel\\Desktop",false);
    PWProtect=Registry->ReadInteger("ScreenSaveUsePassword");
    Registry->CloseKey();}
    Registry->RootKey =HKEY_CURRENT_USER;
    if(Registry->OpenKey("\\Software\\CODEHUNTER", false)){
    picdir=Registry->ReadString("PicDir");
    frequence=Registry->ReadInteger("frequence");}
    else
    {
    Registry->CreateKey("\\Software\\CODEHUNTER");
    Registry->WriteString("PicDir","no");
    Registry->WriteInteger("frequence",3);
    picdir="no";
    }
    if(frequence<=0||frequence>6)
     frequence=3;
    if(picdir!="no"){File1->Directory=picdir;
    Timer1->Interval=800*frequence;}
    else
    Timer1->Interval=200*frequence;
  }
  __finally
  {
    delete Registry;
  }
    //Find out if we are running under NT
    if(Version!=0)
    //Disable Ctrl-Alt-Del if appropriate.  NT handles password-protected
    //screen savers at the system level, so this is only needed for non-NT
    //situations with password protection enabled.
    if(PWProtect&&Version>0x80000000)
     SystemParametersInfo(SPI_SCREENSAVERRUNNING, 1, 0, 0);
    //Copy a clone of the desktop onto the form, to
    //serve as a background for the circle painting
    //CopyScreen Me
    //Make the cursor disappear
    while (!ShowCursor(false)< -5); 
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
Close();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{

Close();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
stoploop=true;
stopcls=true;
Timer1->Enabled =false;
if (PWProtect && Version>0x80000000)
      {
        bool PassChck;
        //We need a cursor so that the user can move around
        //the password form
        while(!ShowCursor(True) > 5);
        typedef UINT(CALLBACK *FUN)(HWND);
                   HINSTANCE hDll=LoadLibrary("password.cpl");
                   FUN myfun;
                   if(hDll!=NULL)
                    {
                     myfun=(FUN)GetProcAddress(hDll,"VerifyScreenSavePwd");
                     if(!myfun)FreeLibrary(hDll);
                     else
                     PassChck=myfun(this->Handle);
                    }
        if(PassChck == false)
          {
            Timer1->Enabled =true;
            //Make the cursor disappear again
            while(!ShowCursor(False) < -5);
            CanClose = false;
            //stoploop=false;
           }
      }        
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
while(!ShowCursor(True) > 5);
  //re-enable Ctrl-Alt-Del if disabled
    if(PWProtect&&Version>0x80000000)
        SystemParametersInfo(SPI_SCREENSAVERRUNNING, 0, 0, 0);

}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormMouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{

static int MouseMoves=0;
    MouseMoves = MouseMoves + 1;
    if(MouseMoves >4)
     {
     this->Close();
     MouseMoves = 0 ;
     }
}       

//---------------------------------------------------------------------------


void TMainForm::clsscreen(TImage * image)
{
  int style;
  randomize();
  style=random(5);
  TColor color;
  Timer1->Enabled=false;
  if(bussy==false)
  {
  stopcls=false;
  switch (style)
  {

    case 1:
     bussy=true;
     color=(TColor)RGB(rand()%256,rand()%256,rand()%256);
       image->Canvas->Pen->Color=color;
           image->Canvas->Brush->Color=color;
     for(int i=0;i<=image->ClientWidth/2;i++)
     {
       if(stopcls) break;
       Application->ProcessMessages();
       image->Canvas->MoveTo(i,0);
       image->Canvas->LineTo(i,image->ClientHeight);
       image->Canvas->MoveTo(image->ClientWidth-i,0);
       image->Canvas->LineTo(image->ClientWidth-i,image->ClientHeight);
     }
     break;
    case 2:  //从中间到左右两边清屏
     bussy=true;
     color=(TColor)RGB(rand()%256,rand()%256,rand()%256);
     image->Canvas->Pen->Color=color;
     image->Canvas->Brush->Color=color;
     for(int i=image->ClientWidth/2;i>=0;i--)
     {
      if(stopcls) break;
      Application->ProcessMessages();
       image->Canvas->MoveTo(i,0);
       image->Canvas->LineTo(i,image->ClientHeight);
       image->Canvas->MoveTo(image->ClientWidth-i,0);
       image->Canvas->LineTo(image->ClientWidth-i,image->ClientHeight);
     }
     break;
    case 3: //从上(顶)下(低)到中间清屏
     bussy=true;
      color=(TColor)RGB(rand()%256,rand()%256,rand()%256);
      image->Canvas->Pen->Color=color;
        image->Canvas->Brush->Color=color;
     for(int i=0;i<=image->ClientHeight/2;i++)
     {
     if(stopcls) break;
      Application->ProcessMessages();
       image->Canvas->MoveTo(0,i);
       image->Canvas->LineTo(image->ClientWidth,i);
       image->Canvas->MoveTo(0,image->ClientHeight-i);
       image->Canvas->LineTo(image->ClientWidth,image->ClientHeight-i);
     }
     break;
    case 4: //从上(顶)下(低)到中间清屏
     bussy=true;
      color=(TColor)RGB(rand()%256,rand()%256,rand()%256);
      image->Canvas->Pen->Color=color;
      image->Canvas->Brush->Color=color;
     for(int i=image->ClientHeight/2;i>=0;i++)
     {
     if(stopcls) break;
      Application->ProcessMessages();
       image->Canvas->MoveTo(0,i);
       image->Canvas->LineTo(image->ClientWidth,i);
       image->Canvas->MoveTo(0,image->ClientHeight-i);
       image->Canvas->LineTo(image->ClientWidth,image->ClientHeight-i);
     }
     break;
    default:  //菱形清屏,从四角向中心
      bussy=true;
       color=(TColor)RGB(rand()%256,rand()%256,rand()%256);
       image->Canvas->Pen->Color=color;
       image->Canvas->Brush->Color=color;
       for(int i=0 ;i<image->ClientWidth;i+=30)
        {
          if(stopcls) break;
           for(int j=0;j<=image->ClientHeight;j+=30*image->ClientHeight/image->ClientWidth)
           {
            if(stopcls) break;
            Application->ProcessMessages();
             image->Canvas->MoveTo(i,0);
             image->Canvas->LineTo(0,j);
             image->Canvas->MoveTo(image->ClientWidth-i,image->ClientHeight);
             image->Canvas->LineTo(image->ClientWidth,image->ClientHeight-j);
             image->Canvas->MoveTo(0,image->ClientHeight-j);
             image->Canvas->LineTo(i,image->ClientHeight);
             image->Canvas->MoveTo(image->ClientWidth-i,0);
             image->Canvas->LineTo(image->ClientWidth,image->ClientHeight-j);
           }
          }
   }
  }
  bussy=false;
  Timer1->Enabled =true;
}
void __fastcall TMainForm::Image1MouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
static int MouseMoves=0;
    MouseMoves = MouseMoves + 1;
    if(MouseMoves >4)
     {
     this->Close();
     MouseMoves = 0 ;
     }        
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
Close();        
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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