扫雷源码--main.cpp

/****************************************************************************
	created:	2011/12/16
	complete:   2011/12/23
	filename    Main.cpp
	file path:	project\WINMINE
	file base:	Main
	file ext:	cpp
	author:	    zpd1976	
	
	purpose:    give me a job	
****************************************************************************/

//#define  WINVER  0x500  //for WM_UNINITMENUPOPUP

#include <windows.h>
//#include <windowsx.h>
#include <stdio.h>
#include <tchar.h>
#include <process.h>

// for sndPlaySound
#pragma comment(lib, "winmm.lib")

//for vc 2005
#if _MSC_VER <= 1200
#define SPRINTF(d,s,n){ _stprintf(d,s,n); }
#else
#define SPRINTF(d,s,n){ _stprintf_s(d,s,n); }
#endif

#define SAFE_DELETE_ARRAY(p){ if(p) { delete[](p);(p)=NULL; } }

//size
#define   MINEW      16
#define   FACEW      24
#define   CAP_H      54
#define   WHOLEMAP_W   (s_nMap_W * MINEW)
#define   WHOLEMAP_H   (s_nMap_H * MINEW)
#define   WIN_W        (WHOLEMAP_W+20)
#define   WIN_H        (WHOLEMAP_H+CAP_H+8)

#define   UNSEL      0
#define   HONGQI     1
#define   MARK       2 
#define   REDMINE     3
#define   ERRORMINE   4
#define   NORMINE     5
#define   SELMARK    6
#define   NUM8       7
#define   NUM7       8
#define   NUM6       9
#define   NUM5       10
#define   NUM4       11
#define   NUM3       12
#define   NUM2       13
#define   NUM1       14
#define   NUM0       15

#define   LEVEL1_W    9
#define   LEVEL1_H    9
#define   LEVEL2_W    16
#define   LEVEL2_H    16
#define   LEVEL3_W    30
#define   LEVEL3_H    16
#define   LEVEL1_COUNT    9
#define   LEVEL2_COUNT    40
#define   LEVEL3_COUNT    99

#define   MINE_KEY     (TEXT("Software\\Microsoft\\Winmine"))

#define   GAMERUN       0
#define   GAMEOVER      1
#define   GAMEWIN       2

static HINSTANCE s_hInst;

static POINT     s_ptCur;
static HBITMAP   s_hbmMine   = NULL;
static HBITMAP   s_hbmFace   = NULL;
static HBITMAP   s_hbmTime   = NULL;
static HDC       s_hdcWindow = NULL;
static HDC       s_hdcFace   = NULL;
static HDC       s_hdcMine   = NULL;
static HDC       s_hdcTime   = NULL;
static BOOL      s_bLKeyDown      = FALSE;
static BOOL      s_bRKeyDown      = FALSE;
static BOOL      s_bDoubleKeyDown = FALSE;
static DWORD     s_dwLevel        = 0;
static DWORD     s_dwTime         = 0;
static DWORD     s_dwLastTime     = 0;
static BOOL      s_bStartTimer    = FALSE;
static DWORD     s_dwState        = GAMERUN;
static DWORD     s_dwHongQiCount  = 0;
static int       s_nMap_W         = LEVEL1_W;
static int       s_nMap_H         = LEVEL1_H;
static DWORD     s_dwMineCount    = LEVEL1_COUNT;

//regedit
static HKEY      s_key = NULL;
static DWORD     s_WindowPX = 50;
static DWORD     s_WindowPY = 50;
static RECT      s_rcFace = { 0, 0, 0, 0 };
static RECT      s_rcWholeMine = { 0, 0, 0, 0 };
static BOOL      s_bPlayWav = 0;
static BOOL      s_bMarker  = 0;

//for thread
static HANDLE    s_hThread;

static BOOL      s_bPlayGameOver = FALSE;
static BOOL      s_bPlayGameWin  = FALSE;
static BOOL      s_bPlayTimer    = FALSE;

typedef struct  { int id; int px; int py; int ds; } Mine;

Mine  mine[30][30] = {0,0,0,0};

HRESULT WinInit( HWND* phWnd );
void InitMineRect( HWND, DWORD level );
void GetMousePosition( HWND hWnd );
void Draw();
void DrawTime();
void DrawFace();
void DrawHongqiCount();
void ReDrawFrame();
void LoadBmp( HWND );
int  CheckMineCount( int x, int y );
int  CheckNoMineRect( int x, int y );
int  CheckHongqiCount( int x, int y );
void CheckClickSpace();

//return false to gameover
BOOL SetZhouweiDisplay( int x, int y );
void CreateAboutDlg( HWND );
void CreateHeroDlg( HWND );
void CreateInputDlg( HWND );
void CreateEditDlg( HWND );
long CALLBACK AboutDlgProc( HWND, UINT msg, WPARAM, LPARAM );
long CALLBACK EditDlgProc(  HWND, UINT msg, WPARAM, LPARAM );
long CALLBACK InputDlgProc( HWND, UINT msg, WPARAM, LPARAM );
long CALLBACK HeroDlgProc(  HWND, UINT msg, WPARAM, LPARAM );

void ResetWindowSize( HWND );
void SetLevel( int level );
void ProcessGameOver();
void CheckGameWin( HWND );
void OnInitMenuPopup( HWND );
void ReadRegKey(  TCHAR* strName, LPBYTE strValue, DWORD dwLength );
void WriteRegKey( TCHAR* strName, TCHAR* strValue, DWORD dwLength );

/***************************************************************************/
void LoadWavToMem( TCHAR** ppData, UINT id )
{
	HRSRC   hResInfo;
	HGLOBAL hResData;
	DWORD   dwSize;
	LPVOID   pvRes;
	
	// Loading it as a resource
	hResInfo = FindResource( NULL, MAKEINTRESOURCE(id), TEXT("WAVE") );	

	if (NULL != hResInfo)
	{
		hResData = LoadResource( NULL, hResInfo );			
		dwSize   = SizeofResource( NULL, hResInfo ); 			
		pvRes    = LockResource( hResData );
		
		if (NULL != pvRes)
		{
			*ppData  = new TCHAR[ dwSize ];
			memcpy( *ppData, pvRes, dwSize );
		}
	}
}


/***************************************************************************/
void RunThread(LPVOID)
{
	sndPlaySound( NULL,SND_MEMORY );

#pragma warning ( disable: 4127 )
	while( TRUE )
#pragma warning(default: 4127)
	{
		if (s_bPlayGameOver)
		{
			sndPlaySound( MAKEINTRESOURCE(434), SND_RESOURCE );
			s_bPlayGameOver = FALSE;
		}
		else if(s_bPlayGameWin)
		{
			sndPlaySound( MAKEINTRESOURCE(433), SND_RESOURCE );
			s_bPlayGameWin = FALSE;
		}
		else if (s_bPlayTimer)
		{
			sndPlaySound( MAKEINTRESOURCE(432), SND_RESOURCE );
			s_bPlayTimer = FALSE;
		}

		Sleep(1);

	}

}

