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

📄 chapter7.htm

📁 为清华大学出版社 C++课后答案
💻 HTM
📖 第 1 页 / 共 5 页
字号:
  }<br>
  void CPuzzleWnd::OnUpdateGrad02(CCmdUI* pCmdUI) <br>
  {<br>
  pCmdUI-&gt;SetCheck(m_nColCount == 8);<br>
  }<br>
  void CPuzzleWnd::OnUpdateGrad03(CCmdUI* pCmdUI) <br>
  {<br>
  pCmdUI-&gt;SetCheck(m_nColCount == 16);<br>
  }<br>
  输入输出:在选择拼图难度时,可在相应选项前打钩(图13-4)。</p>
<p class=style3><br>
  // Example 13-7: 七巧板程序 //////////////////////////////////<br>
  #include &lt;afxwin.h&gt;<br>
  #include &lt;afxext.h&gt;<br>
  // 拼板类 ////////////////////////////////////////////////////<br>
  #define MAX_POINTS 4<br>
  #define CHIP_WIDTH 240<br>
  #define DELTA 30<br>
  class CChip : public CObject<br>
  {<br>
  DECLARE_SERIAL(CChip)<br>
  int m_nType;<br>
  CPoint m_pointList[MAX_POINTS];<br>
  int m_nPointCount;<br>
  public:<br>
  CChip(){}<br>
  void SetChip(int type, POINT *ppointlist, int count);<br>
  void DrawChip(CDC *pDC);<br>
  BOOL PtInChip(POINT point);<br>
  LPCRECT GetRect();<br>
  void MoveTo(CSize offset);<br>
  void Rotation();<br>
  void Serialize(CArchive &amp;ar);<br>
  };<br>
  IMPLEMENT_SERIAL(CChip, CObject, 1)<br>
  // 设置拼图块参数<br>
  void CChip::SetChip(int type, POINT *ppointlist, int count)<br>
  {<br>
  m_nType = type;<br>
  m_nPointCount = count;<br>
  for(int i=0; i&lt;count; i++)<br>
  m_pointList[i] = ppointlist[i];<br>
  }<br>
  // 绘出拼图块<br>
  void CChip::DrawChip(CDC *pDC)<br>
  {<br>
  CPen penNew, *ppenOld;<br>
  CBrush brushNew, *pbrushOld;<br>
  switch(m_nType)<br>
  {<br>
  case 1: <br>
  brushNew.CreateSolidBrush(RGB(127, 127, 127));<br>
  break;<br>
  case 2: <br>
  brushNew.CreateSolidBrush(RGB(255, 0, 0));<br>
  break;<br>
  case 3: <br>
  brushNew.CreateSolidBrush(RGB(0, 255, 0));<br>
  break;<br>
  case 4: <br>
  brushNew.CreateSolidBrush(RGB(0, 0, 255));<br>
  break;<br>
  case 5: <br>
  brushNew.CreateSolidBrush(RGB(127, 127, 0));<br>
  break;<br>
  case 6: <br>
  brushNew.CreateSolidBrush(RGB(127, 0, 127));<br>
  break;<br>
  case 7: <br>
  brushNew.CreateSolidBrush(RGB(0, 127, 127));<br>
  break;<br>
  }<br>
  penNew.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));<br>
  ppenOld = pDC-&gt;SelectObject(&amp;penNew);<br>
  pbrushOld = pDC-&gt;SelectObject(&amp;brushNew);<br>
  pDC-&gt;Polygon(m_pointList, m_nPointCount);<br>
  pDC-&gt;SelectObject(ppenOld);<br>
  pDC-&gt;SelectObject(pbrushOld);<br>
  }<br>
  // 检测一点是否在拼图块中<br>
  BOOL CChip::PtInChip(POINT point)<br>
  {<br>
  CRgn rgn;<br>
  rgn.CreatePolygonRgn(m_pointList, m_nPointCount, 0);<br>
  return rgn.PtInRegion(point);<br>
  }<br>
  // 取拼图块的包含矩形<br>
  LPCRECT CChip::GetRect()<br>
  {<br>
  static RECT rect;<br>
  CRgn rgn;<br>
  rgn.CreatePolygonRgn(m_pointList, m_nPointCount, 0);<br>
  rgn.GetRgnBox(&amp;rect);<br>
  rect.right++;<br>
  rect.bottom++;<br>
  return &amp;rect;<br>
  }<br>
  // 旋转拼图块<br>
  void CChip::Rotation()<br>
  {<br>
  CRect rect;<br>
  CRgn rgn;<br>
  rgn.CreatePolygonRgn(m_pointList, m_nPointCount, 0);<br>
  rgn.GetRgnBox(&amp;rect);<br>
  double x = rect.left+rect.Width()/2; // 计算旋转中心<br>
  double y = rect.top+rect.Height()/2;<br>
  double dx, dy;<br>
  for(int i=0; i&lt;m_nPointCount; i++) // 旋转各点<br>
  {<br>
  dx = m_pointList[i].x-x;<br>
  dy = m_pointList[i].y-y;<br>
  m_pointList[i].x = (int)(x+dx*0.7071-dy*0.7071);<br>
  m_pointList[i].y = (int)(y+dx*0.7071+dy*0.7071);<br>
  }<br>
  }<br>
  // 移动拼图块<br>
  void CChip::MoveTo(CSize offset)<br>
  {<br>
  for(int i=0; i&lt;m_nPointCount; i++)<br>
  m_pointList[i] = m_pointList[i]+offset;<br>
  }<br>
  // 序列化<br>
  void CChip::Serialize(CArchive &amp;ar)<br>
  {<br>
  if(ar.IsStoring())<br>
  {<br>
  ar &lt;&lt; m_nType;<br>
  ar &lt;&lt; m_nPointCount;<br>
  for(int i=0; i&lt;m_nPointCount; i++)<br>
  ar &lt;&lt; m_pointList[i];<br>
  }<br>
  else<br>
  {<br>
  ar &gt;&gt; m_nType;<br>
  ar &gt;&gt; m_nPointCount;<br>
  for(int i=0; i&lt;m_nPointCount; i++)<br>
  ar &gt;&gt; m_pointList[i];<br>
  }<br>
  }<br>
  // 文档类 ////////////////////////////////////////////////////<br>
  #define CHIP_COUNT 7<br>
  class CMyDoc : public CDocument<br>
  {<br>
  DECLARE_DYNCREATE(CMyDoc)<br>
  CChip m_chipList[CHIP_COUNT];<br>
  public:<br>
  void Reset();<br>
  virtual void DeleteContents();<br>
  virtual void Serialize(CArchive&amp; ar);<br>
  };<br>
  IMPLEMENT_DYNCREATE(CMyDoc, CDocument)<br>
  // 初始化拼图块<br>
  void CMyDoc::Reset()<br>
  {<br>
  POINT pointList[MAX_POINTS];<br>
  pointList[0].x = DELTA;<br>
  pointList[0].y = DELTA;<br>
  pointList[1].x = DELTA+CHIP_WIDTH;<br>
  pointList[1].y = DELTA;<br>
  pointList[2].x = DELTA+CHIP_WIDTH/2;<br>
  pointList[2].y = DELTA+CHIP_WIDTH/2;<br>
  m_chipList[0].SetChip(1, pointList, 3);</p>
<p class=style3>pointList[0].x = DELTA;<br>
  pointList[0].y = DELTA;<br>
  pointList[1].x = DELTA;<br>
  pointList[1].y = DELTA+CHIP_WIDTH;<br>
  pointList[2].x = DELTA+CHIP_WIDTH/2;<br>
  pointList[2].y = DELTA+CHIP_WIDTH/2;<br>
  m_chipList[1].SetChip(2, pointList, 3);</p>
<p class=style3>pointList[0].x = DELTA+CHIP_WIDTH;<br>
  pointList[0].y = DELTA;<br>
  pointList[1].x = DELTA+CHIP_WIDTH;<br>
  pointList[1].y = DELTA+CHIP_WIDTH/2;<br>
  pointList[2].x = DELTA+(CHIP_WIDTH*3)/4;<br>
  pointList[2].y = DELTA+CHIP_WIDTH/4;<br>
  m_chipList[2].SetChip(3, pointList, 3);</p>
<p class=style3>pointList[0].x = DELTA+CHIP_WIDTH/2;<br>
  pointList[0].y = DELTA+CHIP_WIDTH/2;<br>
  pointList[1].x = DELTA+CHIP_WIDTH/4;<br>
  pointList[1].y = DELTA+(CHIP_WIDTH*3)/4;<br>
  pointList[2].x = DELTA+(CHIP_WIDTH*3)/4;<br>
  pointList[2].y = DELTA+(CHIP_WIDTH*3)/4;<br>
  m_chipList[3].SetChip(4, pointList, 3);</p>
<p class=style3>pointList[0].x = DELTA+CHIP_WIDTH;<br>
  pointList[0].y = DELTA+CHIP_WIDTH/2;<br>
  pointList[1].x = DELTA+CHIP_WIDTH;<br>
  pointList[1].y = DELTA+CHIP_WIDTH;<br>
  pointList[2].x = DELTA+CHIP_WIDTH/2;<br>
  pointList[2].y = DELTA+CHIP_WIDTH;<br>
  m_chipList[4].SetChip(5, pointList, 3);</p>
<p class=style3>pointList[0].x = DELTA+(CHIP_WIDTH*3)/4;<br>
  pointList[0].y = DELTA+CHIP_WIDTH/4;<br>
  pointList[1].x = DELTA+CHIP_WIDTH/2;<br>
  pointList[1].y = DELTA+CHIP_WIDTH/2;<br>
  pointList[2].x = DELTA+(CHIP_WIDTH*3)/4;<br>
  pointList[2].y = DELTA+(CHIP_WIDTH*3)/4;<br>
  pointList[3].x = DELTA+CHIP_WIDTH;<br>
  pointList[3].y = DELTA+CHIP_WIDTH/2;<br>
  m_chipList[5].SetChip(6, pointList, 4);</p>
<p class=style3>pointList[0].x = DELTA;<br>
  pointList[0].y = DELTA+CHIP_WIDTH;<br>
  pointList[1].x = DELTA+CHIP_WIDTH/4;<br>
  pointList[1].y = DELTA+(CHIP_WIDTH*3)/4;<br>
  pointList[2].x = DELTA+(CHIP_WIDTH*3)/4;<br>
  pointList[2].y = DELTA+(CHIP_WIDTH*3)/4;<br>
  pointLis ¨t[3].x = DELTA+CHIP_WIDTH/2; pointList[3].y = DELTA+CHIP_WIDTH; m_chipList[6].SetChip(7, pointList, 4); // 清理文档:关闭文档、建立新文档和打开文档前调用<br>
  void CMyDoc::DeleteContents() <br>
  {<br>
  Reset();<br>
  CDocument::DeleteContents();<br>
  }<br>
  // 系列化:读写文档时自动调用<br>
  void CMyDoc::Serialize(CArchive &amp;ar)<br>
  {<br>
  for(int i=0; i&lt;CHIP_COUNT; i++)<br>
  m_chipList[i].Serialize(ar);<br>
  }</p>
<p class=style3>// 视图类 ///////////////////////////////////////////////////<br>
  class CMyView : public CView<br>
  {<br>
  DECLARE_DYNCREATE(CMyView)<br>
  BOOL m_bCaptured;<br>
  CPoint m_pointMouse;<br>
  int m_nCurrIndex;<br>
  public:<br>
  CMyView(){m_bCaptured = FALSE;}<br>
  CMyDoc* GetDocument(){return (CMyDoc*)m_pDocument;}<br>
  virtual void OnInitialUpdate();<br>
  virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);<br>
  virtual void OnDraw(CDC* pDC);<br>
  afx_msg void OnLButtonDown(UINT nFlags, CPoint point);<br>
  afx_msg void OnLButtonUp(UINT nFlags, CPoint point);<br>
  afx_msg void OnMouseMove(UINT nFlags, CPoint point);<br>
  afx_msg void OnRButtonDown(UINT nFlags, CPoint point);<br>
  DECLARE_MESSAGE_MAP()<br>
  };<br>
  IMPLEMENT_DYNCREATE(CMyView, CView)<br>
  BEGIN_MESSAGE_MAP(CMyView, CView)<br>
  ON_WM_LBUTTONDOWN()<br>
  ON_WM_LBUTTONUP()<br>
  ON_WM_MOUSEMOVE()<br>
  ON_WM_RBUTTONDOWN()<br>
  ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)<br>
  ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)<br>
  ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)<br>
  END_MESSAGE_MAP()<br>
  // 更新初始化:当建立新文档或打开文档时调用<br>
  void CMyView::OnInitialUpdate()<br>
  {<br>
  CView::OnInitialUpdate();<br>
  Invalidate();<br>
  }<br>
  // 绘制视图:程序开始运行或窗体发生变化时自动调用<br>
  void CMyView::OnDraw(CDC* pDC)<br>
  {<br>
  CMyDoc* pDoc = GetDocument();<br>
  ASSERT_VALID(pDoc);<br>
  for(int i=0; i&lt;CHIP_COUNT; i++)<br>
  pDoc-&gt;m_chipList[i].DrawChip(pDC);<br>
  }<br>
  // 消息响应:用户点击鼠标左键时调用<br>
  void CMyView::OnLButtonDown(UINT nFlags, CPoint point)<br>
  {<br>
  CMyDoc* pDoc = GetDocument();<br>
  ASSERT_VALID(pDoc);<br>
  for(int i=CHIP_COUNT-1; i&gt;=0; i--)<br>
  if(pDoc-&gt;m_chipList[i].PtInChip(point))<br>
  {<br>
  SetCapture();<br>
  m_bCaptured = TRUE;<br>
  m_pointMouse = point;<br>
  m_nCurrIndex = i;<br>
  break;<br>
  }<br>
  }<br>
  // 释放鼠标左键<br>
  void CMyView::OnLButtonUp(UINT nFlags, CPoint point)<br>
  {<br>
  if(m_bCaptured)<br>
  {<br>
  ::ReleaseCapture();<br>
  m_bCaptured = FALSE;<br>
  }<br>
  }</p>
