windows编程问题

zbbanya 2010-06-10 02:36:01
#include<windows.h>
#include<time.h>
#include<stdlib.h>

const int SnowNumber=500; //雪点数量
HDC hScreenDC=0;
struct SnowNode
{
POINT postion; //雪点位置
int iColor; //先前的颜色
int iSpeed; //下落速度
int iMove; //下落距离
int iStick; //粘贴度
};

SnowNode SnowNodes[SnowNumber]; //雪点数组
int hTimer=0;
int CrWind=0;
int CrStep=0; //当前循环步数(用于限速)
int ScreenWidth=0; //屏幕宽度
int ScreenHeight=0; //屏幕高度

void GetScreenSize();
void CALLBACK TimerProc(HANDLE hWnd,UINT uMsg,UINT idEvent,DWORD dwTime);
void InitSnowNodes();
void MoveSnowNodes();

int WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{


MSG msg; //标准windows消息
LARGE_INTEGER Frequency; //高性能定时器频率
LARGE_INTEGER StartCt,EndCt;//高性能定时器计数
__int64 ElapsedTime; //时间间隔
srand((unsigned)time(NULL));
GetScreenSize();
InitSnowNodes();
QueryPerformanceFrequency(&Frequency);//检验硬件是不是支持Frequency频率
hTimer=SetTimer(0,0,rand()%5*500,(TIMERPROC)TimerProc);//设定一个计时器
if(hTimer==0)
{
MessageBox(0,TEXT("创建定时器失败"),TEXT("提示"),MB_OK|MB_ICONINFORMATION);
return -1;
}
RegisterHotKey(0,0,MOD_CONTROL,(int)'L');//定义一个系统热键
while(1)
{
QueryPerformanceCounter(&StartCt); //执行运算前计数值
if(PeekMessage(&msg,0,0,0,1))
{
switch(msg.message)
{
case WM_TIMER: TimerProc(0,0,0,0);
break; //预设风向改变时间已到
case WM_HOTKEY: KillTimer(0,hTimer);//删除随机风向定时 器
UnregisterHotKey(0,0);//删除退出热键
InvalidateRect(0,NULL,true);
exit(1);
break;
case WM_DISPLAYCHANGE:
GetScreenSize(); //重新取屏幕的尺寸
InitSnowNodes(); //初始化雪点的数组
break;
}

}
MoveSnowNodes();
QueryPerformanceCounter(&EndCt);//执行运算后的计数值
ElapsedTime=(EndCt.QuadPart-StartCt.QuadPart)/Frequency.QuadPart;
if((ElapsedTime<0.0005))
Sleep(2); //简单限速
else if(ElapsedTime<0.0010)
Sleep(1);
else if(ElapsedTime<0.0015)
Sleep(3);

}
//MessageBox(0,TEXT("消息"),TEXT("消息"),MB_OK|MB_ICONINFORMATION);
return 0;
}
void GetScreenSize()
{
ScreenWidth=GetSystemMetrics(SM_CXSCREEN);
ScreenHeight=GetSystemMetrics(SM_CYSCREEN);
return ;
}

void CALLBACK TimerProc(HANDLE hWnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
// MessageBox(0,TEXT("消息"),TEXT("消息"),MB_OK|MB_ICONINFORMATION);
srand((unsigned)time(NULL));
if(hTimer==0)
{
MessageBox(0,TEXT("创建定时器失败"),TEXT("提示"),MB_OK|MB_ICONINFORMATION);
return ;
}
SetTimer(0,hTimer,((rand()%27+4)*500),(TIMERPROC)TimerProc); //重设下次风向改变时间
//修改风向
if(CrWind!=0)
CrWind=0;
else
CrWind=rand()%3-1;
return ;
}