/***************************************************************************/
void SetLevel( int level )
{
	switch (level)
	{
	case 0:
		s_nMap_W      = LEVEL1_W;
		s_nMap_H      = LEVEL1_H;
		s_dwMineCount = LEVEL1_COUNT;
		break;
		
	case 1:
		s_nMap_W      = LEVEL2_W;
		s_nMap_H      = LEVEL2_H;
		s_dwMineCount = LEVEL2_COUNT;
		break;
		
	case 2:
		s_nMap_W      = LEVEL3_W;
		s_nMap_H      = LEVEL3_H;
		s_dwMineCount = LEVEL3_COUNT;
		break;

	case 3:
		//s_nMap_W   =LEVEL3_W;
		//s_nMap_H   =LEVEL3_H;
		//s_dwMineCount=LEVEL3_COUNT;
		break;
		
	default:
		s_nMap_W      = LEVEL1_W;
		s_nMap_H      = LEVEL1_H;
		s_dwMineCount = LEVEL1_COUNT;
		break;
	}
}

/***************************************************************************/
LRESULT CALLBACK WndProc( HWND hWnd, UINT Mes,
						 WPARAM wParam, LPARAM lParam )
{
	switch(Mes)
	{
		//HANDLE_MSG( hWnd, WM_INITMENUPOPUP, OnInitMenuPopup );
    case WM_INITMENUPOPUP:
        OnInitMenuPopup( hWnd );
		break;

    case WM_MENUSELECT:
		ReDrawFrame();
		Draw();
		break;

	case WM_MOUSEMOVE:     
		GetMousePosition( hWnd );
		break;	
		
	case WM_ACTIVATE: 
		ReDrawFrame(); 
		break;	
		
	case WM_CLOSE:  
        DestroyWindow( hWnd ); 
		break;		

	case WM_SIZE:   
        ReDrawFrame();  
        break;		

	case WM_TIMER:
		{
			s_dwTime  += s_bStartTimer;
            s_bPlayTimer = (s_bStartTimer&&s_bPlayWav);
		}
		break;

	case WM_KEYDOWN: 
		{	
			switch (wParam) 
			{ 
			case VK_ESCAPE: 
				PostMessage( hWnd, WM_CLOSE, 0, 0 ); 
				break; 

			case VK_F1:  
				CreateAboutDlg( hWnd );
				break;	
				
			case VK_F2:  
				InitMineRect( hWnd, s_dwLevel );
				break;				
			}
		}
		break;	

	case WM_COMMAND:
		{
			switch( LOWORD(wParam) )
			{
			case 593:	
				CreateAboutDlg( hWnd );    
				break;		
				
			case 528:	//hero
				CreateHeroDlg( hWnd );  
				break;	
				
			case 510:   //restart
				InitMineRect( hWnd, s_dwLevel );
				break;		
				
			case 521:	//level1
				InitMineRect( hWnd, 0 );  
				break;
				
			case 522:	//level2
				InitMineRect( hWnd, 1 ); 
				break;		
				
			case 523:  //level3
				InitMineRect( hWnd, 2 );   
				break;	

			case 524:  //level3
				CreateEditDlg( hWnd );     
				break;	
				
			case 512:  //exit
				DestroyWindow( hWnd );  
				break;

			case 526:   //playsound
				s_bPlayWav = !s_bPlayWav;
				break;

			case 527:   //mark
				s_bMarker = !s_bMarker;
				break;
				
			default: break;
			}	
		}
		break;
		
	case WM_MOVE:
		{
			RECT rcClient;
			GetWindowRect( hWnd, &rcClient );
			s_WindowPX = rcClient.left;
			s_WindowPY = rcClient.top;
			ReDrawFrame();
		}
		break;	
		
	case WM_LBUTTONDOWN:
		{
			if( !s_bRKeyDown )
			{
				SetCapture( hWnd );
			}

			s_bLKeyDown = TRUE;
			
			s_bDoubleKeyDown = s_bRKeyDown;
		}
		break;  
		
	case WM_LBUTTONUP:
		{			
			if( !s_bRKeyDown )
			{
				ReleaseCapture() ;
			}

			s_bLKeyDown      = FALSE;

			BOOL bDown = s_bDoubleKeyDown;
			s_bDoubleKeyDown = s_bRKeyDown;
			
			if ( PtInRect( &s_rcFace, s_ptCur ) )
			{
                InitMineRect( hWnd, s_dwLevel );
				break;
			}
			
			if ( !PtInRect( &s_rcWholeMine,s_ptCur ) )
			{
				break;
			}

			if(s_dwState == GAMERUN)
			{
				int x = (s_ptCur.x-12)/MINEW, y = (s_ptCur.y-CAP_H)/MINEW;
				
				if(bDown)
				{
					if( mine[y][x].ds == mine[y][x].id )
					{
						if ( CheckHongqiCount( x, y )\
							== NUM0 - (mine[y][x].id%16) )
						{
							if ( FALSE == SetZhouweiDisplay( x, y ) )
							{
								ProcessGameOver();
							}
							else
							{
								CheckGameWin( hWnd );
							}
						}
					}
					
				}
				else if ( mine[y][x].ds != HONGQI 
					&&    mine[y][x].ds != MARK )
				{
					s_bStartTimer=TRUE;
					
					mine[y][x].ds = mine[y][x].id;
					
					if ( mine[y][x].id == NORMINE )
					{
						ProcessGameOver();
						mine[y][x].ds = REDMINE;
					}
					else 
					{
						if ( mine[y][x].id == NUM0 )
						{
							CheckClickSpace();
						}

                        CheckGameWin( hWnd );
					}
				}
			}

		}
		break;   
		
	case WM_RBUTTONDOWN:
		{
			if( !s_bLKeyDown )
			{
				SetCapture( hWnd );
			}

			s_bRKeyDown = TRUE;
			s_bDoubleKeyDown = s_bLKeyDown;
		
			if ( !PtInRect( &s_rcWholeMine,s_ptCur ) )
				break;
			
			if( s_dwState == GAMERUN )
			{   
				int x = (s_ptCur.x-12)/MINEW, y = (s_ptCur.y-CAP_H)/MINEW;
				
				if ( !s_bLKeyDown )
				{
					if( mine[y][x].ds == UNSEL )
					{
						mine[y][x].ds = HONGQI;
						++s_dwHongQiCount;
					}
					else if ( mine[y][x].ds == HONGQI )
					{
						if (s_bMarker)
						{
							mine[y][x].ds = MARK;
						}
						else
						{
							mine[y][x].ds = UNSEL;

						}

						--s_dwHongQiCount;
					}
					else if ( mine[y][x].ds == MARK )
					{
						mine[y][x].ds = UNSEL;
						//--s_dwHongQiCount;
					}
				}
			}
		}
		break; 
		
	case WM_RBUTTONUP:
		{
			if( !s_bLKeyDown )
			{
				ReleaseCapture() ;
			}

			s_bRKeyDown = FALSE;
			BOOL bDown = s_bDoubleKeyDown;

			s_bDoubleKeyDown = s_bLKeyDown;

			if ( !PtInRect( &s_rcWholeMine, s_ptCur ) )
			{
				break;
			}

			if(s_dwState == GAMERUN)
			{        
				int x = (s_ptCur.x-12)/MINEW, y = (s_ptCur.y-CAP_H)/MINEW;
				
				if(bDown)
				{
					if( mine[y][x].ds == mine[y][x].id )
					{
						if ( CheckHongqiCount( x, y )\
							== NUM0 - (mine[y][x].id%16)
							&& FALSE == SetZhouweiDisplay( x, y ) )
						{
							ProcessGameOver();
						}
						else
						{
							CheckGameWin( hWnd );
						}
					}
					
				}
			}

		}
		break;
		
		
	case WM_DESTROY:    
		{
			//close thread
			CloseHandle( s_hThread );

            KillTimer( hWnd, 1 );

			WriteRegKey( TEXT("Difficulty"),(TCHAR*)&s_dwLevel,  4 );

			WriteRegKey( TEXT("Mark"),      (TCHAR*)&s_bMarker,  4 );
			
			WriteRegKey( TEXT("Xpos"),      (TCHAR*)&s_WindowPX, 4 );
			
			WriteRegKey( TEXT("Ypos"),      (TCHAR*)&s_WindowPY, 4 );

			WriteRegKey( TEXT("AlreadyPlayed"), (TCHAR*)&s_bPlayWav, 4 );
			
			DeleteObject( s_hbmMine );
			DeleteDC( s_hdcMine );
			DeleteDC( s_hdcTime );
			DeleteDC( s_hdcFace );
			ReleaseDC( hWnd,s_hdcWindow );
			DeleteDC( s_hdcWindow );

			PostQuitMessage(0);
		}
		break;
		
	default:
		return DefWindowProc( hWnd, Mes, wParam, lParam );
	}

	return 0;
}

/***************************************************************************/
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow )
{
	HWND hWnd;
	MSG Msg;

	s_hInst = hInstance;
	
	// Read information from registry
    RegCreateKeyEx( 
		HKEY_CURRENT_USER,
		MINE_KEY,
		0, NULL,
		REG_OPTION_NON_VOLATILE,
		KEY_ALL_ACCESS,//win7 no test
		NULL, 
		&s_key, 
		NULL );
	
    ReadRegKey( TEXT("Difficulty"),    (LPBYTE)&s_dwLevel,  4 );
    ReadRegKey( TEXT("Xpos"),          (LPBYTE)&s_WindowPX, 4 );
    ReadRegKey( TEXT("Ypos"),          (LPBYTE)&s_WindowPY, 4 );
	ReadRegKey( TEXT("AlreadyPlayed"), (LPBYTE)&s_bPlayWav, 4 );
	ReadRegKey( TEXT("Mark"),          (LPBYTE)&s_bMarker,  4 );
	
	s_dwLevel%=3;
    s_WindowPX%=500;
    s_WindowPY%=400;

	srand ( GetTickCount() );
	SetLevel( s_dwLevel );
	WinInit( &hWnd );
    LoadBmp( hWnd );
	InitMineRect( hWnd, s_dwLevel );
	ShowWindow( hWnd, nCmdShow );
	UpdateWindow( hWnd );
    SetTimer( hWnd, 1, 1000, NULL );

	s_hThread = (HANDLE)_beginthread( RunThread, 0, 0 );


#pragma warning ( disable: 4127 )
	while( TRUE )
#pragma warning(default: 4127)
	{	
		if( PeekMessage( &Msg, NULL, 0, 0, PM_NOREMOVE ) )
		{
			if( 0 == GetMessage(&Msg, NULL, 0, 0 ) )
			{
				return (int)Msg.wParam;
			}
			
			TranslateMessage( &Msg ); 
			DispatchMessage( &Msg );			
		}
		else 
		{
			Draw();
			Sleep( 10 );
		}	
		
	}	

	return (int)( Msg.wParam );
}

/***************************************************************************/
HRESULT WinInit( HWND* phWnd )
{
	WNDCLASSEX WndClass;
	
	HWND hWnd = FindWindow( TEXT("zpd1976"), TEXT("扫雷") );

	if( NULL != hWnd )
	{
		ShowWindow ( hWnd, SW_RESTORE );
		SetForegroundWindow ( hWnd );
		exit(2011);
	}	
	
	WndClass.cbSize        = sizeof( WNDCLASSEX );
	WndClass.style         = 0;
	WndClass.lpfnWndProc   = WndProc;
	WndClass.cbClsExtra    = 0;
	WndClass.cbWndExtra    = 0;
	WndClass.hInstance     = s_hInst;
	WndClass.hIcon         = LoadIcon( s_hInst, MAKEINTRESOURCE(100) );
	WndClass.hIconSm       = LoadIcon( s_hInst, MAKEINTRESOURCE(100) );
	WndClass.hCursor       = LoadCursor( NULL, IDC_ARROW );
	WndClass.hbrBackground = (HBRUSH)( COLOR_BACKGROUND );
	WndClass.lpszMenuName  = MAKEINTRESOURCE(500);
	WndClass.lpszClassName = TEXT("zpd1976");
	
	if( !RegisterClassEx(&WndClass) )
	{
		return 1;
	}
	
	hWnd = CreateWindowEx( 
		0,
		TEXT("zpd1976"),
		TEXT("扫雷"),
		//WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX&~WS_THICKFRAME,
		 WS_OVERLAPPED|WS_CAPTION| WS_SYSMENU|WS_MINIMIZEBOX,
		0, 0, 0, 0, 
		NULL, 
		NULL,
		s_hInst, 
		NULL);
	
	if ( NULL == hWnd )
	{
		exit(12);
	}

    // Set window size
	ResetWindowSize( hWnd);
    *phWnd = hWnd;
	
	return S_OK;
}