<p class=style3>// 移动鼠标左键<br>
  void CMyView::OnMouseMove(UINT nFlags, CPoint point)<br>
  {<br>
  if(m_bCaptured)<br>
  {<br>
  CMyDoc* pDoc = GetDocument();<br>
  ASSERT_VALID(pDoc);<br>
  InvalidateRect(pDoc-&gt;m_chipList[m_nCurrIndex].GetRect());<br>
  CSize offset(point-m_pointMouse);<br>
  pDoc-&gt;m_chipList[m_nCurrIndex].MoveTo(offset);<br>
  InvalidateRect(pDoc-&gt;m_chipList[m_nCurrIndex].GetRect());<br>
  m_pointMouse = point;<br>
  pDoc-&gt;SetModifiedFlag();<br>
  }<br>
  }<br>
  // 按下鼠标右键: 旋转拼图块<br>
  void CMyView::OnRButtonDown(UINT nFlags, CPoint point)<br>
  {<br>
  CMyDoc* pDoc = GetDocument();<br>
  ASSERT_VALID(pDoc);<br>
  for(int i=CHIP_COUNT-1; i&gt;=0; i--)<br>
  if(pDoc-&gt;m_chipList[i].PtInChip(point))<br>
  {<br>
  InvalidateRect(pDoc-&gt;m_chipList[i].GetRect());<br>
  pDoc-&gt;m_chipList[i].Rotation();<br>
  InvalidateRect(pDoc-&gt;m_chipList[i].GetRect(), FALSE);<br>
  pDoc-&gt;SetModifiedFlag();<br>
  break;<br>
  }<br>
  }<br>
  // 准备打印:设置打印参数<br>
  BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)<br>
  {<br>
  pInfo-&gt;SetMaxPage(1);<br>
  return DoPreparePrinting(pInfo);<br>
  }<br>
  // 主框架类 //////////////////////////////////////////////////<br>
  class CMainFrame : public CFrameWnd<br>
  {<br>
  DECLARE_DYNCREATE(CMainFrame)<br>
  };<br>
  IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)<br>
  // 应用程序类 ///////////////////////////////////////////////<br>
  #define IDR_MAINFRAME 128 // 主框架的资源代号<br>
  class CMyApp : public CWinApp<br>
  {<br>
  public:<br>
  virtual BOOL InitInstance();<br>
  DECLARE_MESSAGE_MAP()<br>
  };<br>
  BEGIN_MESSAGE_MAP(CMyApp, CWinApp)<br>
  ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)<br>
  ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)<br>
  ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)<br>
  END_MESSAGE_MAP()<br>
  // 初始化程序实例:建立并登记文档模板<br>
  BOOL CMyApp::InitInstance()<br>
  {<br>
  CSingleDocTemplate* pDocTemplate;<br>
  pDocTemplate = new CSingleDocTemplate(<br>
  IDR_MAINFRAME,<br>
  RUNTIME_CLASS(CMyDoc),<br>
  RUNTIME_CLASS(CMainFrame),<br>
  RUNTIME_CLASS(CMyView));<br>
  AddDocTemplate(pDocTemplate);<br>
  CCommandLineInfo cmdInfo;<br>
  ParseCommandLine(cmdInfo);<br>
  if (!ProcessShellCommand(cmdInfo))<br>
  return FALSE;<br>
  m_pMainWnd-&gt;ShowWindow(SW_SHOWMAXIMIZED);<br>
  return TRUE;<br>
  }<br>
  // 全局应用程序对象<br>
  CMyApp theApp;</p>
<p class="style3"></p>
<p class="style3"><strong>7-23 为例9-3的吹泡泡程序添加一标识符为IDI_MAINICON的图标(该图标应已按11.8:“向项目中添加资源”中的方法建立并加入项目)。<br>
  说 明:建立项目的方法见9.8:“用Visual C++集成开发环境开发Win32应用程序”。</strong></span><br>
</p>
<p class="style3">程 序:</span></p>
<p class="style3">在例9-3程序前面添加一文件包含命令:<br>
  #include ”resource.h”<br>
  并将CMyApp::InitInstance()函数修改为:<br>
  BOOL CMyApp::InitInstance()<br>
  {<br>
  HICON hIcon;<br>
  hIcon = LoadIcon(IDI_MAINICON); // 载入图标<br>
  CMyWnd *pFrame = new CMyWnd;<br>
  pFrame-&gt;Create(0,_T("吹泡泡程序"));<br>

⌨️ 快捷键说明

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