void InitSnowNodes()
{
//HDC hScreenDC=0;
int j=0;
// hScreenDC=CreateDC(L"DISPLAY",NULL,NULL,NULL);
hScreenDC=GetDC(NULL);
if(hScreenDC==NULL)
{
MessageBox(0,TEXT("获取屏幕DC失败!"),TEXT("信息"),MB_OK|MB_ICONERROR);
return ;
}
srand((unsigned)time(NULL));
for(j=0;j<SnowNumber;j++)
{
SnowNodes[j].postion.x=rand()%ScreenWidth;
SnowNodes[j].postion.y=rand()%ScreenHeight;
SnowNodes[j].iColor=GetPixel(hScreenDC,SnowNodes[j].postion.x,SnowNodes[j].postion.y);
SnowNodes[j].iSpeed=(rand()%5+1); //每次下落距离(1-5)
SnowNodes[j].iStick=(30-rand()%SnowNodes[j].iSpeed); //粘贴度几次循环作一次粘贴连判断

}
//DeleteDC(hScreenDC);
}

void MoveSnowNodes()
{
// MessageBox(0,TEXT("消息"),TEXT("消息"),MB_OK|MB_ICONINFORMATION);
//HDC hScreenDC=0;
srand((unsigned)time(NULL));
int x=0,y=0,i=0;
//hScreenDC=CreateDC(L"DISPLAY",NULL,NULL,NULL);
//if(hScreenDC==NULL)
//{
// MessageBox(0,L"获取屏幕DC失败!",L"信息",MB_OK|MB_ICONERROR);
// return ;
//}
for(i=0;i<SnowNumber;i++)
{
//控制雪点下降 速度
if((CrStep%SnowNodes[i].iSpeed)!=0)
continue;
//恢复上次被覆盖点

if((GetPixel(hScreenDC,SnowNodes[i].postion.x,SnowNodes[i].postion.y))==0XFFFFFF)
SetPixel(hScreenDC,SnowNodes[i].postion.x,SnowNodes[i].postion.y,SnowNodes[i].iColor);
//根据几向作随机飘落
x=SnowNodes[i].postion.x+rand()%3+CrWind;
y=SnowNodes[i].postion.y+SnowNodes[i].iSpeed ;

//积雪(停留)效果处理
if( ( (CrStep%SnowNodes[i].iStick)==0)
&&( (GetPixel(hScreenDC,x,y))!=(GetPixel(hScreenDC,x,y+1)))
&&( (GetPixel(hScreenDC,x-1,y))!=(GetPixel(hScreenDC,x-1,y+1)))
&&( (GetPixel(hScreenDC,x+1,y))!=GetPixel(hScreenDC,x+1,y+1))
)
{
//稍稍调整坐标
if(GetPixel(hScreenDC,x,y-1)==GetPixel(hScreenDC,x,y-2))
{
y--;
}
else
{
if(GetPixel(hScreenDC,x,y-1)==GetPixel(hScreenDC,x,y-2))
y++;
x+=CrWind;
}
//画三个雪花点
SetPixel(hScreenDC,x,y,0XFFFFFF);
SetPixel(hScreenDC,x+1,y+1,0XFFFFFF);
SetPixel(hScreenDC,x-1,y-1,0XFFFFFF);
//重生雪点
SnowNodes[i].postion.x=rand()%ScreenWidth;
SnowNodes[i].postion.y=rand()%10;
SnowNodes[i].iColor=GetPixel(hScreenDC,SnowNodes[i].postion.x,SnowNodes[i].postion.y);

}
else
{
if( (x<0) || (x>ScreenWidth) || (y>ScreenHeight))
{
SnowNodes[i].postion.y=rand()%10;
SnowNodes[i].postion.x=rand()%ScreenWidth;
SnowNodes[i].iColor=GetPixel(hScreenDC,SnowNodes[i].postion.x,SnowNodes[i].postion.y);

}
else
{
//保存颜色并绘制雪点
SnowNodes[i].iColor=GetPixel(hScreenDC,x,y);
SetPixel(hScreenDC,x,y,0XFFFFFF);
//此时保存新雪点位置
SnowNodes[i].postion.x=x;
SnowNodes[i].postion.y=y;
}

}
// DeleteDC(hScreenDC);
CrStep++;
}
}
这是个屏幕下雪的程序,我想问一下如何让它在一个窗口中下呢,能的话能给下代码让我学习学习。
...全文
163 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengdengken888 2010-06-11
  • 打赏
  • 举报
回复
我怎么看不懂?没有CreateWindow或者CreateDialog??
na2650945 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zbbanya 的回复:]

