//DELPHI
//SDK //在Vista下,用SetPixel 画不点,只能用FillRect来画点了;暂时没找到为什么 //还有在VISTA下用CreateDC,然后进行画图,绘制速度很慢,只有去掉VISTA的玻璃效果, //才能正常绘制 #include<windows.h> #include<time.h> #include<stdlib.h> #include<iostream.h> const int SnowNumber=500; //Ñ©µãÊýÁ¿ 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; //ÆÁÄ»¸ß¶È //CBrush brush; void GetScreenSize(); void CALLBACK TimerProc(HANDLE hWnd,UINT uMsg,UINT idEvent,DWORD dwTime); void InitSnowNodes(); void MoveSnowNodes(); int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { MSG msg; //±ê×¼WindowsÏûÏ¢ LARGE_INTEGER Frequency; //¸ßÐÔÄܶ¨Ê±Æ÷ƵÂÊ LARGE_INTEGER StartCt,EndCt;//¸ßÐÔÄܶ¨Ê±Æ÷¼ÆÊý double ElapsedTime; //ʱ¼ä¼ä¸ô srand((unsigned)time(NULL)); GetScreenSize(); InitSnowNodes(); QueryPerformanceFrequency(&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=(double)(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; } //#include "atlbase.h" void InitSnowNodes() { HDC hScreenDC=0; int j=0; hScreenDC=CreateDC("DISPLAY",NULL,NULL,NULL); // hScreenDC=GetDC(0); if(hScreenDC==NULL) { MessageBox(0,"»ñÈ¡ÆÁÄ»DCʧ°Ü!","ÐÅÏ¢",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); //Õ³Ìù¶È£¨¼¸´ÎÑ»·×öÒ»´ÎÕ³Ìù¶ÈÅжϣ© //cout<<SnowNodes[j].postion.x<<" Y:"<<SnowNodes[j].postion.y<<endl; } 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("DISPLAY",NULL,NULL,NULL); // hScreenDC=GetDC(0); if(hScreenDC==NULL) { MessageBox(0,"»ñÈ¡ÆÁÄ»DCʧ°Ü£¡","ÐÅÏ¢",MB_OK|MB_ICONERROR); return; } // TextOut(hScreenDC,0,0,"ÊäÈëÄúÒªÏÔʾµÄÐÅÏ¢",0); HBRUSH hbr2 = CreateSolidBrush(RGB(255,255,255)); 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); HBRUSH hbr = CreateSolidBrush(SnowNodes[i].iColor); RECT rect = {SnowNodes[i].postion.x,SnowNodes[i].postion.y,SnowNodes[i].postion.x+1,SnowNodes[i].postion.y+1} ; ::FillRect(hScreenDC, &rect, hbr) ; DeleteObject(hbr); } x=SnowNodes[i].postion.x+rand()%3+CrWind; //¸ù¾Ý·çÏò×÷Ëæ»úÆ®Âä y=SnowNodes[i].postion.y+SnowNodes[i].iSpeed; // y=SnowNodes[i].postion.y+SnowNodes[i].iMove; 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); RECT rect1 = {x,y,x+1,y+1} ; ::FillRect(hScreenDC, &rect1, hbr2) ; RECT rect2 = {x+1,y+1,x+2,y+2} ; ::FillRect(hScreenDC, &rect2, hbr2) ; RECT rect3 = {x-1,y+1,x,y+2} ; ::FillRect(hScreenDC, &rect3, hbr2) ; 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,RGB(255,255,255)); RECT rect = {x,y,x+1,y+1} ; ::FillRect(hScreenDC, &rect, hbr2) ; SnowNodes[i].postion.x=x; SnowNodes[i].postion.y=y; } } } DeleteDC(hScreenDC); DeleteObject(hbr2); CrStep++; } |