QueenPanel.cpp
上传用户:hncsjd
上传日期:2022-07-08
资源大小:3772k
文件大小:4k
源码类别:

其他智力游戏

开发平台:

Visual C++

  1. // QueenPanel.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "NQueen.h"
  5. #include "QueenPanel.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CQueenPanel
  13. CQueenPanel::CQueenPanel()
  14. {
  15. this->queen = NULL;
  16. N = 0;
  17. queen_mutex = CreateMutex(NULL,FALSE,NULL);
  18. }
  19. CQueenPanel::CQueenPanel(int size)
  20. {
  21. this->queen = NULL;
  22. N = 0;
  23. SetSize(size);
  24. queen_mutex = CreateMutex(NULL,FALSE,NULL);
  25. }
  26. CQueenPanel::~CQueenPanel()
  27. {
  28. if(queen!=NULL)  delete(queen);
  29. }
  30. BEGIN_MESSAGE_MAP(CQueenPanel, CStatic)
  31. //{{AFX_MSG_MAP(CQueenPanel)
  32. ON_WM_PAINT()
  33. //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CQueenPanel message handlers
  37. //画棋盘(黑白方格间隔)
  38. void CQueenPanel::DrawBoard(CDC *pDC, int size, int cell)
  39. {
  40. int i, j;
  41. CBrush w_brush, b_brush;
  42. b_brush.CreateSolidBrush(RGB(0, 0, 0));
  43. w_brush.CreateSolidBrush(RGB(255, 255, 255));
  44. for(i=0; i<size; i++)
  45. {
  46. for(j=0; j<size; j++)
  47. {
  48. if((i+j)%2 ==0 )
  49. pDC->FillRect(CRect(i*cell, j*cell, (i+1)*cell-1, (j+1)*cell-1), &b_brush);
  50. else
  51. pDC->FillRect(CRect(i*cell, j*cell, (i+1)*cell-1, (j+1)*cell-1), &w_brush);
  52. }
  53. }
  54. CPen b_pen(PS_SOLID, 1, RGB(0, 0, 0));
  55. int board = cell*size;
  56. pDC->SelectObject(b_pen);  
  57. pDC->MoveTo(0, 0);
  58. pDC->LineTo(0, board-1);
  59. pDC->LineTo(board-1, board-1);
  60. pDC->LineTo(board-1, 0);
  61. pDC->LineTo(0, 0);
  62. }
  63. void CQueenPanel::OnPaint() 
  64. {
  65. CPaintDC dc(this);
  66. if( N == 0 || queen == NULL )
  67. return;
  68. RECT rect;
  69. GetWindowRect(&rect);
  70. CDC MemDC; //定义一个显示设备对象
  71. CBitmap MemBitmap;//定义一个位图对象
  72. WaitForSingleObject(queen_mutex, INFINITE);
  73.     int w = rect.right - rect.left;
  74.     int h = rect.bottom - rect.top;
  75.     int board = w >= h ? h : w;
  76.     board = board - board % N;
  77.     int cell = board / N;
  78. int count = 0;
  79. int i;
  80. CPen b_pen(PS_SOLID, 1, RGB(0, 0, 255));
  81. CPen r_pen(PS_SOLID, 1, RGB(255, 0, 0));
  82. CBrush y_brush, w_brush;
  83. y_brush.CreateSolidBrush(RGB(255, 255, 0));
  84. w_brush.CreateSolidBrush(RGB(255, 255, 255));
  85. MemDC.CreateCompatibleDC(&dc); //建立兼容的内存显示设备
  86. MemBitmap.CreateCompatibleBitmap(&dc,w, h);//建立一个兼容的位图
  87. CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
  88. MemDC.FillRect(CRect(0, 0, w, h), &w_brush);
  89. if(cell<7)
  90. MemDC.TextOut(0, h/2, "太大,画不下了!");
  91. else
  92. {
  93. DrawBoard(&MemDC, N, cell);
  94. MemDC.SelectObject(b_pen);
  95. MemDC.SelectObject(y_brush);     
  96. for( i = 0; i < N; i++)
  97. {
  98. if( queen[i] >= 0 )
  99. {
  100. MemDC.Ellipse(queen[i] * cell + cell / 6, i * cell + cell / 6,
  101. queen[i] * cell + cell / 6+cell * 2 / 3,
  102. i * cell + cell / 6+cell * 2 / 3);
  103. count++;
  104. }
  105. }
  106. }
  107. ReleaseMutex(queen_mutex);
  108. dc.BitBlt(0,0,w, h,&MemDC,0,0,SRCCOPY);
  109. }
  110. void CQueenPanel::SetSize(int size)
  111. {
  112. WaitForSingleObject(queen_mutex,INFINITE);
  113. if(size>N)
  114. {
  115. if(queen!=NULL)  delete(queen);
  116. queen = new int[size*sizeof(int)];
  117. for(int i=0;i<size;i++)
  118. queen[i] = -1;
  119. }
  120. N = size;
  121. ReleaseMutex(queen_mutex);
  122.     RedrawWindow(); //重绘
  123. }
  124. void CQueenPanel::SetQueen(int *newq)
  125. {
  126. WaitForSingleObject(queen_mutex,INFINITE);
  127. int i;
  128. for( i = 0; i < N; i++ )
  129. queen[i] = newq[i];
  130. ReleaseMutex(queen_mutex);
  131.     RedrawWindow();      
  132. }
  133. void CQueenPanel::SetQueen(int row, int col)
  134. {
  135. WaitForSingleObject(queen_mutex,INFINITE);
  136. queen[row] = col;
  137. ReleaseMutex(queen_mutex);
  138.     RedrawWindow();     
  139. }