windows程序扫雷程序设计

时间:2022-01-08 03:18:48

在学习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=val; } void Mine::dispaly() { if(dis_or_not==true)//假设这块被翻起了,,那么显示这一块的值 { ; } else{//没有被翻过。那么遮盖 ; } ; } int Mine::getvalue() { return value; } //=======函数声明=========== void SearchZeroMine(Mine*mat,int row,int column,HDC hdc,int x,int y); void SetMinesAround(Mine*mat,int row,int coulumn); LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; void display(Mine*mineMat,int row,int column,HDC hdc); void WindowSize(HWND hwnd,int width,int height); void initMineMatrix(Mine*mineMat,int row,int column); void clickRespond(Mine*mineMat,int x,int y,HDC hdc,HWND hwnd); VOID TimerProc(HWND hwnd); void landMineFunc(Mine*mineMat,int row,int column,int mineNum); //======全局变量声明============ static int row,column,NumMine; static int Tswitch=0; static int time; static int pickMineNum=0; static bool sound=false; Mine* mineMatrix; static bool dis;//推断是否踩雷了。踩了则不继续 static HBITMAP hBitmap1,hBitmap2,hBitmap3,hBitmap4,hBitmap5,hBitmap6; static HDC hdcMem1,hdcMem2,hdcMem3,hdcMem4,hdcMem5,hdcMem6; DWORD g_tPre=0,g_tNow=0; //声明l两个函数来记录时间,g_tPre记录上一次画图的时间。g_tNow记录此次准备画图的时间 //=======改变窗体大小的函数======= //=======初始化雷区矩阵========= BOOL CALLBACK HeroProc(HWND hDlg,UINT msg,WPARAM wParam,LPARAM lParam) //英雄榜 { switch(msg) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDOK: case IDCANCEL: EndDialog(hDlg,0); return TRUE; } } return false; } BOOL CALLBACK dlgProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam) { // BOOL flag; int rowtmp,coltmp,minetmp; switch (msg) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: switch (LOWORD(wParam)) { case IDOK: rowtmp=GetDlgItemInt(hwnd,IDC_EDIT1,NULL,true); coltmp=GetDlgItemInt(hwnd,IDC_EDIT2,NULL,true); minetmp=GetDlgItemInt(hwnd,IDC_EDIT3,NULL,true); if((rowtmp>=9)&&(coltmp>=9)&&(rowtmp*coltmp>minetmp)) { row=rowtmp; column=coltmp; NumMine=minetmp; } EndDialog(hwnd, IDOK); case IDCANCEL: EndDialog(hwnd, IDOK); return TRUE; } break; case WM_DESTROY: return TRUE; } return FALSE; } int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("winmine") ; HWND hwnd ; MSG msg= { 0 };; WNDCLASS wndclass ; HMENU hMenu; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (hInstance,MAKEINTRESOURCE(IDI_ICON1)) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1)); if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, // window class name TEXT ("扫雷"), // window caption WS_OVERLAPPED |WS_CAPTION |WS_SYSMENU| WS_MINIMIZEBOX, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle hMenu, // window menu handle hInstance, // program instance handle NULL) ; // creation parameters row=9; column=9; HDC hdc=GetDC(hwnd); hdcMem1 = CreateCompatibleDC (hdc) ; hdcMem2 = CreateCompatibleDC (hdc) ; hdcMem3 = CreateCompatibleDC (hdc) ; hdcMem4 = CreateCompatibleDC (hdc) ; hdcMem5 = CreateCompatibleDC (hdc) ; hdcMem6 = CreateCompatibleDC (hdc) ; WindowSize(hwnd,18+column*16,row*16+59); NumMine=10; initMineMatrix(mineMatrix, row, column); landMineFunc(mineMatrix, row, column, NumMine); SetMinesAround(mineMatrix,row,column); ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while( msg.message != WM_QUIT ) //使用while循环,假设消息不是WM_QUIT消息,就继续循环 { if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ) //查看应用程序消息队列,有消息时将队列中的消息派发出去。

{ TranslateMessage( &msg ); //将虚拟键消息转换为字符消息 DispatchMessage( &msg ); //分发一个消息给窗体程序。