⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queenstatic.cpp

📁 8皇后演示程序
💻 CPP
字号:
// QueenStatic.cpp : implementation file
//

#include "stdafx.h"
#include "EightQueen.h"
#include "QueenStatic.h"

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

/////////////////////////////////////////////////////////////////////////////
// CQueenStatic

CQueenStatic::CQueenStatic()
{
	m_nSumofQueens = 8;
	m_nMoreWidht = 0;
	m_nMoreHeight = 0;
	m_pQueenPlace = new int[m_nSumofQueens];
	memset(m_pQueenPlace, -1, sizeof(int) * m_nSumofQueens);
}

CQueenStatic::~CQueenStatic()
{
	delete []m_pQueenPlace;
	m_nSumofQueens = NULL;
}


BEGIN_MESSAGE_MAP(CQueenStatic, CStatic)
	//{{AFX_MSG_MAP(CQueenStatic)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQueenStatic message handlers

void CQueenStatic::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	CRect rect;
	GetWindowRect(&rect);

	int width = rect.Width();
	int height = rect.Height();
	m_nBlockLength = (width < height ? width : height) / m_nSumofQueens;
	m_nMoreWidht = (width - m_nBlockLength * m_nSumofQueens) / 2;
	m_nMoreHeight = (height - m_nBlockLength * m_nSumofQueens) / 2;
	
	CDC MemDC;
	CBitmap MemBitmap;
	MemDC.CreateCompatibleDC(&dc);	
	MemBitmap.CreateCompatibleBitmap(&dc, width, height);
	
	CPen r_pen(PS_SOLID, 1, RGB(255, 0, 0));
	CBrush b_brush;
	b_brush.CreateSolidBrush(RGB(255, 255, 255));

	CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
	MemDC.FillRect(CRect(1, 1, width - 1, height - 1), &b_brush);

	DrawBoard(&MemDC);
	DrawQueenPlace(&MemDC);

	dc.BitBlt(0, 0, width, height, &MemDC, 0, 0, SRCCOPY);
}

/**************************画底板**************************/
void CQueenStatic::DrawBoard(CDC *pDC)
{
	CBrush w_brush, b_brush;
	b_brush.CreateSolidBrush(RGB(0, 0, 0));	
	w_brush.CreateSolidBrush(RGB(255, 255, 255));

	for(int i = 0; i < m_nSumofQueens; i++) 
	{
		for(int j = 0; j < m_nSumofQueens; j++) 
		{
			if((i + j) % 2 ==0 )
			{
				pDC->FillRect(CRect(i * m_nBlockLength + m_nMoreWidht, j * m_nBlockLength + m_nMoreHeight, 
					(i + 1) * m_nBlockLength + m_nMoreWidht - 1, (j + 1) * m_nBlockLength + m_nMoreHeight - 1), &b_brush);
			} 
			else
			{
				pDC->FillRect(CRect(i * m_nBlockLength + m_nMoreWidht, j * m_nBlockLength + m_nMoreHeight, 
					(i + 1) * m_nBlockLength + m_nMoreWidht - 1, (j + 1) * m_nBlockLength + m_nMoreHeight - 1), &w_brush);
			}
		}
	}
	CPen b_pen(PS_SOLID, 1, RGB(0, 0, 0));
	int board = m_nBlockLength * m_nSumofQueens;
	pDC->SelectObject(b_pen);  
	pDC->MoveTo(m_nMoreWidht, m_nMoreHeight);
	pDC->LineTo(m_nMoreWidht, board-1 + m_nMoreHeight);
	pDC->LineTo(board - 1 + m_nMoreWidht, board - 1 + m_nMoreHeight);
	pDC->LineTo(board - 1 + m_nMoreWidht, m_nMoreHeight);  
	pDC->LineTo(m_nMoreWidht, m_nMoreHeight);
}

/**************************画皇后所在单元格**************************/
void CQueenStatic::DrawQueenPlace(CDC *pDC)
{
	CBrush y_brush;
	y_brush.CreateSolidBrush(RGB(255, 0, 0));
	
	pDC->SelectObject(y_brush);     
	for (int i = 0; i < m_nSumofQueens; i++)
	{			
		if(m_pQueenPlace[i] >= 0)
		{					
			pDC->Ellipse(m_nMoreWidht + m_pQueenPlace[i] * m_nBlockLength + m_nBlockLength / 6, 
				m_nMoreHeight + i * m_nBlockLength + m_nBlockLength / 6,
				m_nMoreWidht + m_pQueenPlace[i] * m_nBlockLength + m_nBlockLength / 6 + m_nBlockLength * 2 / 3,
				m_nMoreHeight + i * m_nBlockLength + m_nBlockLength / 6 + m_nBlockLength * 2 / 3);
		}
	}
}

/**************************设置皇后个数**************************/
void CQueenStatic::SetNumofQueens(const int number)
{
	m_nSumofQueens = number;
	delete []m_pQueenPlace;
	m_pQueenPlace = new int[m_nSumofQueens];
	ASSERT(m_pQueenPlace);
	
	RedrawWindow();
}

/**************************得到某一结果**************************/
void CQueenStatic::GetQueenPlace(int *queenplace)
{
	for(int i = 0; i < m_nSumofQueens; i++)
	{
		m_pQueenPlace[i] = queenplace[i];
	}
	RedrawWindow();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -