www.pudn.com > ɨÀ×Ô´Âë.rar > MineWnd.cpp
// MineWnd.cpp : implementation file
//
#include "stdafx.h"
#include "mine.h"
#include "MineWnd.h"
#include "HeroDlg.h"
#include "CustomDlg.h"
#include "RecordDlg.h"
#include "Mmsystem.h"
#pragma comment(lib, "Winmm.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CMineApp theApp;
UINT& uWidth = theApp.uCustomWidth;
UINT& uHeight = theApp.uCustomHeight;
/////////////////////////////////////////////////////////////////////////////
// CMineWnd
CMineWnd::CMineWnd()
{
bLoadWavRes = FALSE;
pBlastWavMem = NULL;
pWinWavMem = NULL;
pCountWavMem = NULL;
pMines = NULL;
m_nTimer = 0;
backGroundBrush.CreateSolidBrush(COLOR_GRAY);
if(theApp.bColor)
darkColor = COLOR_DARK_GRAY;
else
darkColor = COLOR_BLACK;
SetBitmap();
if(theApp.bSound)
{
LoadWavRes();
}
ReAssign();
GameInit();
}
CMineWnd::~CMineWnd()
{
FreeWavRes();
if(pMines)
{
delete[] pMines;
}
}
///////////////////////////////////////////////////////////////////////
// my functions
void CMineWnd::GameInit()
{
uButtonState = BUTTON_NORMAL;
bHitButton = FALSE;
bInButtonRect = FALSE;
uGameState = WAIT;
bFirstClick = TRUE;
ClickNumber = 0;
nOldPos = -1;
nNewPos = -1;
bDoubleClick = FALSE;
theApp.uSecends = 0;
bInMineArea = FALSE;
bRBUPOutMineArea = FALSE;
theApp.uRemainMines = theApp.uCustomMineNum;
if(m_nTimer)
{
KillTimer( ID_TIMER_EVENT );
m_nTimer = 0;
}
if(pMines)
{
for(UINT i=0; icmp_w && cmp_i%cmp_w>0)
around[j++] = cmp_i-cmp_w-1;
if(cmp_i-cmp_w >= 0)
around[j++] = cmp_i-cmp_w;
if(cmp_i>cmp_w-1 && (cmp_i+1)%cmp_w>0)
around[j++] = cmp_i-cmp_w+1;
if(cmp_i%cmp_w > 0)
around[j++] = cmp_i-1;
if((cmp_i+1)%cmp_w>0)
around[j++] = cmp_i+1;
if(cmp_i<=((int)uHeight-1)*cmp_w && cmp_i%cmp_w>0)
around[j++] = cmp_i+cmp_w-1;
if(cmp_i+cmp_w<(int)uTotle)
around[j++] = cmp_i+cmp_w;
if(cmp_i<((int)uHeight-1)*cmp_w && (cmp_i+1)%cmp_w>0)
around[j++] = cmp_i+cmp_w+1;
around[8] = j;
}
int CMineWnd::GetStation(int x,int y) //
{
int m = x - MINE_AREA_X;
int n = y - MINE_AREA_Y;
if( m<0 || (UINT)m>uWidth*16 ||
n<0 || (UINT)n>uHeight*16 )
{
return -1;
}
m = m/16;
n = n/16;
return m+n*uWidth;
}
void CMineWnd::SetMine( int pos ) //
{
srand( (unsigned)time( NULL ) );
int temp;
for( UINT i=0; i= STATE_NUMBER_8 &&
pMines[around[j]].uState <= STATE_NUMBER_1 &&
pMines[around[j]].uState != STATE_OPENED)
{
pMines[around[j]].uDraw = pMines[around[j]].uState;
pMines[around[j]].uState = STATE_OPENED;
ClickNumber++;
MyInvalidateRect( rectClick, around[j] );
}
}
}
}
}
BOOL CMineWnd::Victory()
{
if( ClickNumber == uTotle-theApp.uCustomMineNum )
{
uGameState = VICTORY;
KillTimer( ID_TIMER_EVENT );
m_nTimer = 0;
uButtonState = BUTTON_VICTORY;
CRect rectButton(uButtonPos[1],15,uButtonPos[2],42);
InvalidateRect( rectButton );
theApp.uRemainMines = 0;
CRect rectMineNum(17,16,55,38);
InvalidateRect( rectMineNum );
RECT rectClick;
for(UINT i=0; i= theApp.uPrimaryRecord )
{
return TRUE;
}
theApp.uPrimaryRecord = theApp.uSecends;
}
else if( theApp.uGameLevel == LEVEL_SECONDARY )
{
if( theApp.uSecends >= theApp.uSecondaryRecord )
{
return TRUE;
}
theApp.uSecondaryRecord = theApp.uSecends;
}
else if( theApp.uGameLevel == LEVEL_ADVANCED )
{
if( theApp.uSecends >= theApp.uAdvancedRecord )
{
return TRUE;
}
theApp.uAdvancedRecord = theApp.uSecends;
}
else // custom
{
return TRUE;
}
CRecordDlg recordDlg;
recordDlg.DoModal();
CHeroDlg heroDlg;
heroDlg.DoModal();
return TRUE;
}
return FALSE;
}
void CMineWnd::MyInvalidateRect( RECT &rectClick, int pos )
{
rectClick.left = MINE_AREA_X + pos%uWidth*16;
rectClick.right = rectClick.left + 16;
rectClick.top = MINE_AREA_Y + pos/uWidth*16;
rectClick.bottom = rectClick.top + 16;
InvalidateRect(&rectClick);
}
void CMineWnd::ProcessLeftButtonUp( int pos )
{
if( bFirstClick ) // first clicked
{
bFirstClick = FALSE;
SetMine( pos );
SetNumbers();
GetEmptyAreas(); // set empty area flag
}
if( pMines[pos].uState >= STATE_NUMBER_8 &&
pMines[pos].uState <= STATE_NUMBER_1 &&
pMines[pos].uDraw != DRAW_FLAG ) // flag number
{
ClickNumber++;
pMines[pos].uDraw = pMines[pos].uState;
pMines[pos].uState = STATE_OPENED;
RECT rectClick;
MyInvalidateRect( rectClick, nNewPos );
Victory(); // victory?
}
else if( pMines[pos].uState == STATE_MINE )
{
if( pMines[pos].uDraw == DRAW_QUESTION_MARK ||
pMines[pos].uDraw == DRAW_EMPTY) // dead
{
sndPlaySound((LPCSTR)LockResource(pBlastWavMem),
SND_MEMORY | SND_ASYNC | SND_NODEFAULT);
uGameState = DEAD;
KillTimer( ID_TIMER_EVENT );
m_nTimer = 0;
CRect rectButton(uButtonPos[1],15,uButtonPos[2],42);
uButtonState = BUTTON_DEAD;
InvalidateRect(rectButton);
pMines[pos].uDraw = DRAW_BLAST;
FlagAllMine();
}
}
else if( pMines[pos].uState >= EMPTY_AREA_START )
{
DrawEmptyArea( pMines[pos].uState ); // flag empty area
Victory(); // is victory
}
}
void CMineWnd::ProcessLeftRightButtonUp( int pos )
{
// compute round
int around[10];
GetAroundPos( pos, around );
if( pMines[ pos ].uDraw >= DRAW_NUMBER_8 &&
pMines[ pos ].uDraw <= DRAW_NUMBER_1 &&
pMines[ pos ].uState == STATE_OPENED )
{
UINT trueFlag = 0;
UINT falseFlag = 0;
UINT flag = 0;
UINT error[8];
UINT errorNum = 0;
for(UINT i=0; i<(UINT)around[8]; i++)
{
if( pMines[around[i]].uState == STATE_MINE &&
pMines[around[i]].uDraw == DRAW_FLAG )//
{
trueFlag++;
flag++;
}
else if( pMines[around[i]].uState == STATE_MINE &&
pMines[around[i]].uDraw != DRAW_FLAG ) //
{
falseFlag++;
error[errorNum++] = around[i];
}
else if(pMines[around[i]].uState != STATE_MINE &&
pMines[around[i]].uDraw == DRAW_FLAG) //
{
flag++;
falseFlag++;
}
}
if( trueFlag == flag && falseFlag == 0 ) //
{
RECT rectClick;
for( i=0; i<(UINT)around[8]; i++)
{
if( pMines[around[i]].uState != STATE_OPENED )
{
if( pMines[around[i]].uState >= STATE_NUMBER_8 &&
pMines[around[i]].uState <= STATE_NUMBER_1 )
{
pMines[around[i]].uDraw = pMines[around[i]].uState;
ClickNumber++;
pMines[around[i]].uState = STATE_OPENED;
MyInvalidateRect( rectClick, around[i] );
}
else if( pMines[around[i]].uState >= EMPTY_AREA_START )
{
// get and flag empty area
DrawEmptyArea( pMines[around[i]].uState );
}
}
}
Victory(); // is victory
}
else if( falseFlag > 0 ) //
{
if( DRAW_NUMBER_1 - pMines[pos].uDraw +1 == flag)
{
sndPlaySound((LPCSTR)LockResource(pBlastWavMem),
SND_MEMORY | SND_ASYNC | SND_NODEFAULT);
uGameState = DEAD;
KillTimer( ID_TIMER_EVENT );
m_nTimer = 0;
CRect rectButton(uButtonPos[1],15,uButtonPos[2],42);
uButtonState = BUTTON_DEAD;
InvalidateRect(rectButton);
for( i=0; iCheckMenuItem(ID_MENU_PRIMARY, MF_UNCHECKED | MF_BYCOMMAND);
pSubMenu->CheckMenuItem(ID_MENU_SECONDARY, MF_UNCHECKED | MF_BYCOMMAND);
pSubMenu->CheckMenuItem(ID_MENU_ADVANCED, MF_UNCHECKED | MF_BYCOMMAND);
pSubMenu->CheckMenuItem(ID_MENU_CUSTOM, MF_UNCHECKED | MF_BYCOMMAND);
switch(theApp.uGameLevel)
{
case LEVEL_PRIMARY:
pSubMenu->CheckMenuItem(ID_MENU_PRIMARY, MF_CHECKED | MF_BYCOMMAND);
break;
case LEVEL_SECONDARY:
pSubMenu->CheckMenuItem(ID_MENU_SECONDARY, MF_CHECKED | MF_BYCOMMAND);
break;
case LEVEL_ADVANCED:
pSubMenu->CheckMenuItem(ID_MENU_ADVANCED, MF_CHECKED | MF_BYCOMMAND);
break;
case LEVEL_CUSTOM:
pSubMenu->CheckMenuItem(ID_MENU_CUSTOM, MF_CHECKED | MF_BYCOMMAND);
break;
default:
break;
}
}
}
void CMineWnd::SetWindowSize()
{
UINT uWindowWidth = theApp.uFrameWidth + uWidth*MINE_WIDTH +
LINE_WIDTH_0*3 + SIDE_WIDTH_0 + SIDE_WIDTH_1;
UINT uWindowHeight = theApp.uFrameHeight + uHeight*MINE_HEIGHT +
LINE_WIDTH_0*3 + SIDE_WIDTH_1 + SIDE_WIDTH_0*2 + RECT_HEIGHT_0;
SetWindowPos(&CWnd::wndBottom,0,0,uWindowWidth,uWindowHeight,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOCOPYBITS );
SetPosition();
}
void CMineWnd::SetPosition()
{
GetClientRect(&rect);
// NUMBER pos
uNumPos[0] = rect.right-55;
uNumPos[1] = rect.right-15;
uNumPos[2] = rect.right-54;
uNumPos[3] = rect.right-41;
uNumPos[4] = rect.right-28;
// BUTTON POS
uButtonPos[0] = rect.right/2-12;
uButtonPos[1] = rect.right/2-13;
uButtonPos[2] = rect.right/2+12;
// Rectangle POS
uRectPosX[0] = rect.right-14;
uRectPosX[1] = rect.right-16;
uRectPosX[2] = rect.right-18;
uRectPosX[3] = rect.right-6;
uRectPosX[4] = rect.right-7;
uRectPosX[5] = rect.right-8;
uRectPosX[6] = RECT_X_0+1;
uRectPosX[7] = RECT_X_1+1;
uRectPosX[8] = RECT_X_1+2;
uRectPosX[9] = rect.right;
uRectPosY[0] = rect.bottom-RECT_Y_1-5;
uRectPosY[1] = rect.bottom-RECT_Y_1-7;
uRectPosY[2] = rect.bottom-RECT_Y_1-9;
uRectPosY[3] = rect.bottom-8;
uRectPosY[4] = rect.bottom-7;
uRectPosY[5] = rect.bottom-6;
uRectPosY[6] = RECT_Y_0+1;
uRectPosY[7] = RECT_Y_1+1;
uRectPosY[8] = RECT_Y_1+2;
uRectPosY[9] = rect.bottom;
}
void CMineWnd::MyDrawNumber( CPaintDC& dc )
{
dc.Draw3dRect(16,15,41,25,darkColor,COLOR_WHITE);
dc.Draw3dRect(uNumPos[0],15,41,25,darkColor,COLOR_WHITE);
dc.SetPixel(56,15,COLOR_GRAY);
dc.SetPixel(16,39,COLOR_GRAY);
dc.SetPixel(uNumPos[1],15,COLOR_GRAY);
dc.SetPixel(uNumPos[0],39,COLOR_GRAY);
CDC cdc;
cdc.CreateCompatibleDC(&dc);
cdc.SelectObject(BMP_NUMBER);
UINT num;
// darw remain mine number
if(theApp.uRemainMines < 0)
num = 11;
else
num = theApp.uRemainMines/100;
dc.StretchBlt(17,16,13,23,&cdc,0,253-23*num,13, 23, SRCCOPY);
num = abs(theApp.uRemainMines)/10%10;
dc.StretchBlt(30,16,13,23,&cdc,0,253-23*num,13, 23, SRCCOPY);
num = abs(theApp.uRemainMines)%10;
dc.StretchBlt(43,16,13,23,&cdc,0,253-23*num,13, 23, SRCCOPY);
// draw remain seconds
num = theApp.uSecends/100;
dc.StretchBlt(uNumPos[2],16,13,23,&cdc,0,253-23*num,13, 23, SRCCOPY);
num = theApp.uSecends/10%10;
dc.StretchBlt(uNumPos[3],16,13,23,&cdc,0,253-23*num,13, 23, SRCCOPY);
num = theApp.uSecends%10;
dc.StretchBlt(uNumPos[4],16,13,23,&cdc,0,253-23*num,13, 23, SRCCOPY);
}
void CMineWnd::MyDrawButton( CPaintDC& dc )
{
CDC cdc;
cdc.CreateCompatibleDC(&dc);
cdc.SelectObject(BMP_BUTTON);
dc.StretchBlt(uButtonPos[0],16,24,24,&cdc,0,24*uButtonState,24,24, SRCCOPY);
dc.Draw3dRect(uButtonPos[1],15,26,26,darkColor,darkColor);
dc.SetPixel(uButtonPos[1],40,COLOR_GRAY);
dc.SetPixel(uButtonPos[2],15,COLOR_GRAY);
}
void CMineWnd::MyDrawMineArea( CPaintDC& dc )
{
CDC cdc;
cdc.CreateCompatibleDC(&dc);
cdc.SelectObject(BMP_MINEAREA);
for(UINT i=0; im_hWnd,theApp.sResource[5],
theApp.sResource[6],theApp.LoadIcon(IDR_MAINFRAME));
}
void CMineWnd::OnMenuHelpList() //?
{
::WinExec("HH winmine.chm", SW_SHOW);
}
void CMineWnd::OnMenuHelpFind() //?
{
::WinExec("HH WINMINE.CHM", SW_SHOW);
}
void CMineWnd::OnMenuHelpUse() //?
{
::WinExec("HH NTHelp.chm", SW_SHOW);
}
void CMineWnd::OnMenuGameStart()
{
ReAssign();
GameInit();
Invalidate();
}
void CMineWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if( nChar == VK_F1 )
{
OnMenuHelpList();
}
else if( nChar == VK_F2 )
{
OnMenuGameStart();
}
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CMineWnd::OnMenuPrimary()
{
theApp.uGameLevel = LEVEL_PRIMARY;
uHeight = PRIMARY_HEIGHT;
uWidth = PRIMARY_WIDTH;
theApp.uCustomMineNum = PRIMARY_MINE_NUM;
MyCheckMenuItem();
SetWindowSize();
ReAssign();
GameInit();
Invalidate();
}
void CMineWnd::OnMenuSecondary()
{
theApp.uGameLevel = LEVEL_SECONDARY;
uHeight = SECONDARY_HEIGHT;
uWidth = SECONDARY_WIDTH;
theApp.uCustomMineNum = SECONDARY_MINE_NUM;
MyCheckMenuItem();
SetWindowSize();
ReAssign();
GameInit();
Invalidate();
}
void CMineWnd::OnMenuAdvanced()
{
theApp.uGameLevel = LEVEL_ADVANCED;
uWidth = ADVANCED_WIDTH;
uHeight = ADVANCED_HEIGHT;
theApp.uCustomMineNum = ADVANCED_MINE_NUM;
MyCheckMenuItem();
SetWindowSize();
ReAssign();
GameInit();
Invalidate();
}
void CMineWnd::OnMenuCustom()
{
theApp.uGameLevel = LEVEL_CUSTOM;
CCustomDlg customDlg;
if(customDlg.DoModal() == IDOK)
{
SetWindowSize();
}
MyCheckMenuItem();
ReAssign();
GameInit();
Invalidate();
}
void CMineWnd::OnMenuMark()
{
theApp.bMarked = !theApp.bMarked;
MyCheckMarkItem();
}
void CMineWnd::OnMenuSound()
{
theApp.bSound = !theApp.bSound;
MyCheckSoundItem();
if( !bLoadWavRes && theApp.bSound)
{
LoadWavRes();
}
else if( bLoadWavRes && !theApp.bSound )
{
FreeWavRes();
}
}
void CMineWnd::OnMenuColor()
{
theApp.bColor = !theApp.bColor;
SetBitmap();
MyCheckColorItem();
if(theApp.bColor)
darkColor = COLOR_DARK_GRAY;
else
darkColor = COLOR_BLACK;
Invalidate();
}
void CMineWnd::MyCheckColorItem()
{
if(theApp.bColor)
{
pSubMenu->CheckMenuItem(ID_MENU_COLOR, MF_CHECKED | MF_BYCOMMAND);
}
else
{
pSubMenu->CheckMenuItem(ID_MENU_COLOR, MF_UNCHECKED | MF_BYCOMMAND);
}
}
void CMineWnd::MyCheckSoundItem()
{
if(theApp.bSound)
{
pSubMenu->CheckMenuItem(ID_MENU_SOUND, MF_CHECKED | MF_BYCOMMAND);
}
else
{
pSubMenu->CheckMenuItem(ID_MENU_SOUND, MF_UNCHECKED | MF_BYCOMMAND);
}
}
void CMineWnd::MyCheckMarkItem()
{
if(theApp.bMarked)
{
pSubMenu->CheckMenuItem(ID_MENU_MARK, MF_CHECKED | MF_BYCOMMAND);
}
else
{
pSubMenu->CheckMenuItem(ID_MENU_MARK, MF_UNCHECKED | MF_BYCOMMAND);
}
}
void CMineWnd::OnMenuHeroList()
{
CHeroDlg heroDlg;
heroDlg.DoModal();
}
void CMineWnd::OnInitMenu(CMenu* pMenu)
{
CWnd::OnInitMenu(pMenu);
if( (pSubMenu = pMenu->GetSubMenu(0)) == NULL )
{
AfxMessageBox(theApp.sResource[7]);
PostQuitMessage(0);
}
else
{
MyCheckMenuItem();
MyCheckColorItem();
MyCheckMarkItem();
MyCheckSoundItem();
}
}
void CMineWnd::OnPaint()
{
CDC cdc;
CPaintDC dc(this); // device context for painting
CBitmap bitmap;
CBitmap* pOldBitmap;
if( !dc.IsPrinting() )
{
if( cdc.CreateCompatibleDC( &dc ) )
{
if(bitmap.CreateCompatibleBitmap(&dc,rect.right,rect.bottom))
{
pOldBitmap = cdc.SelectObject(&bitmap);
cdc.FillRect(&rect,&backGroundBrush);
MyDrawRectangle( (CPaintDC &)cdc );
MyDrawMineArea( (CPaintDC &)cdc );
MyDrawButton( (CPaintDC &)cdc );
MyDrawNumber( (CPaintDC &)cdc );
dc.BitBlt(rect.left, rect.top, rect.right, rect.bottom,
&cdc, 0, 0, SRCCOPY);
cdc.SelectObject( pOldBitmap );
bitmap.DeleteObject();
}
}
}
}
void CMineWnd::OnTimer(UINT nIDEvent)
{
if( nIDEvent == ID_TIMER_EVENT )
{
if(theApp.bSound)
{
sndPlaySound((LPCSTR)LockResource(pCountWavMem),
SND_MEMORY | SND_ASYNC | SND_NODEFAULT);
}
theApp.uSecends++;
CRect rect(uNumPos[0],15,uNumPos[1],39);
InvalidateRect(rect);
if(theApp.uSecends >= DEFAULT_RECORD)
{
KillTimer( ID_TIMER_EVENT );
m_nTimer = 0;
}
}
CWnd::OnTimer(nIDEvent);
}
void CMineWnd::OnClose()
{
KillTimer( ID_TIMER_EVENT );
CWnd::OnClose();
}
void CMineWnd::OnMenuExit()
{
PostQuitMessage(0);
}
void CMineWnd::OnMouseMove(UINT nFlags, CPoint point)
{
bInMineArea = FALSE;
if((nFlags == MK_LBUTTON)||(nFlags == (MK_LBUTTON|MK_RBUTTON)))
{
SetCapture(); // Capture mouse
CRect rectButton(uButtonPos[1],15,uButtonPos[2],42);
CRect rectMineArea(MINE_AREA_X, MINE_AREA_Y,
MINE_AREA_X+uWidth*MINE_WIDTH, MINE_AREA_Y+uHeight*MINE_HEIGHT);
if (rectButton.PtInRect(point))
{
// get into the button area, button down
if( bHitButton && !bInButtonRect )
{
bInButtonRect = TRUE;
uButtonState = BUTTON_DOWN;
InvalidateRect(rectButton);
}
}
else if(rectMineArea.PtInRect(point))
{
bInMineArea = TRUE;
if( uGameState == WAIT || uGameState == RUN )
{
nNewPos = GetStation(point.x,point.y);
if( nNewPos != nOldPos)
{
if( nFlags == (MK_LBUTTON|MK_RBUTTON))
{
int around[10];
int num;
if(nOldPos > -1)
{
GetAroundPos( nOldPos, around );
num = around[8];
around[num] = nOldPos;
SetMineUpEx(around,num); // up
}
GetAroundPos( nNewPos, around );
num = around[8];
around[num] = nNewPos;
SetMineDownEx(around,num); // down
}
else if( nFlags == MK_LBUTTON && !bRBUPOutMineArea )
{
SetMineDown( nNewPos);// down
if(nOldPos > -1)
{
SetMineUp( nOldPos );//up
}
}
}
nOldPos = nNewPos;
}
}
else // at orther area
{
if( bInButtonRect ) // leave the button area, button up
{
bInButtonRect = FALSE;
if( uGameState == WAIT || uGameState == RUN )
{
uButtonState = BUTTON_NORMAL;
}
else if( uGameState == DEAD )
{
uButtonState = BUTTON_DEAD;
}
else
{
uButtonState = BUTTON_VICTORY;
}
InvalidateRect(rectButton);
}
if( uGameState == WAIT || uGameState == RUN )
{
// the mine area is up when mouse is leave
if( nOldPos > -1 )
{
if( nFlags == MK_LBUTTON )// up
{
SetMineUp( nOldPos );
}
else if( nFlags == (MK_LBUTTON|MK_RBUTTON))
{
int around[10];
int num;
if(nOldPos > -1) // up
{
GetAroundPos( nOldPos, around );
num = around[8];
around[num] = nOldPos;
SetMineUpEx(around,num);
}
}
nNewPos = -1;
nOldPos = -1;
}
}
}
}
else
{
ReleaseCapture();
}
CWnd::OnMouseMove(nFlags, point);
}
void CMineWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
bRBUPOutMineArea = FALSE;
CRect rectButton(uButtonPos[1],15,uButtonPos[2],42);
CRect rectMineArea(MINE_AREA_X, MINE_AREA_Y,
MINE_AREA_X+uWidth*MINE_WIDTH, MINE_AREA_Y+uHeight*MINE_HEIGHT);
if (rectButton.PtInRect(point)) // clicked button
{
bHitButton = TRUE;
bInButtonRect = TRUE;
uButtonState = BUTTON_DOWN;
InvalidateRect(rectButton);
}
else if(rectMineArea.PtInRect(point)) // clicked mine area
{
if( uGameState == WAIT || uGameState == RUN )
{
nNewPos = GetStation(point.x,point.y);
SetMineDown( nNewPos ); // down
nOldPos = nNewPos;
uButtonState = BUTTON_CLICK;
InvalidateRect( rectButton );
if( nFlags == (MK_LBUTTON|MK_RBUTTON) )
{
int around[9];
GetAroundPos( nNewPos, around );
SetMineDownEx(around,around[8]-1); // down
bDoubleClick = TRUE;
}
}
}
else // clicked other area
{
if( uGameState == WAIT || uGameState == RUN )
{
uButtonState = BUTTON_CLICK;
InvalidateRect(rectButton);
}
}
CWnd::OnLButtonDown(nFlags, point);
}
void CMineWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
CRect rectButton(uButtonPos[1],15,uButtonPos[2],42);
CRect rectMineArea(MINE_AREA_X, MINE_AREA_Y,
MINE_AREA_X+uWidth*MINE_WIDTH, MINE_AREA_Y+uHeight*MINE_HEIGHT);
if( bHitButton && rectButton.PtInRect(point) )
{
GameInit(); // reset
Invalidate();
return;
}
else if( rectMineArea.PtInRect(point) )
{
if( (uGameState == WAIT || uGameState == RUN) )
{
if( uGameState == WAIT && !bRBUPOutMineArea )
{
if( (bDoubleClick && nFlags == MK_RBUTTON) ||
!bDoubleClick )
{
if(m_nTimer)
{
KillTimer( ID_TIMER_EVENT );
m_nTimer = 0;
}
m_nTimer = SetTimer(ID_TIMER_EVENT,1000,0);
uGameState = RUN;
}
}
if( bDoubleClick )
{
ProcessLeftRightButtonUp( nNewPos );
bDoubleClick = FALSE;
}
else if( !bRBUPOutMineArea )
{
ProcessLeftButtonUp( nNewPos );
}
}
}
bHitButton = FALSE;
bInButtonRect = FALSE;
if( (uGameState == WAIT || uGameState == RUN) &&
uButtonState == BUTTON_CLICK )
{
uButtonState = BUTTON_NORMAL;
InvalidateRect(rectButton);
}
CWnd::OnLButtonUp(nFlags, point);
}
void CMineWnd::OnRButtonDown(UINT nFlags, CPoint point)
{
bRBUPOutMineArea = FALSE;
CRect rectMineArea(MINE_AREA_X, MINE_AREA_Y,
MINE_AREA_X+uWidth*MINE_WIDTH, MINE_AREA_Y+uHeight*MINE_HEIGHT);
if( rectMineArea.PtInRect(point) )
{
if( uGameState == WAIT || uGameState == RUN )
{
if( nFlags == (MK_LBUTTON|MK_RBUTTON) ) // down
{
int around[10];
GetAroundPos( nNewPos, around );
int num = around[8];
around[num] = nNewPos;
SetMineDownEx(around,num);
bDoubleClick = TRUE;
}
else // chang flag
{
int pos = GetStation(point.x,point.y);
ChangeFlag( pos );
}
}
}
CWnd::OnRButtonDown(nFlags, point);
}
void CMineWnd::OnRButtonUp(UINT nFlags, CPoint point)
{
if( (uGameState == WAIT || uGameState == RUN) &&
uButtonState == BUTTON_CLICK )
{
uButtonState = BUTTON_NORMAL;
CRect rectButton(uButtonPos[1],15,uButtonPos[2],42);
InvalidateRect(rectButton);
}
if( !bInMineArea )
{
bDoubleClick = FALSE;
bRBUPOutMineArea = TRUE;
}
else if( nFlags != MK_LBUTTON )
{
bRBUPOutMineArea = FALSE;
}
if( uGameState == RUN || uGameState == WAIT )
{
CRect rectMineArea(MINE_AREA_X, MINE_AREA_Y,
MINE_AREA_X+uWidth*MINE_WIDTH, MINE_AREA_Y+uHeight*MINE_HEIGHT);
if( rectMineArea.PtInRect(point) )
{
if( nFlags == MK_LBUTTON )
{
if( uGameState == WAIT )
{
if(m_nTimer)
{
KillTimer( ID_TIMER_EVENT );
m_nTimer = 0;
}
m_nTimer = SetTimer(ID_TIMER_EVENT,1000,0);
uGameState = RUN; // start game
}
ProcessLeftRightButtonUp( nNewPos );//
}
}
}
CWnd::OnRButtonUp(nFlags, point);
}
BOOL CMineWnd::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}