那里不正规了。能提出来吗。
[/Quote]
你GetDc()之后。
你ReleaseDc()了没。
一般把消息处理放到窗口过程中。
而不是死循环中吧。
ForestDB 2010-06-11
  • 打赏
  • 举报
回复
帮顶。
elegant87 2010-06-11
  • 打赏
  • 举报
回复
const int SnowNumber=500; //雪点数量
C语言中用宏
#define SNOWNUMBER 500
Eleven 2010-06-11
  • 打赏
  • 举报
回复
LIBCMTD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
--------------------------------------------------------------------------
工程建错了,你新建工程的时候选的是新建Win32 Console Application,入口函数main/wmain,而你现在写的是Win32 SDK程序,入口函数WinMain/wWinMain,修改工程属性设置,Link下的subsystem:console修改成subsystem:windows或者干脆去掉这个
zbbanya 2010-06-10
  • 打赏
  • 举报
回复
那里不正规了。能提出来吗。
litaolilan 2010-06-10
  • 打赏
  • 举报
回复
楼主有创意,不错
zhangzhongke007 2010-06-10
  • 打赏
  • 举报
回复
正在学,帮顶。
na2650945 2010-06-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 huamanzhao 的回复:]

Linking...
Snow.obj : error LNK2001: unresolved external symbol __imp__InvalidateRect@12
Snow.obj : error LNK2001: unresolved external symbol __imp__UnregisterHotKey@8
Snow.obj : error LNK2001: un……
[/Quote]
工程建的不对吧。
还有。
LZ的代码不是太正规呀。
而且。
貌似有内存泄露。
na2650945 2010-06-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 huamanzhao 的回复:]

Linking...
Snow.obj : error LNK2001: unresolved external symbol __imp__InvalidateRect@12
Snow.obj : error LNK2001: unresolved external symbol __imp__UnregisterHotKey@8
Snow.obj : error LNK2001: un……
[/Quote]
缺头文件。
奔牛D 2010-06-10
  • 打赏
  • 举报
回复
Linking...
Snow.obj : error LNK2001: unresolved external symbol __imp__InvalidateRect@12
Snow.obj : error LNK2001: unresolved external symbol __imp__UnregisterHotKey@8
Snow.obj : error LNK2001: unresolved external symbol __imp__KillTimer@8
Snow.obj : error LNK2001: unresolved external symbol __imp__PeekMessageA@20
Snow.obj : error LNK2001: unresolved external symbol __imp__RegisterHotKey@16
Snow.obj : error LNK2001: unresolved external symbol __imp__MessageBoxA@16
Snow.obj : error LNK2001: unresolved external symbol __imp__SetTimer@16
Snow.obj : error LNK2001: unresolved external symbol __imp__GetSystemMetrics@4
Snow.obj : error LNK2001: unresolved external symbol __imp__GetPixel@12
Snow.obj : error LNK2001: unresolved external symbol __imp__GetDC@4
Snow.obj : error LNK2001: unresolved external symbol __imp__SetPixel@16
LIBCMTD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/Snow.exe : fatal error LNK1120: 12 unresolved externals
执行 link.exe 时出错.


咋回事?
zbbanya 2010-06-10
  • 打赏
  • 举报
回复
不需要创建一个窗口吗?
Gary@Tokyo 2010-06-10
  • 打赏
  • 举报
回复
void GetScreenSize()
{
ScreenWidth=GetSystemMetrics(SM_CXSCREEN);
ScreenHeight=GetSystemMetrics(SM_CYSCREEN);
return ;
}

这个控制区域的改改就OK了
totti1006 2010-06-10
  • 打赏
  • 举报
回复
不错!
在屏幕上面下雪,有意思
lixing01 2010-06-10
  • 打赏
  • 举报
回复
关注一下
god_sun 2010-06-10
  • 打赏
  • 举报
回复
1.
void GetScreenSize()
{
ScreenWidth=GetSystemMetrics(SM_CXSCREEN);
ScreenHeight=GetSystemMetrics(SM_CYSCREEN);
return ;
}


改为获得窗口长宽, GetClientRect(m_MainWnd, &rcl);
2.
GetDC 中用窗口句柄

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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