/***************************************************************************/
void ResetWindowSize( HWND hWnd )
{
	RECT rcWindow ={ 0, 0, WIN_W, WIN_H };
	
    AdjustWindowRect( &rcWindow, 
		GetWindowLong(hWnd, GWL_STYLE),
		GetMenu(hWnd) != NULL);
	
    SetWindowPos( hWnd,NULL, 
		0, 0, 
		rcWindow.right-rcWindow.left,
		rcWindow.bottom-rcWindow.top,
		SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
	
    SetWindowPos( hWnd, NULL,
		s_WindowPX,s_WindowPY,
		0, 0,
		SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
	
	FillRect( s_hdcWindow, &rcWindow, (HBRUSH)(COLOR_BACKGROUND) );

	ReDrawFrame();
	
}

/***************************************************************************/
void GetMousePosition( HWND hWnd )
{
	GetCursorPos( &s_ptCur );
	ScreenToClient( hWnd, &s_ptCur );
}

/***************************************************************************/
void Draw()
{
	DrawFace();
	DrawHongqiCount();
	DrawTime();
	
	int px= 0,py=0;//for mouse down display
	
	for ( int y = 0; y < s_nMap_H; ++y )
	{
		for ( int x = 0; x < s_nMap_W; ++x )
		{
			int  p = 0;
			
			if (  s_dwState == GAMERUN		
				&&s_ptCur.x >= mine[y][x].px
				&&s_ptCur.x <  mine[y][x].px+MINEW
				&&s_ptCur.y >= mine[y][x].py
				&&s_ptCur.y <  mine[y][x].py+MINEW )
			{
				if (   s_bLKeyDown 
					&& !s_bRKeyDown 
					&& 0 == mine[y][x].ds )
				{
					p = NUM0*MINEW;
				}
				else if ( s_bDoubleKeyDown )
				{
					p = mine[y][x].ds ? mine[y][x].ds*MINEW : NUM0*MINEW;
				}
				else
				{
					p = (mine[y][x].ds%16)*MINEW;
				}
				px = x;
				py = y;
			}
			else
			{
				p = (mine[y][x].ds%16)*MINEW;
			}
			
			BitBlt(s_hdcWindow, mine[y][x].px, mine[y][x].py,
				MINEW, MINEW, s_hdcMine, 0, p, SRCCOPY);
		}
	}
	
	if (   s_dwState == GAMERUN 
		&& s_bDoubleKeyDown 
		&& PtInRect(&s_rcWholeMine,s_ptCur) )
	{
		if ( px > 0 )
		{
			if ( py > 0 && mine[py-1][px-1].ds == 0 )
			{
				BitBlt( s_hdcWindow, 
					mine[py-1][px-1].px, mine[py-1][px-1].py,
					MINEW, MINEW, s_hdcMine, 0, NUM0*MINEW, SRCCOPY );
			}
			
			if ( mine[py][px-1].ds == 0)
			{
				BitBlt( s_hdcWindow, 
					mine[py][px-1].px, mine[py][px-1].py,
					MINEW, MINEW, s_hdcMine, 0, NUM0*MINEW, SRCCOPY );
			}
			
			if ( py < (s_nMap_H-1) && mine[py+1][px-1].ds == 0 )
			{
				BitBlt( s_hdcWindow, 
					mine[py+1][px-1].px, mine[py+1][px-1].py,
					MINEW, MINEW, s_hdcMine, 0, NUM0*MINEW, SRCCOPY );
			}
		}
		
		if ( px < (s_nMap_W-1) )
		{
			if ( py > 0 && mine[py-1][px+1].ds == 0 )
			{
				BitBlt( s_hdcWindow, 
					mine[py-1][px+1].px, mine[py-1][px+1].py,
					MINEW, MINEW, s_hdcMine, 0, NUM0*MINEW, SRCCOPY );
			}
			
			if ( mine[py][px+1].ds == 0 )
			{
				BitBlt( s_hdcWindow, 
					mine[py][px+1].px, mine[py][px+1].py,
					MINEW, MINEW, s_hdcMine, 0, NUM0*MINEW, SRCCOPY );
			}
			
			if ( py < (s_nMap_H-1) && mine[py+1][px+1].ds == 0 )
			{
				BitBlt(s_hdcWindow, 
					mine[py+1][px+1].px, mine[py+1][px+1].py,
					MINEW, MINEW, s_hdcMine, 0, NUM0*MINEW, SRCCOPY);
			}
		}
		
		if ( py > 0 && mine[py-1][px].ds == 0)
		{
			BitBlt( s_hdcWindow, 
				mine[py-1][px].px, mine[py-1][px].py,
				MINEW, MINEW, s_hdcMine, 0, NUM0*MINEW, SRCCOPY );
		}
		
		if (py < (s_nMap_H-1) && mine[py+1][px].ds == 0)
		{
			BitBlt( s_hdcWindow, 
				mine[py+1][px].px, mine[py+1][px].py,
				MINEW, MINEW, s_hdcMine, 0, NUM0*MINEW, SRCCOPY );
		}
	}
}

/***************************************************************************/
void DrawFace()
{
	if ( PtInRect( &s_rcFace, s_ptCur ) 
		&& s_bLKeyDown )
	{
		BitBlt( s_hdcWindow, s_rcFace.left, s_rcFace.top,
			FACEW, FACEW, s_hdcFace, 0, 0, SRCCOPY );
	}
	else
	{
		if ( s_dwState == GAMERUN )
		{
			if ( PtInRect(&s_rcWholeMine, s_ptCur) 
				&& s_bLKeyDown )
			{
				BitBlt( s_hdcWindow, s_rcFace.left, s_rcFace.top,
					FACEW, FACEW, s_hdcFace, 0, 3*FACEW, SRCCOPY );
				
			}
			else
			{
				BitBlt( s_hdcWindow, s_rcFace.left, s_rcFace.top,
					FACEW, FACEW, s_hdcFace, 0, 4*FACEW, SRCCOPY );
			}
		}
		else if ( s_dwState == GAMEOVER )
		{
			BitBlt( s_hdcWindow, s_rcFace.left, s_rcFace.top,
				FACEW, FACEW, s_hdcFace, 0, 2*FACEW, SRCCOPY );
		}
		else if ( s_dwState == GAMEWIN )
		{
			BitBlt( s_hdcWindow, s_rcFace.left, s_rcFace.top,
				FACEW, FACEW, s_hdcFace, 0, 1*FACEW, SRCCOPY );
		}
	}
}

/***************************************************************************/
void DrawTime()
{
	BitBlt( s_hdcWindow, WIN_W-52,15,13,23,
		s_hdcTime,0, ( 11 - ( s_dwTime/100%10 ) )*23, SRCCOPY );

	BitBlt( s_hdcWindow, WIN_W-39,15,13,23,
		s_hdcTime,0, ( 11 - ( s_dwTime/10%10 ) )*23, SRCCOPY );

	BitBlt( s_hdcWindow, WIN_W-26,15,13,23,
		s_hdcTime,0, ( 11 - ( s_dwTime%10 ) )*23, SRCCOPY );
	
}

/***************************************************************************/
void DrawHongqiCount()
{
	int h0, h1, h2;
	if ( s_dwMineCount >= s_dwHongQiCount )
	{
		h0 = 11*23;
		h1 = ( s_dwMineCount - s_dwHongQiCount )%10;
		h2 = ( s_dwMineCount - s_dwHongQiCount )/10%10;
	}
	else
	{
		h0 = 0;
		h1 = ( s_dwHongQiCount-s_dwMineCount )%10;
		h2 = ( s_dwHongQiCount-s_dwMineCount )/10%10;
	}

	BitBlt(s_hdcWindow, 17, 15,13, 23, s_hdcTime, 0, h0,         SRCCOPY );
	BitBlt(s_hdcWindow, 30, 15,13, 23, s_hdcTime, 0, (11-h2)*23, SRCCOPY );
	BitBlt(s_hdcWindow, 43, 15,13, 23, s_hdcTime, 0, (11-h1)*23, SRCCOPY );
}

/***************************************************************************/
void InitMineRect( HWND hWnd, DWORD level )
{
	s_dwState = GAMERUN;
    s_bStartTimer = FALSE;
	s_dwHongQiCount = 0;
	s_dwTime = 0;
	s_bLKeyDown = FALSE;
	s_bRKeyDown = FALSE;
	s_bDoubleKeyDown = FALSE;
	
	if ( s_dwLevel != level )
	{
		s_dwLevel = level;
		SetLevel( s_dwLevel );
		ResetWindowSize( hWnd );		
	}
	else if ( level == 3 )
	{
		ResetWindowSize( hWnd );		
	}
	
	int y,x;
	
	s_rcFace.left   = (WHOLEMAP_W-20)/2 + 10;
	s_rcFace.right  = s_rcFace.left + FACEW;
	s_rcFace.top    = 14;
	s_rcFace.bottom = s_rcFace.top + FACEW;

	s_rcWholeMine.left   = 12;
	s_rcWholeMine.top    = CAP_H;
	s_rcWholeMine.right  = WHOLEMAP_W + 12;
	s_rcWholeMine.bottom = WHOLEMAP_H + CAP_H;

	for ( y = 0; y < s_nMap_H; ++y )
	{
		for ( x =0; x < s_nMap_W; ++x )
		{
			mine[y][x].px = x*MINEW+12;
			mine[y][x].py = y*MINEW+CAP_H;
			mine[y][x].id = UNSEL;
			mine[y][x].ds = UNSEL;
			
		}
	}
	
    //mine post
	for ( DWORD i = 0; i < s_dwMineCount; ++i )
	{
		int l = rand()%(s_nMap_H*s_nMap_W);	
		
		while( mine[l/s_nMap_W][l%s_nMap_W].id == NORMINE )
		{
			l = rand()%(s_nMap_H*s_nMap_W);
		}
		
		mine[l/s_nMap_W][l%s_nMap_W].id = NORMINE;
	}
	
	//mine count
	for ( y =0; y < s_nMap_H; ++y )
	{
		for ( x = 0; x < s_nMap_W; ++x )
		{
			if( mine[y][x].id == NORMINE )
			{
				continue;
			}
			
			mine[y][x].id = NUM0 - CheckMineCount( x, y );				   
		}
	}
} 

/***************************************************************************/
int CheckMineCount( int x, int y )
{	
	int dwCount = 0;
	
	if (x >0)
	{
		if ( y >0 && mine[y-1][x-1].id == NORMINE )
		{
			++dwCount;
		}

		if ( mine[y][x-1].id == NORMINE )
		{
			++dwCount;
		}

		if ( y < (s_nMap_H-1) && mine[y+1][x-1].id == NORMINE )
		{
			++dwCount;
		}
	}
	
	if ( x < (s_nMap_W-1) )
	{
		if ( y >0 && mine[y-1][x+1].id == NORMINE )
		{
			++dwCount;
		}

		if ( mine[y][x+1].id == NORMINE )
		{
			++dwCount;
		}

		if (y < (s_nMap_H-1) && mine[y+1][x+1].id == NORMINE )
		{
			++dwCount;
		}
	}
	
	if ( y > 0 && mine[y-1][x].id == NORMINE )
	{
		++dwCount;
	}

	if ( y < (s_nMap_H-1) && mine[y+1][x].id == NORMINE )
	{
		++dwCount;
	}

	return dwCount;				   
}

/***************************************************************************/
int CheckHongqiCount( int x, int y )
{
	int dwCount = 0;
	
	if ( x >0 )
	{
		if ( y >0 && mine[y-1][x-1].ds == HONGQI )
		{
			++dwCount;
		}

		if ( mine[y][x-1].ds== HONGQI )
		{
			++dwCount;	
		}

		if (y < (s_nMap_H-1) && mine[y+1][x-1].ds == HONGQI )
		{
			++dwCount;
		}
		
	}
	
	if ( x < (s_nMap_W-1) )
	{
		if ( y >0 && mine[y-1][x+1].ds == HONGQI )
		{
			++dwCount;	
		}

		if ( mine[y][x+1].ds == HONGQI)
		{
			++dwCount;
		}

		if ( y <(s_nMap_H-1)&& mine[y+1][x+1].ds == HONGQI )
		{
			++dwCount;
		}
	}
	
	if ( y >0 && mine[y-1][x].ds== HONGQI )
	{
		++dwCount;
	}
	
	if (y < (s_nMap_H-1) && mine[y+1][x].ds == HONGQI )
	{
		++dwCount;
	}
	
	return dwCount;				   
}

/***************************************************************************/
BOOL SetZhouweiDisplay( int x, int y )
{
	int c = 0;
	
	if ( x > 0 )
	{
		if ( y > 0 )
		{
			if (   mine[y-1][x-1].ds == HONGQI 
				&& mine[y-1][x-1].id != NORMINE
				|| mine[y-1][x-1].ds != HONGQI 
				&& mine[y-1][x-1].id == NORMINE )
			{
				return FALSE;
			}
			else
			{
				if (mine[y-1][x-1].ds != HONGQI)
				{
					mine[y-1][x-1].ds = mine[y-1][x-1].id;
				}

				c += mine[y-1][x-1].ds== NUM0 ? 1: 0;
			}
		}
		
		if (   mine[y][x-1].ds == HONGQI 
			&& mine[y][x-1].id != NORMINE
			|| mine[y][x-1].ds != HONGQI 
			&& mine[y][x-1].id == NORMINE )
		{
			return FALSE;
		}
		else
		{
			if ( mine[y][x-1].ds != HONGQI )
			{
				mine[y][x-1].ds = mine[y][x-1].id;
			}

			c += mine[y][x-1].ds== NUM0 ? 1: 0;
		}
		
		if ( y < (s_nMap_H-1) )
		{
			if (   mine[y+1][x-1].ds == HONGQI 
				&& mine[y+1][x-1].id != NORMINE
				|| mine[y+1][x-1].ds != HONGQI 
				&& mine[y+1][x-1].id == NORMINE )
			{
				return FALSE;
			}
			else
			{
				if (mine[y+1][x-1].ds != HONGQI)
				{
					mine[y+1][x-1].ds = mine[y+1][x-1].id;
				}

				c += mine[y+1][x-1].ds== NUM0 ? 1: 0;
			}
		}
		
	}
	
	if ( x < (s_nMap_W-1) )
	{
		if ( y >0 )
		{
			if (   mine[y-1][x+1].ds == HONGQI 
				&& mine[y-1][x+1].id != NORMINE
				|| mine[y-1][x+1].ds != HONGQI 
				&& mine[y-1][x+1].id == NORMINE )
			{
				return FALSE;
			}
			else
			{
				if ( mine[y-1][x+1].ds != HONGQI )
				{
					mine[y-1][x+1].ds = mine[y-1][x+1].id;
				}

				c += mine[y-1][x+1].ds== NUM0 ? 1: 0;
			}
		}
		
		if (   mine[y][x+1].ds == HONGQI 
			&& mine[y][x+1].id != NORMINE
			|| mine[y][x+1].ds != HONGQI 
			&& mine[y][x+1].id == NORMINE )
		{
			return FALSE;
		}
		else
		{
			if ( mine[y][x+1].ds != HONGQI )
			{
				mine[y][x+1].ds = mine[y][x+1].id;
			}

			c += mine[y][x+1].ds== NUM0 ? 1: 0;
		}
		
		if ( y < (s_nMap_H-1) )
		{
			if (   mine[y+1][x+1].ds == HONGQI 
				&& mine[y+1][x+1].id != NORMINE
				|| mine[y+1][x+1].ds != HONGQI 
				&& mine[y+1][x+1].id == NORMINE )
			{
				return FALSE;
			}
			else
			{
				if (mine[y+1][x+1].ds != HONGQI)
				{
					mine[y+1][x+1].ds = mine[y+1][x+1].id;
				}

				c += mine[y+1][x+1].ds== NUM0 ? 1: 0;
			}
		}
	}
	
	if ( y >0 )
	{
		if (   mine[y-1][x].ds == HONGQI 
			&& mine[y-1][x].id != NORMINE
			|| mine[y-1][x].ds != HONGQI 
			&& mine[y-1][x].id == NORMINE )
		{
			return FALSE;
		}
		else
		{
			if (mine[y-1][x].ds != HONGQI)
			{
				mine[y-1][x].ds = mine[y-1][x].id;
			}

			c += mine[y-1][x].ds== NUM0 ? 1: 0;
		}
	}
	
	if ( y < (s_nMap_H-1) )
	{
		if (   mine[y+1][x].ds == HONGQI 
			&& mine[y+1][x].id != NORMINE
			|| mine[y+1][x].ds != HONGQI 
			&& mine[y+1][x].id == NORMINE )
		{
			return FALSE;
		}
		else
		{
			if (mine[y+1][x].ds != HONGQI)
			{
				mine[y+1][x].ds = mine[y+1][x].id;
			}

			c += mine[y+1][x].ds== NUM0 ? 1: 0;
		}
	}
	
	if ( c )
	{
		CheckClickSpace();
	}

	return TRUE;
}

/***************************************************************************/
void ProcessGameOver()
{
	if ( s_bPlayWav )
	{
		s_bPlayGameOver = TRUE;
	}
	
	s_dwState = GAMEOVER;
	s_bStartTimer=FALSE;

	for ( int y = 0; y < s_nMap_H; ++y )
	{
		for ( int x = 0; x < s_nMap_W; ++x )
		{
			if (   mine[y][x].id == NORMINE 
				&& mine[y][x].ds != HONGQI )
			{
				mine[y][x].ds = NORMINE;
			}
			else if (mine[y][x].ds == HONGQI 
				&&   mine[y][x].id != NORMINE )
			{
				mine[y][x].ds = ERRORMINE;
			}
		}
	}

}

/***************************************************************************/
void  CheckGameWin( HWND hWnd )
{
	DWORD dwCount = 0;

	//check mine count
	for ( int y = 0; y < s_nMap_H; ++y )
	{
		for ( int x = 0; x < s_nMap_W; ++x )
		{
			if ( mine[y][x].id == mine[y][x].ds )
			{
				++dwCount;
			}
		}
	}
	

	if ( dwCount == s_nMap_W*s_nMap_H-s_dwMineCount )
	{
		s_dwState = GAMEWIN;

		if (s_bPlayWav)
        {
			s_bPlayGameWin = TRUE;
		}

        s_bStartTimer = FALSE;

		// mark hongqi
		for ( int y = 0; y < s_nMap_H; ++y )
		{
			for ( int x = 0; x < s_nMap_W; ++x )
			{
				if (   mine[y][x].id == NORMINE 
					&& mine[y][x].ds != HONGQI )
				{
					mine[y][x].ds = HONGQI;
					++s_dwHongQiCount;
				}
			}
		}


		DWORD  t = 0;

		switch (s_dwLevel)
		{
		case 0:
			ReadRegKey( TEXT("Time1"),(LPBYTE)&t,4);
			break;

		case 1:
			ReadRegKey( TEXT("Time2"),(LPBYTE)&t,4);
			break;

		case 2:
			ReadRegKey( TEXT("Time3"),(LPBYTE)&t,4);
			break;

        case 3:
			t = 0;
			break;
		default:break;
		}
		
		if (s_dwTime < t)
		{
            Draw();
			CreateInputDlg(hWnd);
		}

	}

}

/***************************************************************************/
void LoadBmp(HWND hWnd)
{
	s_hbmMine    = LoadBitmap(s_hInst, MAKEINTRESOURCE(123));
	
	if (NULL == s_hbmMine)
	{
		MessageBox(0, TEXT("Failed Load the Res 123"), TEXT("error"), 0);
		exit(5);
	}
	
	s_hbmFace    = LoadBitmap(s_hInst, MAKEINTRESOURCE(430));
	
	if (NULL == s_hbmFace)
	{
		MessageBox(0, TEXT("Failed Load the Res 430"), TEXT("error"), 0);
		exit(5);
	}
	
	s_hbmTime    = LoadBitmap(s_hInst, MAKEINTRESOURCE(440));
	
	if (NULL == s_hbmTime)
	{
		MessageBox(0, TEXT("Failed Load the Res 440"), TEXT("error"), 0);
		exit(5);
	}
	
	s_hdcWindow = GetDC(hWnd);
	
	if (NULL == s_hdcWindow)
	{
		MessageBox(0, TEXT("Failed GetDC"), TEXT("error"), 0);
		exit(6);
	}
	
	s_hdcMine = CreateCompatibleDC(s_hdcWindow);	
	
	if (NULL == s_hdcMine)
	{
		MessageBox(0, TEXT("Failed  CreateCompatibleDC"), TEXT("error"), 0);
		exit(7);
	}
	
	s_hdcFace = CreateCompatibleDC(s_hdcWindow);	
	
	if (NULL == s_hdcFace)
	{
		MessageBox(0, TEXT("Failed  CreateCompatibleDC"), TEXT("error"), 0);
		exit(7);
	}
	
	s_hdcTime = CreateCompatibleDC(s_hdcWindow);	
	
	if (NULL == s_hdcTime)
	{
		MessageBox(0, TEXT("Failed  CreateCompatibleDC"), TEXT("error"), 0);
		exit(7);
	}
	
	SelectObject(s_hdcMine, s_hbmMine);
	SelectObject(s_hdcTime, s_hbmTime);
	SelectObject(s_hdcFace, s_hbmFace);
	
}


/***************************************************************************/
long CALLBACK AboutDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM  )
{	
    switch (msg)
    {
	case WM_MOVE: 
		ReDrawFrame(); 
		Draw();
		break;

	case WM_INITDIALOG:
		ReDrawFrame();
		Draw();
		SetFocus(hDlg);
		break;

	case WM_COMMAND:
		{
			switch( LOWORD(wParam) )
			{
			case IDOK:
			case IDCANCEL:
				EndDialog( hDlg, TRUE );
				return TRUE;
			}
		}
		break;
    }
	
    return FALSE;
}

/***************************************************************************/
long CALLBACK InputDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM  )
{	
    switch (msg)
    {
	case WM_INITDIALOG :
		{
			ReDrawFrame();
			Draw();
			SetFocus(hDlg);

			TCHAR str[32];
			switch (s_dwLevel)
			{
			case 0:
				ReadRegKey(TEXT("Name1"),(LPBYTE)&str,32 );
				SetDlgItemText( hDlg, 602, str );
				SPRINTF(str, TEXT("已破初级记录。\r 请留尊姓大名。"), 0 );
				break;
				
			case 1:
				ReadRegKey(TEXT("Name2"),(LPBYTE)&str,32 );
				SetDlgItemText( hDlg, 602, str );
				SPRINTF(str, TEXT("已破中级记录。\r 请留尊姓大名。"), 0 );
				break;
				
			case 2:
				ReadRegKey(TEXT("Name3"),(LPBYTE)&str,32 );
				SetDlgItemText( hDlg, 602, str );
				SPRINTF(str, TEXT("已破高级记录。\r 请留尊姓大名。"), 0 );
				break;
				
			default: break;
				
			}
			
			//still text
			SetDlgItemText( hDlg, 601, str );
		}
		break;	
		
	case WM_COMMAND:
		{
			switch( LOWORD(wParam) )
			{
			case IDOK:
				{
					TCHAR str[32];
					
					GetDlgItemText(hDlg,602, str, 32);
					
					switch (s_dwLevel)
					{
					case 0:
						WriteRegKey( TEXT("Name1"), str, 1 );
						WriteRegKey( TEXT("Time1"), (TCHAR*)&s_dwTime, 4 );
						break;
						
					case 1:
						WriteRegKey( TEXT("Name2"), str, 1 );
						WriteRegKey( TEXT("Time2"), (TCHAR*)&s_dwTime, 4 );
						break;
						
					case 2:
						WriteRegKey( TEXT("Name3"), str, 1 );
						WriteRegKey( TEXT("Time3"), (TCHAR*)&s_dwTime, 4 );
						break;
						
					default: break;
						
					}
					
					EndDialog( hDlg, TRUE );
					CreateHeroDlg( GetParent(hDlg) );
				}
				
				return TRUE;
			}
		}
		break;
    }
	
    return FALSE;
}

