下的MySaver.CPP文件:
#include < windows.h > #include < scrnsave.h >
#include "resource.h"
file://声明三个基本的函数
LRESULT WINAPI ScreenSaveProc
(HWND,UINT,WPARAM,LPARAM);
BOOL WINAPI ScreenSaveConfigureDialog
(HWND,UINT,WPARAM,LPARAM);
BOOL WINAPI RegisterDialogClasses(HINSTANCE);
BOOL WINAPI AboutDialog(HWND,UINT,WPARAM,LPARAM);
file://定义全局变量
char szIniFileName="control.ini";
file://屏幕保护程序配置数据存放在control.ini文件
char szSection[32];
file://屏幕保护程序配置数据在control.ini文件位置区名称
char szEntry="Slide Text:";
file://屏幕保护程序配置数据项名称
char SlideText[256];
file://屏幕保护程序配置数据,这里是文本内容
file://定义三个基本的函数
LRESULT WINAPI ScreenSaverProc
(HWND hWnd,UINT message, WPARAM wParam,
LPARAM lParam)
{ static HBITMAP hBmp;//位图句柄
HDC hDC,hMemDC;//hMemDC是内存设备,缓存位图。
RECT rc;
static int xpos=0;
static UINT timerID;
switch (message)
{
case WM_CREATE:
file://文件位置区名称szSection赋值为资源
idsAPPName。其中hMainInstance为//SCRNSAVE.LIB
定义的屏幕保护程序实例句柄
LoadString(hMainInstance,idsAPPName,
szSection,sizeof(szSection));
strcpy(SlideText,"欢迎使用屏幕保护程序!");
file://读control.ini文件中[Screen Saver.MySaver]
区的配置数据到SlideText
GetPrivateProfileString(szSection,szEntry,
SlideText,SlideText,
sizeof(SlideText),szIniFileName);
file://取位图
hBmp=LoadBitmap(hMainInstance,
MAKEINTRESOURCE(IDB_BITMAP1));
timerID=SetTimer(hWnd,1,250,NULL);
break;
case WM_ERASEBKGND:
hDC=GetDC(hWnd);
file://显示位图
hMemDC=CreateCompatibleDC(hDC);
SelectObject(hMemDC,hBmp);
SetRect(&rc,0,0,GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN)-25);
BitBlt(hDC,rc.top,rc.left,rc.right,rc.bottom,
hMemDC,rc.top,rc.left,SRCCOPY);
file://清除底行,为文本显示作准备。
SetRect(&rc,0,GetSystemMetrics(SM_CYSCREEN)-25
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN));
FillRect(hDC,&rc,GetStockObject(BLACK_BRUSH));
ReleaseDC(hWnd,hDC);
DeleteDC(hMemDC);
return 1;
case WM_TIMER:
hDC=GetDC(hWnd);
file://清除底行
SetRect(&rc,0,GetSystemMetrics(SM_CYSCREEN)-25,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN));
FillRect(hDC,&rc,GetStockObject(BLACK_BRUSH));
file://输出文本
SetTextColor(hDC,RGB(255*rand(),
255*rand(),255*rand()));
SetBkColor(hDC,RGB(0,0,0));
TextOut(hDC,xpos,GetSystemMetrics(SM_CYSCREEN)-25,
SlideText,strlen(SlideText));
xpos=(xpos+10)%GetSystemMetrics(SM_CXSCREEN);
ReleaseDC(hWnd,hDC);
break;
case WM_DESTROY:
DeleteObject(hBmp);
KillTimer(hWnd,timerID);
PostQuitMessage (0);
return 0;
}
return DefScreenSaverProc
(hWnd,message,wParam,lParam);
}
BOOL WINAPI ScreenSaverConfigureDialog
(HWND hWnd,UINT message,WPARAM wParam,
LPARAM lParam)
{ switch (message)
{
case WM_INITDIALOG:
LoadString(hMainInstance,idsAPPName,
szSection,sizeof(szSection));
strcpy(SlideText,"欢迎使用屏幕保护程序!");
GetPrivateProfileString(szSection,
szEntry,SlideText,
SlideText,sizeof(SlideText),szIniFileName);
SetDlgItemText(hWnd,IDC_EDIT,SlideText);
SetFocus(GetDlgItem(hWnd,IDC_EDIT));
return FALSE;
case WM_COMMAND:
switch(wParam