1.新建一个对话框的MFC工程;
2.将对话框的
Border属性设置为
None;这是为了对话框程序的可全屏显示,实现屏幕保护程序;
3.头文件添加类成员变量声明和函数声明:
CPoint curpt;
int x;
int y;
int idirect;
int a;
int b;
void DrawBitmap(CDC &dc, int nIndexBit);
4.在OnInitDialog函数中将屏幕设置大小,并对一些变量进行初始化,代码如下:
BOOL CScrnSaverProductDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
ShowCursor(false);
CRect rc(0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
::GetCursorPos(&curpt);
this->MoveWindow(rc);
SetTimer(1,500,NULL);
x=0;y=0;
a=0;b=0;
idirect=0;
return TRUE;
}
5.添加对WM_MOUSEMOVE消息的实现函数,实现移动鼠标关闭程序,代码如下:
void CScrnSaverProductDlg::OnMouseMove(UINT nFlags, CPoint point)
{
int moveptx=point.x-curpt.x;
int movepty=point.y-curpt.y;
if(moveptx+movepty>3)
PostMessage(WM_CLOSE);
CDialog::OnMouseMove(nFlags, point);
}
6.在OnPaint函数中实现黑色背景的绘制,代码入下:
void CScrnSaverProductDlg::OnPaint()
{
灰色代码处为注释部分
CPaintDC dc(this);
CBrush brush(RGB(0,0,0));
CRect rect;
GetClientRect(rect);
dc.FillRect(&rect,&brush);
}
7.关闭窗口时关闭程序,代码入下:
void CScrnSaverProductDlg::
PostNcDestroy()
//销毁窗口
{
KillTimer(1);
CDialog::PostNcDestroy();
}
8.设置定时器,在定时器内通过自定义函数绘制图片,代码如下:
void CScrnSaverProductDlg::OnTimer(UINT nIDEvent)
{
KillTimer(1);
CClientDC dc(this);
static nIndexBit=0;
if(nIndexBit>3)
nIndexBit=0;
DrawBitmap(dc, nIndexBit++);
SetTimer(1,500,NULL);
CDialog::OnTimer(nIDEvent);
}
9.自定义DrawBitmap函数,实现位图的绘制,代码如下:
void CScrnSaverProductDlg::DrawBitmap(CDC &dc, int nIndexBit)
{
CDC dcmem;
dcmem.CreateCompatibleDC(&dc);
CBitmap m_Bitmap;
m_Bitmap.LoadBitmap(IDB_BITMAP1+nIndexBit);
dcmem.SelectObject(m_Bitmap);
BITMAP bmp;
GetObject(m_Bitmap,sizeof(bmp),&bmp);
int iscreenx=GetSystemMetrics(SM_CXSCREEN);
int iscreeny=GetSystemMetrics(SM_CYSCREEN);
if(a>iscreenx/2)a=0;
if(b>iscreeny/2)b=0;
if(idirect>1)idirect=0;
switch(idirect)
{
case 0:
for(x=bmp.bmWidth;x>0;x--)
{
dc.BitBlt(a,b,bmp.bmWidth,bmp.bmHeight,&dcmem,x,y,SRCCOPY);
}
break;
case 1:
for(y=bmp.bmHeight;y>0;y--)
{
dc.BitBlt(a,b,bmp.bmWidth,bmp.bmHeight,&dcmem,x,y,SRCCOPY);
}
break;
}
idirect++;
a+=60;
b+=20;
Sleep(2000);
dc.BitBlt(0,0,iscreenx,iscreeny,&dcmem,0,0,BLACKNESS);
dcmem.DeleteDC();
}
效果不方便添加,自己去实现吧。。。
补充:对了,忘记告诉大家怎样添加到系统里了,把Debug里的.exe程序后缀名改为.scr,并拷贝到Windows下的System32下,在桌面---属性---屏幕保护程序中就可以看到你自己的作品了