/***************************************************************************/
long CALLBACK EditDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM  )
{	
    switch (msg)
    {
	case WM_INITDIALOG :
		{
			//TCHAR str[32]  = TEXT("30");
			SetDlgItemText( hDlg, 141, TEXT("30") );
			SetDlgItemText( hDlg, 142, TEXT("30") );
			SetDlgItemText( hDlg, 143, TEXT("30") );
			ReDrawFrame();
			Draw();
			SetFocus(hDlg);
		}
		break;	

	case WM_MOVE: 
		ReDrawFrame();
		Draw();
		break;
		
	case WM_COMMAND:
		{
			switch( LOWORD(wParam) )
			{
			case IDOK:
			case IDCANCEL:
				{					
					s_nMap_H      = GetDlgItemInt( hDlg, 141, NULL, 0 );
					s_nMap_W      = GetDlgItemInt( hDlg, 142, NULL, 0 );
					s_dwMineCount = GetDlgItemInt( hDlg, 143, NULL, 0 );

					if (s_nMap_H < 9 )
					{
						s_nMap_H = 9;
					}
					else if (s_nMap_H > 30)
					{
						s_nMap_H = 30;
					}

					if (s_nMap_W < 9 )
					{
						s_nMap_W = 9;
					}
					else if (s_nMap_W > 30)
					{
						s_nMap_W = 30;
					}

					if (s_dwMineCount < 1 )
					{
						s_dwMineCount = 1;
					}
					else if (s_dwMineCount > (DWORD)(s_nMap_W*s_nMap_H/2) )
					{
						s_dwMineCount = s_nMap_W*s_nMap_H/2;
					}

					EndDialog( hDlg, TRUE );

					InitMineRect( GetParent(hDlg),3 );
				}
				
				return TRUE;
			}
		}
		break;
    }
	
    return FALSE;
}

