QueenPanel.cpp
资源名称:NQueen.rar [点击查看]
上传用户:hncsjd
上传日期:2022-07-08
资源大小:3772k
文件大小:3k
源码类别:
其他智力游戏
开发平台:
Visual C++
- // QueenPanel.cpp : implementation file
- //
- #include "stdafx.h"
- #include "NQueen.h"
- #include "QueenPanel.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CQueenPanel
- CQueenPanel::CQueenPanel()
- {
- this->queen = NULL;
- N = 0;
- }
- CQueenPanel::CQueenPanel(int size)
- {
- this->queen = NULL;
- N = 0;
- SetSize(size);
- }
- CQueenPanel::~CQueenPanel()
- {
- if(queen!=NULL) delete(queen);
- }
- BEGIN_MESSAGE_MAP(CQueenPanel, CStatic)
- //{{AFX_MSG_MAP(CQueenPanel)
- ON_WM_PAINT()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CQueenPanel message handlers
- //画棋盘(黑白方格间隔)
- void CQueenPanel::DrawBoard(CDC *pDC, int size, int cell)
- {
- int i, j;
- CBrush w_brush, b_brush;
- b_brush.CreateSolidBrush(RGB(0, 0, 0));
- w_brush.CreateSolidBrush(RGB(255, 255, 255));
- for(i=0; i<size; i++)
- {
- for(j=0; j<size; j++)
- {
- if((i+j)%2 ==0 )
- pDC->FillRect(CRect(i*cell, j*cell, (i+1)*cell-1, (j+1)*cell-1), &b_brush);
- else
- pDC->FillRect(CRect(i*cell, j*cell, (i+1)*cell-1, (j+1)*cell-1), &w_brush);
- }
- }
- CPen b_pen(PS_SOLID, 1, RGB(0, 0, 0));
- int board = cell*size;
- pDC->SelectObject(b_pen);
- pDC->MoveTo(0, 0);
- pDC->LineTo(0, board-1);
- pDC->LineTo(board-1, board-1);
- pDC->LineTo(board-1, 0);
- pDC->LineTo(0, 0);
- }
- void CQueenPanel::OnPaint()
- {
- CPaintDC dc(this);
- if( N == 0 || queen == NULL )
- return;
- RECT rect;
- GetWindowRect(&rect);
- CDC MemDC; //定义一个显示设备对象
- CBitmap MemBitmap;//定义一个位图对象
- int w = rect.right - rect.left;
- int h = rect.bottom - rect.top;
- int board = w >= h ? h : w;
- board = board - board % N;
- int cell = board / N;
- int count = 0;
- int i;
- CPen b_pen(PS_SOLID, 1, RGB(0, 0, 255));
- CPen r_pen(PS_SOLID, 1, RGB(255, 0, 0));
- CBrush y_brush, w_brush;
- y_brush.CreateSolidBrush(RGB(255, 255, 0));
- w_brush.CreateSolidBrush(RGB(255, 255, 255));
- MemDC.CreateCompatibleDC(&dc); //建立兼容的内存显示设备
- MemBitmap.CreateCompatibleBitmap(&dc,w, h);//建立一个兼容的位图
- CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
- MemDC.FillRect(CRect(0, 0, w, h), &w_brush);
- if(cell<7)
- MemDC.TextOut(0, h/2, "太大,画不下了!");
- else
- {
- DrawBoard(&MemDC, N, cell);
- MemDC.SelectObject(b_pen);
- MemDC.SelectObject(y_brush);
- for( i = 0; i < N; i++)
- {
- if( queen[i] >= 0 )
- {
- MemDC.Ellipse(queen[i] * cell + cell / 6, i * cell + cell / 6,
- queen[i] * cell + cell / 6+cell * 2 / 3,
- i * cell + cell / 6+cell * 2 / 3);
- count++;
- }
- }
- }
- dc.BitBlt(0,0,w, h,&MemDC,0,0,SRCCOPY);
- }
- void CQueenPanel::SetSize(int size)
- {
- if(size>N)
- {
- if(queen!=NULL) delete(queen);
- queen = new int[size*sizeof(int)];
- for(int i=0;i<size;i++)
- queen[i] = -1;
- }
- N = size;
- RedrawWindow(); //重绘
- }
- void CQueenPanel::SetQueen(int *newq)
- {
- int i;
- for( i = 0; i < N; i++ )
- queen[i] = newq[i];
- RedrawWindow();
- }
- void CQueenPanel::SetQueen(int row, int col)
- {
- queen[row] = col;
- RedrawWindow();
- }