RaceDlg.cpp
资源名称:10-Race.rar [点击查看]
上传用户:bkierde
上传日期:2022-06-07
资源大小:1840k
文件大小:6k
源码类别:
其他游戏
开发平台:
Visual C++
- // RaceDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Race.h"
- #include "RaceDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CRaceDlg dialog
- CRaceDlg::CRaceDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CRaceDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CRaceDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_pctrls[0]=&m_p1;
- m_pctrls[1]=&m_p2;
- m_pctrls[2]=&m_p3;
- m_pctrls[3]=&m_p4;
- m_pctrls[4]=&m_p5;
- m_pctrls[5]=&m_p6;
- }
- void CRaceDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CRaceDlg)
- DDX_Control(pDX, IDC_PROGRESS6, m_p6);
- DDX_Control(pDX, IDC_PROGRESS5, m_p5);
- DDX_Control(pDX, IDC_PROGRESS4, m_p4);
- DDX_Control(pDX, IDC_PROGRESS3, m_p3);
- DDX_Control(pDX, IDC_PROGRESS2, m_p2);
- DDX_Control(pDX, IDC_PROGRESS1, m_p1);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CRaceDlg, CDialog)
- //{{AFX_MSG_MAP(CRaceDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(ID_START, OnStart)
- ON_BN_CLICKED(ID_EXIT, OnExitAll)
- //手动添加自定义消息响应
- //WM_USER+1000:运动员报告位置
- //WM_USER+1001:运动员到达终点
- ON_MESSAGE((WM_USER+1000),OnStep)
- ON_MESSAGE((WM_USER+1001),OnExit)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CRaceDlg message handlers
- BOOL CRaceDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- for (int i=0;i<6;i++)
- {
- m_pctrls[i]->SetRange(0,CRunner::lenght);
- }
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CRaceDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CRaceDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CRaceDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CRaceDlg::OnStart()
- {
- // 初始化起跑信号,初始化为未激活
- m_hTag=::CreateEvent(NULL,TRUE,FALSE,NULL);
- // 初始化比赛结束信号,初始化为未激活
- m_hExit=::CreateEvent(NULL,TRUE,FALSE,NULL);
- // 创建6个运动员线程,其运行后都开始进入预备状态,等待起跑命令
- memset(&m_stVar,0,sizeof(StThreadVar));
- m_stVar.hTag=m_hTag;
- m_stVar.hExit=m_hExit;
- m_stVar.pWnd=this;
- for (int i=0;i<6;i++)
- {
- m_stVar.index=i;
- m_runners[i].start(m_stVar);
- }
- //等待500毫秒,确保所有运动员都进入预备状态
- Sleep(500);
- //发出起跑命令
- SetEvent(m_hTag);
- //设置按钮响应状态
- GetDlgItem(ID_START)->EnableWindow(FALSE);
- GetDlgItem(ID_EXIT)->EnableWindow(TRUE);
- }
- void CRaceDlg::OnExitAll()
- {
- OnCancel();
- }
- LRESULT CRaceDlg::OnStep(WPARAM w,LPARAM l)
- {
- // 根据运动员报告的位置,图形化显示
- INT index=(INT)w;
- if(index<6 && index>=0)
- {
- INT step=(INT)l;
- m_pctrls[index]->SetPos(step);
- }
- return 0;
- }
- void CRaceDlg::OnCancel()
- {
- // 发出结束比赛命令
- SetEvent(m_hExit);
- CDialog::OnCancel();
- }
- LRESULT CRaceDlg::OnExit(WPARAM w,LPARAM l)
- {
- // 发出结束比赛命令
- SetEvent(m_hExit);
- return 0;
- }