首页 > 代码库 > windows程序扫雷程序设计
windows程序扫雷程序设计
详细资源见:http://download.csdn.net/detail/zhouyelihua/7604765
在学习windows程序设计中。我们希望通过一个完整的程序来学习windows API这是本人在上杨力祥老师的课程是自己写的扫雷程序。
以下是。cpp文件
#include <windows.h> #include"resource.h" #include<stdlib.h> #pragma comment( lib,"winmm.lib") //地雷的结构 class Mine{ int x,y; int value; bool zeroCursor; bool dis_or_not; int cur3; public: bool getZerocursor(); void setZerocursor(bool cur); void setDisOrNot(bool d); void setX(int xx); void setY(int yy); void setValue(int val); void dispaly(); int getvalue() ; bool getdisval() ; int getcur3(); void setcur3(int d); }; int Mine::getcur3() { return cur3; } void Mine::setcur3(int d) { cur3=d; } bool Mine::getZerocursor() { return zeroCursor; } void Mine::setZerocursor(bool cur) { zeroCursor=cur; } bool Mine::getdisval() { return (dis_or_not); } void Mine::setDisOrNot(bool d) { dis_or_not=d; } void Mine::setX(int xx) { x=xx; } void Mine::setY(int yy) { y=yy; } void Mine::setValue(int val) { value=http://www.mamicode.com/val;>{ TranslateMessage( &msg ); //将虚拟键消息转换为字符消息 DispatchMessage( &msg ); //分发一个消息给窗体程序。
} else { g_tNow = GetTickCount(); //获取当前系统时间 if(g_tNow-g_tPre >= 1000) //当此次循环执行与上次画图时间相差0.03秒时再进行重绘操作 TimerProc(hwnd); } } return msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps ; RECT rect1 ; HPEN hPen1; HBRUSH hbrush1; HMENU hMenu; HINSTANCE hInstance; static int iSelection=ID_40002; static int iSelection1=ID_40008; //int i,j,k; int locr; wchar_t szbuf[7]={0}; int x,y; switch (message) { case WM_CREATE: hInstance = ((LPCREATESTRUCT) lParam)->hInstance ; hBitmap1 = LoadBitmap (hInstance,MAKEINTRESOURCE (IDB_BITMAP1)) ; hBitmap2=LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP2)); hBitmap3=LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP3)); hBitmap4= LoadBitmap (hInstance,MAKEINTRESOURCE (IDB_BITMAP4)) ; hBitmap5=LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP5)); hBitmap6=LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP6)); return 0; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; SetRect(&rect1,0,0,18+column*16,row*16+59); hbrush1=CreateSolidBrush(RGB(192,192,192)); FillRect(hdc,&rect1,hbrush1); hPen1=CreatePen(PS_SOLID,3,RGB(128,128,128)); SelectObject(hdc,hPen1); Rectangle(hdc,6,6,18+column*16-6,41); SetRect(&rect1,8,8,18+column*16-8,39); FillRect(hdc,&rect1,hbrush1); Rectangle(hdc,6,50,18+column*16-6,row*16+59-6); SelectObject(hdcMem3,hBitmap3); //{column*16-30,12,column*16+9,35}; BitBlt (hdc, 12 ,12, 13, 23, hdcMem3, 0,253-23*(NumMine/100%10), SRCCOPY) ; BitBlt (hdc,25 ,12, 13, 23, hdcMem3, 0,253-23*(NumMine/10%10), SRCCOPY) ; BitBlt (hdc, 38 ,12, 13, 23, hdcMem3, 0,253-23*(NumMine%10), SRCCOPY) ; BitBlt (hdc,column*16-33 ,12, 13, 23, hdcMem3, 0,23, SRCCOPY) ; BitBlt (hdc,column*16-20 ,12, 13, 23, hdcMem3, 0,23, SRCCOPY) ; BitBlt (hdc,column*16-7 ,12, 13, 23, hdcMem3, 0,23, SRCCOPY) ; SelectObject(hdcMem5,hBitmap5); BitBlt (hdc, column*8-3,10 , 24, 24, hdcMem5, 0,96, SRCCOPY) ; display(mineMatrix,row,column,hdc); EndPaint (hwnd, &ps) ; return 0 ; case WM_COMMAND: hMenu=GetMenu(hwnd); hInstance=GetModuleHandle(0); switch(LOWORD(wParam)) { // CheckMenuItem (hMenu,LOWORD (wParam), MF_CHECKED) ; case ID_40001://开局 hdc=GetDC(hwnd); // CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ; // iSelection = LOWORD (wParam) ; CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ; iSelection = LOWORD (wParam) ; CheckMenuItem (hMenu, iSelection, MF_CHECKED) ; //KillTimer(hwnd,ID_TIMER1); Tswitch=0; time=0; pickMineNum=0; BitBlt (hdc,column*16-33 ,12, 13, 23, hdcMem3, 0,23, SRCCOPY) ; BitBlt (hdc,column*16-20 ,12, 13, 23, hdcMem3, 0,23, SRCCOPY) ; BitBlt (hdc,column*16-7 ,12, 13, 23, hdcMem3, 0,23, SRCCOPY) ; initMineMatrix(mineMatrix,row, column); display(mineMatrix, row, column, hdc); landMineFunc(mineMatrix, row, column, NumMine);//布雷函数 SetMinesAround(mineMatrix,row,column); DeleteDC(hdc); return 0; case ID_40002://0基础 CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ; iSelection = LOWORD (wParam) ; CheckMenuItem (hMenu, iSelection, MF_CHECKED) ; column=9; row=9; NumMine=10; Tswitch=0; time=0; WindowSize(hwnd,18+column*16,row*16+59); initMineMatrix(mineMatrix, row, column); landMineFunc(mineMatrix, row, column, NumMine); SetMinesAround(mineMatrix,row,column); return 0; case ID_40003://中级 CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ; iSelection = LOWORD (wParam) ; CheckMenuItem (hMenu, iSelection, MF_CHECKED) ; column=16; row=16; NumMine=40; Tswitch=0; time=0; WindowSize(hwnd,18+column*16,row*16+59); initMineMatrix(mineMatrix, row, column); landMineFunc(mineMatrix, row, column, NumMine); SetMinesAround(mineMatrix,row,column); return 0; case ID_Menu://高级 CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ; iSelection = LOWORD (wParam) ; CheckMenuItem (hMenu, iSelection, MF_CHECKED) ; column=30; row=16; NumMine=99; Tswitch=0; time=0; WindowSize(hwnd,18+column*16,row*16+59); initMineMatrix(mineMatrix, row, column); landMineFunc(mineMatrix, row, column, NumMine); SetMinesAround(mineMatrix,row,column); return 0; case ID_40005: CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ; iSelection = LOWORD (wParam) ; CheckMenuItem (hMenu, iSelection, MF_CHECKED) ; DialogBox(hInstance,MAKEINTRESOURCE(IDD_USERDEFINE),NULL,dlgProc); Tswitch=0; time=0; WindowSize(hwnd,18+column*16,row*16+59); initMineMatrix(mineMatrix, row, column); landMineFunc(mineMatrix, row, column, NumMine); SetMinesAround(mineMatrix,row,column); return 0; case ID_40008: CheckMenuItem (hMenu, iSelection1, MF_UNCHECKED) ; iSelection1 = LOWORD (wParam) ; CheckMenuItem (hMenu, iSelection1, MF_CHECKED) ; sound=!sound; return 0; case ID_40009: DialogBox(hInstance,MAKEINTRESOURCE(IDD_HERO),NULL,HeroProc); return 0; case ID_40014://关于扫雷 MessageBox(hwnd,TEXT("研究所:深圳先进技术研究\n实验室:智能设计与计算机视觉实验室\n姓名 :叶于平"),TEXT("关于扫雷"),NULL); } break; case WM_LBUTTONDOWN: hdc=GetDC(hwnd); x= LOWORD (lParam) ; y = HIWORD (lParam) ; clickRespond(mineMatrix,x, y,hdc,hwnd); DeleteDC(hdc); return 0; case WM_RBUTTONDOWN: hdc=GetDC(hwnd); x= LOWORD (lParam) ; y = HIWORD (lParam) ; if(dis==false) if((x>=9)&&(x<9+16*column)&&(y>=52)&&(y<52+16*row)) { locr=((y-52)/16)*column+(x-9)/16; if(mineMatrix[locr].getdisval()==false) { if(mineMatrix[locr].getcur3()==-1) mineMatrix[locr].setcur3(0); if(mineMatrix[locr].getcur3()==0) { BitBlt (hdc,((x-9)/16)*16+9,((y-52)/16)*16+52, 16, 16, hdcMem1, 0,16, SRCCOPY) ; } if(mineMatrix[locr].getcur3()==1) { BitBlt (hdc,((x-9)/16)*16+9,((y-52)/16)*16+52, 16, 16, hdcMem1, 0,32, SRCCOPY) ; } if(mineMatrix[locr].getcur3()==2) { BitBlt (hdc,((x-9)/16)*16+9,((y-52)/16)*16+52, 16, 16, hdcMem1, 0,0, SRCCOPY) ; } if(mineMatrix[locr].getcur3()==2) mineMatrix[locr].setcur3(0); else mineMatrix[locr].setcur3(mineMatrix[locr].getcur3()+1); } } DeleteDC(hdc); return 0; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; } void initMineMatrix(Mine*mineMat,int row,int column) { int i; dis=false; for(i=0;i<row*column;i++) { mineMat[i].setX(i/column); mineMat[i].setY(i%column); mineMat[i].setValue(0); mineMat[i].setDisOrNot(false); mineMat[i].setZerocursor(false); mineMat[i].setcur3(-1); } } //----输入:窗体的宽和高.而且设置雷区矩阵大小。
void WindowSize(HWND hwnd,int width,int height) { RECT sizeWin; RECT sizeClient; int borderWidth,borderHeight; GetWindowRect(hwnd,&sizeWin); GetClientRect(hwnd,&sizeClient); borderWidth = (sizeWin.right-sizeWin.left)-(sizeClient.right-sizeClient.left); borderHeight = (sizeWin.bottom-sizeWin.top)-(sizeClient.bottom-sizeClient.top); SetWindowPos(hwnd,0,0,0,borderWidth+width,borderHeight+height,SWP_NOMOVE|SWP_NOZORDER); mineMatrix=new Mine[row*column]; } //======显示函数========= void display(Mine*mineMat,int row,int column,HDC hdc) { //HDC hdcMemOld; SelectObject (hdcMem1, hBitmap1) ; int i, j; for(j=0;j<row;j++) for(i=0;i<column;i++) //if((mineMat[i*column+j].getdisval())==false)//假设不显示 则是显示用盖住 BitBlt (hdc, i*16+9, j*16+52, 16, 16, hdcMem1, 0,0, SRCCOPY) ; } void clickRespond(Mine*mineMat,int x,int y,HDC hdc,HWND hwnd) { int i; //点击区域落在笑脸区域;column*8-3,10 if((x>column*8-3)&&(x<column*8+20)&&(y>10)&&(y<33)) { hdc=GetDC(hwnd); KillTimer(hwnd,ID_TIMER1); Tswitch=0; time=0; BitBlt (hdc,column*16-33 ,12, 13, 23, hdcMem3, 0,23, SRCCOPY) ; BitBlt (hdc,column*16-20 ,12, 13, 23, hdcMem3, 0,23, SRCCOPY) ; BitBlt (hdc,column*16-7 ,12, 13, 23, hdcMem3, 0,23, SRCCOPY) ; BitBlt (hdc, column*8-3,10 , 24, 24, hdcMem5, 0,96, SRCCOPY) ; initMineMatrix(mineMat,row, column); display(mineMat, row, column, hdc); landMineFunc(mineMat, row, column, NumMine);//布雷函数 SetMinesAround(mineMat,row,column); DeleteDC(hdc); } //点击区域落在雷区内。 if((x>=9)&&(x<9+16*column)&&(y>=52)&&(y<52+16*row)) { //Beep(1000,1000); if(Tswitch!=2) Tswitch=1; // SetTimer (hwnd, ID_TIMER1, 1000, TimerProc); int loc=((y-52)/16)*column+(x-9)/16; int value=http://www.mamicode.com/mineMat[loc].getvalue();>
VOID TimerProc(HWND hwnd) { //RECT rect={column*16-30,12,column*16+9,35}; //wchar_t szbuf[7]={0}; //wsprintf(szbuf,TEXT("%03d"),time); HDC hdc; if( Tswitch==2 || Tswitch ==3) { //KillTimer(hwnd,ID_TIMER1); time=0; } if(Tswitch==1) { time+=1; hdc=GetDC(hwnd); //SetTextColor(hdc,RGB(255,0,0)); //SetBkColor(hdc,RGB(12,12,12)); //wsprintf(szbuf,TEXT("%03d"),time); //DrawText(hdc,szbuf,3,&rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER); if(time<1000){ BitBlt (hdc, column*16-33 ,12, 13, 23, hdcMem3, 0,253-23*(time/100%10), SRCCOPY) ; BitBlt (hdc,column*16-20 ,12, 13, 23, hdcMem3, 0,253-23*(time/10%10), SRCCOPY) ; BitBlt (hdc, column*16-7 ,12, 13, 23, hdcMem3, 0,253-23*(time%10), SRCCOPY) ; if(sound==true) PlaySound (TEXT ("432.wav"), NULL, SND_FILENAME | SND_ASYNC) ; } ReleaseDC(hwnd,hdc); } g_tPre = GetTickCount(); } //=====布雷函数======= void landMineFunc(Mine*mineMat,int row,int column,int mineNum) { int i=0; int ii,jj,cursor; int val; srand(GetTickCount()); for(i=0;i<mineNum;i++) { do{ ii=rand()%row; jj=rand()%column; cursor=ii*column+jj; val=mineMat[cursor].getvalue(); }while(val==-1); mineMat[cursor].setValue(-1); } } void SetMinesAround(Mine*mat,int row,int column) { int val; int tmp; int sum; int i,j; for(i=0;i<row;i++) for(j=0;j<column;j++) { sum=0; val=mat[i*column+j].getvalue(); if(val!=-1)//当这个位置不是地雷时候,将四周有的地雷数目填入当中。
{ if((i-1>=0)&&(j-1>=0)) { tmp=mat[(i-1)*column+j-1].getvalue(); if(tmp==-1) sum++; } if((j-1>=0)) { tmp=mat[(i)*column+j-1].getvalue(); if(tmp==-1) sum++; } if((i+1<row)&&(j-1>=0)) { tmp=mat[(i+1)*column+j-1].getvalue(); if(tmp==-1) sum++; } if((i-1>=0)) { tmp=mat[(i-1)*column+j].getvalue(); if(tmp==-1) sum++; } if((i+1<row)) { tmp=mat[(i+1)*column+j].getvalue(); if(tmp==-1) sum++; } if((i-1>=0)&&(j+1<column)) { tmp=mat[(i-1)*column+j+1].getvalue(); if(tmp==-1) sum++; } if((j+1<column)) { tmp=mat[(i)*column+j+1].getvalue(); if(tmp==-1) sum++; } if((i+1<row)&&(j+1<column)) { tmp=mat[(i+1)*column+j+1].getvalue(); if(tmp==-1) sum++; } mat[i*column+j].setValue(sum); } //if(i) } } void SearchZeroMine(Mine*mat,int row,int column,HDC hdc,int i,int j) { int ValTmp; bool ZeroTmp; if(i>=0) { if((i>=0)&&(j>=0)) BitBlt (hdc,j*16+9,i*16+52, 16, 16, hdcMem1, 0,16*15, SRCCOPY) ; mat[i*column+j].setDisOrNot(true); } mat[i*column+j].setZerocursor(true); if((i-1>=0)&&(j-1>=0)) { ValTmp=mat[(i-1)*column+j-1].getvalue(); ZeroTmp=mat[(i-1)*column+j-1].getZerocursor(); if((ValTmp==0)&&(ZeroTmp==false)) { SearchZeroMine(mat, row, column,hdc,i-1,j-1); } if((0<ValTmp)&&(ValTmp<9)) { if((i>=0)&&(j>=0)) BitBlt (hdc,(j-1)*16+9,(i-1)*16+52, 16, 16, hdcMem1, 0,16*15-ValTmp*16, SRCCOPY) ; mat[(i-1)*column+j-1].setDisOrNot(true); } } if((j-1>=0)) { ValTmp=mat[(i)*column+j-1].getvalue(); ZeroTmp=mat[(i)*column+j-1].getZerocursor(); if((ValTmp==0)&&(ZeroTmp==false)) { SearchZeroMine(mat, row, column,hdc,i,j-1); } if((0<ValTmp)&&(ValTmp<9)) { if((i>=0)&&(j>=0)) BitBlt (hdc,(j-1)*16+9,(i)*16+52, 16, 16, hdcMem1, 0,16*15-ValTmp*16, SRCCOPY) ; mat[(i)*column+j-1].setDisOrNot(true); } } if((i+1<column)&&(j-1>=0)) { ValTmp=mat[(i+1)*column+j-1].getvalue(); ZeroTmp=mat[(i+1)*column+j-1].getZerocursor(); if((ValTmp==0)&&(ZeroTmp==false)) { SearchZeroMine(mat, row, column,hdc,i+1,j-1); } if((0<ValTmp)&&(ValTmp<9)) { if((i>=0)&&(j>=0)) BitBlt (hdc,(j-1)*16+9,(i+1)*16+52, 16, 16, hdcMem1, 0,16*15-ValTmp*16, SRCCOPY) ; mat[(i+1)*column+j-1].setDisOrNot(true); } } if((i-1>=0)) { ValTmp=mat[(i-1)*column+j].getvalue(); ZeroTmp=mat[(i-1)*column+j].getZerocursor(); if((ValTmp==0)&&(ZeroTmp==false)) { SearchZeroMine(mat, row, column,hdc,i-1,j); } if((0<ValTmp)&&(ValTmp<9)) { mat[(i-1)*column+j].setDisOrNot(true); if((i>=0)&&(j>=0)) BitBlt (hdc,(j)*16+9,(i-1)*16+52, 16, 16, hdcMem1, 0,16*15-ValTmp*16, SRCCOPY) ; } } if((i+1<row)) { ValTmp=mat[(i+1)*column+j].getvalue(); ZeroTmp=mat[(i+1)*column+j].getZerocursor(); if((ValTmp==0)&&(ZeroTmp==false)) { SearchZeroMine(mat, row, column,hdc,i+1,j); } if((0<ValTmp)&&(ValTmp<9)) { if((i>=0)&&(j>=0)) BitBlt (hdc,(j)*16+9,(i+1)*16+52, 16, 16, hdcMem1, 0,16*15-ValTmp*16, SRCCOPY) ; mat[(i+1)*column+j].setDisOrNot(true); } } if((i-1>=0)&&(j+1<column)) { ValTmp=mat[(i-1)*column+j+1].getvalue(); ZeroTmp=mat[(i-1)*column+j+1].getZerocursor(); if((ValTmp==0)&&(ZeroTmp==false)) { SearchZeroMine(mat, row, column,hdc,i-1,j+1); } if((0<ValTmp)&&(ValTmp<9)) { if((i>=0)&&(j>=0)) BitBlt (hdc,(j+1)*16+9,(i-1)*16+52, 16, 16, hdcMem1, 0,16*15-ValTmp*16, SRCCOPY) ; mat[(i-1)*column+j+1].setDisOrNot(true); } } if((j+1<column)) { ValTmp=mat[(i)*column+j+1].getvalue(); ZeroTmp=mat[(i)*column+j+1].getZerocursor(); if((ValTmp==0)&&(ZeroTmp==false)) { SearchZeroMine(mat, row, column,hdc,i-1,j+1); } if((0<ValTmp)&&(ValTmp<9)) { if((i>=0)&&(j>=0)) BitBlt (hdc,(j+1)*16+9,(i)*16+52, 16, 16, hdcMem1, 0,16*15-ValTmp*16, SRCCOPY) ; mat[(i)*column+j+1].setDisOrNot(true); } } if((i+1<row)&&(j+1<column)) { ValTmp=mat[(i+1)*column+j+1].getvalue(); ZeroTmp=mat[(i+1)*column+j+1].getZerocursor(); if((ValTmp==0)&&(ZeroTmp==false)) { SearchZeroMine(mat, row, column,hdc,i-1,j+1); } if((0<ValTmp)&&(ValTmp<9)) { if((i>=0)&&(j>=0)) BitBlt (hdc,(j+1)*16+9,(i+1)*16+52, 16, 16, hdcMem1, 0,16*15-ValTmp*16, SRCCOPY) ; mat[(i+1)*column+j+1].setDisOrNot(true); } } }
详细文件及见下载地址:
http://download.csdn.net/detail/zhouyelihua/7604765
http://download.csdn.net/detail/zhouyelihua/7604765
windows程序扫雷程序设计