/***************************************************************************/
long CALLBACK HeroDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM  )
{	
    switch (msg)
    {
	case WM_MOVE: 
		ReDrawFrame();
		Draw();
		break;

	case WM_INITDIALOG :
		{
			ReDrawFrame();
			Draw();
			TCHAR str[32];
			
			ReadRegKey( TEXT("Name1"),(LPBYTE)&str,32 );	
			SetDlgItemText( hDlg, 702, str	);
			
			ReadRegKey( TEXT("Name2"),(LPBYTE)&str,32 );
			SetDlgItemText( hDlg, 704, str );
			
			ReadRegKey( TEXT("Name3"),(LPBYTE)&str,32 );
			SetDlgItemText( hDlg, 706, str );
			
			int  t;
			ReadRegKey( TEXT("Time1"),(LPBYTE)&t,4 );
			SPRINTF( str,TEXT("%d 秒"), t );
			SetDlgItemText( hDlg, 701, str );
			
			ReadRegKey( TEXT("Time2"),(LPBYTE)&t,4 );
			SPRINTF( str,TEXT("%d 秒"), t );
			SetDlgItemText( hDlg, 703, str );
			
			ReadRegKey( TEXT("Time3"),(LPBYTE)&t,4 );
			SPRINTF( str,TEXT("%d 秒"), t );
			SetDlgItemText( hDlg, 705, str );
			SetFocus(hDlg);
		}
		break;

	case WM_COMMAND:
		{
			switch( LOWORD(wParam) )
			{
			case 707:
				{
				int t =999;

				TCHAR str[32] = TEXT("匿名");

				WriteRegKey( TEXT("Time1"),  (TCHAR*)&t, 4 );
				WriteRegKey( TEXT("Time2"),  (TCHAR*)&t, 4 );
				WriteRegKey( TEXT("Time3"),  (TCHAR*)&t, 4 );

				WriteRegKey( TEXT("Name1"),  str, 1 );
				WriteRegKey( TEXT("Name2"),  str, 1 );
				WriteRegKey( TEXT("Name3"),  str, 1 );

				SetDlgItemText( hDlg, 701, TEXT("999 秒") );
				SetDlgItemText( hDlg, 703, TEXT("999 秒") );
				SetDlgItemText( hDlg, 705, TEXT("999 秒") );

				SetDlgItemText( hDlg, 702, str );
				SetDlgItemText( hDlg, 704, str );
				SetDlgItemText( hDlg, 706, str );
				}
				break;
				
			case IDOK:
			case IDCANCEL:
				{
					EndDialog( hDlg, TRUE );
				}
				
				return TRUE;
			}
		}
		break;
    }
	
    return FALSE;
}

