Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Okno.cpp : implementation file
- //
- #include "stdafx.h"
- #include "lab3.h"
- #include "Okno.h"
- #include ".\okno.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- //TODO: Deklaracje zmiennych globalnych
- int przerwij;
- CString wynik;
- int jestWynik;
- int czaspracy[5];
- int liczbawatkow;
- CCriticalSection syn;
- CEvent syn2;
- #define WM_WIADOMOSC (WM_USER+100)
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- // Implementation
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- END_MESSAGE_MAP()
- // COkno dialog
- COkno::COkno(CWnd* pParent /*=NULL*/)
- : CDialog(COkno::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void COkno::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_EKRAN, m_ekran);
- DDX_Control(pDX, IDC_W1, m_w1);
- DDX_Control(pDX, IDC_W2, m_w2);
- DDX_Control(pDX, IDC_W3, m_w3);
- DDX_Control(pDX, IDC_W4, m_w4);
- DDX_Control(pDX, IDC_W5, m_w5);
- DDX_Control(pDX, IDC_URUCHOM, m_uruchom);
- DDX_Control(pDX, IDC_PRZERWIJ, m_przerwij);
- DDX_Control(pDX, IDC_KONIEC, m_koniec);
- }
- BEGIN_MESSAGE_MAP(COkno, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_MESSAGE(WM_WIADOMOSC, OnWiadomosc)
- ON_BN_CLICKED(IDC_URUCHOM, OnBnClickedUruchom)
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_PRZERWIJ, OnBnClickedPrzerwij)
- ON_BN_CLICKED(IDC_KONIEC, OnBnClickedKoniec)
- ON_WM_DESTROY()
- ON_EN_CHANGE(IDC_EKRAN, OnEnChangeEkran)
- END_MESSAGE_MAP()
- // COkno message handlers
- BOOL COkno::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
- // Add extra initialization here
- srand((unsigned )time(0));
- SetTimer(1,4000,0);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void COkno::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 COkno::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR COkno::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- UINT COkno::WatekRoboczy(void * pParametr)
- {
- int i=(int)pParametr;
- liczbawatkow++;
- do
- {
- if(przerwij==1)
- {
- break;
- }
- else
- {
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,1);
- }
- if(czaspracy[i]>0){
- Sleep(50);
- czaspracy[i]--;
- }
- else
- {
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,2);
- syn.Lock();
- wynik.Format("Wynik: %d\r\n",i+1);
- jestWynik=1;
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,3);
- CSingleLock sl(&syn2);
- sl.Lock();
- syn2.Unlock();
- break;
- }
- }
- while(1);
- liczbawatkow--;
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,4);
- jestWynik=1;
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,2);
- //syn.Lock();
- wynik.Format("Wynik: %d\r\n",i+1);
- jestWynik=1;
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,1);
- //syn.Lock();
- wynik.Format("Wynik: %d\r\n",i+1);
- jestWynik=1;
- AfxGetMainWnd()->PostMessage(WM_WIADOMOSC,i,3);
- //syn.Lock();
- wynik.Format("Wynik: %d\r\n",i+1);
- jestWynik=1;
- return 0;
- }
- void COkno::OnBnClickedUruchom()
- {
- m_uruchom.EnableWindow(FALSE);
- m_przerwij.EnableWindow();
- przerwij=0;
- for (int i=0;i<5;i++)
- {
- czaspracy[i]=10+(30*rand())/RAND_MAX;
- AfxBeginThread(WatekRoboczy,(void *)i);
- }
- }
- afx_msg LRESULT COkno::OnWiadomosc(WPARAM n, LPARAM m)
- {
- static char* w[4] =
- {"Oblicza...", "Czeka na ekran...", "Wyswietla wynik...", "Zakonczyl prace"};
- CString tmp;
- tmp = w[m-1];
- if((m-1)==0)
- {
- tmp.AppendFormat("Pozostalyczas: %d",czaspracy[n]);
- }
- switch (n) {
- case 0: m_w1.SetWindowText(tmp);
- break;
- case 1: m_w2.SetWindowText(tmp);
- break;
- case 2: m_w3.SetWindowText(tmp);
- break;
- case 3: m_w4.SetWindowText(tmp);
- break;
- case 4: m_w5.SetWindowText(tmp);
- break;
- }
- return 0;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void COkno::OnTimer(UINT nIDEvent)
- {
- CString s;
- char buffer[1024];
- CDialog::OnTimer(nIDEvent);
- if(!jestWynik)
- {
- return;
- }
- else
- {
- CString t;
- m_ekran.GetWindowText(s);
- /*TODO: dodanie zmiennej z wynikiem do s */;
- s=s+wynik;
- m_ekran.SetWindowText(s);
- /*TODO: Ustawienie stanu odpowiedniego obiektu synchronizacji*/
- wynik="";
- if(wynik.GetLength()==0)
- {
- syn2.Unlock();
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- void COkno::OnBnClickedPrzerwij()
- {
- /*TODO: Ustawienie zmiennej informujacej o wcisnieciu "Przerwij"*/
- przerwij=1;
- m_uruchom.EnableWindow();
- m_przerwij.EnableWindow(FALSE);
- OnTimer(1);
- do
- {
- OnTimer(1);
- }while(liczbawatkow>1);
- }
- void COkno::OnBnClickedKoniec()
- {
- EndDialog(1);
- }
- void COkno::OnDestroy()
- {
- KillTimer(1);
- OnBnClickedPrzerwij();
- CDialog::OnDestroy();
- }
- void COkno::OnEnChangeEkran()
- {
- CString s;
- m_ekran.SetWindowText(s);
- wynik="";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement