看看下面代码,看看能不能加一个功能:
怎么把一个光标的位图引入到棋盘中,然后用键盘控制光标移动并下棋
#include "stdafx.h"
#include "Five.h"
#include "Table.h"
#include "Messages.h"
#include "Resource.h"
//////////////////////////////////////////////////////////////////////////
// 构造函数,初始化棋盘数据以及图像数据
//////////////////////////////////////////////////////////////////////////
CTable::CTable()
{
CFiveApp *pApp = (CFiveApp *)AfxGetApp();
// 初始化图像列表
m_iml.Create( 24, 24, ILC_COLOR24 | ILC_MASK, 0, 2 );
// 载入黑、白棋子掩码位图
CBitmap bmpBlack, bmpWhite;
CBitmap m_Choosespace;/////就是这个位图,光标,怎么显示到棋盘中去
bmpBlack.LoadBitmap( IDB_BMP_BLACK );
m_iml.Add( &bmpBlack, 0xff00ff );
bmpWhite.LoadBitmap( IDB_BMP_WHITE );
m_iml.Add( &bmpWhite, 0xff00ff );
m_Choosespace.LoadBitmap(choosespace);
m_iml.Add( &m_Choosespace, 0xff00ff );
// 初始化游戏模式
m_pGame = NULL;
}
//////////////////////////////////////////////////////////////////////////
// 析构函数,释放m_pGame指针
//////////////////////////////////////////////////////////////////////////
CTable::~CTable()
{
CFiveApp *pApp = (CFiveApp *)AfxGetApp();
if ( NULL != m_pGame )
delete m_pGame;
}
//////////////////////////////////////////////////////////////////////////
// 在指定棋盘坐标处绘制指定颜色的棋子
//////////////////////////////////////////////////////////////////////////
void CTable::Draw( int x, int y, int color )
{
POINT pt;
pt.x = 12 + 25 * x;
pt.y = 84 + 25 * y;
CDC *pDC = GetDC();
CPen pen;
pen.CreatePen( PS_SOLID, 1, 0xff );
pDC->SelectObject( &pen );
pDC->SetROP2( R2_NOTXORPEN );
m_iml.Draw( pDC, color, pt, ILD_TRANSPARENT );
STEP step;
// 利用R2_NOTXORPEN擦除先前画出的矩形
if ( !m_pGame->m_StepList.empty() )
{
// 获取最后一个点
step = *( m_pGame->m_StepList.begin() );
pDC->MoveTo( 11 + 25 * step.x, 83 + 25 * step.y );
pDC->LineTo( 36 + 25 * step.x, 83 + 25 * step.y );
pDC->LineTo( 36 + 25 * step.x, 108 + 25 * step.y );
pDC->LineTo( 11 + 25 * step.x, 108 + 25 * step.y );
pDC->LineTo( 11 + 25 * step.x, 83 + 25 * step.y );
}
// 更新最后落子坐标数据,画新的矩形
step.color = color;
step.x = x;
step.y = y;
m_pGame->m_StepList.push_front( step );
pDC->MoveTo( 11 + 25 * step.x, 83 + 25 * step.y );
pDC->LineTo( 36 + 25 * step.x, 83 + 25 * step.y );
pDC->LineTo( 36 + 25 * step.x, 108 + 25 * step.y );
pDC->LineTo( 11 + 25 * step.x, 108 + 25 * step.y );
pDC->LineTo( 11 + 25 * step.x, 83 + 25 * step.y );
ReleaseDC( pDC );
}
//////////////////////////////////////////////////////////////////////////
// 处理对方落子后的工作
//////////////////////////////////////////////////////////////////////////
void CTable::Over()
{
// 判断对方是否胜利
if ( Win( 1 - m_color ) )
{
CFiveApp *pApp = (CFiveApp *)AfxGetApp();
pApp->m_nLost++;
CDialog *pDlg = (CDialog *)GetParent();
PlaySound( MAKEINTRESOURCE( IDR_WAVE_LOST ), NULL, SND_RESOURCE | SND_SYNC );
pDlg->MessageBox( _T("您输了,不过不要灰心,失败乃成功之母哦!"), _T("失败"), MB_ICONINFORMATION );
}
m_bWait = FALSE;
}
// 消息映射表
BEGIN_MESSAGE_MAP( CTable, CWnd )
//{{AFX_MSG_MAP(CTable)
ON_WM_PAINT()
ON_WM_LBUTTONUP()
ON_WM_CHAR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//////////////////////////////////////////////////////////////////////////
// 处理WM_PAINT消息
//////////////////////////////////////////////////////////////////////////
void CTable::OnPaint()
{
CPaintDC dc( this );
CDC MemDC;
MemDC.CreateCompatibleDC( &dc );
// 装载棋盘
CBitmap bmp;
CPen pen;
bmp.LoadBitmap( IDB_BMP_QP );
pen.CreatePen( PS_SOLID, 1, 0xff );
MemDC.SelectObject( &bmp );
MemDC.SelectObject( &pen );
MemDC.SetROP2( R2_NOTXORPEN );
// 根据棋盘数据绘制棋子
int x, y;
POINT pt;
for ( y = 0; y < 15; y++ )
{
for ( x = 0; x < 15; x++ )
{
if ( -1 != m_data[x][y] )
{
pt.x = 12 + 25 * x;
pt.y = 84 + 25 * y;
m_iml.Draw( &MemDC, m_data[x][y], pt, ILD_TRANSPARENT );
}
}
}
// 绘制最后落子的指示矩形
if ( NULL != m_pGame && !m_pGame->m_StepList.empty() )
{
STEP step = *( m_pGame->m_StepList.begin() );
MemDC.MoveTo( 11 + 25 * step.x, 83 + 25 * step.y );
MemDC.LineTo( 36 + 25 * step.x, 83 + 25 * step.y );
MemDC.LineTo( 36 + 25 * step.x, 108 + 25 * step.y );
MemDC.LineTo( 11 + 25 * step.x, 108 + 25 * step.y );
MemDC.LineTo( 11 + 25 * step.x, 83 + 25 * step.y );
}
// 完成绘制
dc.BitBlt( 0, 0, 395, 472, &MemDC,0, 0, SRCCOPY );
}
//////////////////////////////////////////////////////////////////////////
// 处理左键弹起消息,为玩家落子之用
//////////////////////////////////////////////////////////////////////////
void CTable::OnLButtonUp( UINT nFlags, CPoint point )
{
STEP stepPut;
if ( m_bWait )
{
MessageBeep( MB_OK );
return;
}
int x, y;
x = ( point.x - 12 ) / 25;
y = ( point.y - 84 ) / 25;
// 如果在(0, 0)~(14, 14)范围内,且该坐标没有落子,则落子于此,否则发声警告并退出过程
if ( x < 0 || x > 14 || y < 0 || y > 14 || m_data[x][y] != -1 )
{
MessageBeep( MB_OK );
return;
}
else
{
// 如果位置合法,则落子
SetData( x, y, m_color );
stepPut.color = m_color;
stepPut.x = x;
stepPut.y = y;
}
// 判断胜利的情况
if ( Win( m_color ) )
{
CFiveApp *pApp = (CFiveApp *)AfxGetApp();
pApp->m_nWin++;
m_pGame->Win( stepPut );
CDialog *pDlg = (CDialog *)GetParent();
PlaySound( MAKEINTRESOURCE( IDR_WAVE_WIN ), NULL, SND_SYNC | SND_RESOURCE );
pDlg->MessageBox( _T("恭喜,您获得了胜利!"), _T("胜利"), MB_ICONINFORMATION );
m_bWait = TRUE;
return;
}
else
{
// 开始等待
m_bWait = TRUE;
// 发送落子信息
PlaySound( MAKEINTRESOURCE( IDR_WAVE_PUT ), NULL, SND_SYNC | SND_RESOURCE );
m_pGame->SendStep( stepPut );
}
}
//////////////////////////////////////////
//处理键盘消息,为落子之用
/////////////////////////////////////
void CTable::OnChar(UINT nChar,UINT nRepCnt,UINT nFlags)
{
STEP stepPut;
POINT pt;
if ( m_bWait )
{
MessageBeep( MB_OK );
return;
}
int x, y;
x = ( pt.x - 12 ) / 25;
y = ( pt.y - 84 ) / 25;
switch(nChar)
{
case VK_W:
y=y-1;break;
case VK_A:
x=x-1;break;
case VK_S:
y=y+1;break;
case VK_D:
x=x+1;break;
default: break;
}
// 如果在(0, 0)~(14, 14)范围内,且该坐标没有落子,则落子于此,否则发声警告并退出过程
if ( x < 0 || x > 14 || y < 0 || y > 14 || m_data[x][y] != -1 )
{
MessageBeep( MB_OK );
return;
}
else if(nChar==VK_RETURN) /*回车确认下子*/
{
// 如果位置合法,则落子
SetData( x, y, m_color );
stepPut.color = m_color;
stepPut.x = x;
stepPut.y = y;
}
// 判断胜利的情况
if ( Win( m_color ) )
{
CFiveApp *pApp = (CFiveApp *)AfxGetApp();
pApp->m_nWin++;
m_pGame->Win( stepPut );
CDialog *pDlg = (CDialog *)GetParent();
PlaySound( MAKEINTRESOURCE( IDR_WAVE_WIN ), NULL, SND_SYNC | SND_RESOURCE );
pDlg->MessageBox( _T("恭喜,您获得了胜利!"), _T("胜利"), MB_ICONINFORMATION );
m_bWait = TRUE;
return;
}
else
{
// 开始等待
m_bWait = TRUE;
// 发送落子信息
PlaySound( MAKEINTRESOURCE( IDR_WAVE_PUT ), NULL, SND_SYNC | SND_RESOURCE );
m_pGame->SendStep( stepPut );
}
}