屏幕保护程序是SCR文件,其实质就是一个标准的可执行文件。SCR文件由两部份组成:保护程序执行界面、保护程序设置界面。最后为了使保护程序能够按照用户以前设置的参数运行,笔者采用了注册表技术来保存用户的设置信息。现以制作一个屏幕保护程序为例介绍如下:
1.在C++ Builder 5.0中建立一个含有两个表单Form1和Form2 的工程文件Project1。其Form1用来设置运行参数,Form2用来执行屏幕保护程序。
在Form1中添加如下控件:
name caption label1 运行速度 button1 确定 button2 取消 edit1
在Form2中添加如下控件:
name property value timer1 interval 10
2.在Unit1.h中添加如下代码:
#include "registr.hpp"
private:
TRegistry MyReg;
3.在TForm1的OnShow事件中添加如下代码:
void __fastcall TForm1::Form1Show(TObject Sender)
{
MyReg=new TRegistry;
MyReg-〉RootKey=HKEY_LOCAL_MACHINE;
MyReg-〉OpenKey("Software\\MyCompany\\Remember",true);
Edit1-〉Text=MyReg-〉ReadInteger("Interval");//从注册表中读取屏幕保护程序的时间设置信息//
}
4.在Form1的Button1的Click事件中添加如下代码:
void __fastcall TForm1::Button1Click(TObject Sender)
{
MyReg-〉WriteInteger("Intervl",Edit1-〉Text.ToInt( ));
MyReg-〉Free( );//向注册表中写入屏幕保护程序的时间信息//
Close( );
}
5.在Form1的Button2的Click事件中添加如下代码:
void __fastcall TForm1::Button2Click(TObject Sender)
{
Close( );
}
6.在Unit2.h中添加如下代码:
Public:
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_ERASEBKGND,TWMEraseBkgnd,WMEraseBkgnd)
MESSAGE_HANDLER(WM_ACTIVATE,TWMActivate,WMActivate)
MESSAGE_HANDLER(WM_SYSCOMMAND,TWMSysCommand,WMSysCommand)
END_MESSAGE_MAP(TForm)
//定义要处理的Windows消息//
int x1,x2,x3 ,x4,x5,x6;
private:
void __fastcall CreateParams(TCreateParams &&Params);
void __fastcall WMEraseBkgnd(TWMEraseBkgnd &&Msg);
void __fastcall WMActivate(TWMActivate &&Msg);
void __fastcall WMSysCommand(TWMSysCommand &&Msg);
//用以响应和处理Windows消息//
Graphics::TBitmap ScreenBitmap;//屏幕保护程序的背景颜色//
7.在Form2的OnCreate事件中添加如下代码:
void __fastcall TForm2::Form2Create(TObject Sender)
{
Left = 0;
Top = 0;
Width = Screen-〉Width;
Height= Screen-〉Height;
// 使窗口的大小为整个屏幕的大小//
Cursor = crNone;
//隐藏屏幕光标//
ScreenBitmap-〉Width = Width;
ScreenBitmap-〉Height = Height;
SreenBitmap-〉Canvas-〉Brush-〉Color = clBlack;
//设置屏幕背景大小和颜色//
ScreenBitmap-〉Canvas-〉FillRect(Rect(0,0,Width,Height));
Timer1-〉Enabled = true;
Form1-〉MyReg-〉OpenKey("Software\\MyCompany\\Remember",true);
Timer1-〉Interval=Form1-〉MyReg-〉ReadInteger("Interval");
x4=10;
x5=20;
x6=30;
}
8.在Form2的OnClose事件中添加如下代码:
void __fastcall TForm2::Form2Close(TObject Sender)
{
Timer1-〉Enabled = false;
delete ScreenBitmap;
}
9.在Form2的OnKeyDown 、OnMouseDown、OnMouseMoves事件中添加如下代码:
void __fastcall TForm2::Form2KeyDown(TObject Sender, WORD &&Key,
TShiftState Shift)
{Close( );
MyReg-〉Fee( );}
void __fastcall TForm2::Form2MouseDown(TObject Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{Close( );
MyReg-〉Free( );}
void __fastcall TForm2::Form2MouseMove(TObject Sender, TShiftState Shift, int X,int Y)
{
Close( );
MyReg-〉Free ( );
}//如果发生鼠标移动、键盘按键关闭屏幕保护程序//
10.处理Windows的WM_ERARSEBKGND消息:
void __fastcall TForm2::WMEraseBkgnd(TWMEraseBkgnd &&Msg)
{
Msg.Result = false;
//不刷新屏幕背景//
}
11.处理Windows的WM_ACTIVE消息:
void __fastcall TForm2::WMActivate(TWMActivate &&Msg)
{
if(Msg.Active == false)
Close();
//当关闭屏幕保护后,程序自动退出//
}
12.处理Windows的WM_SYSCOMMAND消息:
void __fastcall TForm1::WMSysCommand(TWMSysCommand &&Msg)
{
if(Msg.CmdType == SC_SCREENSAVE)
Msg.Result = true;
//防止屏幕保护程序被重复执行//
else
TForm::Dispatch(&&Msg);
//程序把消息发往其他对象//
13.在Timer1的Timer事件中添加如下代码:
void __fastcall TForm1::Timer1Timer(TObject Sender)
{
x1=Random(Screen.Height);
x2=Random(Screen.Weight);
x3=Random(Screen.Height);
Canvas-〉pen-〉Color=RGB(0,0,0);
Canvas-〉Pen-〉Width=10;
Canvas-〉Brush-〉Color=RGB(0,0,0);
Canvas-〉Ellipse(x4,x5,abs(x4-x6),abs(x5-x6));
Canvas-〉Pen-〉Color=RGB(Ranndom(255),Random(255),Random(255));
Canvas-〉Pen-〉Width=10;
Canvas-〉Brush-〉Color=RGB(Ranndom(255),Random(255),Random(255));
Canvas-〉Ellipse(x1,x2,abs(x1-x3),abs(x2-x3));
x4=x1;
x5=x2;
x6=x3;
//屏幕保护效果,用户可以自己定义//
}
14.在Form2的OnPaint事件中添加如下代码:
void __fastcall TForm1::FormPaint(TObject Sender)
{
Canvas-〉Draw(0,0,ScreenBitmap);//重画背景//
}
15.在Winmain函数中修改如下代码,注意新添加的代码:
{
try{
Application-〉Initialize( );
if(paramstr(1)=="/s")
Application-〉CreateForm(Classid(TForm1),&&Form1);
//显示屏幕保护程序设置界面//
else
Application-〉CreateForm(Classid(TForm2),&&Form2);
//显示屏幕保护程序执行界面//
Applcation-〉Run();
}
}
以上代码在Windows 98、C++ Builder 5.0中运行通过。
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/