最近刚刚接触vc的MFC编程
有幸看到一篇关于在vc下制作屏幕保护程序的程序,编译后生成了.scr文件,但在系统下运行后只是黑屏,由于对win32下的应用程序不是很熟,所以请大家指点一下,谢谢!我想问题大概是出现在DrawLines函数中源程序:
#include<windows.h>
#include<scrnsave.h>
#include<math.h>UINT wTimer = 0;    //计时器全局变量
void DrawLines(HWND);  //屏保的画折线代码
// 无需注册子控件窗口类型,因此直接返回ture
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
return TRUE;
}BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
EndDialog(hDlg,0);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}//创建时启动定时器,退出时停止定时器LRESULT WINAPI ScreenSaverProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
wTimer = SetTimer(hwnd,0,30,NULL);
break;
case WM_TIMER:
DrawLines(hwnd);
break;
case WM_DESTROY:
if(wTimer)
KillTimer(hwnd,wTimer);
break;
}
return DefScreenSaverProc(hwnd,message,wParam,lParam);
}// 画出你自己的屏保void DrawLines(HWND hwnd)

static double dPetals = 2.0;
/*
HDC:Handle to a device context (DC).
The GetDC function retrieves a handle to a display device context 
for the client area of a specified window or for the entire screen.
You can use the returned handle in subsequent GDI functions to draw 
in the device context
*/
HDC hwindowDC = GetDC(hwnd);
if(hwindowDC)
{
RECT rc;
/*
        The GetClientRect function retrieves the
coordinates of a window's client area. 
The client coordinates specify the upper-left 
and lower-right corners of the client area. 
Because client coordinates are relative to 
the upper-left corner of a window's client area, 
the coordinates of the upper-left corner are (0,0). 
        */ GetClientRect(hwnd,&rc); //使用内存DC缓冲处理
//The CreateCompatibleDC function creates a memory 
//device context (DC) compatible with the specified device. 

HDC hMemDC = CreateCompatibleDC(hwindowDC);

//内存DC需要的BITMAP句柄

HBITMAP hBitmap = CreateCompatibleBitmap(hwindowDC,rc.right,rc.bottom);

//HGDIOBJ:Handle to a GDI object.

HGDIOBJ hOldBitmap = SelectObject(hMemDC,hBitmap); //写成黑屏 PatBlt(hMemDC,0,0,rc.right,rc.bottom,BLACKNESS);
double mx = rc.right>>1;
double my = rc.bottom>>1; //绿色的1个像素
HPEN hPen = CreatePen(PS_SOLID,1,RGB(0,255,0));
HGDIOBJ hOldPen = SelectObject(hMemDC,hPen); double sx = mx/2;
double sy = my/2; double sAngle = 0.0;
for(int i = 0;i<(int)dPetals;i++)
{
double sla = sin(sAngle);
double cla = cos(sAngle);
double s2a = sin(sAngle * dPetals);
double c2a = cos(sAngle * dPetals);
int x = (int)(mx+sx*cla+sx*c2a);
int y = (int)(my+sy*sla-sy*s2a);
if(i==0)
MoveToEx(hMemDC,x,y,NULL);
else
LineTo(hMemDC,x,y);
sAngle+=0,01745;     //1度的值,pi/180
}
dPetals+=0.1; //将内存DC拷贝至窗口DC
BitBlt(hwindowDC,0,0,rc.right,rc.bottom,hMemDC,0,0,SRCCOPY);
SelectObject(hMemDC,hOldPen);
DeleteObject(hPen);//删除笔对象
SelectObject(hMemDC,hOldBitmap);
DeleteObject(hBitmap);//删除Bitmap对象
DeleteDC(hMemDC);//删除内存DC
}
MessageBox(hwnd,0,"hello",0);
ReleaseDC(hwnd,hwindowDC);//释放窗口DC
}