/***************************************************************************/
void CreateAboutDlg(HWND hWnd)
{		
	DialogBox( s_hInst, MAKEINTRESOURCE(107), hWnd, (DLGPROC)AboutDlgProc );	
}

/***************************************************************************/
void CreateInputDlg(HWND hWnd)
{	
	DialogBox( s_hInst, MAKEINTRESOURCE(600), hWnd, (DLGPROC)InputDlgProc );	
}

/***************************************************************************/
void CreateEditDlg(HWND hWnd)
{
	DialogBox( s_hInst, MAKEINTRESOURCE(80), hWnd, (DLGPROC)EditDlgProc );	
}

/***************************************************************************/
void CreateHeroDlg(HWND hWnd)
{	
	DialogBox( s_hInst, MAKEINTRESOURCE(700), hWnd, (DLGPROC)HeroDlgProc );	
}

/***************************************************************************/
void OnInitMenuPopup( HWND hWnd )
{
	HMENU hMenu = GetMenu( hWnd );
	if( s_dwLevel == 0 ) 
	{
		CheckMenuItem( hMenu, 521, MF_BYCOMMAND | MF_CHECKED );
		CheckMenuItem( hMenu, 522, MF_BYCOMMAND | MF_UNCHECKED );
		CheckMenuItem( hMenu, 523, MF_BYCOMMAND | MF_UNCHECKED );
		CheckMenuItem( hMenu, 524, MF_BYCOMMAND | MF_UNCHECKED );
	} 
	else if ( s_dwLevel == 1 )
	{
		CheckMenuItem( hMenu, 522, MF_BYCOMMAND | MF_CHECKED );
		CheckMenuItem( hMenu, 521, MF_BYCOMMAND | MF_UNCHECKED );
		CheckMenuItem( hMenu, 523, MF_BYCOMMAND | MF_UNCHECKED );
		CheckMenuItem( hMenu, 524, MF_BYCOMMAND | MF_UNCHECKED );
	}
	else if ( s_dwLevel == 2 )
	{
		CheckMenuItem( hMenu, 523, MF_BYCOMMAND | MF_CHECKED );
		CheckMenuItem( hMenu, 521, MF_BYCOMMAND | MF_UNCHECKED );
		CheckMenuItem( hMenu, 522, MF_BYCOMMAND | MF_UNCHECKED );
		CheckMenuItem( hMenu, 524, MF_BYCOMMAND | MF_UNCHECKED );
	}
	else if ( s_dwLevel == 3 )
	{
		CheckMenuItem( hMenu, 524, MF_BYCOMMAND | MF_CHECKED );
		CheckMenuItem( hMenu, 521, MF_BYCOMMAND | MF_UNCHECKED );
		CheckMenuItem( hMenu, 522, MF_BYCOMMAND | MF_UNCHECKED );
		CheckMenuItem( hMenu, 523, MF_BYCOMMAND | MF_UNCHECKED );
	}

	if ( s_bPlayWav )
	{
		CheckMenuItem( hMenu, 526, MF_BYCOMMAND | MF_CHECKED );
	}
	else
	{
		CheckMenuItem( hMenu, 526, MF_BYCOMMAND | MF_UNCHECKED );
	}

	if ( s_bMarker )
	{
		CheckMenuItem( hMenu, 527, MF_BYCOMMAND | MF_CHECKED );
	}
	else
	{
		CheckMenuItem( hMenu, 527, MF_BYCOMMAND | MF_UNCHECKED );
	}
}

