QueenPanel.cpp
资源名称:NQueen.rar [点击查看]
上传用户:hncsjd
上传日期:2022-07-08
资源大小:3772k
文件大小:4k
源码类别:
其他智力游戏
开发平台:
Visual C++
- // QueenPanel.cpp : implementation file
- //
- #include "stdafx.h"
- #include "EightQueen.h"
- #include "QueenPanel.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // QueenPanel
- QueenPanel::QueenPanel()
- {
- this->queen = NULL;
- n = 0;
- queen_mutex = CreateMutex(NULL,FALSE,NULL);
- }
- QueenPanel::QueenPanel(int size)
- {
- this->queen = NULL;
- n = 0;
- SetSize(size);
- queen_mutex = CreateMutex(NULL,FALSE,NULL);
- }
- QueenPanel::~QueenPanel()
- {
- if(queen!=NULL) free(queen);
- }
- BEGIN_MESSAGE_MAP(QueenPanel, CWnd)
- //{{AFX_MSG_MAP(QueenPanel)
- ON_WM_PAINT()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // QueenPanel message handlers
- void QueenPanel::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- if( n == 0 || queen == NULL )
- return;
- RECT rect;
- GetWindowRect(&rect);
- CDC MemDC; //定义一个显示设备对象
- CBitmap MemBitmap;//定义一个位图对象
- WaitForSingleObject(queen_mutex, INFINITE);
- 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;
- //System.out.println("w:" + w + " h:" + h + " x: " + x + " y:" + y + " cell:" +
- // cell + " board:" + board + " rl:" + rl);
- 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, b_brush;
- y_brush.CreateSolidBrush(RGB(255, 255, 0));
- b_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), &b_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++;
- }
- }
- }
- ReleaseMutex(queen_mutex);
- dc.BitBlt(0,0,w, h,&MemDC,0,0,SRCCOPY);
- }
- void QueenPanel::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 QueenPanel::SetSize(int size)
- {
- WaitForSingleObject(queen_mutex,INFINITE);
- if(size>n)
- {
- if(queen!=NULL) free(queen);
- queen = (int *)malloc(size*sizeof(int));
- for(int i=0;i<size;i++)
- queen[i] = -1;
- }
- n = size;
- ReleaseMutex(queen_mutex);
- RedrawWindow();
- }
- void QueenPanel::SetQueen(int *newq)
- {
- WaitForSingleObject(queen_mutex,INFINITE);
- int i;
- for( i = 0; i < n; i++ )
- queen[i] = newq[i];
- ReleaseMutex(queen_mutex);
- RedrawWindow();
- }
- void QueenPanel::SetQueen(int row, int col)
- {
- WaitForSingleObject(queen_mutex,INFINITE);
- queen[row] = col;
- ReleaseMutex(queen_mutex);
- RedrawWindow();
- }