📄 screen.cpp
字号:
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();
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -