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

其他智力游戏

开发平台:

Visual C++

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