按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
}
else if ( lpHorse…》color == BLUE )
{
hMutex = ::OpenMutex(MUTEX_ALL_ACCESS; FALSE; g_strBName);
}
·220 ·
…………………………………………………………Page 232……………………………………………………………
第 9 章 多线程
else
return 1; // 颜色不对
if( hMutex == NULL )
{
AfxMessageBox(〃open Mutex error。。。〃);
return 1;
}
// 循环,等待信号
while ( true )
{
if ( WaitForSingleObject(hMutex; INFINITE) == WAIT_FAILED )
{
continue;
}
// 马前进
if ( lpHorse…》x 《 lpHorse…》rect。right )
{
lpHorse…》x ++;
}
// 重绘窗口
InvalidateRect(lpHorse…》hWnd; lpHorse…》rect; TRUE);
// 释放 Mutex
ReleaseMutex(hMutex);
// 等待; 速度快的等待时间短
Sleep(lpHorse…》speed);
}
return 0;
}
(8)初始化对话框
改写对话框的 OnInitDialog,进行必要的初始化,代码如下:
BOOL CRacingDlg::OnInitDialog()
·221 ·
…………………………………………………………Page 233……………………………………………………………
Visual C++ 6。0 程序设计从入门到精通
{
……
// 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 章 多线程