/***************************************************************************/
void ReadRegKey( TCHAR* strName, LPBYTE strValue, DWORD dwLength )
{
	RegQueryValueEx( s_key, strName, 0, NULL, strValue, &dwLength );	
}

/***************************************************************************/
void WriteRegKey( TCHAR* strName, TCHAR* strValue, DWORD dwType )
{
	if ( dwType == REG_SZ )
	{
		RegSetValueEx( s_key, strName, 0, dwType, 
			(LPBYTE)strValue, _tcslen(strValue)+1 );
	}
	else if (dwType == REG_DWORD)
	{
		RegSetValueEx( s_key, strName, 0, dwType, 
			(LPBYTE) strValue, sizeof(DWORD) );
	}
	else
	{
		MessageBox( NULL, TEXT(" type error"), TEXT("error"), 0 );
	}
}

/***************************************************************************/
void ReDrawFrame()
{
	//draw frame
	SelectObject( s_hdcWindow, GetStockObject(WHITE_PEN) );

	MoveToEx( s_hdcWindow, 0,     0, NULL );
	LineTo(   s_hdcWindow, WIN_W, 0 );
	LineTo(   s_hdcWindow, WIN_W, 1 );
	LineTo(   s_hdcWindow, 0,     1 );
	
	LineTo(   s_hdcWindow, 0, WIN_H );
	LineTo(   s_hdcWindow, 1, WIN_H );
	LineTo(   s_hdcWindow, 1,     0 );
	LineTo(   s_hdcWindow, 2,     0 );
	LineTo(   s_hdcWindow, 2, WIN_H );
	
	MoveToEx( s_hdcWindow, 9,       WIN_H-8, NULL );
	LineTo(   s_hdcWindow, WIN_W-8, WIN_H-8 );
	LineTo(   s_hdcWindow, WIN_W-8, WIN_H-7 );
	LineTo(   s_hdcWindow, 9,       WIN_H-7 );
	LineTo(   s_hdcWindow, 9,       WIN_H-6 );
	LineTo(   s_hdcWindow, WIN_W-8, WIN_H-6);
	
	LineTo(   s_hdcWindow, WIN_W-8,      51 );
	LineTo(   s_hdcWindow, WIN_W-7,      51 );
	LineTo(   s_hdcWindow, WIN_W-7, WIN_H-6 );
	LineTo(   s_hdcWindow, WIN_W-6, WIN_H-6 );
	LineTo(   s_hdcWindow, WIN_W-6,      51 );
	
	MoveToEx( s_hdcWindow, WIN_W-6, 44, NULL );
	LineTo(   s_hdcWindow,       9, 44 );
	LineTo(   s_hdcWindow,       9, 43 );
	LineTo(   s_hdcWindow, WIN_W-7, 43 );
	
	LineTo(   s_hdcWindow, WIN_W-7,  9 );
	LineTo(   s_hdcWindow, WIN_W-6,  8 );
	LineTo(   s_hdcWindow, WIN_W-6, 44 );
	
	//time frame
	MoveToEx( s_hdcWindow, 16, 38, NULL );
	LineTo(   s_hdcWindow, 56, 38 );
	LineTo(   s_hdcWindow, 56, 14 );
	
	MoveToEx( s_hdcWindow, WIN_W-53, 38, NULL );
	LineTo(   s_hdcWindow, WIN_W-13, 38 );
	LineTo(   s_hdcWindow, WIN_W-13, 14 );
	
	
	HPEN hpen = CreatePen( PS_SOLID, 1, RGB(128,128,128) );
	SelectObject( s_hdcWindow, hpen );

	MoveToEx( s_hdcWindow, WIN_W-6, 51,NULL );
	
	LineTo(   s_hdcWindow, 9,       51 );
	LineTo(   s_hdcWindow, 9,       52 );
	LineTo(   s_hdcWindow, WIN_W-7, 52 );
	LineTo(   s_hdcWindow, WIN_W-8, 53 );
	LineTo(   s_hdcWindow, 9,       53 );	
	LineTo(   s_hdcWindow, 9,  WIN_H-6 );
	LineTo(   s_hdcWindow, 10, WIN_H-7 );
	LineTo(   s_hdcWindow, 10,      52 );
	LineTo(   s_hdcWindow, 11,      52 );
	LineTo(   s_hdcWindow, 11, WIN_H-7 );
	
	MoveToEx( s_hdcWindow,       9, 8, NULL );
	LineTo(   s_hdcWindow, WIN_W-6, 8 );
	LineTo(   s_hdcWindow, WIN_W-7, 9 );
	LineTo(   s_hdcWindow,       9, 9 );
	
	LineTo(   s_hdcWindow,  9, 44 );
	LineTo(   s_hdcWindow, 10, 43 );
	LineTo(   s_hdcWindow, 10,  9 );
	
	MoveToEx( s_hdcWindow, 56, 14, NULL );
	LineTo(   s_hdcWindow, 16, 14 );
	LineTo(   s_hdcWindow, 16, 38 );
	
	MoveToEx( s_hdcWindow, WIN_W-53, 38, NULL );
	LineTo(   s_hdcWindow, WIN_W-53, 14 );
	LineTo(   s_hdcWindow, WIN_W-13, 14 );
	
	DeleteObject( hpen );
}

/***************************************************************************/
int CheckNoMineRect(int x, int y)
{
    if ( mine[y][x].ds == 31 )
	{
		return 0;
	}
	
	if (mine[y][x].ds == NUM0)
	{
		if ( x > 0 )
		{
			if ( y > 0 && mine[y-1][x-1].ds != HONGQI )
			{
				mine[y-1][x-1].ds = mine[y-1][x-1].id;
			}
			
			if ( mine[y][x-1].ds != HONGQI )
			{
				mine[y][x-1].ds =mine[y][x-1].id;
			}
			
			if ( y < (s_nMap_H-1) && mine[y+1][x-1].ds != HONGQI )
			{
				mine[y+1][x-1].ds = mine[y+1][x-1].id;
			}
			
		}
		
		if ( x < (s_nMap_W-1) )
		{
			if ( y >0 && mine[y-1][x+1].ds != HONGQI )
			{
				mine[y-1][x+1].ds = mine[y-1][x+1].id;
			}
			
			if ( mine[y][x+1].ds != HONGQI )
			{
				mine[y][x+1].ds = mine[y][x+1].id;
			}
			
			if ( y < (s_nMap_H-1) && mine[y+1][x+1].ds != HONGQI )
			{
				mine[y+1][x+1].ds = mine[y+1][x+1].id;
			}
		}
		
		if ( y > 0 && mine[y-1][x].ds!= HONGQI )
		{
			mine[y-1][x].ds = mine[y-1][x].id;
		}
		
		if ( y < (s_nMap_H-1) && mine[y+1][x].ds != HONGQI )
		{
			mine[y+1][x].ds = mine[y+1][x].id;
		}
		
		mine[y][x].ds =mine[y][x].id= 31;

		return 1;
	}

	return 0;
	
}

/***************************************************************************/
void CheckClickSpace()
{
#pragma warning ( disable: 4127 )
	while( TRUE )
#pragma warning(default: 4127)
	{
		int b = 0;
		
		for ( int y1 = 0; y1 < s_nMap_H; ++y1 )
		{
			for ( int x1 = 0; x1 < s_nMap_W; ++x1 )
			{
				b += CheckNoMineRect( x1, y1 );
			}
		}
		
		if ( b==0 )
		{
			break;
		}
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值