GridPicture.cpp
资源名称:连连看源程序.rar [点击查看]
上传用户:z3308516
上传日期:2013-01-23
资源大小:703k
文件大小:32k
源码类别:
2D图形编程
开发平台:
Visual C++
- #include "StdAfx.h"
- #include "gridpicture.h"
- #include "resource.h"
- #include "TimeElapse.h"
- #include "Registry.h"
- extern CTimeElapse timeElapse;
- extern CRegistry reg;
- #define ShowMessage(X) MessageBox(NULL,X,"发生错误",MB_OK|MB_TOPMOST|MB_ICONWARNING);
- PostQuitMessage(0);
- int PICTURE_WIDTH=34;
- #define PICTURE_SPACE 0
- CGridPicture::CGridPicture(){
- leftEdge=0;
- rightEdge=0;
- topEdge=0;
- bottomEdge=0;
- bSound=TRUE;
- ifen=0;
- ilevel=0;
- lv=0;
- }
- void CGridPicture::SetFenLevel(int ifen,int ilevel,int lv){
- this->ifen =ifen;
- this->ilevel =ilevel;
- this->lv=lv;
- }
- void CGridPicture::GetLevelAndFen(int &ifen,int &lv){
- ifen=this->ifen;
- lv=this->lv;
- }
- void CGridPicture::CancelFocus(){
- int id;
- id=focusid;
- focusid=-1;
- if(id!=-1){
- InvalidateRect(hParent,(CONST RECT*)&arrayPic[id].rc ,TRUE);
- }
- }
- void CGridPicture::ReadMap(LPCTSTR pszRes){
- HRSRC hsrc;
- HGLOBAL hGlobal;
- size_t sz;
- int ifor;
- int max;
- int icount;
- int v;
- LPVOID pData;
- max=horzNum*vertNum;
- if(pszRes!=NULL){
- hsrc=FindResource(GetModuleHandle(NULL),(LPCTSTR)pszRes,(LPCTSTR)"RT_JPGDATA");
- if(hsrc){
- sz=SizeofResource(GetModuleHandle(NULL),hsrc);
- hGlobal=LoadResource(GetModuleHandle(NULL),hsrc);
- if(hGlobal){
- pData=GlobalLock(hGlobal);
- ///////////////////calc picture number
- icount=0;
- for(ifor=0;ifor<sz;ifor++){
- if(*((char*)pData+ifor)=='1')
- icount++;
- }
- if(icount%4!=0){
- for(ifor=0;ifor<max;ifor++){
- if(*((char*)pData+ifor)=='1'){ //fill 1
- *((char*)pData+ifor)='0';
- icount--;
- if(icount%4==0)
- break;
- }
- }
- }
- picNum=icount/4;
- ////////////////////////////////////////////
- icount=0;
- for(ifor=0;ifor<max;ifor++){
- if(*((char*)pData+ifor)=='1'){
- arrayPic[ifor].resourceid =icount%picNum;
- icount++;
- }else{
- arrayPic[ifor].resourceid =-1;
- }
- }
- GlobalUnlock(hGlobal);
- }
- FreeResource(hsrc);
- }
- }else{ //randomize
- srand((unsigned int)time(NULL));
- icount=0;
- for(ifor=0;ifor<max;ifor++){
- arrayPic[ifor].resourceid =-1;
- }
- for(ifor=0;ifor<max;ifor++){
- v=(rand()%2);
- arrayPic[ifor].resourceid =v;
- if(v>0)
- icount++;
- if(icount>144) //图片数目极限
- break;
- }
- if(icount%4!=0){
- for(ifor=0;ifor<max;ifor++){
- if(arrayPic[ifor].resourceid>0){ //fill 1
- arrayPic[ifor].resourceid=0;
- icount--;
- if(icount%4==0)
- break;
- }
- }
- }
- picNum=icount/4;
- icount=0;
- for(ifor=0;ifor<max;ifor++){
- if(arrayPic[ifor].resourceid>0){
- arrayPic[ifor].resourceid =icount%picNum;
- icount++;
- }else{
- arrayPic[ifor].resourceid =-1;
- }
- }
- }
- }
- void CGridPicture::CGridPictureInit(HWND hParent,int iHorz,int iVert,long lThumb,BOOL bSpecial)
- {
- FILE *fp;
- char szReadBuf[512];
- BITMAP bm;
- RECT rc;
- int ifor;
- int max,val;
- HANDLE hThread;
- DWORD tid;
- bFirstDraw=TRUE;
- this->bSpecial =bSpecial;
- ///////////////////check and destroy
- if(allThumb){
- DeleteObject(allThumb);
- allThumb=NULL;
- }
- if(tempDC)
- DeleteDC(tempDC);
- if(arrayPic)
- delete[] arrayPic;
- arrayPic=NULL;
- focusid=-1;
- if(!IsWindow(hParent)){
- ShowMessage("父窗口句柄无效");
- }
- this->hParent =hParent;
- this->lThumb =lThumb;
- horzNum=iHorz;
- vertNum=iVert;
- int cx,cy;
- if(fp=fopen("beautifulGame.ini","r")){
- while(fgets((char*)szReadBuf,sizeof(szReadBuf),fp)){
- if((strlen(szReadBuf)>0) && (szReadBuf[0]!='/')){
- if(atoi((const char*)szReadBuf)>0){
- sscanf((const char*)szReadBuf,"%dX%d",&cx,&cy);
- }else{
- fclose(fp);
- break;
- }
- }
- }
- }
- if(cx>10 && cy>10){
- if(_access((const char*)szReadBuf,0)!=-1){
- allThumb=(HBITMAP)LoadImage(NULL,(LPCTSTR)szReadBuf,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_DEFAULTSIZE);
- if(allThumb){
- PICTURE_WIDTH=cx;
- }
- }
- }
- if(!allThumb)
- allThumb=LoadBitmap((HINSTANCE)GetModuleHandle(NULL),(LPCTSTR)lThumb);
- if(!allThumb){
- ShowMessage("装入图片库失败,可能内存不够,请关闭一些程序后再试");
- }
- GetObject(allThumb,sizeof(bm),(LPVOID)&bm);
- if(!bSpecial){
- if(bm.bmWidth/PICTURE_WIDTH<(iHorz*iVert/4)){
- ShowMessage("图片数目太少,无法运行,请确定有36个34X34像素的图片");
- }
- }
- GetClientRect(hParent,&rc);
- startPoint.x=((rc.right -rc.left)-(PICTURE_WIDTH*iHorz))/2;
- if(!bSpecial){
- startPoint.y=((rc.bottom -rc.top)-(bm.bmHeight*iVert))/2;
- }else{
- startPoint.y=((rc.bottom - rc.top)-(bm.bmHeight*iVert))/2+50;
- }
- picture_height=bm.bmHeight;
- rcPicture_region.left=startPoint.x;
- rcPicture_region.top =startPoint.y;
- rcPicture_region.right =rcPicture_region.left+((PICTURE_WIDTH+PICTURE_SPACE)*iHorz);
- rcPicture_region.bottom =rcPicture_region.top+((picture_height+PICTURE_SPACE)*iVert);
- leftEdge=startPoint.x-((PICTURE_WIDTH+PICTURE_SPACE)/2);
- topEdge=startPoint.y-((picture_height+PICTURE_SPACE)/2);
- rightEdge=rcPicture_region.right+((PICTURE_WIDTH+PICTURE_SPACE)/2);
- bottomEdge=rcPicture_region.bottom+((picture_height+PICTURE_SPACE)/2);
- arrayPic=new SINGLE_BLOCK[iVert*iHorz];
- memset((void*)arrayPic,0,sizeof(SINGLE_BLOCK)*iVert*iHorz);
- //init
- max=vertNum*horzNum;
- if(!bSpecial){
- val=max/4;
- picNum=val;
- for(ifor=0;ifor<max;ifor++){
- arrayPic[ifor].resourceid=ifor%val;
- }
- }else{
- switch(ilevel){
- case 4:
- ReadMap((LPCTSTR)IDR_MAP1);
- break;
- case 0:
- ReadMap((LPCTSTR)IDR_MAP2);
- break;
- case 3:
- ReadMap((LPCTSTR)IDR_MAP3);
- break;
- case 2:
- ReadMap((LPCTSTR)IDR_MAP4);
- break;
- case 1:
- ReadMap((LPCTSTR)IDR_MAP5);
- break;
- case 5:
- ReadMap((LPCTSTR)IDR_MAP6);
- break;
- case 6:
- ReadMap((LPCTSTR)IDR_MAP7);
- break;
- case 7:
- ReadMap((LPCTSTR)IDR_MAP8);
- break;
- case 8:
- ReadMap((LPCTSTR)IDR_MAP9);
- break;
- case 9:
- ReadMap((LPCTSTR)IDR_MAP10);
- break;
- case 10:
- ReadMap((LPCTSTR)IDR_MAP11);
- break;
- default:
- ReadMap((LPCTSTR)NULL);
- break;
- }
- }
- Shuffle();
- tempDC=NULL;
- bTerminate=FALSE;
- hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadWatch,(LPVOID)this,0,&tid);
- if(hThread)
- CloseHandle(hThread);
- }
- CGridPicture::~CGridPicture(void)
- {
- if(allThumb)
- DeleteObject(allThumb);
- if(tempDC)
- DeleteDC(tempDC);
- if(arrayPic)
- delete[] arrayPic;
- arrayPic=NULL;
- }
- #define DRAW_EDGE(color,w,x,y,tox,toy)
- hpen=CreatePen(PS_SOLID,w,color);
- oldpen=(HPEN)SelectObject(hDC,hpen);
- MoveToEx(hDC,x,y,NULL);
- LineTo(hDC,tox,toy);
- SelectObject(hDC,oldpen);
- DeleteObject(hpen);
- void CGridPicture::Draw(HDC hDC,PSINGLE_BLOCK arrayPic){
- int ifor;
- int vPos;
- int hPos;
- HBRUSH hbr;
- HPEN hpen,oldpen;
- RECT rcfill;
- if(!tempDC){
- tempDC=CreateCompatibleDC(hDC);
- SelectObject(tempDC,allThumb);
- }
- hPos=startPoint.x;
- vPos=startPoint.y;
- for(ifor=0;ifor<horzNum*vertNum;ifor++){
- if(ifor!=0 && ifor%horzNum==0){
- vPos+=picture_height+PICTURE_SPACE;
- hPos=startPoint.x;
- }
- if(arrayPic[ifor].resourceid ==-1){ //已经打掉了
- if(bFirstDraw)
- SetRect(&arrayPic[ifor].rc,hPos-PICTURE_SPACE-3,vPos-PICTURE_SPACE-3,PICTURE_WIDTH+hPos+PICTURE_SPACE+5,picture_height+vPos+PICTURE_SPACE+5);
- hPos+=PICTURE_WIDTH+PICTURE_SPACE;
- continue;
- }
- if(focusid==ifor){
- SetRect(&rcfill,hPos,vPos,PICTURE_WIDTH+hPos,picture_height+vPos);
- hbr=CreateSolidBrush(RGB(200,200,200));
- FillRect(hDC,&rcfill,hbr);
- DeleteObject(hbr);
- }else{
- SetRect(&rcfill,hPos,vPos,PICTURE_WIDTH+hPos,picture_height+vPos);
- hbr=CreateSolidBrush(RGB(249,254,214));
- FillRect(hDC,&rcfill,hbr);
- DeleteObject(hbr);
- }
- DRAW_EDGE(RGB(209,182,161),2,hPos,vPos,hPos,vPos+picture_height);
- DRAW_EDGE(RGB(209,182,161),2,hPos,vPos+picture_height,hPos+PICTURE_WIDTH,vPos+picture_height);
- DRAW_EDGE(RGB(255,255,255),1,hPos+PICTURE_WIDTH-2,vPos,hPos+PICTURE_WIDTH-2,vPos+picture_height);
- DRAW_EDGE(RGB(248,182,94),1,hPos+PICTURE_WIDTH,vPos,hPos+PICTURE_WIDTH+4,vPos+4);
- DRAW_EDGE(RGB(248,182,94),1,hPos+PICTURE_WIDTH,vPos+picture_height,hPos+PICTURE_WIDTH+4,vPos+picture_height+4);
- //fill
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH,vPos+1,hPos+PICTURE_WIDTH,vPos+picture_height+1);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+1,vPos+2,hPos+PICTURE_WIDTH+1,vPos+picture_height+2);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+2,vPos+3,hPos+PICTURE_WIDTH+2,vPos+picture_height+3);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+3,vPos+4,hPos+PICTURE_WIDTH+3,vPos+picture_height+4);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+4,vPos+5,hPos+PICTURE_WIDTH+4,vPos+picture_height+5);
- //////////////
- DRAW_EDGE(RGB(248,182,94),1,hPos,vPos+picture_height,hPos+4,vPos+picture_height+4);
- //fill
- DRAW_EDGE(RGB(238,162,64),1,hPos+1,vPos+picture_height,hPos+PICTURE_WIDTH+1,vPos+picture_height);
- DRAW_EDGE(RGB(238,162,64),1,hPos+2,vPos+picture_height+1,hPos+PICTURE_WIDTH+2,vPos+picture_height+1);
- DRAW_EDGE(RGB(238,162,64),1,hPos+3,vPos+picture_height+2,hPos+PICTURE_WIDTH+3,vPos+picture_height+2);
- DRAW_EDGE(RGB(238,162,64),1,hPos+4,vPos+picture_height+3,hPos+PICTURE_WIDTH+4,vPos+picture_height+3);
- DRAW_EDGE(RGB(238,162,64),1,hPos+5,vPos+picture_height+4,hPos+PICTURE_WIDTH+5,vPos+picture_height+4);
- ///////////////////
- BitBlt(hDC,hPos,vPos,PICTURE_WIDTH,picture_height,tempDC,arrayPic[ifor].resourceid*PICTURE_WIDTH,0,SRCAND);
- if(bFirstDraw)
- SetRect(&arrayPic[ifor].rc,hPos-PICTURE_SPACE-3,vPos-PICTURE_SPACE-3,PICTURE_WIDTH+hPos+PICTURE_SPACE+5,picture_height+vPos+PICTURE_SPACE+5);
- hPos+=PICTURE_WIDTH+PICTURE_SPACE;
- }
- RECT rc;
- HFONT hfont,oldfont;
- char szfen[20];
- GetClientRect(hParent,&rc);
- hfont=CreateFont(55,0,0,0,0,0,0,0,0,0,0,0,0,FONTNAME);
- oldfont=(HFONT)SelectObject(hDC,hfont);
- SetTextColor(hDC,RGB(0,255,0));
- wsprintf((LPTSTR)szfen," %d ",ifen);
- TextOut(hDC,rc.right -200,rc.top,(LPCTSTR)szfen,strlen(szfen));
- GetClientRect(hParent,(LPRECT)&rc);
- wsprintf((LPTSTR)szfen,"Level %d ",ilevel+1);
- SetTextColor(hDC,RGB(80,80,80));
- TextOut(hDC,0,0,(LPCTSTR)szfen,strlen(szfen));
- SelectObject(hDC,oldfont);
- DeleteObject(hfont);
- bFirstDraw=FALSE;
- }
- void CGridPicture::Shuffle(){
- int ifor;
- // int val;
- int max;
- int temp;
- int num1,num2,num3,num4;
- max=vertNum*horzNum;
- srand((unsigned int)time(NULL));
- // val=max/4;
- //打乱
- for(ifor=0;ifor<(max*4);ifor++){
- num1=((rand())%max);
- num2=((rand())%max);
- if(arrayPic[num1].resourceid !=-1 && arrayPic[num2].resourceid !=-1){
- if(num1!=num2){
- num3=num1;
- num4=num2;
- num1=arrayPic[num3].resourceid;
- num2=arrayPic[num4].resourceid;
- temp=num1;
- num1=num2;
- num2=temp;
- arrayPic[num3].resourceid=num1;
- arrayPic[num4].resourceid=num2;
- }
- }
- }
- }
- void CGridPicture::OrderPicture(int id1,int id2){ //排序图片
- int temp;
- RECT rc;
- HDC hDC;
- HFONT hfont,oldfont;
- char szfen[20];
- #define EXCHANGE_NUM(x1,x2) temp=arrayPic[x1].resourceid;
- arrayPic[x1].resourceid=arrayPic[x2].resourceid;
- arrayPic[x2].resourceid=temp;
- InvalidateRect(hParent,&arrayPic[x1].rc,TRUE);
- InvalidateRect(hParent,&arrayPic[x2].rc,TRUE);
- if(id2<id1){
- temp=id1;
- id1=id2;
- id2=temp;
- }
- InvalidateRect(hParent,&arrayPic[id1].rc,TRUE);
- InvalidateRect(hParent,&arrayPic[id2].rc,TRUE);
- if(!bSpecial)
- switch(ilevel){
- case 0: //不变
- break;
- case 1: //向下
- id1-=horzNum;
- while(id1>-1){
- EXCHANGE_NUM(id1,id1+horzNum);
- id1-=horzNum;
- }
- id2-=horzNum;
- while(id2>-1){
- EXCHANGE_NUM(id2,id2+horzNum);
- id2-=horzNum;
- }
- break;
- case 2: //向上
- if(id2>id1){
- temp=id1;
- id1=id2;
- id2=temp;
- }
- id1+=horzNum;
- while(id1<horzNum*vertNum){
- EXCHANGE_NUM(id1,id1-horzNum);
- id1+=horzNum;
- }
- id2+=horzNum;
- while(id2<horzNum*vertNum){
- EXCHANGE_NUM(id2,id2-horzNum);
- id2+=horzNum;
- }
- break;
- case 3: //向左
- if(id2>id1){
- temp=id1;
- id1=id2;
- id2=temp;
- }
- id1++;
- while(id1%horzNum!=0){
- EXCHANGE_NUM(id1,id1-1);
- id1++;
- }
- id2++;
- while(id2%horzNum!=0){
- EXCHANGE_NUM(id2,id2-1);
- id2++;
- }
- break;
- case 4: //向右
- id1--;
- while(id1%horzNum!=(horzNum-1) && id1>-1){
- EXCHANGE_NUM(id1,id1+1);
- id1--;
- }
- id2--;
- while(id2%horzNum!=(horzNum-1) && id2>-1){
- EXCHANGE_NUM(id2,id2+1);
- id2--;
- }
- break;
- }
- GetClientRect(hParent,&rc);
- hfont=CreateFont(55,0,0,0,0,0,0,0,0,0,0,0,0,FONTNAME);
- hDC=GetDC(hParent);
- oldfont=(HFONT)SelectObject(hDC,hfont);
- SetTextColor(hDC,RGB(0,255,0));
- wsprintf((LPTSTR)szfen," %d ",ifen);
- TextOut(hDC,rc.right -200,rc.top,(LPCTSTR)szfen,strlen(szfen));
- SelectObject(hDC,oldfont);
- DeleteObject(hfont);
- ReleaseDC(hParent,hDC);
- UpdateWindow(hParent);
- }
- bool CGridPicture::Picture_Click(POINT &ptPos){
- BOOL bContinue;
- int ifor;
- int x,y;
- int v;
- int id1,id2;
- static int prevtime=0;
- static int preid=-10;
- int id;
- int tid;
- if(PtInRect(&rcPicture_region,ptPos)){
- x=ptPos.x-startPoint.x;
- y=ptPos.y-startPoint.y;
- x=x/(PICTURE_WIDTH+PICTURE_SPACE);
- y=y/(picture_height+PICTURE_SPACE);
- v=(int)y*horzNum+(int)x;
- pt1.x=0;
- pt1.y=0;
- pt2.x=0;
- pt2.y=0;
- if(focusid!=-1 && v!=focusid && arrayPic[v].resourceid!=-1)
- if(arrayPic[v].resourceid ==arrayPic[focusid].resourceid &&isCanLink(v,focusid,enumAny)){
- ////test explore
- // HANDLE hThread;
- // DWORD tid;
- DrawLinkLine(v,focusid);
- Play((LPCTSTR)IDR_CLEAR);
- /* pe=new EXPLORE_STRUCT;
- pe->hParent=hParent;
- memcpy((void*)&pe->rc,(const void*)&arrayPic[focusid].rc,sizeof(RECT));
- hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadExplore,(LPVOID)pe,0,&tid);
- if(hThread)
- CloseHandle(hThread);
- pe=new EXPLORE_STRUCT;
- pe->hParent=hParent;
- memcpy((void*)&pe->rc,(const void*)&arrayPic[v].rc,sizeof(RECT));
- hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadExplore,(LPVOID)pe,0,&tid);
- if(hThread)
- CloseHandle(hThread);
- */
- id1=focusid;
- id2=v;
- id=arrayPic[focusid].resourceid;
- arrayPic[focusid].resourceid =-1;
- arrayPic[v].resourceid =-1;
- focusid=-1;
- bContinue=FALSE;
- for(ifor=0;ifor<vertNum*horzNum;ifor++){ //是否还没打完
- if(arrayPic[ifor].resourceid!=-1){
- bContinue=TRUE;
- break;
- }
- }
- if(timeElapse.totalSecond -prevtime<4) //二秒
- ifen+=18;
- else if(timeElapse.totalSecond-prevtime<8)
- ifen+=16;
- else if(timeElapse.totalSecond-prevtime<12)
- ifen+=14;
- else if(timeElapse.totalSecond-prevtime<16)
- ifen+=12;
- else
- ifen+=10;
- prevtime=timeElapse.totalSecond;
- if(!bContinue){
- ifen+=timeElapse.totalSecond*2;
- if(!bSpecial){
- if(ilevel<4){
- timeElapse.Pause();
- MessageBox(hParent,"请按确定以进入到下一关","信息",MB_OK);
- timeElapse.Pause();
- timeElapse.totalSecond =600;
- CGridPictureInit(hParent,horzNum,vertNum,lThumb,bSpecial);
- InvalidateRect(hParent,NULL,TRUE);
- ilevel++;
- return true;
- }
- }else{
- if(ilevel<100){
- timeElapse.Pause();
- // MessageBox(hParent,"请按确定以进入到下一关","信息",MB_OK);
- timeElapse.Pause();
- timeElapse.totalSecond =600;
- ilevel++;
- CGridPictureInit(hParent,22,11,lThumb,bSpecial);
- InvalidateRect(hParent,NULL,TRUE);
- return true;
- }
- }
- }
- if(!bContinue){
- bTerminate=TRUE;
- SendMessage(hParent,STOP_TIME,0,0);
- MessageBox(hParent,"你已完成此皆段的所有关卡,历害","信息",MB_OK);
- if(lv==0){
- if(reg.CompareAndSetValue(hParent,enumLow,ifen)!=-1)
- MessageBox(hParent,"你获得了排名,高手","信息",MB_OK|MB_ICONINFORMATION);
- }else if(lv==1){
- if(reg.CompareAndSetValue(hParent,enumNormal,ifen)!=-1)
- MessageBox(hParent,"你获得了排名,高手","信息",MB_OK|MB_ICONINFORMATION);
- }else if(lv==2){
- if(reg.CompareAndSetValue(hParent,enumHigh,ifen)!=-1)
- MessageBox(hParent,"你获得了排名,高手","信息",MB_OK|MB_ICONINFORMATION);
- }else if(lv==3){
- if(reg.CompareAndSetValue(hParent,enumSpecial,ifen)!=-1)
- MessageBox(hParent,"你获得了排名,高手","信息",MB_OK|MB_ICONINFORMATION);
- }
- }
- if(id%2==0 && !bTerminate)
- OutputWin("ok ");
- else if(!bTerminate)
- OutputWin("good");
- if(timeElapse.totalSecond+8<600) //打掉一个加两秒时间
- timeElapse.totalSecond +=8;
- if(id==preid){ //一等奖
- OutputWin("得了一等奖,送你1000分....:");
- ifen+=1000;
- }else if(preid==0 && id==(horzNum*vertNum-1)){ //二等奖
- OutputWin("得了二等奖,送你500分....:");
- ifen+=500;
- }else if(preid==(horzNum*vertNum-1)-id){ //三等奖
- OutputWin("得了三等奖,送你250分....:");
- ifen+=250;
- }else if(preid==horzNum && id==horzNum+1){ //时间一等奖
- OutputWin("得了时间一等奖,时间加满....:");
- timeElapse.totalSecond=600;
- }else if(abs(preid-id)<1){ //时间二等奖
- OutputWin("得了时间二等奖,时间加10秒....:");
- if(timeElapse.totalSecond+100<600)
- timeElapse.totalSecond +=50; //将二十秒种
- else
- timeElapse.totalSecond =600;
- }else if(abs(preid-id)<2){ //时间三等将
- OutputWin("得了时间三等奖,时间加5秒....:");
- if(timeElapse.totalSecond+25<600)
- timeElapse.totalSecond +=25; //将十秒种
- else
- timeElapse.totalSecond =600;
- }
- preid=id;
- if(!bTerminate){
- OrderPicture(id1,id2);
- }
- return true;
- }
- if(v!=focusid){
- // if(arrayPic[v].resourceid!=-1 && focusid!=-1)
- if(arrayPic[v].resourceid!=-1&& focusid!=-1){ //把原来的焦点去掉
- OutputWin("嗨,点错了");
- tid=focusid;
- focusid=-1;
- ParsePosAndInvalidate(tid,TRUE);
- }
- if(arrayPic[v].resourceid !=-1){
- Play((LPCTSTR)IDR_CLICK);
- focusid=v; //设置新焦点
- ParsePosAndInvalidate(focusid);
- }
- UpdateWindow(hParent);
- }
- return true;
- }
- return false;
- }
- void CGridPicture::OutputWin(char *pszInfo){
- HDC hDC;
- RECT rc;
- static int prelen=-1;
- HFONT hfont,oldfont;
- hDC=GetDC(hParent);
- hfont=CreateFont(25,0,0,0,0,0,0,0,0,0,0,0,0,"宋体");
- oldfont=(HFONT)SelectObject(hDC,hfont);
- SetTextColor(hDC,RGB(0,0,255));
- if(prelen>6){
- rc.left =rcPicture_region.left;
- rc.top =rcPicture_region.top -30;
- rc.right=rc.left +500;
- rc.bottom =rc.top +30;
- InvalidateRect(hParent,&rc,TRUE);
- UpdateWindow(hParent);
- }
- prelen=strlen(pszInfo);
- TextOut(hDC,rcPicture_region.left,rcPicture_region.top-30,pszInfo,strlen(pszInfo));
- SelectObject(hDC,oldfont);
- DeleteObject(hfont);
- ReleaseDC(hParent,hDC);
- }
- #define OUTOFUP(IDQ) IDQ<0
- #define OUTOFLEFT(IDQ) (IDQ+1)%horzNum==0
- #define OUTOFRIGHT(IDQ) (IDQ)%horzNum==0
- #define OUTOFBOTTOM(IDQ) IDQ>(max-1)
- #define IDXPOS(idpos) arrayPic[idpos].rc.left+((PICTURE_WIDTH+PICTURE_SPACE)/2)
- #define IDYPOS(idpos) arrayPic[idpos].rc.top+((picture_height+PICTURE_SPACE)/2)
- bool CGridPicture::CanDirectToTop(int idq){
- int max;
- max=horzNum*vertNum;
- while(true){
- idq-=horzNum;
- if(OUTOFUP(idq))
- return true;
- if( arrayPic[idq].resourceid!=-1)
- break;
- }
- return false;
- }
- bool CGridPicture::CanDirectToLeft(int idq){
- int max;
- max=horzNum*vertNum;
- while(true){
- idq--;
- if(OUTOFLEFT(idq))
- return true;
- if( arrayPic[idq].resourceid!=-1)
- break;
- }
- return false;
- }
- bool CGridPicture::CanDirectToRight(int idq){
- int max;
- max=horzNum*vertNum;
- while(true){
- idq++;
- if(OUTOFRIGHT(idq))
- return true;
- if( arrayPic[idq].resourceid!=-1)
- break;
- }
- return false;
- }
- bool CGridPicture::CanDirectToBottom(int idq){
- int max;
- max=horzNum*vertNum;
- while(true){
- idq+=horzNum;
- if(OUTOFBOTTOM(idq))
- return true;
- if( arrayPic[idq].resourceid!=-1)
- break;
- }
- return false;
- }
- void CGridPicture::DrawLinkLine(int id1,int id2){
- HDC hDC;
- int temp;
- int ifor;
- HPEN oldpen,newpen;
- POINT ptStart,ptEnd,ptOne,ptTwo;
- if(!(pt1.x && pt1.y && pt2.x && pt2.y))
- return;
- memcpy((void*)&ptOne,(const void*)&pt1,sizeof(POINT));
- memcpy((void*)&ptTwo,(const void*)&pt2,sizeof(POINT));
- if(id1>id2){ //使小的数字在id1上面
- temp=id1;
- id1=id2;
- id2=temp;
- }
- ptStart.x=IDXPOS(id1);
- ptStart.y=IDYPOS(id1);
- ptEnd.x=IDXPOS(id2);
- ptEnd.y=IDYPOS(id2);
- hDC=GetDC(hParent);
- newpen=CreatePen(PS_INSIDEFRAME,3,RGB(0,255,255));
- oldpen=(HPEN)SelectObject(hDC,newpen);
- SetROP2(hDC,R2_XORPEN);
- for(ifor=0;ifor<2;ifor++){
- MoveToEx(hDC,ptStart.x,ptStart.y,NULL);
- LineTo(hDC,ptOne.x,ptOne.y);
- LineTo(hDC,ptTwo.x,ptTwo.y);
- LineTo(hDC,ptEnd.x,ptEnd.y);
- Sleep(100);
- }
- SelectObject(hDC,oldpen);
- DeleteObject(newpen);
- ReleaseDC(hParent,hDC);
- }
- bool CGridPicture::isCanLink(int id1,int id2,SEARCH_OPERATOR se,bool bfirst,int recur){
- int temp;
- int max;
- max=horzNum*vertNum;
- if(recur==0)
- return false;
- recur--;
- if(bfirst){
- if(id1>id2){ //使小的数字在id1上面
- temp=id1;
- id1=id2;
- id2=temp;
- }
- }
- //在一起并排的
- if(recur==2){
- pt1.x=IDXPOS(id1);
- pt1.y=IDYPOS(id1);
- pt2.x=IDXPOS(id2);
- pt2.y=IDYPOS(id2);
- }
- if(id1+1==id2){ //防止一行结束后托到另一行
- if(id2%horzNum!=0){
- return true;
- }
- }
- if(id1-1==id2){
- if(id1%horzNum!=0){
- return true;
- }
- }
- if(id1+horzNum==id2){
- return true;
- }
- if(id1-horzNum==id2){
- return true;
- }
- /*
- if(bfirst){ //都可以直接析出来
- if(CanDirectToTop(id1)){
- if(CanDirectToTop(id2)){
- pt1.x=IDXPOS(id1);
- pt1.y=topEdge;
- pt2.x=IDXPOS(id2);
- pt2.y=topEdge;
- return true;
- }
- }
- if(CanDirectToLeft(id1)){
- if(CanDirectToLeft(id2)){
- pt1.x=leftEdge;
- pt1.y=IDYPOS(id1);
- pt2.x=leftEdge;
- pt2.y=IDYPOS(id2);
- return true;
- }
- }
- if(CanDirectToRight(id1)){
- if(CanDirectToRight(id2)){
- pt1.x=rightEdge;
- pt1.y=IDYPOS(id1);
- pt2.x=rightEdge;
- pt2.y=IDYPOS(id2);
- return true;
- }
- }
- if(CanDirectToBottom(id1)){
- if(CanDirectToBottom(id2)){
- pt1.x=IDXPOS(id1);
- pt1.y=bottomEdge;
- pt2.x=IDXPOS(id2);
- pt2.y=bottomEdge;
- return true;
- }
- }
- }
- */
- //向上找
- if(se!=enumUP){
- temp=id1;
- while(true){
- temp=temp-horzNum;
- if(OUTOFUP(temp)){
- break;
- }else{
- if(recur==2){
- pt1.x=IDXPOS(temp);
- pt1.y=IDYPOS(temp);
- pt2.x=pt1.x;
- pt2.y=pt1.y;
- }else if(recur==1){
- pt2.x=IDXPOS(temp);
- pt2.y=IDYPOS(temp);
- }
- if(temp==id2){
- return true;
- }
- if(arrayPic[temp].resourceid !=-1)
- break;
- if(isCanLink(temp,id2,enumUP,false,recur))
- return true;
- }
- }
- }
- //向下找
- if(se!=enumDown){
- temp=id1;
- while(true){
- temp=temp+horzNum;
- if(OUTOFBOTTOM(temp)){
- break;
- }else{
- if(recur==2){
- pt1.x=IDXPOS(temp);
- pt1.y=IDYPOS(temp);
- pt2.x=pt1.x;
- pt2.y=pt1.y;
- }else if(recur==1){
- pt2.x=IDXPOS(temp);
- pt2.y=IDYPOS(temp);
- }
- if(temp==id2){
- return true;
- }
- if(arrayPic[temp].resourceid !=-1)
- break;
- if(isCanLink(temp,id2,enumDown,false,recur))
- return true;
- }
- }
- }
- //向左找
- if(se!=enumLeft){
- temp=id1;
- while(true){
- temp--;
- if(OUTOFLEFT(temp)){
- break;
- }else{
- if(recur==2){
- pt1.x=IDXPOS(temp);
- pt1.y=IDYPOS(temp);
- pt2.x=pt1.x;
- pt2.y=pt1.y;
- }else if(recur==1){
- pt2.x=IDXPOS(temp);
- pt2.y=IDYPOS(temp);
- }
- if(temp==id2){
- return true;
- }
- if(arrayPic[temp].resourceid !=-1)
- break;
- if(isCanLink(temp,id2,enumLeft,false,recur))
- return true;
- }
- }
- }
- //向右找
- if(se!=enumRight){
- temp=id1;
- while(true){
- temp++;
- if(OUTOFRIGHT(temp)){
- break;
- }else{
- if(recur==2){
- pt1.x=IDXPOS(temp);
- pt1.y=IDYPOS(temp);
- pt2.x=pt1.x;
- pt2.y=pt1.y;
- }else if(recur==1){
- pt2.x=IDXPOS(temp);
- pt2.y=IDYPOS(temp);
- }
- if(temp==id2){
- return true;
- }
- if(arrayPic[temp].resourceid !=-1)
- break;
- if(isCanLink(temp,id2,enumRight,false,recur))
- return true;
- }
- }
- }
- if(bfirst){ //都可以直接析出来
- if(CanDirectToTop(id1)){
- if(CanDirectToTop(id2)){
- pt1.x=IDXPOS(id1);
- pt1.y=topEdge;
- pt2.x=IDXPOS(id2);
- pt2.y=topEdge;
- return true;
- }
- }
- if(CanDirectToLeft(id1)){
- if(CanDirectToLeft(id2)){
- pt1.x=leftEdge;
- pt1.y=IDYPOS(id1);
- pt2.x=leftEdge;
- pt2.y=IDYPOS(id2);
- return true;
- }
- }
- if(CanDirectToRight(id1)){
- if(CanDirectToRight(id2)){
- pt1.x=rightEdge;
- pt1.y=IDYPOS(id1);
- pt2.x=rightEdge;
- pt2.y=IDYPOS(id2);
- return true;
- }
- }
- if(CanDirectToBottom(id1)){
- if(CanDirectToBottom(id2)){
- pt1.x=IDXPOS(id1);
- pt1.y=bottomEdge;
- pt2.x=IDXPOS(id2);
- pt2.y=bottomEdge;
- return true;
- }
- }
- }
- return false;
- }
- void CGridPicture::ParsePosAndInvalidate(int id,BOOL bClear){
- // InvalidateRect(hParent,&arrayPic[id].rc,TRUE);
- int vPos;
- int hPos;
- BOOL bHaveNext;
- BOOL bHavePNext;
- HDC hDC;
- HBRUSH hbr;
- HPEN hpen,oldpen;
- RECT rcfill;
- hPos=arrayPic[id].rc.left +3;
- vPos=arrayPic[id].rc.top +3;
- hDC=GetDC(hParent);
- if(!bClear){
- SetRect(&rcfill,hPos,vPos,PICTURE_WIDTH+hPos,picture_height+vPos);
- hbr=CreateSolidBrush(RGB(200,200,200));
- FillRect(hDC,&rcfill,hbr);
- DeleteObject(hbr);
- }else{
- SetRect(&rcfill,hPos,vPos,PICTURE_WIDTH+hPos,picture_height+vPos);
- hbr=CreateSolidBrush(RGB(249,254,214));
- FillRect(hDC,&rcfill,hbr);
- DeleteObject(hbr);
- }
- DRAW_EDGE(RGB(209,182,161),2,hPos,vPos,hPos,vPos+picture_height);
- DRAW_EDGE(RGB(209,182,161),2,hPos,vPos+picture_height,hPos+PICTURE_WIDTH,vPos+picture_height);
- DRAW_EDGE(RGB(255,255,255),1,hPos+PICTURE_WIDTH-2,vPos,hPos+PICTURE_WIDTH-2,vPos+picture_height);
- bHaveNext=bHavePNext=TRUE;
- if(id+1>horzNum*vertNum||(arrayPic[id+1].resourceid==-1)){
- bHaveNext=FALSE;
- }
- if((id+horzNum)<(horzNum*vertNum) && arrayPic[id+horzNum].resourceid ==-1){
- bHavePNext=FALSE;
- }
- if(!bHaveNext){
- DRAW_EDGE(RGB(248,182,94),1,hPos+PICTURE_WIDTH,vPos,hPos+PICTURE_WIDTH+4,vPos+4);
- if(!bHavePNext){
- DRAW_EDGE(RGB(248,182,94),1,hPos+PICTURE_WIDTH,vPos+picture_height,hPos+PICTURE_WIDTH+4,vPos+picture_height+4);
- }
- //fill
- if(!bHavePNext){
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH,vPos+1,hPos+PICTURE_WIDTH,vPos+picture_height+1);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+1,vPos+2,hPos+PICTURE_WIDTH+1,vPos+picture_height+2);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+2,vPos+3,hPos+PICTURE_WIDTH+2,vPos+picture_height+3);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+3,vPos+4,hPos+PICTURE_WIDTH+3,vPos+picture_height+4);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+4,vPos+5,hPos+PICTURE_WIDTH+4,vPos+picture_height+5);
- }else{
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH,vPos+1,hPos+PICTURE_WIDTH,vPos+picture_height);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+1,vPos+2,hPos+PICTURE_WIDTH+1,vPos+picture_height);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+2,vPos+3,hPos+PICTURE_WIDTH+2,vPos+picture_height);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+3,vPos+4,hPos+PICTURE_WIDTH+3,vPos+picture_height);
- DRAW_EDGE(RGB(242,203,66),1,hPos+PICTURE_WIDTH+4,vPos+5,hPos+PICTURE_WIDTH+4,vPos+picture_height);
- }
- }
- //////////////
- if(!bHavePNext){
- DRAW_EDGE(RGB(248,182,94),1,hPos,vPos+picture_height,hPos+4,vPos+picture_height+4);
- //fill
- if(!bHaveNext){
- DRAW_EDGE(RGB(238,162,64),1,hPos+1,vPos+picture_height,hPos+PICTURE_WIDTH+1,vPos+picture_height);
- DRAW_EDGE(RGB(238,162,64),1,hPos+2,vPos+picture_height+1,hPos+PICTURE_WIDTH+2,vPos+picture_height+1);
- DRAW_EDGE(RGB(238,162,64),1,hPos+3,vPos+picture_height+2,hPos+PICTURE_WIDTH+3,vPos+picture_height+2);
- DRAW_EDGE(RGB(238,162,64),1,hPos+4,vPos+picture_height+3,hPos+PICTURE_WIDTH+4,vPos+picture_height+3);
- DRAW_EDGE(RGB(238,162,64),1,hPos+5,vPos+picture_height+4,hPos+PICTURE_WIDTH+5,vPos+picture_height+4);
- }else{
- DRAW_EDGE(RGB(238,162,64),1,hPos+1,vPos+picture_height,hPos+PICTURE_WIDTH,vPos+picture_height);
- DRAW_EDGE(RGB(238,162,64),1,hPos+2,vPos+picture_height+1,hPos+PICTURE_WIDTH,vPos+picture_height+1);
- DRAW_EDGE(RGB(238,162,64),1,hPos+3,vPos+picture_height+2,hPos+PICTURE_WIDTH,vPos+picture_height+2);
- DRAW_EDGE(RGB(238,162,64),1,hPos+4,vPos+picture_height+3,hPos+PICTURE_WIDTH,vPos+picture_height+3);
- DRAW_EDGE(RGB(238,162,64),1,hPos+5,vPos+picture_height+4,hPos+PICTURE_WIDTH,vPos+picture_height+4);
- }
- }
- ///////////////////
- BitBlt(hDC,hPos,vPos,PICTURE_WIDTH,picture_height,tempDC,arrayPic[id].resourceid*PICTURE_WIDTH,0,SRCAND);
- ReleaseDC(hParent,hDC);
- }
- int CGridPicture::QueryResourceID(int ids,int idq){
- int ifor;
- for(ifor=ids;ifor<vertNum*horzNum;ifor++){
- if(arrayPic){
- if(arrayPic[ifor].resourceid==idq)
- return ifor;
- }
- }
- return -1;
- }
- void CGridPicture::FoundLink(CGridPicture *pgrid,int *idOne,int *idTwo,BOOL &bFound){
- int id1,id2,id3,id4;
- int ifor;
- #define SETID(iid1,iid2) idOne?*idOne=iid1,*idTwo=iid2:bFound=TRUE;
- for(ifor=0;ifor<picNum;ifor++){
- id1=QueryResourceID(0,ifor);
- id2=QueryResourceID(id1+1,ifor);
- id3=QueryResourceID(id2+1,ifor);
- id4=QueryResourceID(id3+1,ifor);
- if(id3==-1){ //还有一对
- if(id1==-1){ //一对都没有
- continue;
- }else{ //一对
- if(pgrid->isCanLink(id1,id2,enumAny)){
- bFound=TRUE;
- SETID(id1,id2);
- break;
- }
- }
- }else{//两对
- if(pgrid->isCanLink(id1,id2,enumAny)){
- bFound=TRUE;
- SETID(id1,id2);
- break;
- }
- if(pgrid->isCanLink(id1,id3,enumAny)){
- bFound=TRUE;
- SETID(id1,id3);
- break;
- }
- if(pgrid->isCanLink(id1,id4,enumAny)){
- bFound=TRUE;
- SETID(id1,id4);
- break;
- }
- if(pgrid->isCanLink(id2,id3,enumAny)){
- bFound=TRUE;
- SETID(id2,id3);
- break;
- }
- if(pgrid->isCanLink(id2,id4,enumAny)){
- bFound=TRUE;
- SETID(id2,id4);
- break;
- }
- if(pgrid->isCanLink(id3,id4,enumAny)){
- bFound=TRUE;
- SETID(id3,id4);
- break;
- }
- }
- }
- }
- LRESULT CALLBACK CGridPicture::ThreadWatch(LPARAM lParam){
- CGridPicture *pgrid;
- BOOL bFound;
- pgrid=(CGridPicture*)lParam;
- while(true){
- bFound=FALSE;
- FoundLink(pgrid,NULL,NULL,bFound);
- if(bTerminate)
- break;
- if(bFound==FALSE && timeElapse.bPause ==FALSE){
- pgrid->Shuffle();
- InvalidateRect(hParent,NULL,TRUE);
- }
- Sleep(2000);
- }
- return 0;
- }
- LRESULT CALLBACK CGridPicture::ThreadExplore(LPARAM lParam){
- HDC hTempDC;
- HDC hDesktop;
- HBITMAP hExplore;
- BITMAP bm;
- PEXPLORE_STRUCT pe;
- RECT rcInvalid;
- int ifor;
- int pic_height,pic_width;
- pe=(PEXPLORE_STRUCT)lParam;
- hExplore=LoadBitmap((HINSTANCE)GetModuleHandle(NULL),(LPCTSTR)IDB_EXPLORE);
- if(hExplore){
- GetObject(hExplore,sizeof(bm),(LPVOID)&bm);
- pic_width=bm.bmWidth;
- pic_height=bm.bmHeight;
- pic_width/=5;
- hDesktop=GetDC(pe->hParent);
- hTempDC=CreateCompatibleDC(hDesktop);
- SelectObject(hTempDC,hExplore);
- for(ifor=0;ifor<5;ifor++){
- BitBlt(hDesktop,pe->rc.left ,pe->rc.top,pe->rc.right-pe->rc.left,pe->rc.bottom-pe->rc.top,hTempDC,pic_width*ifor,0,SRCCOPY);
- Sleep(100);
- }
- ReleaseDC(pe->hParent,hDesktop);
- memcpy((void*)&rcInvalid,(const void*)&pe->rc,sizeof(RECT));
- rcInvalid.left -=2;
- rcInvalid.top -=2;
- rcInvalid.bottom +=2;
- rcInvalid.right +=2;
- InvalidateRect(pe->hParent,&rcInvalid,TRUE);
- }
- delete pe;
- return 0;
- }
- void CGridPicture::Explore(int id1,int id2){
- POINT ptPos;
- SetSelect(id1);
- ptPos.x=(arrayPic[id2].rc.right-arrayPic[id2].rc.left)/2+arrayPic[id2].rc.left;
- ptPos.y=(arrayPic[id2].rc.bottom -arrayPic[id2].rc.top)/2+arrayPic[id2].rc.top;
- Picture_Click(ptPos);
- }
- void CGridPicture::SetSelect(int id){
- int invalidid;
- if(focusid!=-1){
- invalidid=focusid;
- focusid=-1;
- InvalidateRect(hParent,(CONST RECT*)&arrayPic[invalidid].rc,TRUE);
- }
- focusid=id;
- InvalidateRect(hParent,(const RECT*)&arrayPic[id].rc,TRUE);
- }
- void CGridPicture::Discard(){
- bTerminate=TRUE;
- }
- void CGridPicture::Play(LPCTSTR pszRes){
- HRSRC hsrc;
- HGLOBAL hGlobal;
- LPVOID pData;
- if(bSound){
- hsrc=FindResource(GetModuleHandle(NULL),(LPCTSTR)pszRes,(LPCTSTR)"RT_JPGDATA");
- if(hsrc){
- hGlobal=LoadResource(GetModuleHandle(NULL),hsrc);
- if(hGlobal){
- pData=GlobalLock(hGlobal);
- PlaySound((LPCTSTR)pData,GetModuleHandle(NULL),SND_ASYNC|SND_NOWAIT|SND_MEMORY);
- GlobalUnlock(hGlobal);
- // GlobalFree(hGlobal);
- }
- FreeResource(hsrc);
- }
- }
- }
- int CGridPicture::vertNum =0;
- int CGridPicture::horzNum =0;
- int CGridPicture::picNum =0;
- HWND CGridPicture::hParent =NULL;
- PSINGLE_BLOCK CGridPicture::arrayPic =NULL;
- BOOL CGridPicture::bTerminate =FALSE;