关于屏幕保护程序的问题

mengfly 2005-10-07 11:38:49
最近刚刚接触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
}
...全文
67 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengfly 2005-10-08
  • 打赏
  • 举报
回复
没人回答吗?

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