gusucode.com > 马走日棋盘搜索算法C++源码程序 > 马走日棋盘搜索算法/ChessDisplay/ChessDisplay/ChessDisplayView.cpp

    // ChessDisplayView.cpp : implementation of the CChessDisplayView class
//

#include "stdafx.h"
#include "ChessDisplay.h"

#include "ChessDisplayDoc.h"
#include "ChessDisplayView.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CDC* paintDC = NULL;
//===================================================================
// CChessDisplayView
IMPLEMENT_DYNCREATE(CChessDisplayView, CView)

BEGIN_MESSAGE_MAP(CChessDisplayView, CView)
	//{{AFX_MSG_MAP(CChessDisplayView)
	ON_COMMAND(ID_CHESS_RANGE_EIGHT_EIGHT, OnChessRangeEightEight)
	ON_COMMAND(ID_CHESS_RANGE_FIVE_FIVE, OnChessRangeFiveFive)
	ON_COMMAND(ID_CHESS_RANGE_NINE_NINE, OnChessRangeNineNine)
	ON_COMMAND(ID_CHESS_RANGE_OTHER, OnChessRangeOther)
	ON_COMMAND(ID_CHESS_RANGE_SEVEN_SEVEN, OnChessRangeSevenSeven)
	ON_COMMAND(ID_CHESS_RANGE_SIX_SIX, OnChessRangeSixSix)
	ON_COMMAND(ID_CHESS_RANGE_TEN_TEN, OnChessRangeTenTen)
	ON_COMMAND(ID_START_CALCULATE, OnStartCalculate)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_RESULT_TEXT, OnTextResultDisplay)
	ON_WM_TIMER()
	ON_COMMAND(ID_CARTOON_SHOW, OnCartoonShow)
	ON_COMMAND(ID_DIRECT_SHOW, OnDirectShow)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChessDisplayView construction/destruction

CChessDisplayView::CChessDisplayView()
{
	// TODO: add construction code here
	m_painted = false;
}

CChessDisplayView::~CChessDisplayView()
{
//	delete m_chessSetDlg;
}

BOOL CChessDisplayView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}


/////////////////////////////////////////////////////////////////////////////
// CChessDisplayView drawing

void CChessDisplayView::OnDraw(CDC* pDC)
{

	CChessDisplayDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
/*  
	CBitmap bmp;
   if (bmp.LoadBitmap( IDB_BACK_BMP ))
   {
      // Get the size of the bitmap
      BITMAP bmpInfo;
      bmp.GetBitmap(&bmpInfo);

      // Create an in-memory DC compatible with the
      // display DC we're using to paint
      CDC dcMemory;
      dcMemory.CreateCompatibleDC( pDC );

      // Select the bitmap into the in-memory DC
      CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);

      // Find a centerpoint for the bitmap in the client area
      CRect rect;
      GetClientRect(&rect);

      // Copy the bits from the in-memory DC into the on-
      // screen DC to actually do the painting. Use the centerpoint
      // we computed for the target offset.
      pDC->StretchBlt( 0 , 0 , rect.Width() , rect.Height() , &dcMemory , 0 , 0 , bmpInfo.bmWidth , bmpInfo.bmHeight , SRCCOPY);

      dcMemory.SelectObject(pOldBitmap);
   }
   else
      TRACE0("ERROR: Where's IDB_BITMAP1?\n");
*/
}

/////////////////////////////////////////////////////////////////////////////
// CChessDisplayView diagnostics

#ifdef _DEBUG
void CChessDisplayView::AssertValid() const
{
	CView::AssertValid();
}

