友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!
第三电子书 返回本书目录 加入书签 我的书架 我的书签 TXT全本下载 『收藏到我的浏览器』

VC语言6.0程序设计从入门到精通-第47部分

快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部! 如果本书没有阅读完,想下次继续接着阅读,可使用上方 "收藏到我的浏览器" 功能 和 "加入书签" 功能!

             ……     



              // TODO: Add extra initialization here  



         



              //  初始化 Mutex  



              InitMutexes();  



         



              //  初始化对话框  



              InitDlgItem();  



             ……  



       }  



       (9 )绘制场景  

       为 CRacingDlg 添加成员函数 DrawScene ,用来绘制赛马的场景,代码如下:  



       //  绘制场景的函数  



       void CRacingDlg::DrawScene(CPaintDC * pDC)  



       {  



              CRect         rect;  



              CBrush        redBrush(RGB(255; 0; 0));  



              CBrush        greenBrush(RGB(0; 255; 0));  



              CBrush        blueBrush(RGB(0; 0; 255));  



              CBrush* oldBrush;  



         



              m_RacingBox。GetClientRect(rect);  



              m_RacingBox。ClientToScreen(rect);  



         



              pDC…》Rectangle(rect);  



         



              //  绘制红马  



              if( m_hRed )  



              {  



                     pDC…》Rectangle(m_RedHorse。rect);  



                     oldBrush = pDC…》SelectObject(&redBrush);  



                     pDC…》Ellipse(m_RedHorse。x…m_RedHorse。size;                                  m_RedHorse。y…m_RedHorse。size;  



m_RedHorse。x+m_RedHorse。size; m_RedHorse。y+m_RedHorse。size);  



                     pDC…》SelectObject(oldBrush);  



              }  



                



              //  绘制绿马  



              if( m_hGreen )  



 ·222 ·  


…………………………………………………………Page 234……………………………………………………………

                                                                                                                     第 9 章    多线程  



              {  



                     pDC…》Rectangle(m_GreenHorse。rect);  



                     oldBrush = pDC…》SelectObject(&greenBrush);                              



                     pDC…》Ellipse(m_GreenHorse。x…m_GreenHorse。size;                         m_GreenHorse。y…m_GreenHorse。size;  



m_GreenHorse。x+m_GreenHorse。size; m_GreenHorse。y+m_GreenHorse。size);  



                     pDC…》SelectObject(oldBrush);  



              }  



         



              //  绘制蓝马  



              if( m_hBlue )  



              {  



                     pDC…》Rectangle(m_BlueHorse。rect);  



                     oldBrush = pDC…》SelectObject(&blueBrush);                        



                     pDC…》Ellipse(m_BlueHorse。x…m_BlueHorse。size;                               m_BlueHorse。y…m_BlueHorse。size;  



m_BlueHorse。x+m_BlueHorse。size; m_BlueHorse。y+m_BlueHorse。size);  



                     pDC…》SelectObject(oldBrush);  



              }  



       }  



       改写 CRacingDlg 的 OnPaint 函数,调用 DrawScene ,代码如下:  



       void CRacingDlg::OnPaint()    



       {  



              if (IsIconic())  



              {  



                     ……  



              }  



              else  



              {  



                     CPaintDC dc(this); // device context for painting  



                     CDialog::OnPaint();  



         



                     //  绘制场景  



                     DrawScene(&dc);  



              }  



       }  



       (10)响应“开始”按钮  

       响应“开始”按钮,用来启动线程,代码如下:  



       void CRacingDlg::OnStart()    



       {  



              // TODO: Add your control notification handler code here  



                                                                                                                                 ·223 ·  


…………………………………………………………Page 235……………………………………………………………

Visual C++ 6。0 程序设计从入门到精通  



               DWORD tID;  



               DWORD code;  



         



               CRect clientrect;  



               CRect racingrect;  



         



               GetClientRect(clientrect);  



               ClientToScreen(clientrect);  



         



               m_RacingBox。GetClientRect(racingrect);  



               m_RacingBox。ClientToScreen(racingrect);  



         



               racingrect …= clientrect。TopLeft();  



         



               if( !GetExitCodeThread(m_hRed; &code) ||    



                       code != STILL_ACTIVE )  



               {  



                       //  填写红马信息  



                       m_RedHorse。hWnd = this…》GetSafeHwnd();  



                       m_RedHorse。color = RED;  



                       m_RedHorse。speed = m_RSpeed。GetPos();  



                       m_RedHorse。size = 10;  



                       m_RedHorse。rect。TopLeft() = racingrect。TopLeft();  



                       m_RedHorse。rect。right = m_RedHorse。rect。TopLeft()。x + racingrect。Width();  



                       m_RedHorse。rect。bottom = m_RedHorse。rect。TopLeft()。y + racingrect。Height()/3;  



                       m_RedHorse。x = m_RedHorse。rect。left + m_RedHorse。size;  



                       m_RedHorse。y = m_RedHorse。rect。top + m_RedHorse。rect。Height()/2;  



         



                       //  创建红马线程  



                       m_hRed = CreateThread(NULL; 0; ThreadProc; &m_RedHorse; 0; &tID);  



         



                       if(m_hRed == NULL)  



                       {  



                              AfxMessageBox(〃创建线程失败〃);  



                       }  



               }  



         



               if( !GetExitCodeThread(m_hGreen; &code) ||    



                       code != STILL_ACTIVE )  



               {  



 ·224 ·  


…………………………………………………………Page 236……………………………………………………………

                                                                                                         第 9 章    多线程  



       //  填写绿马信息  



       m_GreenHorse。hWnd = this…》GetSafeHwnd();  



       m_GreenHorse。color = GREEN;  



       m_GreenHorse。speed = m_GSpeed。GetPos();  



       m_GreenHorse。size = 10;  



       m_GreenHorse。rect。TopLeft() = racingrect。TopLeft()+CPoint(0; racingrect。Height()/3);  



       m_GreenHorse。rect。right = m_GreenHorse。rect。TopLeft()。x + racingrect。Width();  



       m_GreenHorse。rect。bottom = m_GreenHorse。rect。TopLeft()。y + racingrect。Height()/3;  



       m_GreenHorse。x = m_GreenHorse。rect。left + m_GreenHorse。size;  



       m_GreenHorse。y = m_GreenHorse。rect。top + m_GreenHorse。rect。Height()/2;  



         



       //  创建绿马线程  



       m_hGreen = CreateThread(NULL; 0; ThreadProc; &m_GreenHorse; 0; &tID);  



  



       if(m_hGreen == NULL)  



       {  



              AfxMessageBox(〃创建线程失败〃);  



       }  



  }  



  



  if( !GetExitCodeThread(m_hBlue; &code) ||    



       code != STILL_ACTIVE )  



  {  



       //  填写蓝马信息  



       m_BlueHorse。hWnd = this…》GetSafeHwnd();  



       m_BlueHorse。color = BLUE;  



       m_BlueHorse。speed = m_BSpeed。GetPos();  



       m_BlueHorse。size = 10;  



       m_BlueHorse。rect。TopLeft() = racingrect。TopLeft()+CPoint(0; racingrect。Height()*2/3);  



       m_BlueHorse。rect。right = m_BlueHorse。rect。TopLeft()。x + racingrect。Width();  



       m_BlueHorse。rect。bottom = m_BlueHorse。rect。TopLeft()。y + racingrect。Height()/3;  



       m_BlueHorse。x = m_BlueHorse。rect。left + m_BlueHorse。size;  



       m_BlueHorse。y = m_BlueHorse。rect。top + m_BlueHorse。rect。Height()/2;  



  



       //  创建蓝马线程  



       m_hBlue = CreateThread(NULL; 0; ThreadProc; &m_BlueHorse; 0; &tID);  



  



       if(m_hBlue == NULL)  



       {  



              AfxMessageBox(〃创建线程失败〃);  



                                                                                                                     ·225 ·  


…………………………………………………………Page 237……………………………………………………………

Visual C++ 6。0 程序设计从入门到精通  



                      }  



               }  



         



               //  设置按钮状态  



               CButton* pBtn;  



         



               pBtn = (CButton*)GetDlgItem(IDSTART);  



               pBtn…》EnableWindow(FALSE);  



         



               pBtn = (CButton*)GetDlgItem(IDPAUSE);  



               pBtn…》EnableWindow(TRUE);  



         



               pBtn = (CButton*)GetDlgItem(IDSTOP);  



               pBtn…》EnableWindow(TRUE);  



         



        }  



        (11)响应“暂停”按钮  

        响应“暂停”按钮,用来暂停和继续线程,代码如下:  



       void CRacingDlg::OnPause()    



        {  



               //  得到 button 的指针  



               CButton* pBtn = (CButton*)GetDlgItem(IDPAUSE);  



         



               //  改变按钮的名称  



               if( !m_bPaused )  



               {  



                      SuspendThread(m_hRed);  



                      SuspendThread(m_hGreen);  



                      SuspendThread(m_hBlue);  



                        



                      pBtn…》SetWindowText(〃继续〃);  



               }  



         



               else  



               {  



                      ResumeThread(m_hRed);  



                      ResumeThread(m_hGreen);  



                      ResumeThread(m_hBlue);  



                        



 ·226 ·  


…………………………………………………………Page 238……………………………………………………………

                                                                                                                 第 9 章    多线程  



              pBtn…》SetWindowText(〃暂停〃);  



       }  



  



       //  改变按钮的状态  



       m_bPaused = !m_bPaused;  



}  



(12)响应“停止”按钮  

响应“停止”按钮,用来停止线程,代码如下:  



void CRacingDlg::OnStop()    



{  



       // TODO: Add your control notification handler code here  



  



       //  存放线程的退出码  



       DWORD code;                    



  



       if (!GetExitCodeThread(m_hRed; &code) ||  



              code == STILL_ACTIVE)  



       {  



              TerminateThread(m_hRed; code);  



              CloseHandle(m_hRed);  



       }  



       if (!GetExitCodeThread(m_hGreen; &code) ||  



              code == STILL_ACTIVE)  



       {  



              TerminateThread(m_hGreen; code);  



              CloseHandle(m_hGreen);  



       }  



  



       if (!GetExitCodeThread(m_hBlue; &code) ||  



              code == STILL_ACTIVE)  



       {  



              TerminateThread(m_
返回目录 上一页 下一页 回到顶部 1 2
快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!