void CChessDisplayView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CChessDisplayDoc* CChessDisplayView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CChessDisplayDoc)));
	return (CChessDisplayDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChessDisplayView message handlers

void CChessDisplayView::OnChessRangeEightEight() 
{
	// TODO: Add your command handler code here
	m_chessCalculator.SetSize( 8 , 8 );
}

void CChessDisplayView::OnChessRangeFiveFive() 
{
	// TODO: Add your command handler code here
	m_chessCalculator.SetSize( 5 , 5 );
}

void CChessDisplayView::OnChessRangeNineNine() 
{
	// TODO: Add your command handler code here
	m_chessCalculator.SetSize( 9 , 9 );
}

void CChessDisplayView::OnChessRangeOther() 
{
	// TODO: Add your command handler code here
	if( m_chessSetDlg.DoModal() == IDOK )
	{
		int width  = m_chessSetDlg.GetChessTableWidth();
		int height = m_chessSetDlg.GetChessTableHeight();

		m_chessCalculator.SetSize( width , height );

		int curLocOnX = m_chessSetDlg.GetCurStartLocWidth();
		int curLocOnY = m_chessSetDlg.GetCurStartLocHeight();

		m_chessCalculator.SetStartLocation( curLocOnX , curLocOnY );
	}
}

void CChessDisplayView::OnChessRangeSevenSeven() 
{
	// TODO: Add your command handler code here
	m_chessCalculator.SetSize( 7, 7 );

}

void CChessDisplayView::OnChessRangeSixSix() 
{
	// TODO: Add your command handler code here
	m_chessCalculator.SetSize( 6, 6);
	
}

void CChessDisplayView::OnChessRangeTenTen() 
{
	// TODO: Add your command handler code here
	m_chessCalculator.SetSize( 10 , 10 );
	
}
void CChessDisplayView::OnTextResultDisplay() 
{
	// TODO: Add your command handler code here
}

void CChessDisplayView::OnGraphicResultDisplay() 
{

}

void CChessDisplayView::OnStartCalculate() 
{
	// TODO: Add your command handler code here
	SetTimer( 0 , 10 , NULL );

	CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
	pFrame->UpdateStatusBar( 200 );
	
	AfxBeginThread( MyThreadProcCal , &m_chessCalculator );

	KillTimer( 1 );
}

void CChessDisplayView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if( m_chessCalculator.GetCalculateResult() == WORKING )
		AfxMessageBox("正在搜索结果,请耐心等待!");
	else if( m_chessCalculator.GetCalculateResult() == WAITING )
		AfxMessageBox("请设置参数,开始搜索!");

	CView::OnLButtonDown(nFlags, point);
}

void CChessDisplayView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
	pFrame->UpdateStatusBar( m_chessCalculator.GetSearchSpace() );	
	
	CView::OnTimer(nIDEvent);
}
UINT MyThreadProcCal( LPVOID pParam )
{
	CChessCalculator* chessCalculator = (CChessCalculator*)pParam;
	chessCalculator->StartSearch();
	if( chessCalculator->GetCalculateResult() == FAILED )
		AfxMessageBox("搜索完毕,没有找到可行解!");

	if( chessCalculator->GetCalculateResult() == SUCCESS )
	    AfxMessageBox( "搜索结束,找到可行解,请查看结果!" );

	return 1;
}

void CChessDisplayView::OnCartoonShow() 
{
	// TODO: Add your command handler code here

	if( m_chessCalculator.GetCalculateResult() == WAITING )
		AfxMessageBox("请设置参数,然后开始搜索!");

	else if( m_chessCalculator.GetCalculateResult() == WORKING )
		AfxMessageBox("正在搜索,请耐心等待!");

	if( m_chessCalculator.GetCalculateResult() == FAILED )
		AfxMessageBox("搜索完毕,没有找到可行解!");

	if( m_chessCalculator.GetCalculateResult() == SUCCESS )
	{

		m_chessCalculator.SetShowDelay( true );

    	CDC* pDC = GetDC();
		paintDC = pDC;
		if( m_painted )
			pDC->FillSolidRect( 0 , 0 , 800 , 600 , RGB( 255,255,255) );
		m_chessCalculator.DisplayResult( pDC );	
		m_painted = true;
		ReleaseDC( pDC );
	}	
}

void CChessDisplayView::OnDirectShow() 
{
	// TODO: Add your command handler code here

	if( m_chessCalculator.GetCalculateResult() == WAITING )
		AfxMessageBox("请设置参数,然后开始搜索!");

	else if( m_chessCalculator.GetCalculateResult() == WORKING )
		AfxMessageBox("正在搜索,请耐心等待!");

	if( m_chessCalculator.GetCalculateResult() == FAILED )
		AfxMessageBox("搜索完毕,没有找到可行解!");

	if( m_chessCalculator.GetCalculateResult() == SUCCESS )
	{
		m_chessCalculator.SetShowDelay( false );

    	CDC* pDC = GetDC();
		paintDC = pDC;
		if( m_painted )
			pDC->FillSolidRect( 0 , 0 , 800 , 600 , RGB( 255,255,255) );

		m_chessCalculator.DisplayResult( pDC );	
		m_painted = true;
		ReleaseDC( pDC );

	}
	
}